parent
ce293a9c5f
commit
d47ee57b3c
@ -0,0 +1,98 @@
|
||||
import Taro, { clearStorage } from '@tarojs/taro';
|
||||
import { CommonRes } from '../services/common';
|
||||
import { showModal } from './wechat-ui';
|
||||
import { getGlobalData, setGlobalData } from './global-data';
|
||||
// export const BASE_URL = 'http://i100c.3322.org:31233';
|
||||
export const BASE_URL = 'https://ylt.ikbvip.com';
|
||||
// export const BASE_URL = 'https://wechatapps.test-ylt.ikbvip.com/';
|
||||
|
||||
// export const BASE_URL =
|
||||
// process.env.NODE_ENV === 'development'
|
||||
// ? 'http://i100c.3322.org:30701'
|
||||
// : 'https://ylt.ikbvip.com'; //测试线
|
||||
|
||||
type Method =
|
||||
| 'OPTIONS'
|
||||
| 'GET'
|
||||
| 'HEAD'
|
||||
| 'POST'
|
||||
| 'PUT'
|
||||
| 'DELETE'
|
||||
| 'TRACE'
|
||||
| 'CONNECT';
|
||||
export const RzRequestEnhance = <T>(
|
||||
url: string,
|
||||
data?: unknown,
|
||||
method: Method = 'POST',
|
||||
headers = {},
|
||||
isShowLoading = true
|
||||
): Promise<T> => {
|
||||
const option = {
|
||||
url: BASE_URL + url,
|
||||
data,
|
||||
method,
|
||||
header: {
|
||||
Authorization: Taro.getStorageSync('authorization'),
|
||||
...headers,
|
||||
},
|
||||
};
|
||||
|
||||
if (isShowLoading) {
|
||||
Taro.showLoading({ title: '加载中..' });
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
Taro.request(option)
|
||||
.then(({ statusCode, data }: { statusCode: number; data: CommonRes }) => {
|
||||
console.log('response data', data);
|
||||
if (isShowLoading) {
|
||||
Taro.hideLoading();
|
||||
}
|
||||
if (statusCode >= 200 && statusCode < 300) {
|
||||
if (data.code === 1000) {
|
||||
resolve(data.data);
|
||||
} else if (data.code === 1002) {
|
||||
clearStorage();
|
||||
if (!getGlobalData('isNavToLogin')) {
|
||||
setGlobalData('isNavToLogin', true);
|
||||
showModal(
|
||||
'提示',
|
||||
'凭证过期需要重新登录以访问更多功能,是否需要登录?'
|
||||
).then((res) => {
|
||||
if (res.confirm) {
|
||||
Taro.navigateTo({ url: '/pages/login/login' }).then(() => {
|
||||
setTimeout(() => {
|
||||
setGlobalData('isNavToLogin', false);
|
||||
}, 3000);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
reject({ msg: data.msg, code: 1002 });
|
||||
} else if (data.code === 1007) {
|
||||
reject({ msg: data.msg, code: 1007 });
|
||||
} else if (data.code === 1009) {
|
||||
reject({ msg: data.msg, code: 1009 });
|
||||
} else {
|
||||
console.log('data.msg', data.msg);
|
||||
reject(data.msg);
|
||||
}
|
||||
} else {
|
||||
const msg = `Error: code ${statusCode}`;
|
||||
reject(msg);
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
if (isShowLoading) {
|
||||
Taro.hideLoading();
|
||||
}
|
||||
Taro.showToast({
|
||||
title: JSON.stringify(error),
|
||||
icon: 'none',
|
||||
duration: 1500,
|
||||
mask: true,
|
||||
});
|
||||
reject(error);
|
||||
});
|
||||
});
|
||||
};
|
Loading…
Reference in new issue