JM-MiniApp/unpackage/dist/dev/mp-weixin/api/request.js
2023-10-18 21:00:42 +08:00

191 lines
5.3 KiB
JavaScript

"use strict";
const common_vendor = require("../common/vendor.js");
const config_index = require("../config/index.js");
const loginUrl = "pages/user/login";
const net = {
REQUEST(url, method = "GET", data, checkLogin = true, header) {
let token = common_vendor.index.getStorageSync("access_token") || "Basic ZGV2OmRldjEyMw==";
const headers = {
// "Content-Type": "application/json",
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": token,
"x-token": token,
"token": token
// "X-Forwarded-For":'client_ip'
// 'Referer':'twzxjy.com'
};
var pages = getCurrentPages();
var page = pages[pages.length - 1];
return common_vendor.index.request({
url: config_index.config.baseUrl + url,
method,
header: {
...header,
...headers
},
data
}).then((res) => {
if (res.statusCode === 200 && res.data) {
if (res.data.code === 409) {
common_vendor.index.showModal({
title: "提示",
content: "您的账号已在其他设备登录,已强制下线!",
confirmColor: "#006647",
showCancel: false,
success: (res2) => {
if (res2.confirm) {
common_vendor.index.removeStorageSync("access_token");
common_vendor.index.removeStorageSync("user_info");
if (checkLogin && page.route != loginUrl) {
common_vendor.index.navigateTo({
url: "/" + loginUrl
});
}
}
}
});
return {
...res.data
};
}
if (res.data.code === 5) {
common_vendor.index.removeStorageSync("access_token");
if (page.route != loginUrl && checkLogin) {
common_vendor.index.navigateTo({
url: "/" + loginUrl
});
}
}
return res.data;
} else {
const reg = /abort/;
let code = 0;
let msg = res[0] && res[0].errMsg || "未知错误";
if (res[0] && res[0].errMsg && reg.test(res[0].errMsg)) {
msg = "网络请求中断";
}
return {
code,
msg,
data: null
};
}
}).catch((parmas) => {
return parmas;
});
},
GET(url, body, checkLogin = true, header) {
return this.REQUEST(url, "GET", body, checkLogin, header);
},
POST(url, body, checkLogin = true, header) {
return this.REQUEST(url, "POST", body, checkLogin, header);
},
PUT(url, body, header) {
return this.REQUEST(url, "PUT", body, header);
},
DELETE(url, body, header) {
return this.REQUEST(url, "DELETE", body, header);
},
UPLOAD(data) {
return new Promise((resolve, reject) => {
let token = common_vendor.index.getStorageSync("access_token") || "";
common_vendor.index.uploadFile({
...data,
url: config_index.config.base_url + data.url,
header: {
"Access-Token": token
},
success(res) {
res.data = JSON.parse(res.data);
if (res.data.code === 401) {
common_vendor.index.removeStorageSync("access_token");
common_vendor.index.removeStorageSync("userInfo");
common_vendor.index.navigateTo({
url: "/pages/login/login"
});
}
resolve(res);
},
fail(e) {
console.log(e);
reject(e);
}
});
});
},
getShareProvider: () => {
return common_vendor.index.getProvider({
service: "share"
}).then((res) => {
let data = [];
for (let i = 0; i < res.provider.length; i++) {
switch (res.provider[i]) {
case "weixin":
data.push({
name: "微信好友",
id: "weixin",
icon: "/static/wx.png"
});
data.push({
name: "朋友圈",
id: "weixin",
type: "WXSenceTimeline",
icon: "/static/pyq.png"
});
break;
case "qq":
data.push({
name: "QQ好友",
id: "qq",
icon: "/static/qq.png"
});
break;
}
}
return data;
}).catch((parmas) => {
return Promise.reject();
});
},
getPaymentProvider: () => {
return common_vendor.index.getProvider({
service: "payment"
}).then((res) => {
let data = [];
const aliPay = {
name: "支付宝支付",
id: "alipay",
icon: "/static/order/zfbp@3x.png"
};
const wxPay = {
name: "微信支付",
id: "wxpay",
icon: "/static/order/wxp@3x.png"
};
const yuePay = {
name: "余额支付",
id: "yepay",
icon: "/static/order/yep@3x.png"
};
for (let i = 0; i < res[1].provider.length; i++) {
switch (res[1].provider[i]) {
case "alipay":
data.push({
...aliPay
});
break;
case "wxpay":
data.push({
...wxPay
});
break;
}
}
return [...data, yuePay];
}).catch((parmas) => {
return Promise.reject();
});
}
};
exports.net = net;