@tmsfe/tms-core 0.0.184 → 0.0.186

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.184",
3
+ "version": "0.0.186",
4
4
  "description": "tms运行时框架",
5
5
  "repository": {
6
6
  "type": "git",
@@ -346,12 +346,12 @@ const cryptRuleUtil = {
346
346
  },
347
347
  // 不参与加密的请求路径规则: { 允许的域名: 不需要加密的path }
348
348
  _encryptPathRule: {
349
- 'tim.map.qq.com': ['^/user/login', '^/api/getClientConfigs', '^/basic/crypto/lastkey2', '^/uben/takecar/combine/v2/getRecommendPointAndEstimatePrice'],
349
+ 'tim.map.qq.com': ['^/user/login', '^/api/getClientConfigs', '^/basic/crypto/lastkey2', '^/mp/mobility/api/v1/takecar/charge/getRecommendPointAndEstimatePrice'],
350
350
  'tim.sparta.html5.qq.com': [
351
351
  '^/user/login',
352
352
  '^/cnabroad', '^~/ReChargeCard/', '^/gasolinerecharge/v2/', '^/gasolinerecharge/rechargecard/',
353
353
  '^/tde', '^/basic/crypto/lastkey2',
354
- '^/uben/takecar/combine/v2/getRecommendPointAndEstimatePrice',
354
+ '^/mp/mobility/api/v1/takecar/charge/getRecommendPointAndEstimatePrice',
355
355
  ],
356
356
  },
357
357
  isHostValid: (url) => {
package/src/index.js CHANGED
@@ -30,6 +30,8 @@ import {
30
30
  } from './stringUtils';
31
31
  import {
32
32
  round,
33
+ floatArithmetic,
34
+ getPrecision,
33
35
  } from './numUtils';
34
36
  import {
35
37
  groupTimeDuration,
@@ -53,6 +55,7 @@ import { getMpOpenId, getOuterOpenId, getEncryptUserInfo } from './mpInfo';
53
55
  import * as storage from './storage';
54
56
  import * as uiUtil from './tmsuiUtils';
55
57
  import { throttle, debounce } from './funcUtils';
58
+ import { formatAmount } from './priceUtils';
56
59
 
57
60
  /**
58
61
  * @public
@@ -232,6 +235,11 @@ const api = {
232
235
 
233
236
  /* 数字方法 */
234
237
  round,
238
+ floatArithmetic,
239
+ getPrecision,
240
+
241
+ /* 金额方法 */
242
+ formatAmount,
235
243
 
236
244
  /* 时间方法 */
237
245
  groupTimeDuration,
@@ -0,0 +1,86 @@
1
+ /**
2
+ * @desc: 数字处理相关函数
3
+ */
4
+
5
+ import { roundStr } from './stringUtils';
6
+ /**
7
+ * 四舍五入(支持保留n位小数,n>=0)
8
+ * @param {any} x 原数字, 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
9
+ * @param {any} n 保留几位小数,默认0;
10
+ 1. 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN;
11
+ 2. 如果n小于0,round结果返回NaN;
12
+ 3. 如果n的值包含小数部分,round处理时只关注n的整数部分值
13
+ * @return {number} 返回一个保留n位小数的数字,异常情况下可能是NaN
14
+ */
15
+ const round = (x, n = 0) => parseFloat(roundStr(x, n, false));
16
+
17
+
18
+ /**
19
+ * 获取数字的精度(小数位数)
20
+ * @param {number} num 输入数字
21
+ * @returns {number} 小数位数
22
+ */
23
+ function getPrecision(num = 0) {
24
+ const str = num.toString();
25
+ if (str.indexOf('e-') >= 0) {
26
+ return parseInt(str.split('e-')[1], 10);
27
+ }
28
+ const decimalPart = str.split('.')[1] || '';
29
+ return decimalPart.length;
30
+ }
31
+
32
+
33
+ /**
34
+ * 浮点数运算精度问题解决函数
35
+ * @param {MathType} mathType 运算符类型 + - * /
36
+ * @param {...Number} args 运算数组
37
+ * @returns 运算结果
38
+ */
39
+ type MathType = '+' | '-' | '*' | '/'; // 四则运算符
40
+ function floatArithmetic(mathType: MathType, ...args: number[]) {
41
+ const [a, b] = args;
42
+ const precisionA = getPrecision(a);
43
+ const precisionB = getPrecision(b);
44
+
45
+ // 处理加法
46
+ if (mathType === '+') {
47
+ return args.reduce((prev, curr) => {
48
+ const precision = Math.max(getPrecision(prev), getPrecision(curr));
49
+ const factor = 10 ** precision;
50
+ return (prev * factor + curr * factor) / factor;
51
+ });
52
+ }
53
+
54
+ // 处理减法
55
+ if (mathType === '-') {
56
+ const factor = 10 ** Math.max(precisionA, precisionB);
57
+ return (a * factor - b * factor) / factor;
58
+ }
59
+
60
+ // 处理乘法
61
+ if (mathType === '*') {
62
+ const factor = 10 ** (precisionA + precisionB);
63
+ return (a * (10 ** precisionA)) * (b * (10 ** precisionB)) / factor;
64
+ }
65
+
66
+ // 处理除法
67
+ if (mathType === '/') {
68
+ // 除数为0,返回0
69
+ if (b === 0) {
70
+ // @ts-ignore
71
+ console.warn('除数为0,返回0');
72
+ return 0;
73
+ };
74
+ const factor = (10 ** (precisionA - precisionB));
75
+ return (a * (10 ** precisionA)) / (b * (10 ** precisionB)) / factor;
76
+ }
77
+
78
+ // 不支持的运算符
79
+ throw new Error(`不支持的运算符:${mathType}`);
80
+ }
81
+
82
+ export {
83
+ round,
84
+ floatArithmetic,
85
+ getPrecision,
86
+ };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * 金额相关工具类
3
+ */
4
+
5
+
6
+ /**
7
+ * 格式化金额,单位元
8
+ * @param amount 金额,单位分
9
+ * @param digit 小数位
10
+ */
11
+ function formatAmount(amount = 0, digit: number): number {
12
+ return parseFloat((amount / 100).toFixed(digit));
13
+ }
14
+
15
+
16
+ export {
17
+ formatAmount,
18
+ };
package/src/numUtils.js DELETED
@@ -1,19 +0,0 @@
1
- /**
2
- * @desc: 数字处理相关函数
3
- */
4
-
5
- import { roundStr } from './stringUtils';
6
- /**
7
- * 四舍五入(支持保留n位小数,n>=0)
8
- * @param {any} x 原数字, 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN
9
- * @param {any} n 保留几位小数,默认0;
10
- 1. 如果n不是合法数字或者无法转换为合法数字,round结果返回NaN;
11
- 2. 如果n小于0,round结果返回NaN;
12
- 3. 如果n的值包含小数部分,round处理时只关注n的整数部分值
13
- * @return {number} 返回一个保留n位小数的数字,异常情况下可能是NaN
14
- */
15
- const round = (x, n = 0) => parseFloat(roundStr(x, n, false));
16
-
17
- export {
18
- round,
19
- };