You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
73 lines
2.2 KiB
JavaScript
73 lines
2.2 KiB
JavaScript
import userModel from "./userModel";
|
|
import {
|
|
baseUrl
|
|
} from "../api/api"
|
|
|
|
export default function request(obj) {
|
|
wx.showLoading({
|
|
title: '加载中',
|
|
mask: true
|
|
})
|
|
return new Promise((resolve, reject) => {
|
|
const {
|
|
url,
|
|
data,
|
|
method,
|
|
header,
|
|
dataType
|
|
} = obj;
|
|
const token = userModel.getToken();
|
|
let headers = {}
|
|
if (token && url !== baseUrl + '/login/auth') {
|
|
headers['Authorization'] = token;
|
|
}
|
|
wx.request({
|
|
url: url,
|
|
data,
|
|
method,
|
|
header: headers,
|
|
dataType,
|
|
success: (res) => {
|
|
wx.hideLoading();
|
|
const result = res.data;
|
|
console.log(result)
|
|
if (result) {
|
|
if (result.retcode == 1) {
|
|
resolve(result);
|
|
} else if (result.retcode == 201 || result.retcode == 401) {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: "登录过期",
|
|
});
|
|
wx.clearStorage({
|
|
success: (res) => {
|
|
wx.reLaunch({
|
|
url: "/pages/login/login",
|
|
});
|
|
},
|
|
})
|
|
} else {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: result.retmsg || '服务器或网络错误',
|
|
});
|
|
reject(result.retmsg);
|
|
}
|
|
} else {
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: '服务器或网络错误',
|
|
});
|
|
reject(res);
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
reject(err);
|
|
wx.showToast({
|
|
icon: "none",
|
|
title: '服务器或网络错误',
|
|
});
|
|
}
|
|
})
|
|
})
|
|
} |