request.js
5.38 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { secretKey, domain, path, sign } from "./http.js";
// console.log(secretKey, 'secretKey')
import util from "~/utils/util.js";
import { envIndex } from "~/config";
/**接口 */
class Request {
get(method, data, noSignAttr, requestPath, domainName, failTips) {
return this.request("GET", method, data, noSignAttr, requestPath, domainName, failTips);
}
post(method, data, noSignAttr, requestPath, domainName, failTips) {
return this.request("POST", method, data, noSignAttr, requestPath, domainName, failTips);
}
request(RequestMethods, method, data, noSignAttr = [], requestPath, domainName = "dj", failTips = true) {
let _this = this;
return new Promise((resolve, reject) => {
wx.showNavigationBarLoading();
wx.getNetworkType({
success(res) {
if (res.networkType == "none") {
wx.hideLoading();
wx.showToast({
title: "网络异常",
icon: "none",
});
reject(new Error("网络异常"));
return;
}
if (!data) data = {};
let header =
RequestMethods === "POST"
? {
"content-type": "application/x-www-form-urlencoded",
}
: {};
let url = `${domain[domainName]}${requestPath || path}`;
// console.log(requestPath)
if (method) {
data = _this.getData(data, method, noSignAttr, domainName == "merchants");
url = `${domain[domainName]}${requestPath || path}?method=${method}`;
} else {
header = {
"content-type": "application/json",
};
}
// console.log('reqData',data?.sessionId);
// delete
// if (data?.sessionId) {
// delete data?.sessionId
// }
wx.request({
url,
data,
method: RequestMethods,
header,
dataType: "json",
responseType: "text",
timeout: noSignAttr?.includes("file") ? 120 * 1000 : 60 * 1000,
success(response) {
/**业务代码 */
let {
data: { code, message, subErrors },
data,
} = response;
if (code == 20 || code == 21) {
let thisPage = wx.$router.getPageInstance();
// console.log(thisPage);
wx.hideLoading();
if (thisPage.route != "pages/wxLogin/wxLogin") {
getApp().login(true, "", url);
}
reject(
new Error(message || "登录已失效,请重新登录")
);
return;
} else if (code == -1) {
if (failTips) {
wx.showToast({
title: message || "网络请求失败",
icon: "none",
});
}
resolve(data);
} else {
// util.debounce( getApp().test(true),500)
// getApp().test()
// console.log(data,'data');
resolve(data);
}
},
fail(error) {
wx.hideLoading();
const errMsg = error?.errMsg || String(error || "");
const isTimeout =
/timeout/i.test(errMsg) || error?.errno === 5;
let pageRoute = "";
try {
const pages = getCurrentPages();
const cur = pages[pages.length - 1];
pageRoute = cur?.route || "";
} catch (e) {
pageRoute = "";
}
console.error(
"[request fail]",
{
method: method || "(no method)",
errMsg,
isTimeout,
pageRoute,
url: `${domain[domainName]}${requestPath || path}`,
},
error
);
if (failTips) {
wx.showToast({
title: isTimeout ? "请求超时,请重试" : "系统异常",
icon: "none",
});
}
reject(error);
},
complete() {
wx.stopPullDownRefresh();
wx.hideNavigationBarLoading();
},
});
},
});
});
}
getData(data, method, noSignAttr, merchant) {
let sessionId = util.getSessionId();
if (sessionId) data["sessionId"] = sessionId;
// console.log(data,'request');
data["v"] = "1.0.0";
data["method"] = method;
data["format"] = "json";
data["locale"] = "zh_CN";
data["source"] = "dj-wechat";
// 兼容线上dj和scrm appkey和secret不一样的情况
let apiName = data.method.split(".api")[0];
// 为空报错
if (!apiName) {
throw new Error("apiName is null");
}
if (apiName.split(".").length > 1) apiName = apiName.split(".")[0];
if (merchant) apiName = "merchants";
const appKey = secretKey[apiName].appKey;
const secret = secretKey[apiName].secret;
data["appKey"] = appKey;
let obj = sign(data, secret, noSignAttr);
//data['sign'] = obj.sign;
return obj;
}
getMock(configModule = "dj-car-boutique-work-wechat-miniapp") {
const env = {
1: "develop",
2: "test",
3: "production",
4: "beta",
}[envIndex],
configUrl = `https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=${configModule}&env=${env}`;
return new Promise((resolve) => {
wx.request({
url: configUrl,
data: {},
method: "GET",
header: {},
dataType: "json",
responseType: "text",
success(response) {
resolve(response.data);
},
});
});
}
getApiFoxMock(method, params) {
return new Promise((resolve) => {
wx.request({
url: `http://127.0.0.1:4523/m1/7739320-7483460-default/rop-scrm-clue/router?method=${method}`,
data: params,
method: "post",
header: {
"content-type": "application/x-www-form-urlencoded",
},
dataType: "json",
responseType: "text",
success(response) {
resolve(response.data);
},
});
});
}
}
export default new Request();