@tmsfe/tms-core 0.0.4 → 0.0.5

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,14 +1,13 @@
1
1
  {
2
2
  "name": "@tmsfe/tms-core",
3
- "version": "0.0.4",
3
+ "version": "0.0.5",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "http://git.code.oa.com/lbsweb/tms/tms-arch.git"
8
8
  },
9
- "main": "dist/index",
10
- "types": "dist/src",
11
- "miniprogram": "dist",
9
+ "main": "src/index",
10
+ "miniprogram": "src",
12
11
  "buildOptions": {
13
12
  "formats": [
14
13
  "esm"
@@ -17,4 +16,4 @@
17
16
  "scripts": {},
18
17
  "author": "tms·web",
19
18
  "gitHead": "72bf52451594b49a1c9f78edbad5956d414a66ca"
20
- }
19
+ }
package/src/config.js CHANGED
@@ -4,6 +4,17 @@
4
4
  import Request from './request';
5
5
  import { getEnvInfo } from './env';
6
6
 
7
+ const parseAllCfgs = (configPaths, resData, defaultCfgs) => configPaths.map((path, index) => {
8
+ const found = resData.find(cfg => cfg.configPath === path);
9
+ if (found) {
10
+ return JSON.parse(found.configValue);
11
+ }
12
+ if (defaultCfgs && defaultCfgs[index]) {
13
+ return defaultCfgs[index];
14
+ }
15
+ return {}; // 没找到配置,返回一个空对象
16
+ });
17
+
7
18
  /**
8
19
  * getConfig 批量拉取配置
9
20
  * @description 拉取运营平台上的配置内容。关于运营平台的具体用法,参见{@link https://iwiki.woa.com/pages/viewpage.action?pageId=527948584}
@@ -51,16 +62,7 @@ function getConfig(configPath, extendAttr = {}, defaultCfg) {
51
62
  .then((res = {}) => {
52
63
  const { errCode, resData } = res || {};
53
64
  if (resData && resData.length > 0) {
54
- const parsed = configPaths.map((path, index) => {
55
- const found = resData.find(cfg => cfg.configPath === path);
56
- if (found) {
57
- return JSON.parse(found.configValue);
58
- }
59
- if (defaultCfgs && defaultCfgs[index]) {
60
- return defaultCfgs[index];
61
- }
62
- return {}; // 没找到配置,返回一个空对象
63
- });
65
+ const parsed = parseAllCfgs(configPaths, resData, defaultCfgs);
64
66
  if (Array.isArray(configPath)) {
65
67
  return parsed;
66
68
  }
@@ -78,6 +80,7 @@ function getConfig(configPath, extendAttr = {}, defaultCfg) {
78
80
  return Promise.reject({ err: e, msg: `获取${configPaths.join(',')}配置失败,接口调用出错` });
79
81
  });
80
82
  }
81
- export default {
83
+
84
+ export {
82
85
  getConfig,
83
86
  };
package/src/env.js CHANGED
@@ -4,6 +4,7 @@ const env = {
4
4
  appVersion: '', // 当前运行时所在项目的版本号
5
5
  appEnv: '', // 运行环境 test - 测试、production - 正式
6
6
  client: '', // 运行时项目名,sinan - 腾讯出行服务; mycar - 我的车小程序
7
+ cloudEnvId: '', // 运行云函数的环境id
7
8
  };
8
9
 
9
10
  let baseAuthInfo = undefined;
@@ -48,11 +49,12 @@ export const getEnvInfo = () => env;
48
49
  * @returns {undefined} 无.
49
50
  */
50
51
  export const setEnvInfo = (envInfo) => {
51
- const { wxAppId, appVersion, appEnv, client } = envInfo;
52
+ const { wxAppId, appVersion, appEnv, client, cloudEnvId } = envInfo;
52
53
  env.wxAppId = wxAppId;
53
54
  env.appVersion = appVersion;
54
55
  env.appEnv = appEnv;
55
56
  env.client = client;
57
+ env.cloudEnvId = cloudEnvId;
56
58
  };
57
59
 
58
60
  let appPagePaths = []; // app页面路径集合
package/src/fastreport.js CHANGED
@@ -5,9 +5,17 @@
5
5
  */
6
6
  import Request from './request';
7
7
  import { getEnvInfo } from './env';
8
+ import Sync from './syncfnmanager';
8
9
 
9
10
  let simulatedUserIdCache; // 模拟用户id在内存里的缓存
10
11
 
12
+ const getSystemInfo = () => {
13
+ const system = Sync.getSystemInfoSync();
14
+ const { model = '', version: wxVersion = '', platform = '', SDKVersion = '', host = '' } = system;
15
+
16
+ return { model, wxVersion, platform, SDKVersion, host };
17
+ };
18
+
11
19
  /**
12
20
  * @class FastReport
13
21
  * @classdesc 快速上报模块,不依赖用户标识和位置
@@ -28,12 +36,10 @@ export default class FastReport {
28
36
  const data = new Array(41);
29
37
  const env = getEnvInfo();
30
38
  Object.keys(param).forEach((key) => {
31
- const valType = typeof param[key];
32
- if (valType === 'string') data[key] = param[key];
33
- else if (valType === 'object') data[key] = JSON.stringify(param[key]);
34
- else data[key] = String(param[key]);
39
+ data[key] = param[key] === null ? '' : JSON.stringify(param[key]);
35
40
  });
36
41
  data[9] = '2';
42
+ data[33] = getSystemInfo();
37
43
  appVer && !data[10] && (data[10] = env.appVersion);
38
44
  if (!data[26]) data[26] = data[27]?.[0];
39
45
  data[28] = env.client;
@@ -45,7 +51,7 @@ export default class FastReport {
45
51
  data[simulatedUserIdIndex] = FastReport.getSimulatedUserId();
46
52
  }
47
53
 
48
- return new Request().post('basic/event/upload', { batch: [data] })
54
+ return new Request({ withAuth: false }).post('basic/event/upload', { batch: [data] })
49
55
  .catch(() => null);
50
56
  }
51
57
 
package/src/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import Request from './request';
2
2
  import FastReport from './fastreport';
3
- import configApi from './config';
3
+ import { getConfig } from './config';
4
4
  import syncApi from './syncfnmanager';
5
5
  import nav from './navigator';
6
6
  import { getLogManager, getRealtimeLogManager } from './log';
@@ -100,6 +100,7 @@ const init = (options = {}) => {
100
100
  appVersion,
101
101
  appEnv,
102
102
  client,
103
+ cloudEnvId,
103
104
  });
104
105
  setAppPagePaths(appPagePaths, homePage);
105
106
  Request.defaultHost = defaultHost;
@@ -132,7 +133,7 @@ const api = {
132
133
  getLocationBaseClass,
133
134
  getEventDispatcher,
134
135
  getEnvInfo,
135
- getConfig: configApi.getConfig,
136
+ getConfig,
136
137
  navigateToWebview: nav.navigateToWebview,
137
138
  isAppPageExist,
138
139
  getHomePage,
@@ -55,12 +55,12 @@ class LocationBase {
55
55
  */
56
56
  public getLocation(showModalWhenCloseAuth: boolean, type: string, content = '', showCancel = true): Promise<WxPostionType> {
57
57
  // 如果缓存中没有, 并且getLocPromise有值, 说明正在请求中。 返回请求的promise
58
- if (!userLocationCache && getLocPromise) {
58
+ if (!userLocationCache && getLocPromise) { // eslint-disable-line
59
59
  return getLocPromise;
60
60
  }
61
61
 
62
62
  // 如果缓存中没有, 并且getLocPromise没有值,新建一个请求
63
- if (!userLocationCache && !getLocPromise) {
63
+ if (!userLocationCache && !getLocPromise) { // eslint-disable-line
64
64
  getLocPromise = this.getWxLocationPromise(showModalWhenCloseAuth, type, content, showCancel);
65
65
  return getLocPromise;
66
66
  }
@@ -61,7 +61,7 @@ class Location extends LocationBase {
61
61
  const cacheName = `${lat}-${lng}-${getPoi}`;
62
62
 
63
63
  // @ts-ignore
64
- if (userLocationCache[cacheName]) {
64
+ if (userLocationCache[cacheName]) { // eslint-disable-line
65
65
  return userLocationCache[cacheName];
66
66
  }
67
67
 
@@ -72,7 +72,7 @@ export async function updateLocStatus(status: boolean): Promise<void> {
72
72
  * @returns { Promise } 用户授权对象
73
73
  */
74
74
  export function getSetting(scopeKey: string = LocationScopeKey) {
75
- if (getSettingPromise) {
75
+ if (getSettingPromise) { // eslint-disable-line
76
76
  return getSettingPromise;
77
77
  }
78
78
 
package/src/navigator.js CHANGED
@@ -46,6 +46,7 @@ const navigateToWebview = ({ url: webUrl, complete = DEFN, message = DEFN, share
46
46
  wx.navigateTo(navSetting);
47
47
  };
48
48
 
49
- export default {
49
+ const obj = {
50
50
  navigateToWebview,
51
51
  };
52
+ export default obj;
package/src/report.js CHANGED
@@ -117,16 +117,19 @@ const getProvinceInfoByIp = () => {
117
117
  return ProvinceInfoCacheByIp;
118
118
  };
119
119
 
120
+ // 用于缓存用户唯一标识userId
121
+ let userId = '';
120
122
  /**
121
- * 获取userId
123
+ * 获取用户唯一标识userId
122
124
  * @private
123
125
  * @returns {string} userId
124
126
  */
125
127
  const getUserId = async () => {
126
128
  if (userId) {
127
- return Promise.resolve(userId);
129
+ return userId;
128
130
  }
129
- const { userId } = await getAuthInfo();
131
+
132
+ ({ userId } = await getAuthInfo());
130
133
  return userId;
131
134
  };
132
135
 
@@ -193,32 +196,25 @@ const formatReportData = async (reportData, deviceData) => {
193
196
 
194
197
  param[5] = await getUserId();
195
198
  param[10] = getMiniProgramVersion(); // 上报小程序版本号
196
- param[19] = system?.host; // 上报宿主app信息 { appId, env, version }
197
199
  param[16] = getLaunchFrom(); // 渠道公共参数
198
200
  param[17] = param[17] || provinceInfo?.province;
199
201
  param[18] = param[18] || provinceInfo?.cityName;
202
+ param[19] = system?.host; // 上报宿主app信息 { appId, env, version }
200
203
  param[28] = client;
201
204
 
202
- if (!param[9]) {
203
- param[9] = '2';
204
- }
205
- if (!param[12]) {
206
- param[12] = netType;
207
- }
208
- if (!param[13]) {
209
- param[13] = '2';
210
- }
211
- if (!param[29]) {
212
- // 上报打开小程序的场景值
213
- param[29] = getAppShowOptions().scene;
214
- }
215
- if (!param[33]) {
216
- param[33] = JSON.stringify(system);
217
- }
218
- if (!param[36]) {
219
- // 上报打开小程序的场景值及参数
220
- param[36] = getAppShowOptions();
221
- }
205
+ // 默认上报数据
206
+ const defaultReportData = {
207
+ 9: '2',
208
+ 12: netType,
209
+ 13: '2',
210
+ 29: getAppShowOptions().scene, // 打开小程序的场景值
211
+ 33: JSON.stringify(system), // 数据字符串化
212
+ 36: getAppShowOptions(), // 打开小程序的场景值及参数
213
+ };
214
+ // 对部分空数据使用默认数据填充
215
+ Object.keys(defaultReportData).forEach((key) => {
216
+ param[key] = (param[key] !== null && JSON.stringify(param[key])) || defaultReportData[key];
217
+ });
222
218
 
223
219
  // 所有上报数据都转换为字符串
224
220
  param.forEach((reportItem, index) => {
package/src/request.js CHANGED
@@ -260,9 +260,6 @@ export default class Request {
260
260
  */
261
261
  async doRequest(path, param = {}, method = 'POST', header = {}) {
262
262
  const res = await this.createRequestTask(path, param, method, header);
263
- const logger = getLogManager();
264
-
265
- logger.log({ path, header, method, param, res: res?.data });
266
263
 
267
264
  if (typeof res?.data === 'string') {
268
265
  return JSON.parse(res?.data);
@@ -298,18 +295,26 @@ export default class Request {
298
295
  * @param {string} param 业务参数
299
296
  * @param {string} method 请求方法 get/post
300
297
  * @param {object} header 自定义的请求头
301
- * @returns {object} 接口返回结果
298
+ * @returns {Promise} 接口返回结果
302
299
  */
303
300
  async createRequestTask(path, param = {}, method = 'POST', header = {}) {
304
301
  const requestParam = await composeParam(param, this.withAuth, this.baseParam);
302
+ const data = sign(requestParam);
303
+ const logger = getLogManager();
305
304
  const res = await new Promise((resolve, reject) => {
306
305
  wx.request({
307
306
  url: this.makeUrl(path),
308
307
  header,
309
308
  method,
310
- data: sign(requestParam),
311
- success: resolve,
312
- fail: reject,
309
+ data,
310
+ success: (res) => {
311
+ resolve(res);
312
+ logger.log({ path, header, method, param: data, res: res?.data });
313
+ },
314
+ fail: (err) => {
315
+ reject(err);
316
+ logger.log({ path, header, method, param: data, err });
317
+ },
313
318
  });
314
319
  });
315
320
  return res;
@@ -95,7 +95,7 @@ export const getAccountInfoSync = () => {
95
95
  }
96
96
  };
97
97
 
98
- export default {
98
+ const obj = {
99
99
  getPlatform,
100
100
  getSystemInfoSync,
101
101
  getLaunchOptionsSync,
@@ -103,3 +103,4 @@ export default {
103
103
  resetSystemInfoSync,
104
104
  getAccountInfoSync,
105
105
  };
106
+ export default obj;
@@ -1,60 +0,0 @@
1
- import { a as getAuthInfo, g as getEnvInfo } from './env-3dc05530.js';
2
-
3
- /**
4
- * callCloudFunc 方法 调用云函数
5
- * @param {String} name 要调用的云函数
6
- * @param {Object} data 需要传给云函数的参数
7
- * @param {Boolean} withAuth 是否需要补充userId,token参数,当云函数需要调用SinanServer时,需要这些参数来完成鉴权
8
- * @returns {viod} 云函数
9
- */
10
-
11
- const callCloudFunc = async (name = '', data = {}, withAuth = true) => {
12
- const {
13
- cloudEnvId,
14
- appVersion
15
- } = getEnvInfo();
16
- const timestamp = Date.now();
17
- const random = Math.random().toString().slice(2, 7);
18
- const sourceId = 7; // 6 未知 7 云函数 8 出行 9 我的车
19
-
20
- const mergedData = { ...data,
21
- timestamp,
22
- seqId: `${timestamp}${sourceId}${random}`,
23
- // 补充seqId参数
24
- appVersion // 补充appVersion参数
25
-
26
- };
27
-
28
- if (withAuth) {
29
- // 补充userId, token参数
30
- const {
31
- userId,
32
- token
33
- } = await getAuthInfo();
34
- Object.assign(mergedData, {
35
- userId,
36
- token
37
- });
38
- }
39
-
40
- const res = await new Promise((resolve, reject) => {
41
- wx.cloud.callFunction({
42
- name,
43
- data: mergedData,
44
- config: {
45
- env: cloudEnvId
46
- },
47
- success: resolve,
48
- fail: err => {
49
- reject({
50
- err,
51
- name,
52
- mergedData
53
- });
54
- }
55
- });
56
- });
57
- return res;
58
- };
59
-
60
- export { callCloudFunc };
@@ -1,142 +0,0 @@
1
- const env = {
2
- wxAppId: '',
3
- // 当前运行时小程序的appId
4
- appVersion: '',
5
- // 当前运行时所在项目的版本号
6
- appEnv: '',
7
- // 运行环境 test - 测试、production - 正式
8
- client: '' // 运行时项目名,sinan - 腾讯出行服务; mycar - 我的车小程序
9
-
10
- };
11
- let baseAuthInfo = undefined;
12
- const getAuthInfoQueue = [];
13
- /**
14
- * @description 设置用户信息
15
- * @param {Object} authInfo 用户信息
16
- * @param {Object} err err
17
- * @returns {Void} 无返回值
18
- */
19
-
20
- const setAuthInfo = (authInfo, err) => {
21
- baseAuthInfo = authInfo;
22
-
23
- while (getAuthInfoQueue.length) {
24
- const pro = getAuthInfoQueue.shift();
25
-
26
- if (err) {
27
- pro.reject(err);
28
- } else {
29
- pro.resolve(baseAuthInfo);
30
- }
31
- }
32
- };
33
- /**
34
- * 获取登录状态信息
35
- * @returns {Object} 当前用户状态参数
36
- */
37
-
38
- const getAuthInfo = async () => {
39
- if (baseAuthInfo !== undefined) {
40
- return baseAuthInfo;
41
- }
42
-
43
- return new Promise((resolve, reject) => getAuthInfoQueue.push({
44
- resolve,
45
- reject
46
- }));
47
- };
48
- /**
49
- * tms.getEnvInfo 用于获取运行时所在的环境信息
50
- * @returns {object} 运行时环境信息.
51
- */
52
-
53
- const getEnvInfo = () => env;
54
- /**
55
- * 设置环境变量
56
- * @param {object} envInfo 环境变量
57
- * @returns {undefined} 无.
58
- */
59
-
60
- const setEnvInfo = envInfo => {
61
- const {
62
- wxAppId,
63
- appVersion,
64
- appEnv,
65
- client
66
- } = envInfo;
67
- env.wxAppId = wxAppId;
68
- env.appVersion = appVersion;
69
- env.appEnv = appEnv;
70
- env.client = client;
71
- };
72
- let appPagePaths = []; // app页面路径集合
73
-
74
- let homePageInfo = null; // 首页信息 { isTab, path, tabs }
75
- /**
76
- * 设置app内所有页面的路径
77
- * @param {Array<String>} paths 页面路径集合
78
- * @param {Object|null} homePage 首页信息
79
- * @param {Boolean} homePath.isTab 首页是否tab页
80
- * @param {String} homePath.path 首页页面路径(isTab=false时使用此字段)
81
- * @param {Array<String>} homePath.tabs tab页面路径列表(isTab=true时使用自此段)
82
- * @returns {void}
83
- */
84
-
85
- const setAppPagePaths = (paths, homePage) => {
86
- appPagePaths = paths;
87
- const {
88
- isTab = false,
89
- path = '',
90
- tabs = []
91
- } = homePage || {};
92
- homePageInfo = {
93
- isTab,
94
- path,
95
- tabs
96
- };
97
- };
98
- /**
99
- * 判断页面是否存在于当前app中
100
- * @param {String} page 页面路径
101
- * @returns {Boolean} 页面是否存在app中
102
- */
103
-
104
- const isAppPageExist = page => {
105
- const route = !page ? '' : String(page).split('?')[0];
106
- if (!route || !Array.isArray(appPagePaths)) return false;
107
- const routeWithoutPrefixSlash = route[0] === '/' ? route.substring(1) : route;
108
- return appPagePaths.some(path => path === routeWithoutPrefixSlash || path === `/${routeWithoutPrefixSlash}`);
109
- }; // 以下变量标识各小程序appId
110
-
111
- const MOBILITY_APPID = 'wx65cc950f42e8fff1'; // 出行服务小程序AppId
112
-
113
- const MOBILITY_DEMO_APPID = 'wxa7ce727b525f80b0'; // 出行服务demo小程序AppId
114
-
115
- const SINAN_HOME = '/modules/index/carlife/pages/index/index'; // 出行首页地址
116
-
117
- const MYCAR_HOME = '/modules/car/index/index'; // 我的车首页地址
118
-
119
- /**
120
- * 获取首页信息
121
- * @returns {Object|null} homePage 首页信息
122
- * @returns {Boolean} homePath.isTab 首页是否tab页
123
- * @returns {String} homePath.path 首页页面路径(isTab=false时使用此字段)
124
- * @returns {Array<String>} homePath.tabs tab页面路径列表(isTab=true时使用自此段)
125
- */
126
-
127
- const getHomePage = () => {
128
- var _wx$getAccountInfoSyn, _wx$getAccountInfoSyn2;
129
-
130
- if (homePageInfo) return homePageInfo; // 返回默认信息
131
-
132
- const mpAppId = (_wx$getAccountInfoSyn = wx.getAccountInfoSync()) === null || _wx$getAccountInfoSyn === void 0 ? void 0 : (_wx$getAccountInfoSyn2 = _wx$getAccountInfoSyn.miniProgram) === null || _wx$getAccountInfoSyn2 === void 0 ? void 0 : _wx$getAccountInfoSyn2.appId;
133
- const homePagePath = [MOBILITY_APPID, MOBILITY_DEMO_APPID].indexOf(mpAppId) > -1 ? SINAN_HOME : MYCAR_HOME;
134
- homePageInfo = {
135
- isTab: false,
136
- path: homePagePath,
137
- tabs: []
138
- };
139
- return homePageInfo;
140
- };
141
-
142
- export { getAuthInfo as a, getHomePage as b, setEnvInfo as c, setAppPagePaths as d, getEnvInfo as g, isAppPageExist as i, setAuthInfo as s };