fetch-api.js
7.12 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
let config = require('./../config');
let Storage = require('./../utils/stroage');
let util = require('./../utils/util');
let Router = require('./../router/index');
let Encrypt = require('./../utils/enc/enc');
let api = require('./api');
const errMsg = '服务异常,请稍后重试';
/**
* 重新获取 sessionId
* 1.重新获取wxcode,
* 2.调用login方法,传wxcode,给后端重新换取js_session
* @returns
*/
function login() {
return new Promise((resolve, reject) => {
// my.login({
// success: (res) => {
let res = {
code: util.uuid()
}
let baseUrl = config.NET_CONFIG.BASE_API + config.NET_CONFIG.API_PREFIX;
let header = {
'-kd-platform-env': config.ENV,
'-kd-platform-module': config.NET_CONFIG.MODULE_CODE
}
let app = getApp();
let globalData = app && app.globalData || null
let data = {
code: res.code,
miniproChannel: globalData.channelCode || "",
channelCode: globalData.channelCode || "",
parentMemberCode: globalData.parentMemberCode || "",
platform: "minipro",
};
data = Encrypt.makeEncrypt(data);
my.request({
// url: baseUrl + api.wxLogin,
url: baseUrl + api.login,
sid: false,
data: data,
method: 'POST',
header: header,
success: (res) => {
let {
code,
content
} = res.data;
if (code == 200 && content.sessionId) {
Storage.setItem('sessionId', content.sessionId); // 保存新的sessionId
resolve();
} else {
reject();
}
}
})
// }
// });
});
}
/**
* 检查并埋下sessionId
* @param {*} sid 接口是否需要sessionId
* @returns
*/
function checkSessionId(sid) {
return new Promise((resolve, reject) => {
let sessionId = Storage.getItem('sessionId');
if (sid) {
// 拥有sessionId
if (sessionId) {
resolve();
} else {
// 重新获取 sessionId
login().then((result) => {
resolve();
}).catch((err) => {
reject();
});
}
} else {
// 不需要 sessionId 忽略
resolve();
}
});
}
function fetchMethod(val) {
let params = Object.assign({
loading: false,
toast: true,
sid: true,
mode: "",
url: "",
data: null,
method: "GET",
enc: false
}, val);
let promise = new Promise((resolve, reject) => {
checkSessionId(params.sid).then(() => {
doRequest(params).then((result) => {
resolve(result);
}).catch((err) => {
// 404 或 403 错误码的时候需要重新拉一次
if (err.code == 404 || err.code == 403) {
login().then((loginRes) => {
doRequest(params).then((result2) => {
resolve(result2);
});
}).catch((err) => {});
} else {
reject(err)
}
});
});
});
return promise;
}
/**
* 做实际请求
* @param {*} params
* @returns
*/
function doRequest(params) {
return new Promise((resolve, reject) => {
let {
loading,
toast,
mode,
url,
data,
method,
responseType,
enc
} = params;
let baseUrl = config.NET_CONFIG.BASE_API + config.NET_CONFIG.API_PREFIX;
// 使用通用接口前缀
if (mode == "common") baseUrl = config.NET_CONFIG.COMMON_API;
// 自定义前缀,即不使用前缀
if (mode == "custom") baseUrl = "";
if (loading && mode != 'log') my.showLoading({
content: '加载中',
mask: true
});
// 判断是否开启post方法默认加密
if (config.NET_CONFIG.IS_POST_ENC && (method == "post" || method == "POST")) {
enc = true;
}
if (enc) data = Encrypt.makeEncrypt(data);
// 设置请求头
let header = {
'-kd-platform-env': config.ENV,
'-kd-platform-module': config.NET_CONFIG.MODULE_CODE,
'sessionId': Storage.getItem("sessionId")
}
my.request({
url: baseUrl + url, //请求地址
data: data, //自定义参数
method: method || 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
header: header,
responseType: responseType,
success: (result) => {
// 日志上传不需要处理结果
if (mode == 'log') return;
let res = result.data;
if (responseType == "arraybuffer") {
if (loading) my.hideLoading();
resolve(res)
} else if (res.code === 200) {
if (loading) my.hideLoading();
resolve(res.content);
} else {
my.hideLoading();
// 404 不需要toast
if (toast && res.code != 404) {
my.showToast({
content: res.errMsg || errMsg,
icon: 'none'
})
}
reject(res);
}
},
fail: (err) => {
my.hideLoading();
}
});
});
}
// -------- 业务接口 Start --------
/**
* 上传文件到恪道
* @param {*} filePath 图片地址
* @param {*} subPath 上传目录 (此参数为空则使用 oss目录)
*/
function uploadFile(filePath, subPath) {
return new Promise((resolve, reject) => {
if (!subPath && !config.NET_CONFIG.OSS_PATH) {
throw new Error('请配置上传目录 !');
reject();
return;
}
my.uploadFile({
url: config.NET_CONFIG.BASE_API + config.NET_CONFIG.API_PREFIX + api.uploadFile,
filePath: filePath,
name: 'file',
header: {
'content-type': 'multipart/form-data',
'-kd-platform-env': config.ENV,
'-kd-platform-module': config.NET_CONFIG.MODULE_CODE,
},
formData: {
subPath: subPath || config.NET_CONFIG.OSS_PATH
},
success(res) {
let result = JSON.parse(res.data).content;
resolve(result);
},
fail() {
reject()
},
complete() {}
})
});
}
/**
* 上传多个文件到服务器
* urlList 需要上传的图片地址 (my.chooseImage 的 paths列表)
*
*/
function uploadFileMultiple(filePathList, subPath) {
return new Promise((resolve, reject) => {
if (!subPath && !config.NET_CONFIG.OSS_PATH) {
throw new Error('请配置上传目录 !');
reject();
return;
}
console.log("config.NET_CONFIG.COMMON_API + api.uploadFile:", config.NET_CONFIG.COMMON_API + api.uploadFile);
my.showLoading({
content: `加载中`
})
console.log("filePathList:", filePathList);
if (filePathList && filePathList.length > 0) {
let promiseList = [];
filePathList.forEach(element => {
let promiseItem = new Promise((resolve2, reject2) => {
uploadFile(element, subPath).then((result2) => {
resolve2(result2)
}).catch((err) => {
reject2();
});
});
promiseList.push(promiseItem);
});
console.log("promiseList:", promiseList);
Promise.all(promiseList).then(uploadFiles => {
my.hideLoading()
resolve(uploadFiles)
}, reason => {
my.hideLoading()
reject();
});
} else {
my.hideLoading()
resolve([]);
}
});
}
module.exports = {
fetchMethod,
uploadFile,
uploadFileMultiple
}