tools-min-ns 1.15.0 → 1.17.0

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.
Files changed (40) hide show
  1. package/README.md +28 -1
  2. package/esm/array/TreeUtil.js +9 -9
  3. package/esm/browser/fingerprint.js +12 -12
  4. package/esm/date/DateUtil.d.ts +8 -0
  5. package/esm/date/DateUtil.js +42 -1
  6. package/esm/func/FunctionUtil.d.ts +20 -20
  7. package/esm/func/FunctionUtil.js +42 -51
  8. package/esm/number/NumberUtil.d.ts +2 -1
  9. package/esm/number/NumberUtil.js +6 -2
  10. package/esm/pwd/PasswordUtil.d.ts +28 -0
  11. package/esm/pwd/PasswordUtil.js +90 -3
  12. package/esm/pwd/crypto.d.ts +6 -6
  13. package/esm/pwd/crypto.js +3 -3
  14. package/esm/pwd/lz-string.d.ts +15 -0
  15. package/esm/pwd/lz-string.js +486 -0
  16. package/esm/request/nsPromiseFn.js +3 -3
  17. package/esm/storage/StorageUtil.d.ts +2 -2
  18. package/esm/storage/StorageUtil.js +4 -3
  19. package/esm/transfer/MapTransferUtil.d.ts +14 -0
  20. package/esm/transfer/MapTransferUtil.js +37 -0
  21. package/lib/array/TreeUtil.js +9 -9
  22. package/lib/browser/fingerprint.js +12 -12
  23. package/lib/date/DateUtil.d.ts +8 -0
  24. package/lib/date/DateUtil.js +42 -1
  25. package/lib/func/FunctionUtil.d.ts +20 -20
  26. package/lib/func/FunctionUtil.js +41 -50
  27. package/lib/number/NumberUtil.d.ts +2 -1
  28. package/lib/number/NumberUtil.js +6 -2
  29. package/lib/pwd/PasswordUtil.d.ts +28 -0
  30. package/lib/pwd/PasswordUtil.js +100 -13
  31. package/lib/pwd/crypto.d.ts +6 -6
  32. package/lib/pwd/crypto.js +3 -3
  33. package/lib/pwd/lz-string.d.ts +15 -0
  34. package/lib/pwd/lz-string.js +491 -0
  35. package/lib/request/nsPromiseFn.js +3 -3
  36. package/lib/storage/StorageUtil.d.ts +2 -2
  37. package/lib/storage/StorageUtil.js +3 -2
  38. package/lib/transfer/MapTransferUtil.d.ts +14 -0
  39. package/lib/transfer/MapTransferUtil.js +37 -0
  40. package/package.json +3 -4
package/README.md CHANGED
@@ -1,10 +1,37 @@
1
1
  # use tools-min-ns
2
2
 
3
3
  ```
4
- cnpm i tools-min-ns --save
4
+ pnpm i tools-min-ns --save
5
5
  ```
6
6
 
7
7
  # updates
8
+ ## v1.17.0
9
+ >StorageUtil
10
+ 加解密
11
+
12
+ ## v1.16.3
13
+ >FunctionUti
14
+ 防抖节流兼容小程序
15
+ ## v1.16.2
16
+ > PasswordUtil
17
+ 新增minEncryptData/minDecryptData
18
+
19
+ ## v1.16.1
20
+ > MapTransferUtil
21
+ 4326/3857转换
22
+
23
+ ## v1.16.0
24
+ > PasswordUtil
25
+ 新增xor加解密
26
+
27
+ ## v1.15.3
28
+ > DateUtil.formatDateToFriendly3
29
+
30
+ ## v1.15.2
31
+ > NumberUtil.number2percentage 加入参数m
32
+
33
+ ## v1.15.1
34
+ > DateUtil formatDate 修复星期w和季度q
8
35
 
9
36
  ## v1.14.4
10
37
  > ArrayUtil repeatArray 创建一个数组,其中包含原数组的元素,重复一定次数
@@ -91,17 +91,17 @@ var TreeUtil;
91
91
  children = 'children';
92
92
  }
93
93
  var list = [];
94
- var fn = function fn(a, c, d) {
94
+ var _fn = function fn(a, c, d) {
95
95
  for (var index = 0; index < ((a === null || a === void 0 ? void 0 : a.length) || 0); index++) {
96
96
  var item = a[index];
97
97
  var back = c(item, d);
98
98
  if (back) {
99
99
  list.push(item);
100
100
  }
101
- fn(item[children], c, item);
101
+ _fn(item[children], c, item);
102
102
  }
103
103
  };
104
- fn(tree, callback, defaultParent);
104
+ _fn(tree, callback, defaultParent);
105
105
  return list;
106
106
  }
107
107
  TreeUtil.filterTreeData = filterTreeData;
@@ -206,19 +206,19 @@ var TreeUtil;
206
206
  return t[value] === id;
207
207
  });
208
208
  var list = getListByTree(tree, children);
209
- var getList = function getList(importOid) {
209
+ var _getList = function getList(importOid) {
210
210
  forEachTree(tree, function (info, _parent) {
211
211
  if (String(info[value]) === String(importOid)) {
212
212
  if (parent in info && info[parent] && list.some(function (e) {
213
213
  return String(e[value]) === String(info[parent]);
214
214
  })) {
215
215
  idList.unshift(_parent);
216
- getList(info === null || info === void 0 ? void 0 : info[parent]);
216
+ _getList(info === null || info === void 0 ? void 0 : info[parent]);
217
217
  }
218
218
  }
219
219
  }, children);
220
220
  };
221
- getList(id);
221
+ _getList(id);
222
222
  if (!BaseUtil.isEmpty(rootPid)) {
223
223
  var idx = idList.findIndex(function (item) {
224
224
  return String(item) === String(rootPid);
@@ -410,19 +410,19 @@ function treeData2List(element, children) {
410
410
  children = 'children';
411
411
  }
412
412
  var array = [];
413
- var getList = function getList(item) {
413
+ var _getList2 = function getList(item) {
414
414
  var _a;
415
415
  var newItem = JSON.parse(JSON.stringify(item || '{}')); //序列化
416
416
  delete newItem[children];
417
417
  if ((_a = item[children]) === null || _a === void 0 ? void 0 : _a.length) {
418
418
  if (!isEmpty(newItem)) array.push(newItem);
419
419
  item[children].forEach(function (el) {
420
- return getList(el);
420
+ return _getList2(el);
421
421
  });
422
422
  } else if (!isEmpty(newItem)) {
423
423
  array.push(newItem);
424
424
  }
425
425
  };
426
- getList(element);
426
+ _getList2(element);
427
427
  return array;
428
428
  }
@@ -462,11 +462,11 @@ function J(e, r, i) {
462
462
  };
463
463
  var a = l.style;
464
464
  a.setProperty("display", "block", "important"), a.position = "absolute", a.top = "0", a.left = "0", a.visibility = "hidden", r && "srcdoc" in l ? l.srcdoc = r : l.src = "about:blank", n.body.appendChild(l);
465
- var c = function c() {
465
+ var _c = function c() {
466
466
  var e, n;
467
- o || ("complete" === (null === (n = null === (e = l.contentWindow) || void 0 === e ? void 0 : e.document) || void 0 === n ? void 0 : n.readyState) ? i() : setTimeout(c, 10));
467
+ o || ("complete" === (null === (n = null === (e = l.contentWindow) || void 0 === e ? void 0 : e.document) || void 0 === n ? void 0 : n.readyState) ? i() : setTimeout(_c, 10));
468
468
  };
469
- c();
469
+ _c();
470
470
  })];
471
471
  case 5:
472
472
  t.sent(), t.label = 6;
@@ -539,10 +539,10 @@ function _() {
539
539
  return r = new Promise(function (e) {
540
540
  var n = document,
541
541
  t = "visibilitychange",
542
- r = function r() {
543
- n.hidden || (n.removeEventListener(t, r), e());
542
+ _r = function r() {
543
+ n.hidden || (n.removeEventListener(t, _r), e());
544
544
  };
545
- n.addEventListener(t, r), r();
545
+ n.addEventListener(t, _r), _r();
546
546
  }).then(function () {
547
547
  return o(500);
548
548
  }), i = function () {
@@ -610,15 +610,15 @@ function O(e) {
610
610
  e.oncomplete = function (e) {
611
611
  return n(e.renderedBuffer);
612
612
  };
613
- var o = function o() {
613
+ var _o = function o() {
614
614
  try {
615
615
  var i = e.startRendering();
616
- a(i) && l(i), "suspended" === e.state && (document.hidden || r--, r > 0 ? setTimeout(o, 200) : n(null));
616
+ a(i) && l(i), "suspended" === e.state && (document.hidden || r--, r > 0 ? setTimeout(_o, 200) : n(null));
617
617
  } catch (c) {
618
618
  t(c);
619
619
  }
620
620
  };
621
- o();
621
+ _o();
622
622
  });
623
623
  }
624
624
  var U = ["monospace", "sans-serif", "serif"],
@@ -698,11 +698,11 @@ function re() {
698
698
  var e = this;
699
699
  return function () {
700
700
  if (void 0 === te) {
701
- var e = function e() {
701
+ var _e2 = function e() {
702
702
  var n = oe();
703
- ie(n) ? te = setTimeout(e, 2500) : (ne = n, te = void 0);
703
+ ie(n) ? te = setTimeout(_e2, 2500) : (ne = n, te = void 0);
704
704
  };
705
- e();
705
+ _e2();
706
706
  }
707
707
  }(), function () {
708
708
  return n(e, void 0, void 0, function () {
@@ -66,6 +66,14 @@ declare namespace DateUtil {
66
66
  * formatDateToFriendly2() => 2017年11月11日
67
67
  */
68
68
  function formatDateToFriendly2(date?: Date | number | string): string;
69
+ /**
70
+ * 将一个日期格式化成友好格式,比如,当天返回几秒前、几分钟前、几小时前、几天前、几个月前还是几年前
71
+ * @param {Date | number | string} date 如: new Date('2017-11-11')
72
+ * @returns {string} 解析后的友好化日期格式
73
+ * @example
74
+ * formatDateToFriendly2() => 2017年11月11日
75
+ */
76
+ function formatDateToFriendly3(date?: Date | number | string): string;
69
77
  /**
70
78
  * 判断是否为本周
71
79
  * @param date
@@ -117,7 +117,10 @@ var DateUtil;
117
117
  dd: ('0' + d.getDate()).slice(-2),
118
118
  HH: ('0' + d.getHours()).slice(-2),
119
119
  mm: ('0' + d.getMinutes()).slice(-2),
120
- ss: ('0' + d.getSeconds()).slice(-2)
120
+ ss: ('0' + d.getSeconds()).slice(-2),
121
+ w: ['日', '一', '二', '三', '四', '五', '六'][d.getDay()],
122
+ q: "".concat(Math.floor((d.getMonth() + 3) / 3)),
123
+ SSS: "".concat(d.getMilliseconds())
121
124
  };
122
125
  var formattedDate = format;
123
126
  for (var char in formatChars) {
@@ -192,6 +195,44 @@ var DateUtil;
192
195
  }
193
196
  }
194
197
  DateUtil.formatDateToFriendly2 = formatDateToFriendly2;
198
+ /**
199
+ * 将一个日期格式化成友好格式,比如,当天返回几秒前、几分钟前、几小时前、几天前、几个月前还是几年前
200
+ * @param {Date | number | string} date 如: new Date('2017-11-11')
201
+ * @returns {string} 解析后的友好化日期格式
202
+ * @example
203
+ * formatDateToFriendly2() => 2017年11月11日
204
+ */
205
+ function formatDateToFriendly3(date) {
206
+ if (date === void 0) {
207
+ date = new Date();
208
+ }
209
+ if (BaseUtil.isString(date)) {
210
+ date = DateUtil.parseDate(date, 'yyyy-MM-dd HH:mm:ss');
211
+ } else {
212
+ date = BaseUtil.isNumber(date) ? new Date(date) : date;
213
+ }
214
+ var currentTime = new Date().getTime(); // 当前时间
215
+ var timeDiff = currentTime - date.getTime(); // 计算时间差
216
+ var seconds = Math.floor(timeDiff / 1000);
217
+ var minutes = Math.floor(seconds / 60);
218
+ var hours = Math.floor(minutes / 60);
219
+ var days = Math.floor(hours / 24);
220
+ var months = Math.floor(days / 30.4375); // 平均每月的天数
221
+ var years = Math.floor(days / 365.25); // 平均每年的天数
222
+ if (seconds < 60) {
223
+ return "".concat(seconds, "\u79D2\u524D");
224
+ } else if (minutes < 60) {
225
+ return "".concat(minutes, "\u5206\u949F\u524D");
226
+ } else if (hours < 24) {
227
+ return "".concat(hours, "\u5C0F\u65F6\u524D");
228
+ } else if (days < 30.4375) {
229
+ return "".concat(days, "\u5929\u524D");
230
+ } else if (months < 12) {
231
+ return "".concat(months, "\u4E2A\u6708\u524D");
232
+ }
233
+ return "".concat(years, "\u5E74\u524D");
234
+ }
235
+ DateUtil.formatDateToFriendly3 = formatDateToFriendly3;
195
236
  /**
196
237
  * 判断是否为本周
197
238
  * @param date
@@ -1,23 +1,23 @@
1
1
  declare namespace FunctionUtil {
2
2
  /**
3
- * 函数柯里化
4
- * @param { Function } fn 方法
5
- * @param { Any } agrs 参数,可选
6
- * @example
7
- * // 实现一个判断数据类型的方法
8
- const checktype = function(type, content) {
9
- return Object.prototype.toString.call(content) === `[object ${type}]`;
10
- }
11
- checktype('Number',2); => true
12
- // 这种方法总是要把type参数传过去,如果写错了就会影响到正确的结果了,可以考虑下如何做到把“Number“做到复用
13
-
14
- const checktype = function(type, content) {
15
- return Object.prototype.toString.call(content) === `[object ${type}]`;
16
- }
17
- const curry = curryFn(checktype)
18
- const isNumber = curry('Number')
19
- isNumber(2) => true
20
- */
3
+ * 函数柯里化
4
+ * @param { Function } fn 方法
5
+ * @param { Any } agrs 参数,可选
6
+ * @example
7
+ * // 实现一个判断数据类型的方法
8
+ const checktype = function(type, content) {
9
+ return Object.prototype.toString.call(content) === `[object ${type}]`;
10
+ }
11
+ checktype('Number',2); => true
12
+ // 这种方法总是要把type参数传过去,如果写错了就会影响到正确的结果了,可以考虑下如何做到把“Number“做到复用
13
+
14
+ const checktype = function(type, content) {
15
+ return Object.prototype.toString.call(content) === `[object ${type}]`;
16
+ }
17
+ const curry = curryFn(checktype)
18
+ const isNumber = curry('Number')
19
+ isNumber(2) => true
20
+ */
21
21
  function curryFn(fn: any, ...arg: any): () => any;
22
22
  /**
23
23
  * 函数防抖
@@ -34,7 +34,7 @@ declare namespace FunctionUtil {
34
34
  * query()
35
35
  * query() => 3
36
36
  */
37
- function debounceFn<T extends Function>(fn: T, t?: number, immediate?: boolean): T;
37
+ function debounceFn<T extends (...args: any[]) => any>(fn: T, t?: number, immediate?: boolean): T;
38
38
  /**
39
39
  * 函数节流
40
40
  * @param fn 需要节流的函数
@@ -45,7 +45,7 @@ declare namespace FunctionUtil {
45
45
  * query()
46
46
  * query()
47
47
  */
48
- function throttleFn<T extends Function>(fn: T, t?: number): T;
48
+ function throttleFn<T extends (...args: any[]) => any>(fn: T, t?: number): T;
49
49
  /**
50
50
  * 函数只能调用一次
51
51
  * @param { Function } fn 需要被处理的函数
@@ -35,7 +35,7 @@ var __spreadArray = this && this.__spreadArray || function (to, from, pack) {
35
35
  * @author nanshen
36
36
  * @creat 2021-09-15 14:22:55
37
37
  */
38
- import BaseUtil from "../base/BaseUtil";
38
+ import BaseUtil from '../base/BaseUtil';
39
39
  var FunctionUtil;
40
40
  (function (FunctionUtil) {
41
41
  /**
@@ -44,25 +44,25 @@ var FunctionUtil;
44
44
  * @param { Any } agrs 参数,可选
45
45
  * @example
46
46
  * // 实现一个判断数据类型的方法
47
- const checktype = function(type, content) {
48
- return Object.prototype.toString.call(content) === `[object ${type}]`;
49
- }
50
- checktype('Number',2); => true
51
- // 这种方法总是要把type参数传过去,如果写错了就会影响到正确的结果了,可以考虑下如何做到把“Number“做到复用
52
-
53
- const checktype = function(type, content) {
54
- return Object.prototype.toString.call(content) === `[object ${type}]`;
55
- }
56
- const curry = curryFn(checktype)
57
- const isNumber = curry('Number')
58
- isNumber(2) => true
59
- */
47
+ const checktype = function(type, content) {
48
+ return Object.prototype.toString.call(content) === `[object ${type}]`;
49
+ }
50
+ checktype('Number',2); => true
51
+ // 这种方法总是要把type参数传过去,如果写错了就会影响到正确的结果了,可以考虑下如何做到把“Number“做到复用
52
+
53
+ const checktype = function(type, content) {
54
+ return Object.prototype.toString.call(content) === `[object ${type}]`;
55
+ }
56
+ const curry = curryFn(checktype)
57
+ const isNumber = curry('Number')
58
+ isNumber(2) => true
59
+ */
60
60
  function curryFn(fn) {
61
61
  var arg = [];
62
62
  for (var _i = 1; _i < arguments.length; _i++) {
63
63
  arg[_i - 1] = arguments[_i];
64
64
  }
65
- if (!BaseUtil.isFunction(fn)) throw new Error("第一个参数必须是方法");
65
+ if (!BaseUtil.isFunction(fn)) throw new Error('第一个参数必须是方法');
66
66
  var _this = this;
67
67
  var args = Array.from(arguments).slice(1);
68
68
  // fn.length 属性指明函数的形参个数。
@@ -99,33 +99,23 @@ var FunctionUtil;
99
99
  if (immediate === void 0) {
100
100
  immediate = true;
101
101
  }
102
- if (!BaseUtil.isFunction(fn)) throw new Error("第一个参数必须是方法");
103
- var time;
104
- // 立刻执行第一次该方法
105
- if (immediate) {
106
- return function () {
107
- clearTimeout(time);
108
- if (!time) {
109
- fn.apply(this, arguments);
110
- }
111
- time = setTimeout(function () {
112
- setTimeout(time);
113
- time = null;
114
- }, t);
115
- };
116
- } else {
117
- // 满足 time 时间结束之后自动执行一次该方法
118
- return function () {
119
- clearTimeout(time);
120
- var that = this;
121
- var arg = arguments;
122
- time = setTimeout(function () {
123
- setTimeout(time);
124
- fn.apply(that, arg);
125
- time = null;
126
- }, t);
127
- };
128
- }
102
+ if (!BaseUtil.isFunction(fn)) throw new Error('第一个参数必须是方法');
103
+ var time = null;
104
+ var debounceFunction = function debounceFunction() {
105
+ var args = [];
106
+ for (var _i = 0; _i < arguments.length; _i++) {
107
+ args[_i] = arguments[_i];
108
+ }
109
+ clearTimeout(time);
110
+ if (immediate && !time) {
111
+ fn.apply(void 0, __spreadArray([], __read(args), false));
112
+ }
113
+ time = setTimeout(function () {
114
+ time = null;
115
+ fn.apply(void 0, __spreadArray([], __read(args), false));
116
+ }, t);
117
+ };
118
+ return debounceFunction;
129
119
  }
130
120
  FunctionUtil.debounceFn = debounceFn;
131
121
  /**
@@ -142,25 +132,26 @@ var FunctionUtil;
142
132
  if (t === void 0) {
143
133
  t = 2000;
144
134
  }
145
- if (!BaseUtil.isFunction(fn)) throw new Error("第一个参数必须是方法");
146
- var _fn = fn;
135
+ if (!BaseUtil.isFunction(fn)) throw new Error('第一个参数必须是方法');
147
136
  var time = null;
148
137
  var first = true;
149
- return function () {
150
- var arg = arguments;
151
- var _this = this;
138
+ var throttleFunction = function throttleFunction() {
139
+ var args = [];
140
+ for (var _i = 0; _i < arguments.length; _i++) {
141
+ args[_i] = arguments[_i];
142
+ }
152
143
  if (first) {
153
- _fn.apply(_this, arg);
144
+ fn.apply(void 0, __spreadArray([], __read(args), false));
154
145
  first = false;
155
146
  return;
156
147
  }
157
148
  if (time) return;
158
149
  time = setTimeout(function () {
159
- setTimeout(time);
160
150
  time = null;
161
- _fn.apply(_this, arg);
151
+ fn.apply(void 0, __spreadArray([], __read(args), false));
162
152
  }, t);
163
153
  };
154
+ return throttleFunction;
164
155
  }
165
156
  FunctionUtil.throttleFn = throttleFn;
166
157
  /**
@@ -174,7 +165,7 @@ var FunctionUtil;
174
165
  * // 1
175
166
  */
176
167
  function once(fn) {
177
- if (!BaseUtil.isFunction(fn)) throw new Error("参数必须是方法");
168
+ if (!BaseUtil.isFunction(fn)) throw new Error('参数必须是方法');
178
169
  var result;
179
170
  return function () {
180
171
  if (fn) {
@@ -89,8 +89,9 @@ declare namespace NumberUtil {
89
89
  * n在count中的占比%
90
90
  * @param n
91
91
  * @param count
92
+ * @param m 小数位数 默认为2
92
93
  * @returns
93
94
  */
94
- const number2percentage: (n: number, count: number) => string;
95
+ const number2percentage: (n: number, count: number, m?: number) => string;
95
96
  }
96
97
  export default NumberUtil;
@@ -241,10 +241,14 @@ var NumberUtil;
241
241
  * n在count中的占比%
242
242
  * @param n
243
243
  * @param count
244
+ * @param m 小数位数 默认为2
244
245
  * @returns
245
246
  */
246
- NumberUtil.number2percentage = function (n, count) {
247
- return "".concat(count ? correctNumberInput((n || 0) * 100 / count, 2) : 0, "%");
247
+ NumberUtil.number2percentage = function (n, count, m) {
248
+ if (m === void 0) {
249
+ m = 2;
250
+ }
251
+ return "".concat(count ? correctNumberInput((n || 0) * 100 / count, m) : 0, "%");
248
252
  };
249
253
  })(NumberUtil || (NumberUtil = {}));
250
254
  export default NumberUtil;
@@ -29,5 +29,33 @@ declare namespace PasswordUtil {
29
29
  * str2MD5('1111') => ea69274f6555a750bec9985a07f80455
30
30
  */
31
31
  const str2MD5: (str: string, pwd?: string) => any;
32
+ /**
33
+ * 加密函数
34
+ * @param {Object} data - 要加密的 JSON 对象
35
+ * @param {string} key - 密钥
36
+ * @returns {string} 加密并编码后的字符串
37
+ */
38
+ function xorEncryptData(data: any, pwd?: string): string;
39
+ /**
40
+ * 解密函数
41
+ * @param {string} encodedData - 加密并编码后的字符串
42
+ * @param {string} pwd - 密钥
43
+ * @returns {Object} 解密后的 JSON 对象
44
+ */
45
+ function xorDecryptData(encodedData: string, pwd?: string): any;
46
+ /**
47
+ * 加密函数
48
+ * @param {Object} data - 要加密的 JSON 对象
49
+ * @param {string} key - 密钥
50
+ * @returns {string} 加密并编码后的字符串
51
+ */
52
+ function minEncryptData(data: any, pwd?: string): string;
53
+ /**
54
+ * 解密函数
55
+ * @param {string} encodedData - 加密并编码后的字符串
56
+ * @param {string} pwd - 密钥
57
+ * @returns {Object} 解密后的 JSON 对象
58
+ */
59
+ function minDecryptData(encodedData: string, pwd?: string): any;
32
60
  }
33
61
  export default PasswordUtil;
@@ -1,6 +1,6 @@
1
1
  import config from '../config';
2
- import crypto from './crypto';
3
- var CryptoJS = crypto();
2
+ import CryptoJS from './crypto';
3
+ import LZString from './lz-string';
4
4
  /**
5
5
  * 加密工具
6
6
  * @description
@@ -64,5 +64,92 @@ var PasswordUtil;
64
64
  if (!str) return '';
65
65
  return CryptoJS.MD5(pwd + str).toString();
66
66
  };
67
+ /**
68
+ * 加密函数
69
+ * @param {Object} data - 要加密的 JSON 对象
70
+ * @param {string} key - 密钥
71
+ * @returns {string} 加密并编码后的字符串
72
+ */
73
+ function xorEncryptData(data, pwd) {
74
+ if (pwd === void 0) {
75
+ pwd = defaultMD5Key;
76
+ }
77
+ var jsonString = JSON.stringify(data); // 将对象转为字符串
78
+ var compressed = LZString.compressToUTF16(jsonString); // 压缩数据
79
+ var encrypted = xorEncryptDecrypt(compressed, pwd); // XOR 加密
80
+ var encoded = LZString.compressToEncodedURIComponent(encrypted); // URL 安全编码
81
+ return encoded;
82
+ }
83
+ PasswordUtil.xorEncryptData = xorEncryptData;
84
+ /**
85
+ * 解密函数
86
+ * @param {string} encodedData - 加密并编码后的字符串
87
+ * @param {string} pwd - 密钥
88
+ * @returns {Object} 解密后的 JSON 对象
89
+ */
90
+ function xorDecryptData(encodedData, pwd) {
91
+ if (pwd === void 0) {
92
+ pwd = defaultMD5Key;
93
+ }
94
+ try {
95
+ var decryptedEncoded = LZString.decompressFromEncodedURIComponent(encodedData); // 解码
96
+ if (!decryptedEncoded) {
97
+ throw new Error('Failed to decompress encoded data.');
98
+ }
99
+ var decrypted = xorEncryptDecrypt(decryptedEncoded, pwd); // XOR 解密
100
+ var decompressed = LZString.decompressFromUTF16(decrypted); // 解压
101
+ if (!decompressed) {
102
+ throw new Error('Failed to decompress decrypted data.');
103
+ }
104
+ return JSON.parse(decompressed); // 解析 JSON
105
+ } catch (error) {
106
+ return '';
107
+ }
108
+ }
109
+ PasswordUtil.xorDecryptData = xorDecryptData;
110
+ /**
111
+ * 加密函数
112
+ * @param {Object} data - 要加密的 JSON 对象
113
+ * @param {string} key - 密钥
114
+ * @returns {string} 加密并编码后的字符串
115
+ */
116
+ function minEncryptData(data, pwd) {
117
+ if (pwd === void 0) {
118
+ pwd = defaultMD5Key;
119
+ }
120
+ return encodeURIComponent(xorEncryptDecrypt(JSON.stringify(data), pwd));
121
+ }
122
+ PasswordUtil.minEncryptData = minEncryptData;
123
+ /**
124
+ * 解密函数
125
+ * @param {string} encodedData - 加密并编码后的字符串
126
+ * @param {string} pwd - 密钥
127
+ * @returns {Object} 解密后的 JSON 对象
128
+ */
129
+ function minDecryptData(encodedData, pwd) {
130
+ if (pwd === void 0) {
131
+ pwd = defaultMD5Key;
132
+ }
133
+ try {
134
+ return JSON.parse(xorEncryptDecrypt(decodeURIComponent(encodedData), pwd)); // 解析 JSON
135
+ } catch (error) {
136
+ return undefined;
137
+ }
138
+ }
139
+ PasswordUtil.minDecryptData = minDecryptData;
67
140
  })(PasswordUtil || (PasswordUtil = {}));
68
- export default PasswordUtil;
141
+ export default PasswordUtil;
142
+ /**
143
+ * XOR 加密与解密函数
144
+ * @param {string} data - 要加密或解密的字符串
145
+ * @param {string} pwd - 密钥
146
+ * @returns {string} 加密或解密后的字符串
147
+ */
148
+ function xorEncryptDecrypt(data, pwd) {
149
+ var keyLength = pwd.length;
150
+ var result = '';
151
+ for (var i = 0; i < data.length; i++) {
152
+ result += String.fromCharCode(data.charCodeAt(i) ^ pwd.charCodeAt(i % keyLength));
153
+ }
154
+ return result;
155
+ }
@@ -1,6 +1,6 @@
1
- export default cryptoFn;
2
- declare function cryptoFn(): {
3
- lib: {};
4
- enc: {};
5
- algo: {};
6
- };
1
+ export default CryptoJS;
2
+ declare namespace CryptoJS {
3
+ const lib: {};
4
+ const enc: {};
5
+ const algo: {};
6
+ }
package/esm/pwd/crypto.js CHANGED
@@ -1,4 +1,4 @@
1
- function cryptoFn() {
1
+ var CryptoJS = function () {
2
2
  var n,
3
3
  o,
4
4
  s,
@@ -1820,5 +1820,5 @@ function cryptoFn() {
1820
1820
  blockSize: 4,
1821
1821
  ivSize: 2
1822
1822
  }), M.RabbitLegacy = F._createHelper(P), U;
1823
- }
1824
- export default cryptoFn;
1823
+ }();
1824
+ export default CryptoJS;
@@ -0,0 +1,15 @@
1
+ export default LZString;
2
+ declare namespace LZString {
3
+ function compressToBase64(input: any): string;
4
+ function decompressFromBase64(input: any): string | null;
5
+ function compressToUTF16(input: any): string;
6
+ function decompressFromUTF16(compressed: any): string | null;
7
+ function compressToUint8Array(uncompressed: any): Uint8Array;
8
+ function decompressFromUint8Array(compressed: any): string | null;
9
+ function compressToEncodedURIComponent(input: any): string;
10
+ function decompressFromEncodedURIComponent(input: any): string | null;
11
+ function compress(uncompressed: any): string;
12
+ function _compress(uncompressed: any, bitsPerChar: any, getCharFromInt: any): string;
13
+ function decompress(compressed: any): string | null;
14
+ function _decompress(length: any, resetValue: any, getNextValue: any): string | null;
15
+ }