mini-check
Version:
47 lines • 1.4 kB
JavaScript
import Taro, { request as _request, showToast as _showToast } from "@tarojs/taro-h5";
import { baseUrl } from "../config/index";
import { setItem, getItem, removeItem } from './index';
export default (options => {
const url = Taro.getEnv() === 'WEAPP' ? `${baseUrl}${options.url}` : options.url;
const token = getItem('token');
const header = Taro.getEnv() === 'WEAPP' ? {
token,
'Content-Type': 'application/json'
} : {
'Content-Type': 'application/json'
};
return _request({
url,
data: {
...options.data
},
header,
method: options.method || 'GET',
credentials: "include"
}).then(res => {
const { data } = res;
if (res.statusCode === 401) {
removeItem("token");
Taro.redirectTo({ url: '/pages/login/index' });
_showToast({ title: res.data.message, icon: "none" });
}
if (data.content && data.content.sessionId) {
setItem({ key: 'token', data: data.content.sessionId });
}
return data;
}).catch(re => {
try {
re.json().then(({ status, message }) => {
if (status === 401) {
removeItem("token");
Taro.redirectTo({ url: '/pages/login/index' });
}
_showToast({ title: message, icon: "none" });
throw re;
});
} catch (e) {
_showToast({ title: "系统出错,请稍后重试", icon: "none" });
throw re;
}
});
});