@tmsfe/tms-core 0.0.22 → 0.0.23

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,543 +0,0 @@
1
- import { a as safeJsonParse, m as md5 } from './objUtils-154b94db.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).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
- }); // 清理undefined和NaN的参数
250
-
251
- Object.keys(combinedParam).forEach(key => {
252
- if (typeof combinedParam[key] === 'number' && isNaN(combinedParam[key])) {
253
- delete combinedParam[key];
254
- }
255
-
256
- if (typeof combinedParam[key] === 'undefined') {
257
- delete combinedParam[key];
258
- }
259
- });
260
- return combinedParam;
261
- };
262
- /**
263
- * 用于保证业务参数的登录态参数,
264
- * 若接口不依赖登录态 如 user/login,则保证参数中不包括userId & token,
265
- * 若接口依赖登录态,则保证参数中填充userId & token,
266
- * @private
267
- * @param {object} param 要校验登录态的业务参数
268
- * @param {boolean} withAuth 是否要校验登录态
269
- * @returns {object} 增加登录态后的参数
270
- */
271
-
272
-
273
- const modifyAuthParam = async (param, withAuth) => {
274
- const requestParam = { ...param
275
- };
276
-
277
- if (withAuth) {
278
- const {
279
- userId,
280
- token
281
- } = await getAuthInfo();
282
- requestParam.userId = userId;
283
- requestParam.token = token;
284
- return requestParam;
285
- }
286
-
287
- delete requestParam.userId;
288
- delete requestParam.userid;
289
- delete requestParam.token;
290
- return requestParam;
291
- };
292
- /**
293
- * @public
294
- * @class Request
295
- * @classdesc 网络请求类,对签名、鉴权等逻辑进行封装处理,用于向腾讯出行服务平台后台发送网络请求
296
- */
297
-
298
-
299
- class Request {
300
- /**
301
- * 默认的request host域名
302
- * defaultHost 在tms-runtime初始化时进行设置,为出行服务接入层域名
303
- * 具体业务模块 new Request() 使用时,不指定自定义 host ,将使用defaultHost
304
- */
305
- static defaultHost = '';
306
- host = '';
307
- withAuth = true;
308
- baseParam = {};
309
- /**
310
- * Request 构造函数
311
- * @param {Object} config 构造参数
312
- * @param {Object} config.withAuth 是否填充登录态参数
313
- * @param {Object} config.host 自定义的host域名
314
- * @param {Object} config.baseParam 默认携带的参数
315
- */
316
-
317
- constructor(config = {
318
- withAuth: true
319
- }) {
320
- if (config.host) {
321
- this.host = config.host;
322
- }
323
-
324
- if (typeof config.withAuth !== 'undefined') {
325
- this.withAuth = !!config.withAuth;
326
- }
327
-
328
- this.baseParam = config.baseParam || {};
329
- }
330
- /**
331
- * 格式化接口路径
332
- * @private
333
- * @param {string} path 需要格式化的接口路径
334
- * @returns {string} 格式化后的接口路径
335
- */
336
-
337
-
338
- makeUrl(path) {
339
- if (/^http/i.test(path)) return path;
340
- const host = this.host || Request.defaultHost;
341
- const validHost = /^http/i.test(host) ? host : `https://${host}`;
342
- return `${validHost}/${path}`;
343
- }
344
-
345
- /**
346
- * @public
347
- * @memberof Request
348
- * @param {String} path 请求接口路径
349
- * @param {Object} [param] 请求参数
350
- * @param {Object} [header] 自定义请求头
351
- * @returns {Promise} 接口响应
352
- * @example
353
- * const $ = getApp().tms.createRequest();
354
- * $.get(apiPath)
355
- * .then((data) => {
356
- * // data {Object} 响应数据
357
- * // {
358
- * // errCode {Number} 接口响应状态码
359
- * // errMsg {String} 接口响应状态信息
360
- * // resData {Object} 接口返回数据
361
- * // }
362
- * })
363
- * .catch((e) => {
364
- * // e {Object} 错误信息
365
- * });
366
- */
367
- get(path, param, header) {
368
- return this.doRequest(path, param, 'GET', header);
369
- }
370
- /**
371
- * @public
372
- * @memberof Request
373
- * @param {String} path 请求接口路径
374
- * @param {Object} [param] 请求参数
375
- * @param {Object} [header] 自定义请求头
376
- * @returns {Promise} 接口响应
377
- * @example
378
- * const $ = getApp().tms.createRequest();
379
- * $.post(apiPath)
380
- * .then((data) => {
381
- * // data {Object} 响应数据
382
- * // {
383
- * // errCode {Number} 接口响应状态码
384
- * // errMsg {String} 接口响应状态信息
385
- * // resData {Object} 接口返回数据
386
- * // }
387
- * })
388
- * .catch((e) => {
389
- * // e {Object} 错误信息
390
- * });
391
- */
392
-
393
-
394
- post(path, param, header) {
395
- return this.doRequest(path, param, 'POST', header);
396
- }
397
- /**
398
- * 发送get方式的请求,该方法会返回wx.request全量的返回值(含data,header,cookies,statusCode)
399
- * @memberof Request
400
- * @param {string} path 请求接口路径
401
- * @param {object} param 业务参数
402
- * @param {object} header 自定义请求头
403
- * @returns {promise} 接口请求promise
404
- */
405
-
406
-
407
- execGet(path, param, header) {
408
- return this.createRequestTask(path, param, 'GET', header);
409
- }
410
- /**
411
- * 发送post方式的请求,该方法会返回wx.request全量的返回值(含data,header,cookies,statusCode等)
412
- * @memberof Request
413
- * @param {string} path 请求接口路径
414
- * @param {object} param 业务参数
415
- * @param {object} header 自定义请求头
416
- * @returns {promise} 接口请求promise
417
- */
418
-
419
-
420
- execPost(path, param, header) {
421
- return this.createRequestTask(path, param, 'POST', header);
422
- }
423
- /**
424
- * @memberof Request
425
- * @param {String} path 请求接口路径
426
- * @param {String} filePath 上传文件的本地路径
427
- * @param {Object} param 需要携带的其他参数
428
- * @param {Object} header 自定义的请求头
429
- * @returns {Object} 接口返回结果
430
- */
431
-
432
-
433
- async upload(path, filePath, param, header) {
434
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
435
- const res = await new Promise((resolve, reject) => {
436
- wx.uploadFile({
437
- name: 'content',
438
- url: this.makeUrl(path),
439
- filePath,
440
- formData: sign(requestParam),
441
- header,
442
- success: resolve,
443
- fail: reject
444
- });
445
- });
446
-
447
- if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
448
- return safeJsonParse(res === null || res === void 0 ? void 0 : res.data);
449
- }
450
-
451
- return res === null || res === void 0 ? void 0 : res.data;
452
- }
453
- /**
454
- * @memberof Request
455
- * @param {string} path 请求接口路径
456
- * @param {string} param 业务参数
457
- * @param {string} method 请求方法 get/post
458
- * @param {object} header 自定义的请求头
459
- * @returns {object} 接口返回结果
460
- */
461
-
462
-
463
- async doRequest(path, param = {}, method = 'POST', header = {}) {
464
- const res = await this.createRequestTask(path, param, method, header);
465
-
466
- if (typeof (res === null || res === void 0 ? void 0 : res.data) === 'string') {
467
- return safeJsonParse(res === null || res === void 0 ? void 0 : res.data);
468
- }
469
-
470
- return res === null || res === void 0 ? void 0 : res.data;
471
- }
472
- /**
473
- * 序列化一个 get 请求地址
474
- * @memberof Request
475
- * @param {string} path 请求接口路径
476
- * @param {object} data 业务参数
477
- * @returns {Promise} 返回序列化之后的 get 请求地址
478
- */
479
-
480
-
481
- async serialize(path, data = {}) {
482
- let url = this.makeUrl(path);
483
- const signData = await composeParam(data, this.withAuth, this.baseParam);
484
- const signture = sign(signData);
485
- const params = [];
486
- Object.keys(signture).forEach(key => {
487
- const val = encodeURIComponent(signture[key]);
488
- params.push(`${key}=${val}`);
489
- });
490
- if (params.length) url += (/\?/.test(url) ? '&' : '?') + params.join('&');
491
- return Promise.resolve({
492
- url
493
- });
494
- }
495
- /**
496
- * 创建发送请求任务
497
- * @memberof Request
498
- * @param {string} path 请求接口路径
499
- * @param {string} param 业务参数
500
- * @param {string} method 请求方法 get/post
501
- * @param {object} header 自定义的请求头
502
- * @returns {Promise} 接口返回结果
503
- */
504
-
505
-
506
- async createRequestTask(path, param = {}, method = 'POST', header = {}) {
507
- const requestParam = await composeParam(param, this.withAuth, this.baseParam);
508
- const data = sign(requestParam);
509
- const logger = getLogManager();
510
- const res = await new Promise((resolve, reject) => {
511
- wx.request({
512
- url: this.makeUrl(path),
513
- header,
514
- method,
515
- data,
516
- success: res => {
517
- resolve(res);
518
- logger.log({
519
- path,
520
- header,
521
- method,
522
- param: data,
523
- res: res === null || res === void 0 ? void 0 : res.data
524
- });
525
- },
526
- fail: err => {
527
- reject(err);
528
- logger.log({
529
- path,
530
- header,
531
- method,
532
- param: data,
533
- err
534
- });
535
- }
536
- });
537
- });
538
- return res;
539
- }
540
-
541
- }
542
-
543
- export { Request as R, getRealtimeLogManager as a, getLogManager as g };
package/dist/request.js DELETED
@@ -1,3 +0,0 @@
1
- import './objUtils-154b94db.js';
2
- export { R as default } from './request-f8a4745b.js';
3
- import './env-c7da70e1.js';