69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
import {
|
|
setStorage,
|
|
getStorage
|
|
} from '~/utils/storage'
|
|
|
|
const ServerBasePath = 'http://localhost:8888/api/';
|
|
|
|
// 网络请求封装
|
|
export function request(option) {
|
|
const that = this;
|
|
const { url } = option;
|
|
const { data } = option;
|
|
const { fail } = option;
|
|
const { success } = option;
|
|
const { method } = option;
|
|
|
|
wx.showLoading({
|
|
title: '数据加载中',
|
|
mask: true,
|
|
});
|
|
wx.request(
|
|
(option,
|
|
{
|
|
header: {
|
|
Authorization: `Bearer ${getStorage('Authorization')}`,
|
|
},
|
|
timeout: 50000, //设置超时时间
|
|
url: ServerBasePath + url,
|
|
data: data,
|
|
method: method,
|
|
success: function (res) {
|
|
if (res.data.Code === 401) {
|
|
const userInfo = getStorage('userInfo');
|
|
//初次跳转登录页
|
|
if (!userInfo) {
|
|
wx.navigateTo({
|
|
url: '/pages/login/login',
|
|
});
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (typeof success === 'function') {
|
|
if (res.statusCode === 200) {
|
|
success(res.data, res);
|
|
wx.hideLoading();
|
|
} else {
|
|
wx.showToast({
|
|
icon: 'error',
|
|
title: '网络异常,请重试',
|
|
});
|
|
wx.hideLoading();
|
|
}
|
|
}
|
|
},
|
|
fail: function (res) {
|
|
if (typeof fail === 'function') {
|
|
fail(res);
|
|
}
|
|
wx.showToast({
|
|
icon: 'error',
|
|
title: '网络异常,请重试',
|
|
});
|
|
console.log(`请求:【${url}】fail`, res);
|
|
},
|
|
}),
|
|
);
|
|
}
|