68 lines
1.5 KiB
TypeScript
68 lines
1.5 KiB
TypeScript
import Taro from '@tarojs/taro';
|
|
|
|
export const BASE_URL = process.env.TARO_APP_API;
|
|
|
|
interface Res<T> {
|
|
code: number;
|
|
data: T;
|
|
msg: string;
|
|
}
|
|
|
|
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE';
|
|
|
|
// 忽略系统提示白名单
|
|
const IGNORED_TIPS = ['/user/find/phone', '/user/check/payPassword'];
|
|
|
|
const request = (
|
|
url: string,
|
|
data: object = {},
|
|
method: Method = 'GET',
|
|
): Promise<Res<any>> => {
|
|
return new Promise((resolve, reject) => {
|
|
Taro.request({
|
|
url: BASE_URL + url,
|
|
data: data,
|
|
method: method,
|
|
header: {
|
|
'content-type': 'application/json',
|
|
token: Taro.getStorageSync('token') || '',
|
|
},
|
|
success: ({ data }) => {
|
|
if (data.code !== 200 && data.code !== 401) {
|
|
if (!IGNORED_TIPS.includes(url)) {
|
|
Taro.showToast({
|
|
title: data.msg,
|
|
icon: 'none',
|
|
});
|
|
}
|
|
reject(data);
|
|
return;
|
|
}
|
|
if (data.code === 401) {
|
|
Taro.showModal({
|
|
title: '提示',
|
|
content: '你当前未登录,是否前往登录?',
|
|
confirmText: '去登录',
|
|
cancelText: '先逛逛',
|
|
success: ({ confirm }) => {
|
|
if (confirm) {
|
|
Taro.reLaunch({
|
|
url: '/pages/users/login/index',
|
|
});
|
|
}
|
|
},
|
|
});
|
|
reject(data);
|
|
return;
|
|
}
|
|
resolve(data);
|
|
},
|
|
fail: err => {
|
|
reject(err);
|
|
},
|
|
});
|
|
});
|
|
};
|
|
|
|
export default request;
|