request.js
2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
"use strict";
const common_vendor = require("./vendor.js");
const common_login = require("./login.js");
function getToken() {
try {
const token = common_vendor.index.getStorageSync("token");
if (token) {
return token;
} else {
return "";
}
} catch (e) {
console.log(e);
}
}
function getHeaders() {
const token = getToken();
const header = {
"Authorization": token,
"Content-Type": "application/json",
// 根据自己的数据类型
// "Content-Type":"application/x-www-form-urlencoded",
"Ztx-Per-Id": common_vendor.index.getStorageSync("perId") || "-1"
};
return header;
}
const request = function(req) {
req.method = req.method.toUpperCase();
if (!["GET", "POST", "PUT", "DELETE"].includes(req.method)) {
common_vendor.index.showToast({
title: `暂不支持的请求方式: ${req.method}`,
icon: "none"
});
return;
}
return new Promise((resolve, reject) => {
common_vendor.index.request({
url: req.url,
method: req.method,
data: req.params,
header: getHeaders()
}).then((res) => {
var _a;
switch (res.statusCode) {
case 200:
const data = res.data || {};
if (!data || data.code === 0 || data.code === 200 || ((_a = data.pageData) == null ? void 0 : _a.code) === 200) {
resolve(data);
} else if (req.url.indexOf("getMemberCountInfo") > -1) {
resolve(data);
} else {
if (data.msg) {
common_vendor.index.showToast({
title: data.msg,
icon: "none",
duration: 2e3
});
}
if (data.code === 60002 || data.code === 60001) {
common_vendor.index.redirectTo({
url: "/pages/index/login"
});
} else if (data.code === 401) {
common_login.h5LoginAuto().then(() => {
common_vendor.index.hideLoading();
common_vendor.index.redirectTo({
url: getCurrentPages()[getCurrentPages().length - 1].$page.fullPath
});
}).catch(() => {
common_vendor.index.showToast({
title: "服务异常,请稍后重试",
icon: "none"
});
});
}
reject(res);
}
break;
default:
reject(res);
}
}).catch((res) => {
reject(res);
}).finally(() => {
});
});
};
exports.request = request;