refactor: 统一API请求路径并优化请求逻辑
将多个页面中的API请求路径统一为相对路径,并在`utils/login.js`中自动补全为完整URL。优化了请求方法,默认为POST,同时支持GET请求。简化了代码结构,提高了代码的可维护性和一致性。
This commit is contained in:
101
utils/login.js
101
utils/login.js
@@ -1,8 +1,8 @@
|
||||
var e = require("../@babel/runtime/helpers/typeof"), o = (require("./common.js"),
|
||||
null), a = null;
|
||||
null), a = null;
|
||||
|
||||
function t() {
|
||||
return new Promise(function(e, o) {
|
||||
return new Promise(function (e, o) {
|
||||
return wx.login({
|
||||
success: e,
|
||||
fail: o
|
||||
@@ -12,7 +12,20 @@ function t() {
|
||||
|
||||
function n(a) {
|
||||
var t = arguments.length > 1 && void 0 !== arguments[1] ? arguments[1] : {};
|
||||
return wx.showLoading(), new Promise(function(n, c) {
|
||||
|
||||
// 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
|
||||
@@ -26,8 +39,8 @@ function n(a) {
|
||||
Cookie: "_miucms_session=" + wx.getStorageSync("_miucms_session"),
|
||||
token: wx.getStorageSync("session") || ""
|
||||
},
|
||||
method: "POST",
|
||||
success: function(o) {
|
||||
method: method,
|
||||
success: function (o) {
|
||||
var t = o.data;
|
||||
if ("object" != e(t) && null != t) {
|
||||
t = t.replace("\ufeff", "");
|
||||
@@ -37,40 +50,40 @@ function n(a) {
|
||||
wx.hideLoading();
|
||||
var l = o.data;
|
||||
switch (1 * l.code) {
|
||||
case 200:
|
||||
case 888:
|
||||
n(l);
|
||||
break;
|
||||
case 200:
|
||||
case 888:
|
||||
n(l);
|
||||
break;
|
||||
|
||||
case 201:
|
||||
a.indexOf("carwashcoupon/receive") > -1 || wx.showModal({
|
||||
title: "提示",
|
||||
content: l.message
|
||||
}), c(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 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;
|
||||
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);
|
||||
default:
|
||||
wx.showModal({
|
||||
title: "提示",
|
||||
content: l.message
|
||||
}), c(l);
|
||||
}
|
||||
},
|
||||
fail: function(e) {
|
||||
fail: function (e) {
|
||||
wx.hideLoading(), console.log("reqest error", e), c("fail");
|
||||
}
|
||||
});
|
||||
@@ -80,11 +93,11 @@ function n(a) {
|
||||
function c(e, a) {
|
||||
console.log("appstart", e), o = a, n("https://t-jm.v0750.com/miniprogram", {
|
||||
code: e
|
||||
}, !0).then(function(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;
|
||||
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;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -92,23 +105,23 @@ 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() {
|
||||
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) {
|
||||
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) {
|
||||
}).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) {
|
||||
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;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user