@tmsfe/tms-core 0.0.18 → 0.0.22

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.
@@ -1,533 +0,0 @@
1
- import { m as md5 } from './md5-34a9daf3.js';
2
- import { a as getAuthInfo, g as getEnvInfo } from './env-c7da70e1.js';
3
-
4
- /**
5
- * 本文件主要负责在小程序中日志打印功能,包含本地日志及实时日志. 主要做了两件事:
6
- * 1、参数序列化处理;支持传递任意多个参数,并对类型为对象的参数进行字符串序列化处理(避免打印出来是'[Object Object]'的格式);
7
- * 2、低版本兼容;
8
- */
9
- // 低版本不支持getLogManager或者getRealtimeLogManager时,用ManagerForLowerVersionLib来兼容
10
- const ManagerForLowerVersionLib = {
11
- debug: () => {},
12
- info: () => {},
13
- log: () => {},
14
- warn: () => {},
15
- error: () => {},
16
- addFilterMsg: () => {},
17
- setFilterMsg: () => {}
18
- }; // 小程序基础库2.7.1版本以上支持,所以需要兼容性处理
19
-
20
- let logInstance = null;
21
- let rtLogInstance = null;
22
-
23
- function getLogInstance() {
24
- if (logInstance === null) {
25
- logInstance = wx.getLogManager ? wx.getLogManager() : ManagerForLowerVersionLib;
26
- }
27
-
28
- return logInstance;
29
- }
30
-
31
- function getRTLogInstance() {
32
- if (rtLogInstance === null) {
33
- rtLogInstance = wx.getRealtimeLogManager ? wx.getRealtimeLogManager() : ManagerForLowerVersionLib;
34
- }
35
-
36
- return rtLogInstance;
37
- }
38
- /**
39
- * 参数中有对象类型的,将其转换为字符串类型,以便查看
40
- * @param {Array<Any>} params 需要格式化的数据
41
- * @returns {Array<String>} 字符串序列化后的数据
42
- */
43
-
44
-
45
- const format = params => params.map(param => typeof param === 'string' ? param : JSON.stringify(param));
46
- /**
47
- * @namespace LOG
48
- * @description 普通日志管理器,将日志记录在小程序日志文件中,用户上传后,可以在小程序后台-反馈管理中看到
49
- */
50
-
51
-
52
- const LOG = {
53
- /**
54
- * @description 写debug日志
55
- * @param {...Any} params 需要打印的数据,支持任意多个
56
- * @returns {Void} 无返回值
57
- */
58
- debug(...params) {
59
- getLogInstance().debug(...format(params));
60
- },
61
-
62
- /**
63
- * @description 写info日志
64
- * @param {...Any} params 需要打印的数据,支持任意多个
65
- * @returns {Void} 无返回值
66
- */
67
- info(...params) {
68
- getLogInstance().info(...format(params));
69
- },
70
-
71
- /**
72
- * @description 写log日志
73
- * @param {...Any} params 需要打印的数据,支持任意多个
74
- * @returns {Void} 无返回值
75
- */
76
- log(...params) {
77
- getLogInstance().log(...format(params));
78
- },
79
-
80
- /**
81
- * @description 写warn日志
82
- * @param {...Any} params 需要打印的数据,支持任意多个
83
- * @returns {Void} 无返回值
84
- */
85
- warn(...params) {
86
- getLogInstance().warn(...format(params));
87
- },
88
-
89
- /**
90
- * @description 写warn日志. LogManager并没有error方法,为了兼容旧代码,所以声明一个error方法
91
- * @param {...Any} params 需要打印的数据,支持任意多个
92
- * @returns {Void} 无返回值
93
- */
94
- error(...params) {
95
- LOG.warn(...params);
96
- }
97
-
98
- };
99
- /**
100
- * @namespace RTLOG
101
- * @description 实时日志,将日志实时上传至小程序后台-开发-运维中心-实时日志,方便快速排查漏洞,定位问题
102
- */
103
-
104
- const RTLOG = {
105
- /**
106
- * @description 写info日志
107
- * @param {...Any} params 需要打印的数据,支持任意多个
108
- * @returns {Void} 无返回值
109
- */
110
- info(...params) {
111
- getRTLogInstance().info(...format(params));
112
- },
113
-
114
- /**
115
- * @description 写warn日志
116
- * @param {...Any} params 需要打印的数据,支持任意多个
117
- * @returns {Void} 无返回值
118
- */
119
- warn(...params) {
120
- getRTLogInstance().warn(...format(params));
121
- },
122
-
123
- /**
124
- * @description 写error日志
125
- * @param {...Any} params 需要打印的数据,支持任意多个
126
- * @returns {Void} 无返回值
127
- */
128
- error(...params) {
129
- getRTLogInstance().error(...format(params));
130
- },
131
-
132
- /**
133
- * @description 添加过滤关键字
134
- * @param {String} msg 关键字
135
- * @returns {Void} 无返回值
136
- */
137
- addFilterMsg(msg) {
138
- getRTLogInstance().addFilterMsg(msg);
139
- },
140
-
141
- /**
142
- * @description 设置过滤关键字
143
- * @param {String} msg 关键字
144
- * @returns {Void} 无返回值
145
- */
146
- setFilterMsg(msg) {
147
- getRTLogInstance().setFilterMsg(msg);
148
- }
149
-
150
- };
151
- /**
152
- * @description 获取日志管理器对象,该对象提供的方法同wx.getLogManager()提供的方法,详见微信文档
153
- * @returns {Object} [LOG](#namespace-log)
154
- * @example
155
- * const logger = getLogManager();
156
- * logger.log(1, 'str', { a: 1 }, ...);
157
- * logger.info(1, 'str', { a: 1 }, ...);
158
- * logger.debug(1, 'str', { a: 1 }, ...);
159
- * logger.awrn(1, 'str', { a: 1 }, ...);
160
- */
161
-
162
- const getLogManager = () => LOG;
163
- /**
164
- * @description 获取实时日志管理器对象,该对象提供的方法同wx.getRealtimeLogManager()提供的方法,详见微信文档
165
- * @returns {Object} [RTLOG](#namespace-rtlog)
166
- * @example
167
- * const logger = getRealtimeLogManager();
168
- * logger.info(1, 'str', { a: 1 }, ...);
169
- * logger.warn(1, 'str', { a: 1 }, ...);
170
- * logger.error(1, 'str', { a: 1 }, ...);
171
- */
172
-
173
-
174
- const getRealtimeLogManager = () => RTLOG;
175
-
176
- /**
177
- * @copyright 2021-present, Tencent, Inc. All rights reserved.
178
- * @brief request.js用于发起网络请求.
179
- * request模块作为基于 tms-core & tms-runtime 的应用的公共请求模块。
180
- * 目前支持在出行服务小程序或基于出行服务的小程序中调用。在后续tms-runtime支持公众号H5后,
181
- * 将支持在H5中调用。
182
- *
183
- * 考虑到对不同运行环境的支持,强依赖运行环境的依赖,比如 wx.request,应通过注入的形式提供。
184
- * 框架判断在不同的运行环境,切换调用不同运行环境提供的方法。
185
- */
186
- /**
187
- * 用于序列化需要签名的参数
188
- * @private
189
- * @param {object} param 需要序列化的参数
190
- * @returns {string} 序列化之后的参数字符串
191
- */
192
-
193
- const seriesParam = param => {
194
- const keys = Object.keys(param).filter(key => typeof param[key] !== 'undefined').sort();
195
- const series = keys.map(key => {
196
- const val = param[key];
197
- return `${key}${typeof val === 'object' ? JSON.stringify(val) : val}`;
198
- });
199
- return series.join('');
200
- };
201
- /**
202
- * 用于对request请求对象做签名
203
- * @private
204
- * @param {object} param 需要做签名的参数
205
- * @returns {object} 签名后的参数对象
206
- */
207
-
208
-
209
- const sign = (param = {}) => {
210
- const token = '';
211
- const signture = md5(seriesParam(param) + token);
212
- return { ...param,
213
- sign: signture
214
- };
215
- };
216
- /**
217
- * 用于对request请求对象添加系统参数
218
- * @private
219
- * @param {object} param 接口调用传入的参数
220
- * @param {Boolean} withAuth 是否需要登录参数
221
- * @param {object} baseParam request实例定义的基础参数
222
- * @returns {object} 全部参数对象
223
- */
224
-
225
-
226
- const composeParam = async (param = {}, withAuth = true, baseParam = {}) => {
227
- const version = '1.0';
228
- const {
229
- appVersion,
230
- wxAppId,
231
- client
232
- } = getEnvInfo();
233
- const nonce = Math.random().toString(36).substr(2, 10);
234
- const timestamp = Date.now();
235
- const random = Math.random().toString().slice(2, 7);
236
- const sourceId = ['', 'sinan', 'mycar'].indexOf(client) + 7; // 6 未知 7 云函数 8 出行 9 我的车
237
-
238
- const seqId = `${timestamp}${sourceId}${random}`;
239
- const paramsWithAuth = await modifyAuthParam(param, withAuth);
240
- const combinedParam = Object.assign({
241
- version,
242
- appVersion,
243
- nonce,
244
- timestamp,
245
- seqId,
246
- wxAppId
247
- }, { ...baseParam
248
- }, { ...paramsWithAuth
249
- });
250
- return combinedParam;
251
- };
252
- /**
253
- * 用于保证业务参数的登录态参数,
254
- * 若接口不依赖登录态 如 user/login,则保证参数中不包括userId & token,
255
- * 若接口依赖登录态,则保证参数中填充userId & token,
256
- * @private
257
- * @param {object} param 要校验登录态的业务参数
258
- * @param {boolean} withAuth 是否要校验登录态
259
- * @returns {object} 增加登录态后的参数
260
- */
261
-
262
-
263
- const modifyAuthParam = async (param, withAuth) => {
264
- const requestParam = { ...param
265
- };
266
-
267
- if (withAuth) {
268
- const {
269
- userId,
270
- token
271
- } = await getAuthInfo();
272
- requestParam.userId = userId;
273
- requestParam.token = token;
274
- return requestParam;
275
- }
276
-
277
- delete requestParam.userId;
278
- delete requestParam.userid;
279
- delete requestParam.token;
280
- return requestParam;
281
- };
282
- /**
283
- * @public
284
- * @class Request
285
- * @classdesc 网络请求类,对签名、鉴权等逻辑进行封装处理,用于向腾讯出行服务平台后台发送网络请求
286
- */
287
-
288
-
289
- class Request {
290
- /**
291
- * 默认的request host域名
292
- * defaultHost 在tms-runtime初始化时进行设置,为出行服务接入层域名
293
- * 具体业务模块 new Request() 使用时,不指定自定义 host ,将使用defaultHost
294
- */
295
- static defaultHost = '';
296
- host = '';
297
- withAuth = true;
298
- baseParam = {};
299
- /**
300
- * Request 构造函数
301
- * @param {Object} config 构造参数
302
- * @param {Object} config.withAuth 是否填充登录态参数
303
- * @param {Object} config.host 自定义的host域名
304
- * @param {Object} config.baseParam 默认携带的参数
305
- */
306
-
307
- constructor(config = {
308
- withAuth: true
309
- }) {
310
- if (config.host) {
311
- this.host = config.host;
312
- }
313
-
314
- if (typeof config.withAuth !== 'undefined') {
315
- this.withAuth = !!config.withAuth;
316
- }
317
-
318
- this.baseParam = config.baseParam || {};
319
- }
320
- /**
321
- * 格式化接口路径
322
- * @private
323
- * @param {string} path 需要格式化的接口路径
324
- * @returns {string} 格式化后的接口路径
325
- */
326
-
327
-
328
- makeUrl(path) {
329
- if (/^http/i.test(path)) return path;
330
- const host = this.host || Request.defaultHost;
331
- const validHost = /^http/i.test(host) ? host : `https://${host}`;
332
- return `${validHost}/${path}`;
333
- }
334
-
335
- /**
336
- * @public
337
- * @memberof Request
338
- * @param {String} path 请求接口路径
339
- * @param {Object} [param] 请求参数
340
- * @param {Object} [header] 自定义请求头
341
- * @returns {Promise} 接口响应
342
- * @example
343
- * const $ = getApp().tms.createRequest();
344
- * $.get(apiPath)
345
- * .then((data) => {
346
- * // data {Object} 响应数据
347
- * // {
348
- * // errCode {Number} 接口响应状态码
349
- * // errMsg {String} 接口响应状态信息
350
- * // resData {Object} 接口返回数据
351
- * // }
352
- * })
353
- * .catch((e) => {
354
- * // e {Object} 错误信息
355
- * });
356
- */
357
- get(path, param, header) {
358
- return this.doRequest(path, param, 'GET', header);
359
- }
360
- /**
361
- * @public
362
- * @memberof Request
363
- * @param {String} path 请求接口路径
364
- * @param {Object} [param] 请求参数
365
- * @param {Object} [header] 自定义请求头
366
- * @returns {Promise} 接口响应
367
- * @example
368
- * const $ = getApp().tms.createRequest();
369
- * $.post(apiPath)
370
- * .then((data) => {
371
- * // data {Object} 响应数据
372
- * // {
373
- * // errCode {Number} 接口响应状态码
374
- * // errMsg {String} 接口响应状态信息
375
- * // resData {Object} 接口返回数据
376
- * // }
377
- * })
378
- * .catch((e) => {
379
- * // e {Object} 错误信息
380
- * });
381
- */
382
-
383
-
384
- post(path, param, header) {
385
- return this.doRequest(path, param, 'POST', header);
386
- }
387
- /**
388
- * 发送get方式的请求,该方法会返回wx.request全量的返回值(含data,header,cookies,statusCode)
389
- * @memberof Request
390
- * @param {string} path 请求接口路径
391
- * @param {object} param 业务参数
392
- * @param {object} header 自定义请求头
393
- * @returns {promise} 接口请求promise
394
- */
395
-
396
-
397
- execGet(path, param, header) {
398
- return this.createRequestTask(path, param, 'GET', header);
399
- }
400
- /**
401
- * 发送post方式的请求,该方法会返回wx.request全量的返回值(含data,header,cookies,statusCode等)
402
- * @memberof Request
403
- * @param {string} path 请求接口路径
404
- * @param {object} param 业务参数
405
- * @param {object} header 自定义请求头
406
- * @returns {promise} 接口请求promise
407
- */
408
-
409
-
410
- execPost(path, param, header) {
411
- return this.createRequestTask(path, param, 'POST', header);
412
- }
413
- /**
414
- * @memberof Request
415
- * @param {String} path 请求接口路径
416
- * @param {String} filePath 上传文件的本地路径
417
- * @param {Object} param 需要携带的其他参数
418
- * @param {Object} header 自定义的请求头
419
- * @returns {Object} 接口返回结果
420
- */
421
-
422
-
423
- async upload(path, filePath, param, header) {
424
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
425
- const res = await new Promise((resolve, reject) => {
426
- wx.uploadFile({
427
- name: 'content',
428
- url: this.makeUrl(path),
429
- filePath,
430
- formData: sign(requestParam),
431
- header,
432
- success: resolve,
433
- fail: reject
434
- });
435
- });
436
-
437
- if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
438
- return JSON.parse(res === null || res === void 0 ? void 0 : res.data);
439
- }
440
-
441
- return res === null || res === void 0 ? void 0 : res.data;
442
- }
443
- /**
444
- * @memberof Request
445
- * @param {string} path 请求接口路径
446
- * @param {string} param 业务参数
447
- * @param {string} method 请求方法 get/post
448
- * @param {object} header 自定义的请求头
449
- * @returns {object} 接口返回结果
450
- */
451
-
452
-
453
- async doRequest(path, param = {}, method = 'POST', header = {}) {
454
- const res = await this.createRequestTask(path, param, method, header);
455
-
456
- if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
457
- return JSON.parse(res === null || res === void 0 ? void 0 : res.data);
458
- }
459
-
460
- return res === null || res === void 0 ? void 0 : res.data;
461
- }
462
- /**
463
- * 序列化一个 get 请求地址
464
- * @memberof Request
465
- * @param {string} path 请求接口路径
466
- * @param {object} data 业务参数
467
- * @returns {Promise} 返回序列化之后的 get 请求地址
468
- */
469
-
470
-
471
- async serialize(path, data = {}) {
472
- let url = this.makeUrl(path);
473
- const signData = await composeParam(data, this.withAuth, this.baseParam);
474
- const signture = sign(signData);
475
- const params = [];
476
- Object.keys(signture).forEach(key => {
477
- const val = encodeURIComponent(signture[key]);
478
- params.push(`${key}=${val}`);
479
- });
480
- if (params.length) url += (/\?/.test(url) ? '&' : '?') + params.join('&');
481
- return Promise.resolve({
482
- url
483
- });
484
- }
485
- /**
486
- * 创建发送请求任务
487
- * @memberof Request
488
- * @param {string} path 请求接口路径
489
- * @param {string} param 业务参数
490
- * @param {string} method 请求方法 get/post
491
- * @param {object} header 自定义的请求头
492
- * @returns {Promise} 接口返回结果
493
- */
494
-
495
-
496
- async createRequestTask(path, param = {}, method = 'POST', header = {}) {
497
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
498
- const data = sign(requestParam);
499
- const logger = getLogManager();
500
- const res = await new Promise((resolve, reject) => {
501
- wx.request({
502
- url: this.makeUrl(path),
503
- header,
504
- method,
505
- data,
506
- success: res => {
507
- resolve(res);
508
- logger.log({
509
- path,
510
- header,
511
- method,
512
- param: data,
513
- res: res === null || res === void 0 ? void 0 : res.data
514
- });
515
- },
516
- fail: err => {
517
- reject(err);
518
- logger.log({
519
- path,
520
- header,
521
- method,
522
- param: data,
523
- err
524
- });
525
- }
526
- });
527
- });
528
- return res;
529
- }
530
-
531
- }
532
-
533
- export { Request as R, getRealtimeLogManager as a, getLogManager as g };