添加一个公共方法

This commit is contained in:
HJ 2023-03-08 15:44:35 +08:00
parent f17784132e
commit 1136cfbb22

View File

@ -1,67 +1,67 @@
import config from './config.js'; import config from './config.js';
export default { export default {
request(options = {}) { request(options = {}) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
let url = options.url; let url = options.url;
if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) { if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) {
options.url = config.domain + url; options.url = config.domain + url;
} }
options.header.token = uni.getStorageSync("token"); options.header.token = uni.getStorageSync("token");
options.complete = (response) => { options.complete = (response) => {
if (response.statusCode == 200 || response.statusCode == 0) { if (response.statusCode == 200 || response.statusCode == 0) {
if (response.data.code == 401 || response.data.code == 420) { if (response.data.code == 401 || response.data.code == 420) {
// #ifdef MP-WEIXIN // #ifdef MP-WEIXIN
uni.navigateTo({ uni.navigateTo({
url: "/pages/login/weixin" url: "/pages/login/weixin"
}) })
// #endif // #endif
// #ifdef H5 // #ifdef H5
uni.navigateTo({ uni.navigateTo({
url: "/pages/login/login" url: "/pages/login/login"
}) })
// #endif // #endif
} }
if (response.data.code == 500) { if (response.data.code == 500) {
uni.showToast({ uni.showToast({
title: response.data.msg, title: response.data.msg,
icon: "none", icon: "none",
duration: 2000 duration: 2000
}); });
} }
resolve(response.data) resolve(response.data)
} else { } else {
uni.showToast({ uni.showToast({
title: '请求异常!', title: '请求异常!',
icon: "none" icon: "none"
}); });
} }
} }
uni.request(options) uni.request(options)
}) })
}, },
post(url, data = {}, header = {}) { post(url, data = {}, header = {}) {
let options = { let options = {
url: url, url: url,
data: data, data: data,
header: header, header: header,
method: "POST" method: "POST"
} }
return this.request(options); return this.request(options);
}, },
get(url, data = {}, header = {}) { get(url, data = {}, header = {}) {
let options = { let options = {
url: url, url: url,
data: data, data: data,
header: header header: header
} }
return this.request(options); return this.request(options);
} }
}; };