http.uts
6.75 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { storage } from "../../utils/storage.uts";
import { djDomain, djSecretKey, domain, merchantDomain, pathEnum, secretKey } from "../env.uts";
import { sign } from "./sign.uts";
type HostType = "dj" | "ip" | "merchant" | "scrm";
const HOST: Record<HostType, string> = {
scrm: domain,
dj: djDomain,
merchant: merchantDomain,
ip: "http://192.168.1.125:8080"
};
/**
* HTTP 请求封装(从 gl_tape/api/request/http.js 迁移)
* - 统一处理 GET / POST
* - 自动注入 appKey / secret / sessionId 等并完成参数签名
* - 处理业务 code 及登录态失效跳转
*/
class Request {
/**
* 业务方法调用封装
* @param method ROP 接口方法名
* @param data 业务参数
* @param returnAll 是否返回完整响应体
* @param hostType 域名类型
*/
fetch(method: string, data: UTSJSONObject, returnAll: boolean = true, hostType?: HostType): Promise<any> {
const payload = {} as UTSJSONObject;
// method
payload.method = method;
// 合并业务参数
const dataKeys = Object.keys(data as any) as string[];
for (const key of dataKeys) {
// @ts-ignore
payload[key] = (data as UTSJSONObject)[key];
}
return this.post(payload, returnAll, hostType);
}
get(params: UTSJSONObject, returnAll: boolean = false, hostType?: HostType): Promise<any> {
return this.request("GET", params, returnAll, hostType);
}
post(params: UTSJSONObject, returnAll: boolean = false, hostType?: HostType, timeout?: number): Promise<any> {
return this.request("POST", params, returnAll, hostType, timeout);
}
/**
* 简单的原始 uni.request 封装
*/
uni_request(options: {
data?: UTSJSONObject;
dataType?: string;
header?: UTSJSONObject;
method?: "GET" | "POST";
responseType?: string;
url: string;
}): Promise<any> {
return new Promise((resolve) => {
uni.request({
url: options.url,
data: options.data ?? ({} as UTSJSONObject),
method: options.method ?? "GET",
header: options.header ?? ({} as UTSJSONObject),
dataType: options.dataType ?? "json",
responseType: options.responseType ?? "text",
success: (response) => {
resolve(response.data);
}
});
});
}
/**
* 组装 ROP 请求参数并完成签名
*/
private getData(params: UTSJSONObject, hostType: HostType): UTSJSONObject {
let projectName = (params.projectName ?? "") as string;
if (projectName == "") {
projectName = this.getRopProject(params.method as string);
} else {
// 使用后删除 projectName 字段
delete params.projectName;
}
let appKey = "48e5e13229b82c1b4e6e8c96151f0637";
let secret = "c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b";
const projectSecret = secretKey[projectName];
if (projectSecret != null) {
appKey = projectSecret.appKey;
secret = projectSecret.secret;
}
if (hostType == "dj") {
appKey = djSecretKey.appKey;
secret = djSecretKey.secret;
}
const sessionId = storage.get("sessionId") as null | string;
let noSignAttr: string[] = [];
if (params.sessionId == null && sessionId != null && sessionId != "") {
params.sessionId = sessionId;
}
if (params.delSessionId != null) {
delete params.sessionId;
delete params.delSessionId;
}
if (params.noSignAttr != null) {
noSignAttr = params.noSignAttr as string[];
delete params.noSignAttr;
}
params.v = "1.0.0";
params.format = "json";
params.locale = "zh_CN";
params.source = "app";
params.appKey = appKey;
return sign(params, secret, noSignAttr);
}
/**
* 根据 method 解析出 rop 项目名
* @example sso.xxx.xxx => 第三个段作为项目名
*/
private getRopProject(method: string): string {
const parts = method.split(".");
if (parts.length >= 3) {
return parts[2];
}
return "";
}
private request(
requestMethod: "GET" | "POST",
params: UTSJSONObject,
returnAll: boolean,
hostType: HostType | undefined,
timeout?: number
): Promise<any> {
const finalHostType: HostType = hostType ?? "dj";
return new Promise((resolve, reject) => {
uni.getNetworkType({
success: (res) => {
if (res.networkType == "none") {
uni.hideLoading();
uni.showToast({
title: "网络异常",
icon: "none"
});
reject("网络异常");
return;
}
const header =
requestMethod == "POST"
? {
"content-type": "application/x-www-form-urlencoded"
}
: {
"Cache-Control": "no-cache"
};
let projectName = (params.projectName ?? "") as string;
if (projectName == "") {
projectName = this.getRopProject(params.method as string);
}
const path = pathEnum[projectName] ?? "";
const requestData = this.getData(params, finalHostType);
uni.request({
url: `${HOST[finalHostType]}${path}router?method=${requestData.method}`,
data: requestData,
method: requestMethod,
header,
dataType: "json",
responseType: "text",
timeout: timeout ?? 60_000,
success: (response) => {
const data: any = response.data;
const statusCode = response.statusCode;
if (data == null || statusCode != 200) {
uni.hideLoading();
let errMsg = "";
if (data == null) {
errMsg = "获取数据失败,请重试。";
} else {
errMsg = "服务器暂时不可用,请稍后再次尝试。";
}
reject(errMsg);
return;
}
const code = (data.code ?? "") as number | string;
const message = (data.message ?? "") as string;
if (returnAll) {
resolve(data);
return;
}
if (code == 20 || code == 21) {
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
uni.hideLoading();
if (currentPage.route != "pages/common/login") {
uni.reLaunch({
url: "/pages/common/login"
});
}
} else {
if (`${code}` == "0") {
resolve(data.data);
} else {
uni.hideLoading();
uni.showToast({
title: message != "" ? message : "系统异常",
icon: "none"
});
}
}
},
fail: (error) => {
uni.hideLoading();
let title = "系统异常";
if (typeof error === "string") {
title = error;
} else {
// @ts-ignore
const errMsg = (error as any).errMsg as null | string;
if (errMsg != null && errMsg != "") {
title = errMsg;
} else {
title = JSON.stringify(error);
}
}
console.error(`${params.method} 接口异常:`, error);
uni.showToast({
title,
icon: "error"
});
reject(error);
},
complete: () => {
uni.stopPullDownRefresh();
}
});
}
});
});
}
}
export const request = new Request();