@tmsfe/tms-core 0.0.178 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.178",
3
+ "version": "0.0.180",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -317,7 +317,7 @@ const cryptRuleUtil = {
317
317
  // 远程加密服务是否开启
318
318
  isServerOpen: (): boolean => !!wx.$_publicKey,
319
319
  // 检查path是否符合下发的路由前缀
320
- pathInUnablePrefix: (path: string): boolean => {
320
+ pathInEnablePrefix: (path: string): boolean => {
321
321
  if (!wx.$_publicKey) {
322
322
  return false;
323
323
  }
@@ -392,8 +392,8 @@ const isCryptoRuleMath = (path: string, reqData: any): BaseResp<boolean> => {
392
392
  if (!cryptRuleUtil.isServerOpen()) {
393
393
  return new baseUtil.BaseRespFac(false, false, '服务端加密未开启');
394
394
  }
395
- // 请求路由满足服务端下发的不加密规则,不走加密
396
- if (cryptRuleUtil.pathInUnablePrefix(path)) {
395
+ // 请求路由不满足服务端下发的加密规则,不走加密
396
+ if (!cryptRuleUtil.pathInEnablePrefix(path)) {
397
397
  return new baseUtil.BaseRespFac(false, false, '未命中服务端加密规则');
398
398
  }
399
399
  // 请求接口是加密性能埋点上报接口,不加密
@@ -135,6 +135,7 @@ const calNavBarHeight = (menuTop, menuHeight, statusBarHeight, apiCategory) => {
135
135
  * @returns {Object} data.enterOptions 启动参数
136
136
  * @returns {Object} data.enterOptions.apiCategory API 类别(不同apiCategory场景下的API有不同限制,UI也有不同)
137
137
  */
138
+ /* eslint-disable */
138
139
  const getNavBarConfigData = () => { // eslint-disable-line require-jsdoc
139
140
  const systemInfo = getSysInfo();
140
141
  const { brand, getSystemInfoSuccess, platform } = systemInfo;
@@ -151,7 +152,9 @@ const getNavBarConfigData = () => { // eslint-disable-line require-jsdoc
151
152
  statusBarHeight = isIOS ? StatusBarHeightOnIOS : StatusBarHeightOnAndroid;
152
153
  }
153
154
 
154
- const enable = compareVersion(version, '7.0.0') >= 0 || brand === 'devtools' || platform === 'ohos'; // 微信版本是否支持自定义顶栏,不支持时自动隐藏
155
+ const isPCShowNavBar = getPCNavbarShowStatus(systemInfo);
156
+ // 手机端微信版本version格式为7.0.0,电脑端为4.0.3.93,compareVersion(version, '7.0.0') >= 0只处理了手机端是否展示自定义导航栏
157
+ const enable = compareVersion(version, '7.0.0') >= 0 || brand === 'devtools' || platform === 'ohos' || isPCShowNavBar; // 微信版本是否支持自定义顶栏,不支持时自动隐藏
155
158
  const enterOptions = getEnterOptions();
156
159
  if (enable) {
157
160
  return { enable, ...calculateNavBarLayout(isIOS, statusBarHeight, enterOptions.apiCategory) };
@@ -160,6 +163,20 @@ const getNavBarConfigData = () => { // eslint-disable-line require-jsdoc
160
163
  return { enable };
161
164
  };
162
165
 
166
+ // 电脑端是否展示导航栏
167
+ const getPCNavbarShowStatus = (systemInfo) => {
168
+ const { SDKVersion, platform } = systemInfo;
169
+ if (['windows', 'mac'].includes(platform)) {
170
+ try {
171
+ return compareVersion(SDKVersion, '3.6.1') >= 0;
172
+ } catch (_) {
173
+ return true;
174
+ }
175
+ } else {
176
+ return false;
177
+ }
178
+ };
179
+
163
180
  // 获取需要对外暴露的启动参数
164
181
  const getEnterOptions = () => {
165
182
  const options = wx.getEnterOptionsSync();
package/src/request.js CHANGED
@@ -9,7 +9,7 @@
9
9
  * 框架判断在不同的运行环境,切换调用不同运行环境提供的方法。
10
10
  */
11
11
  import md5 from './md5';
12
- import { getEnvInfo, getAuthInfo } from './env';
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 { userId, token, openId } = await getAuthInfo();
129
- requestParam.userId = 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
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
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: (res) => {
414
- printLog(true, res.data);
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
+ }
@@ -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