@tmsfe/tms-core 0.0.179 → 0.0.180
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/request.js +103 -13
- package/src/runtime/index.js +2 -0
package/package.json
CHANGED
package/src/request.js
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* 框架判断在不同的运行环境,切换调用不同运行环境提供的方法。
|
|
10
10
|
*/
|
|
11
11
|
import md5 from './md5';
|
|
12
|
-
import { getEnvInfo
|
|
12
|
+
import { getEnvInfo } from './env';
|
|
13
13
|
import { safeJsonParse } from './objUtils';
|
|
14
14
|
import { encryptObjInit } from './encrypt/index';
|
|
15
15
|
import reporter from './report/index';
|
|
@@ -125,8 +125,8 @@ const composeParam = async (param = {}, withAuth = true, baseParam = {}) => {
|
|
|
125
125
|
const modifyAuthParam = async (param, withAuth) => {
|
|
126
126
|
const requestParam = { ...param };
|
|
127
127
|
if (withAuth) {
|
|
128
|
-
const {
|
|
129
|
-
requestParam.userId =
|
|
128
|
+
const { uid, token, openId } = await wx.tmsLoginPromise;
|
|
129
|
+
requestParam.userId = uid;
|
|
130
130
|
requestParam.token = token;
|
|
131
131
|
requestParam.openId = openId;
|
|
132
132
|
return requestParam;
|
|
@@ -364,8 +364,10 @@ export default class Request {
|
|
|
364
364
|
* @param {object} header 自定义的请求头
|
|
365
365
|
* @returns {Promise} 接口返回结果
|
|
366
366
|
*/
|
|
367
|
-
async createRequestTask(path, param = {}, method = 'POST', header = {}) {
|
|
368
|
-
|
|
367
|
+
async createRequestTask(path, param = {}, method = 'POST', header = {}, hasRetry = false) {
|
|
368
|
+
// maplbs接口不需要token
|
|
369
|
+
const withAuth = path.includes('maplbs/ws') ? false : this.withAuth;
|
|
370
|
+
const requestParam = await composeParam(param, withAuth, this.baseParam);
|
|
369
371
|
const data = sign(requestParam, this.secretKey);
|
|
370
372
|
return new Promise((resolve, reject) => {
|
|
371
373
|
const requestTime = Date.now();
|
|
@@ -404,21 +406,109 @@ export default class Request {
|
|
|
404
406
|
}
|
|
405
407
|
};
|
|
406
408
|
|
|
409
|
+
const success = async (res) => {
|
|
410
|
+
printLog(true, res.data);
|
|
411
|
+
// format 业务errcode,用于判断是否做重试
|
|
412
|
+
const { errCode } = formatResErrCode(path, res.data);
|
|
413
|
+
// 处理token失效情况(errcode=35)
|
|
414
|
+
const needRetryLogin = !hasRetry && errCode === 35 && isPathNeedRetryLogin(path);
|
|
415
|
+
if (needRetryLogin) {
|
|
416
|
+
try {
|
|
417
|
+
// 调用runtime的login方法刷新token
|
|
418
|
+
await loginRefresh();
|
|
419
|
+
// 刷新后重试请求
|
|
420
|
+
const retryRes = await this.createRequestTask(path, param, method, header, true);
|
|
421
|
+
resolve(retryRes);
|
|
422
|
+
return;
|
|
423
|
+
} catch (e) {
|
|
424
|
+
// 刷新token失败,直接返回原错误
|
|
425
|
+
resolve(res);
|
|
426
|
+
return;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
resolve(res);
|
|
430
|
+
};
|
|
431
|
+
const fail = (err) => {
|
|
432
|
+
printLog(false, err);
|
|
433
|
+
reject(err);
|
|
434
|
+
};
|
|
435
|
+
|
|
407
436
|
wx.request({
|
|
408
437
|
url: this.makeUrl(path),
|
|
409
438
|
header,
|
|
410
439
|
method,
|
|
411
440
|
data,
|
|
412
441
|
enableHttp2: true,
|
|
413
|
-
success
|
|
414
|
-
|
|
415
|
-
resolve(res);
|
|
416
|
-
},
|
|
417
|
-
fail: (err) => {
|
|
418
|
-
printLog(false, err);
|
|
419
|
-
reject(err);
|
|
420
|
-
},
|
|
442
|
+
success,
|
|
443
|
+
fail,
|
|
421
444
|
});
|
|
422
445
|
});
|
|
423
446
|
}
|
|
424
447
|
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* 判断指定路径是否需要token失效重试
|
|
451
|
+
* @param {string} path 请求路径
|
|
452
|
+
* @returns {boolean} 是否需要重试
|
|
453
|
+
*/
|
|
454
|
+
const isPathNeedRetryLogin = (path) => {
|
|
455
|
+
// 定义不需要重试的路由白名单
|
|
456
|
+
const noRetryPaths = [
|
|
457
|
+
'user/login', // 登录接口本身
|
|
458
|
+
'basic/event/upload', // 事件上报接口
|
|
459
|
+
];
|
|
460
|
+
|
|
461
|
+
// 检查路径是否在白名单中
|
|
462
|
+
return !noRetryPaths.some(noRetryPath => path.includes(noRetryPath));
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
|
|
466
|
+
let refreshFlag = false;
|
|
467
|
+
async function loginRefresh() {
|
|
468
|
+
if (!refreshFlag) {
|
|
469
|
+
// 调用runtime的login方法刷新token
|
|
470
|
+
const { tms } = getApp();
|
|
471
|
+
wx.tmsLoginPromise = undefined;
|
|
472
|
+
try {
|
|
473
|
+
refreshFlag = true;
|
|
474
|
+
await tms.login();
|
|
475
|
+
// 登录成功,清除Promise
|
|
476
|
+
refreshFlag = false;
|
|
477
|
+
} catch (err) {
|
|
478
|
+
// 登录失败重试
|
|
479
|
+
setTimeout(() => {
|
|
480
|
+
refreshFlag = false;
|
|
481
|
+
}, 100);
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// 统一返回格式
|
|
487
|
+
function formatResErrCode(path, res) {
|
|
488
|
+
// 比如apollo/config接口在sinan-service层因鉴权失败等原因,
|
|
489
|
+
// 返回的结果是不带res.resData的,所以取res,有errCode可以让后续走到错误流程中去
|
|
490
|
+
const resData = res.resData || res;
|
|
491
|
+
|
|
492
|
+
// 接入层转发的Apollo接口
|
|
493
|
+
if (path === 'marketing/apollo/config') {
|
|
494
|
+
return {
|
|
495
|
+
errCode: resData.status,
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// 接入层转发的代驾、顺风车、飞机、im接口
|
|
500
|
+
if (/^(dd|hitchride|integration|im\/server)\//.test(path)) {
|
|
501
|
+
return {
|
|
502
|
+
errCode: resData.code,
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// 接入层转发公共交通接口
|
|
507
|
+
if (path.startsWith('v2/bus')) {
|
|
508
|
+
return {
|
|
509
|
+
errCode: resData.errCode,
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
return res;
|
|
514
|
+
}
|
package/src/runtime/index.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import Login from './login';
|
|
9
9
|
import Car from './car';
|
|
10
|
+
import { setAuthInfo } from '../env';
|
|
10
11
|
import getOpenAppTrafficData from './getopenapptrafficdata';
|
|
11
12
|
|
|
12
13
|
const { loginFn, getOpenId, getMycarPubOpenId, getSinanPubOpenId, getPhone, registerPhone } = Login;
|
|
@@ -79,6 +80,7 @@ const login = async () => {
|
|
|
79
80
|
const res = await loginFn();
|
|
80
81
|
const loginInfo = { ...res, userId: res.uid };
|
|
81
82
|
__resolver__.getLoginInfo(loginInfo);
|
|
83
|
+
setAuthInfo(loginInfo);
|
|
82
84
|
return loginInfo;
|
|
83
85
|
};
|
|
84
86
|
|