You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
107 lines
2.7 KiB
107 lines
2.7 KiB
var global = require("../config/config.js");
|
|
import words from "../config/words";
|
|
const request = function (url, options) {
|
|
let token = wx.getStorageSync("token");
|
|
if (token == undefined || token == null) {
|
|
token = "";
|
|
}
|
|
return new Promise((resolve, reject) => {
|
|
wx.request({
|
|
url: `${global.config.apiUrl}${url}`,
|
|
method: options.method,
|
|
data:
|
|
options.method === "GET"
|
|
? options.data
|
|
: JSON.stringify(options.data),
|
|
header: {
|
|
"Content-Type": "application/json; charset=UTF-8",
|
|
Authorization: wx.getStorageSync("token"),
|
|
},
|
|
success(response) {
|
|
if (response.statusCode === 200) {
|
|
if (response.data.code === 0) {
|
|
// 置换图片前缀
|
|
if (response.data.data) {
|
|
let str = JSON.stringify(response.data.data);
|
|
response.data.data = JSON.parse(
|
|
str.replaceAll(
|
|
"http://172.20.46.177/oss/",
|
|
"https://www.yantai1s.com/oss/"
|
|
)
|
|
);
|
|
}
|
|
resolve(response.data);
|
|
} else {
|
|
const { code, msg } = response.data;
|
|
if (code == 10005 || code == 10006 || code == 10007) {
|
|
// 10005 token为空 10006登陆超时 10007别处登录
|
|
console.log("登录失效");
|
|
getApp().toGuidePageAfterHint();
|
|
} else if (code < 10000 && code >= 8000) {
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: "none",
|
|
duration: 3000,
|
|
});
|
|
} else if (code == 10086) {
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: msg,
|
|
success(res) {
|
|
if (res.confirm) {
|
|
wx.reLaunch({
|
|
url: "/pages/main/index",
|
|
});
|
|
} else if (res.cancel) {
|
|
console.log("用户点击取消");
|
|
}
|
|
},
|
|
});
|
|
} else if (code != 0) {
|
|
wx.showToast({
|
|
title: words.common.requestFail,
|
|
icon: "none",
|
|
duration: 1500,
|
|
});
|
|
}
|
|
console.log("response.data", response.data);
|
|
reject(response.data);
|
|
}
|
|
} else {
|
|
wx.showToast({
|
|
title: "网络问题,请稍后再试~",
|
|
icon: "none",
|
|
duration: 2000,
|
|
});
|
|
reject(false);
|
|
}
|
|
},
|
|
fail(error) {
|
|
wx.showToast({
|
|
title: "网络问题,请稍后再试~",
|
|
icon: "none",
|
|
duration: 2000,
|
|
});
|
|
console.log("error", error);
|
|
reject(error.data);
|
|
},
|
|
});
|
|
});
|
|
};
|
|
function get(url, options = {}) {
|
|
return request(url, { method: "GET", data: options });
|
|
}
|
|
|
|
function post(url, options = {}) {
|
|
return request(url, { method: "POST", data: options });
|
|
}
|
|
|
|
function put(url, options = {}) {
|
|
return request(url, { method: "PUT", data: options });
|
|
}
|
|
|
|
module.exports = {
|
|
get: get,
|
|
post: post,
|
|
put: put,
|
|
};
|
|
|