将多个页面中的API请求路径统一为相对路径,并在`utils/login.js`中自动补全为完整URL。优化了请求方法,默认为POST,同时支持GET请求。简化了代码结构,提高了代码的可维护性和一致性。
129 lines
4.3 KiB
JavaScript
129 lines
4.3 KiB
JavaScript
var e = require("../@babel/runtime/helpers/typeof"), o = (require("./common.js"),
|
|
null), a = null;
|
|
|
|
function t() {
|
|
return new Promise(function (e, o) {
|
|
return wx.login({
|
|
success: e,
|
|
fail: o
|
|
});
|
|
});
|
|
}
|
|
|
|
function n(a) {
|
|
var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
|
|
|
// a 是否 https, 如果不是自动补全
|
|
if (!/^https/.test(a)) {
|
|
a = 'https://t-jm.v0750.com' + a;
|
|
}
|
|
|
|
// method 默认为 GET
|
|
let method = "POST";
|
|
if (t.method) {
|
|
method = t.method;
|
|
delete t.method;
|
|
}
|
|
|
|
return wx.showLoading(), new Promise(function (n, c) {
|
|
console.log("requestdevicecode", o.globalData.devicecode);
|
|
var i = Object.assign({}, {
|
|
devicecode: o.globalData.devicecode
|
|
}, o.globalData.options, t);
|
|
console.log("sendData", a, i), wx.request({
|
|
url: a,
|
|
data: i,
|
|
header: {
|
|
"content-type": "application/json",
|
|
Accept: "application/json, text/plain",
|
|
Cookie: "_miucms_session=" + wx.getStorageSync("_miucms_session"),
|
|
token: wx.getStorageSync("session") || ""
|
|
},
|
|
method: method,
|
|
success: function (o) {
|
|
var t = o.data;
|
|
if ("object" != e(t) && null != t) {
|
|
t = t.replace("\ufeff", "");
|
|
var i = JSON.parse(t);
|
|
o.data = i;
|
|
}
|
|
wx.hideLoading();
|
|
var l = o.data;
|
|
switch (1 * l.code) {
|
|
case 200:
|
|
case 888:
|
|
n(l);
|
|
break;
|
|
|
|
case 201:
|
|
a.indexOf("carwashcoupon/receive") > -1 || wx.showModal({
|
|
title: "提示",
|
|
content: l.message
|
|
}), c(l);
|
|
break;
|
|
|
|
case 404:
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: "未找到当前接口"
|
|
}), c(l);
|
|
break;
|
|
|
|
case 401:
|
|
var s = getCurrentPages(), r = s[s.length - 1];
|
|
console.log(401, a), wx.redirectTo({
|
|
url: "/pages/login/login?fromPage=".concat(r.route)
|
|
}), c(l);
|
|
break;
|
|
|
|
default:
|
|
wx.showModal({
|
|
title: "提示",
|
|
content: l.message
|
|
}), c(l);
|
|
}
|
|
},
|
|
fail: function (e) {
|
|
wx.hideLoading(), console.log("reqest error", e), c("fail");
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
function c(e, a) {
|
|
console.log("appstart", e), o = a, n("https://t-jm.v0750.com/miniprogram", {
|
|
code: e
|
|
}, !0).then(function (e) {
|
|
var o = e.data;
|
|
console.log("总接口", o), a.globalData.user = o.user, wx.setStorageSync("session", o.user.session),
|
|
a.globalData.isUserAuthorization = o.user > 0 ? 2 : 1, a.globalData.config = o.config,
|
|
a.globalData.servicetel = o.servicetel, a.globalData.canGetData = !0, a.globalData.protocol = o.protocol;
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
initial_url: "https://t-jm.v0750.com/miniprogram",
|
|
login: t,
|
|
request: n,
|
|
pageStart: function (e) {
|
|
return new Promise(function (o, t) {
|
|
e.globalData.canGetData ? o() : a = setInterval(function () {
|
|
e.globalData.canGetData && (clearInterval(a), o());
|
|
}, 1e3);
|
|
});
|
|
},
|
|
getLogin: function (e) {
|
|
o = e, t().then(function (a) {
|
|
console.log("获取用户登录态成功!", a.code), o.globalData.code = a.code, c(a.code, e);
|
|
}).catch(function (o) {
|
|
c(res.code, e);
|
|
});
|
|
},
|
|
baseUrl: "https://t-jm.v0750.com/",
|
|
getDevicecode: function (e, a) {
|
|
o = e, n("https://t-jm.v0750.com/miniprogram/identify/reload", a, !0).then(function (e) {
|
|
var a = e.data;
|
|
console.log("onshow获取到的数据", a), o.globalData.devicecode = a.devicecode || o.globalData.devicecode;
|
|
});
|
|
}
|
|
}; |