@tmsfe/tms-core 0.0.179 → 0.0.181

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.179",
3
+ "version": "0.0.181",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
package/src/request.js CHANGED
@@ -125,10 +125,18 @@ 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 { userId, token, openId } = await getAuthInfo();
129
- requestParam.userId = userId;
130
- requestParam.token = token;
131
- requestParam.openId = openId;
128
+ // 优先使用wx.tmsLoginPromise获取鉴权信息,否则调用getAuthInfo()
129
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
130
+ const authInfo = wx.tmsLoginPromise
131
+ ? await wx.tmsLoginPromise
132
+ : await getAuthInfo();
133
+
134
+ // 统一处理鉴权参数赋值
135
+ Object.assign(requestParam, {
136
+ userId: authInfo.uid || authInfo.userId,
137
+ token: authInfo.token,
138
+ openId: authInfo.openId,
139
+ });
132
140
  return requestParam;
133
141
  }
134
142
  delete requestParam.userId;
@@ -364,8 +372,10 @@ export default class Request {
364
372
  * @param {object} header 自定义的请求头
365
373
  * @returns {Promise} 接口返回结果
366
374
  */
367
- async createRequestTask(path, param = {}, method = 'POST', header = {}) {
368
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
375
+ async createRequestTask(path, param = {}, method = 'POST', header = {}, hasRetry = false) {
376
+ // maplbs接口不需要token
377
+ const withAuth = path.includes('maplbs/ws') ? false : this.withAuth;
378
+ const requestParam = await composeParam(param, withAuth, this.baseParam);
369
379
  const data = sign(requestParam, this.secretKey);
370
380
  return new Promise((resolve, reject) => {
371
381
  const requestTime = Date.now();
@@ -404,21 +414,109 @@ export default class Request {
404
414
  }
405
415
  };
406
416
 
417
+ const success = async (res) => {
418
+ printLog(true, res.data);
419
+ // format 业务errcode,用于判断是否做重试
420
+ const { errCode } = formatResErrCode(path, res.data);
421
+ // 处理token失效情况(errcode=35)
422
+ const needRetryLogin = !hasRetry && errCode === 35 && isPathNeedRetryLogin(path);
423
+ if (needRetryLogin) {
424
+ try {
425
+ // 调用runtime的login方法刷新token
426
+ await loginRefresh();
427
+ // 刷新后重试请求
428
+ const retryRes = await this.createRequestTask(path, param, method, header, true);
429
+ resolve(retryRes);
430
+ return;
431
+ } catch (e) {
432
+ // 刷新token失败,直接返回原错误
433
+ resolve(res);
434
+ return;
435
+ }
436
+ }
437
+ resolve(res);
438
+ };
439
+ const fail = (err) => {
440
+ printLog(false, err);
441
+ reject(err);
442
+ };
443
+
407
444
  wx.request({
408
445
  url: this.makeUrl(path),
409
446
  header,
410
447
  method,
411
448
  data,
412
449
  enableHttp2: true,
413
- success: (res) => {
414
- printLog(true, res.data);
415
- resolve(res);
416
- },
417
- fail: (err) => {
418
- printLog(false, err);
419
- reject(err);
420
- },
450
+ success,
451
+ fail,
421
452
  });
422
453
  });
423
454
  }
424
455
  }
456
+
457
+ /**
458
+ * 判断指定路径是否需要token失效重试
459
+ * @param {string} path 请求路径
460
+ * @returns {boolean} 是否需要重试
461
+ */
462
+ const isPathNeedRetryLogin = (path) => {
463
+ // 定义不需要重试的路由白名单
464
+ const noRetryPaths = [
465
+ 'user/login', // 登录接口本身
466
+ 'basic/event/upload', // 事件上报接口
467
+ ];
468
+
469
+ // 检查路径是否在白名单中
470
+ return noRetryPaths.every(noRetryPath => !path.includes(noRetryPath));
471
+ };
472
+
473
+ let refreshPromise;
474
+ async function loginRefresh() {
475
+ if (!refreshPromise) {
476
+ // 调用runtime的login方法刷新token
477
+ const { tms } = getApp();
478
+ wx.tmsLoginPromise = undefined;
479
+ try {
480
+ refreshPromise = tms.login();
481
+ await refreshPromise;
482
+ // 登录成功,清除Promise
483
+ refreshPromise = undefined;
484
+ } catch (err) {
485
+ // 登录失败重试
486
+ setTimeout(() => {
487
+ refreshPromise = undefined;
488
+ }, 100);
489
+ }
490
+ }
491
+ return await refreshPromise;
492
+ }
493
+
494
+ // 统一返回格式
495
+ function formatResErrCode(path, res) {
496
+ // 比如apollo/config接口在sinan-service层因鉴权失败等原因,
497
+ // 返回的结果是不带res.resData的,所以取res,有errCode可以让后续走到错误流程中去
498
+ const resData = res.resData || res;
499
+
500
+ // 接入层转发的Apollo接口
501
+ if (path === 'marketing/apollo/config') {
502
+ return {
503
+ errCode: resData.status,
504
+ };
505
+ }
506
+
507
+ // 接入层转发的代驾、顺风车、飞机、im接口
508
+ if (/^(dd|hitchride|integration|im\/server)\//.test(path)) {
509
+ return {
510
+ errCode: resData.code,
511
+ };
512
+ }
513
+
514
+ // 接入层转发公共交通接口
515
+ if (path.startsWith('v2/bus')) {
516
+ return {
517
+ errCode: resData.errCode,
518
+ };
519
+ }
520
+
521
+ return res;
522
+ }
@@ -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