hy-app 0.3.16 → 0.4.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 (102) hide show
  1. package/components/hy-action-sheet/hy-action-sheet.vue +2 -2
  2. package/components/hy-action-sheet/typing.d.ts +32 -33
  3. package/components/hy-back-top/hy-back-top.vue +36 -32
  4. package/components/hy-button/hy-button.vue +7 -7
  5. package/components/hy-button/typing.d.ts +39 -40
  6. package/components/hy-calendar/index.scss +1 -1
  7. package/components/hy-card/hy-card.vue +3 -3
  8. package/components/hy-cell/hy-cell.vue +125 -118
  9. package/components/hy-cell/index.scss +7 -7
  10. package/components/hy-checkbox/hy-checkbox.vue +17 -16
  11. package/components/hy-checkbox/index.scss +1 -1
  12. package/components/hy-checkbox-item/hy-checkbox-item.vue +25 -30
  13. package/components/hy-checkbox-item/index.scss +1 -1
  14. package/components/hy-code-input/hy-code-input.vue +314 -314
  15. package/components/hy-code-input/index.scss +12 -12
  16. package/components/hy-count-down/index.scss +1 -1
  17. package/components/hy-datetime-picker/index.scss +1 -1
  18. package/components/hy-datetime-picker/typing.d.ts +0 -1
  19. package/components/hy-divider/index.scss +3 -3
  20. package/components/hy-dropdown-item/hy-dropdown-item.vue +2 -2
  21. package/components/hy-dropdown-item/index.scss +5 -5
  22. package/components/hy-float-button/hy-float-button.vue +1 -1
  23. package/components/hy-float-button/index.scss +9 -18
  24. package/components/hy-folding-panel/index.scss +1 -1
  25. package/components/hy-form-item/hy-form-item.vue +10 -11
  26. package/components/hy-grid/hy-grid.vue +23 -23
  27. package/components/hy-grid/index.scss +2 -8
  28. package/components/hy-icon/index.scss +8 -8
  29. package/components/hy-input/hy-input.vue +45 -44
  30. package/components/hy-input/index.scss +12 -11
  31. package/components/hy-line-progress/index.scss +3 -3
  32. package/components/hy-list/hy-list.vue +2 -2
  33. package/components/hy-list/index.scss +4 -4
  34. package/components/hy-loading/hy-loading.vue +2 -3
  35. package/components/hy-loading/index.scss +17 -15
  36. package/components/hy-menu/hy-menu.vue +38 -30
  37. package/components/hy-menu/index.scss +1 -1
  38. package/components/hy-menu/typing.d.ts +16 -12
  39. package/components/hy-modal/hy-modal.vue +11 -11
  40. package/components/hy-modal/index.scss +17 -17
  41. package/components/hy-modal/typing.d.ts +0 -2
  42. package/components/hy-navbar/hy-navbar.vue +3 -3
  43. package/components/hy-navbar/index.scss +10 -11
  44. package/components/hy-notice-bar/hy-column-notice.vue +2 -2
  45. package/components/hy-notice-bar/hy-row-notice.vue +3 -3
  46. package/components/hy-notice-bar/index.scss +9 -12
  47. package/components/hy-notify/hy-notify.vue +8 -5
  48. package/components/hy-notify/index.scss +2 -2
  49. package/components/hy-number-step/index.scss +9 -10
  50. package/components/hy-pagination/index.scss +7 -2
  51. package/components/hy-parse/index.scss +0 -9
  52. package/components/hy-picker/hy-picker.vue +1 -1
  53. package/components/hy-picker/index.scss +9 -16
  54. package/components/hy-popover/hy-popover.vue +6 -4
  55. package/components/hy-popup/hy-popup.vue +4 -2
  56. package/components/hy-popup/index.scss +45 -32
  57. package/components/hy-qrcode/hy-qrcode.vue +3 -3
  58. package/components/hy-qrcode/index.scss +18 -17
  59. package/components/hy-radio/hy-radio.vue +20 -18
  60. package/components/hy-radio/index.scss +26 -22
  61. package/components/hy-rate/hy-rate.vue +5 -5
  62. package/components/hy-rate/index.scss +4 -4
  63. package/components/hy-read-more/hy-read-more.vue +19 -20
  64. package/components/hy-read-more/index.scss +12 -6
  65. package/components/hy-scroll-list/hy-scroll-list.vue +3 -6
  66. package/components/hy-scroll-list/index.scss +4 -4
  67. package/components/hy-search/index.scss +9 -8
  68. package/components/hy-slider/hy-slider.vue +5 -5
  69. package/components/hy-slider/index.scss +12 -12
  70. package/components/hy-status-bar/index.scss +3 -1
  71. package/components/hy-steps/hy-steps.vue +50 -23
  72. package/components/hy-submit-bar/hy-submit-bar.vue +1 -1
  73. package/components/hy-submit-bar/index.scss +5 -4
  74. package/components/hy-subsection/hy-subsection.vue +3 -3
  75. package/components/hy-subsection/index.scss +7 -7
  76. package/components/hy-swipe-action/hy-swipe-action.vue +8 -8
  77. package/components/hy-swipe-action/index.scss +8 -4
  78. package/components/hy-swiper/hy-swiper-indicator.vue +2 -2
  79. package/components/hy-swiper/hy-swiper.vue +6 -6
  80. package/components/hy-swiper/index.scss +6 -10
  81. package/components/hy-switch/hy-switch.vue +4 -4
  82. package/components/hy-switch/index.scss +4 -22
  83. package/components/hy-tabs/hy-tabs.vue +16 -16
  84. package/components/hy-tabs/index.scss +16 -16
  85. package/components/hy-tag/index.scss +1 -1
  86. package/components/hy-text/index.scss +3 -3
  87. package/components/hy-textarea/hy-textarea.vue +11 -11
  88. package/components/hy-textarea/index.scss +11 -10
  89. package/components/hy-toast/hy-toast.vue +5 -5
  90. package/components/hy-toast/index.scss +8 -8
  91. package/components/hy-tooltip/hy-tooltip.vue +193 -183
  92. package/components/hy-tooltip/index.scss +69 -45
  93. package/components/hy-upload/hy-upload.vue +15 -10
  94. package/components/hy-upload/index.scss +37 -36
  95. package/components/hy-warn/hy-warn.vue +11 -11
  96. package/components/hy-warn/index.scss +76 -82
  97. package/components/hy-waterfall/index.scss +2 -1
  98. package/composables/usePopover.ts +2 -2
  99. package/libs/css/mixin.scss +0 -9
  100. package/package.json +2 -2
  101. package/utils/inside.ts +4 -1
  102. package/utils/utils.ts +187 -162
package/utils/utils.ts CHANGED
@@ -1,7 +1,7 @@
1
- import Base64 from './base64'
2
- import type { CSSProperties } from 'vue'
3
- import { isNumber } from './index'
4
- let base64: any = new Base64()
1
+ import Base64 from "./base64";
2
+ import type { CSSProperties } from "vue";
3
+ import { error, isNumber } from "./index";
4
+ let base64: any = new Base64();
5
5
 
6
6
  /**
7
7
  * 加密函数
@@ -9,7 +9,7 @@ let base64: any = new Base64()
9
9
  * @return { string } 加密字符串
10
10
  * */
11
11
  function encryptData(data: Record<string, any> | string): string {
12
- return base64.encode(JSON.stringify(data))
12
+ return base64.encode(JSON.stringify(data));
13
13
  }
14
14
 
15
15
  /**
@@ -18,7 +18,7 @@ function encryptData(data: Record<string, any> | string): string {
18
18
  * @returns { any | any[] } 解码的数据
19
19
  * */
20
20
  function decryptData(encryptedVal: string): Record<string, any> {
21
- return JSON.parse(base64.decode(encryptedVal.toString()))
21
+ return JSON.parse(base64.decode(encryptedVal.toString()));
22
22
  }
23
23
 
24
24
  /**
@@ -43,17 +43,20 @@ function decryptData(encryptedVal: string): Record<string, any> {
43
43
  * @param {String} unit 添加的单位名 比如px
44
44
  * @returns {String}
45
45
  */
46
- const addUnit = (value: string | number = 'auto', unit: string = ''): string => {
46
+ const addUnit = (
47
+ value: string | number = "auto",
48
+ unit: string = "",
49
+ ): string => {
47
50
  if (!unit) {
48
- unit = 'px'
51
+ unit = "px";
49
52
  }
50
- if (unit == 'rpx' && isNumber(String(value))) {
51
- value = Number(value) * 2
53
+ if (unit == "rpx" && isNumber(String(value))) {
54
+ value = Number(value) * 2;
52
55
  }
53
- value = String(value)
56
+ value = String(value);
54
57
  // 用内置验证规则中的number判断是否为数值
55
- return isNumber(value) ? `${value}${unit}` : value
56
- }
58
+ return isNumber(value) ? `${value}${unit}` : value;
59
+ };
57
60
 
58
61
  /**
59
62
  * @description 日期的月或日补零操作
@@ -61,42 +64,42 @@ const addUnit = (value: string | number = 'auto', unit: string = ''): string =>
61
64
  * @returns {String}
62
65
  */
63
66
  const padZero = (value: string | number): string => {
64
- return `00${value}`.slice(-2)
65
- }
67
+ return `00${value}`.slice(-2);
68
+ };
66
69
 
67
70
  /**
68
71
  * @description 清空对象里面的值
69
72
  * @param val 任意类型的值
70
73
  * */
71
74
  const clearVal = (val: any) => {
72
- const type = typeof val
73
- const isArray = val instanceof Array
75
+ const type = typeof val;
76
+ const isArray = val instanceof Array;
74
77
  switch (type) {
75
- case 'string':
76
- return ''
77
- case 'number':
78
- return 0
79
- case 'boolean':
80
- return false
81
- case 'undefined':
82
- return null
83
- case 'object':
84
- if (!val) return null
78
+ case "string":
79
+ return "";
80
+ case "number":
81
+ return 0;
82
+ case "boolean":
83
+ return false;
84
+ case "undefined":
85
+ return null;
86
+ case "object":
87
+ if (!val) return null;
85
88
  if (isArray) {
86
89
  val.map((item) => {
87
- clearVal(item)
88
- })
89
- return val
90
+ clearVal(item);
91
+ });
92
+ return val;
90
93
  } else {
91
94
  Object.keys(val).map((k) => {
92
- val[k] = clearVal(val[k])
93
- })
94
- return val
95
+ val[k] = clearVal(val[k]);
96
+ });
97
+ return val;
95
98
  }
96
99
  default:
97
- return ''
100
+ return "";
98
101
  }
99
- }
102
+ };
100
103
 
101
104
  /**
102
105
  * 时间戳格式化
@@ -104,30 +107,36 @@ const clearVal = (val: any) => {
104
107
  * @param fmt 例:yyyy-MM-dd HH:mm:ss / yyyy-MM-dd
105
108
  * @return date 例:2023-12-09
106
109
  */
107
- const formatTime = (timestamp: number | string, fmt: string = 'yyyy-MM-dd HH:mm:ss'): string => {
108
- let date: any
110
+ const formatTime = (
111
+ timestamp: number | string,
112
+ fmt: string = "yyyy-MM-dd HH:mm:ss",
113
+ ): string => {
114
+ let date: any;
109
115
  if (timestamp) {
110
- date = new Date(timestamp) ? new Date(timestamp) : timestamp
111
- let ret
116
+ date = new Date(timestamp) ? new Date(timestamp) : timestamp;
117
+ let ret;
112
118
  const opt: any = {
113
- 'y+': date.getFullYear().toString(), //年
114
- 'M+': (date.getMonth() + 1).toString(), //月
115
- 'd+': date.getDate().toString(), //日
116
- 'H+': date.getHours().toString(), //时
117
- 'm+': date.getMinutes().toString(), //分
118
- 's+': date.getSeconds().toString(), //秒
119
+ "y+": date.getFullYear().toString(), //年
120
+ "M+": (date.getMonth() + 1).toString(), //月
121
+ "d+": date.getDate().toString(), //日
122
+ "H+": date.getHours().toString(), //时
123
+ "m+": date.getMinutes().toString(), //分
124
+ "s+": date.getSeconds().toString(), //秒
119
125
  //如果有其他格式字符需求可以继续添加,必须转化为字符串
120
- }
126
+ };
121
127
  for (let k in opt) {
122
- ret = new RegExp('(' + k + ')').exec(fmt)
128
+ ret = new RegExp("(" + k + ")").exec(fmt);
123
129
  if (ret) {
124
- fmt = fmt.replace(ret[1], ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0'))
130
+ fmt = fmt.replace(
131
+ ret[1],
132
+ ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0"),
133
+ );
125
134
  }
126
135
  }
127
- return fmt
136
+ return fmt;
128
137
  }
129
- return date
130
- }
138
+ return date;
139
+ };
131
140
 
132
141
  /**
133
142
  * @description 时间戳或年月日格式转为多久之前
@@ -139,51 +148,56 @@ const formatTime = (timestamp: number | string, fmt: string = 'yyyy-MM-dd HH:mm:
139
148
  */
140
149
  const formatTimeToString = (
141
150
  timestamp: string | number,
142
- format: string | boolean = 'yyyy-mm-dd',
151
+ format: string | boolean = "yyyy-mm-dd",
143
152
  ): string => {
144
- const now = new Date()
145
- const oneYear = new Date(now.getFullYear(), 0, 1).getTime() // 当年一月一号时间戳
153
+ const now = new Date();
154
+ const oneYear = new Date(now.getFullYear(), 0, 1).getTime(); // 当年一月一号时间戳
146
155
 
147
- if (timestamp == null) timestamp = Number(now)
148
- timestamp = typeof timestamp === 'string' ? parseInt(timestamp) : new Date(timestamp).getTime()
156
+ if (timestamp == null) timestamp = Number(now);
157
+ timestamp =
158
+ typeof timestamp === "string"
159
+ ? parseInt(timestamp)
160
+ : new Date(timestamp).getTime();
149
161
  // 判断用户输入的时间戳是秒还是毫秒,一般前端js获取的时间戳是毫秒(13位),后端传过来的为秒(10位)
150
- if (timestamp.toString().length == 10) timestamp *= 1000
151
- let timer = now.getTime() - timestamp
152
- timer = parseInt(String(timer / 1000))
162
+ if (timestamp.toString().length == 10) timestamp *= 1000;
163
+ let timer = now.getTime() - timestamp;
164
+ timer = parseInt(String(timer / 1000));
153
165
  // 如果小于5分钟,则返回"刚刚",其他以此类推
154
- let tips = ''
166
+ let tips = "";
155
167
  switch (true) {
156
168
  case timer < 300:
157
- tips = '刚刚'
158
- break
169
+ tips = "刚刚";
170
+ break;
159
171
  case timer >= 300 && timer < 3600:
160
- tips = `${parseInt(String(timer / 60))}分钟前`
161
- break
172
+ tips = `${parseInt(String(timer / 60))}分钟前`;
173
+ break;
162
174
  case timer >= 3600 && timer < 86400:
163
- tips = `${parseInt(String(timer / 3600))}小时前`
164
- break
175
+ tips = `${parseInt(String(timer / 3600))}小时前`;
176
+ break;
165
177
  case timer >= 86400 && timer < 2592000:
166
- tips = `${parseInt(String(timer / 86400))}天前`
167
- break
178
+ tips = `${parseInt(String(timer / 86400))}天前`;
179
+ break;
168
180
  default:
169
181
  // 如果format为false,则无论什么时间戳,都显示xx之前
170
182
  if (format === false) {
171
183
  if (timer >= 2592000 && timer < 365 * 86400) {
172
- tips = `${parseInt(String(timer / (86400 * 30)))}个月前`
184
+ tips = `${parseInt(String(timer / (86400 * 30)))}个月前`;
173
185
  } else {
174
- tips = `${parseInt(String(timer / (86400 * 365)))}年前`
186
+ tips = `${parseInt(String(timer / (86400 * 365)))}年前`;
175
187
  }
176
188
  } else {
177
189
  if (timestamp > oneYear) {
178
- formatTime(timestamp, 'MM-dd')
190
+ formatTime(timestamp, "MM-dd");
179
191
  } else {
180
192
  tips =
181
- format === true ? formatTime(timestamp, 'yyyy-MM-dd') : formatTime(timestamp, format)
193
+ format === true
194
+ ? formatTime(timestamp, "yyyy-MM-dd")
195
+ : formatTime(timestamp, format);
182
196
  }
183
197
  }
184
198
  }
185
- return tips
186
- }
199
+ return tips;
200
+ };
187
201
 
188
202
  /**
189
203
  * @description 本地图片转base64方法(兼容APP、H5、小程序)
@@ -193,37 +207,37 @@ const formatTimeToString = (
193
207
  const imageToBase64 = (path: string) => {
194
208
  return new Promise((resolve, reject) => {
195
209
  // #ifdef APP-PLUS
196
- plus.io.resolveLocalFileSystemURL(path, (entry) => {
197
- entry.file((file) => {
198
- let fileReader = new plus.io.FileReader()
199
- fileReader.readAsDataURL(file)
200
- fileReader.onloadend = (evt) => {
201
- let base64 = evt.target.result.split(',')[1]
202
- resolve(base64)
203
- }
204
- })
205
- })
210
+ plus.io.resolveLocalFileSystemURL(path, (entry: any) => {
211
+ entry.file((file: any) => {
212
+ let fileReader = new plus.io.FileReader();
213
+ fileReader.readAsDataURL(file);
214
+ fileReader.onloadend = (evt: any) => {
215
+ let base64 = evt.target.result.split(",")[1];
216
+ resolve(base64);
217
+ };
218
+ });
219
+ });
206
220
  // #endif
207
221
  // #ifdef H5
208
222
  uni.request({
209
223
  url: path,
210
- responseType: 'arraybuffer',
211
- success: (res) => {
212
- resolve(uni.arrayBufferToBase64(res.data))
224
+ responseType: "arraybuffer",
225
+ success: (res: UniApp.RequestSuccessCallbackResult) => {
226
+ resolve(uni.arrayBufferToBase64(res.data as ArrayBuffer));
213
227
  },
214
- })
228
+ });
215
229
  // #endif
216
230
  // #ifdef MP-WEIXIN
217
231
  uni.getFileSystemManager().readFile({
218
232
  filePath: path,
219
- encoding: 'base64',
233
+ encoding: "base64",
220
234
  success: (res) => {
221
- resolve(res.data)
235
+ resolve(res.data);
222
236
  },
223
- })
237
+ });
224
238
  // #endif
225
- })
226
- }
239
+ });
240
+ };
227
241
 
228
242
  /**
229
243
  * 函数防抖:一段实现执行多次,只执行最后一次
@@ -232,31 +246,31 @@ const imageToBase64 = (path: string) => {
232
246
  * @returns {void}
233
247
  * @constructor
234
248
  */
235
- let timeout: ReturnType<typeof setTimeout> | null = null
249
+ let timeout: ReturnType<typeof setTimeout> | null = null;
236
250
  function debounce<T extends (...args: any[]) => void>(
237
251
  fn: T,
238
252
  wait: number = 500,
239
253
  immediate: boolean = false,
240
254
  ) {
241
255
  // 清除定时器
242
- if (timeout !== null) clearTimeout(timeout)
256
+ if (timeout !== null) clearTimeout(timeout);
243
257
  // 立即执行,此类情况一般用不到
244
258
  if (immediate) {
245
- const callNow = !timeout
259
+ const callNow = !timeout;
246
260
  timeout = setTimeout(() => {
247
- timeout = null
248
- }, wait)
249
- if (callNow) typeof fn === 'function' && fn()
261
+ timeout = null;
262
+ }, wait);
263
+ if (callNow) typeof fn === "function" && fn();
250
264
  } else {
251
265
  // 设置定时器,当最后一次操作后,timeout不会再被清除,所以在延时wait毫秒后执行func回调方法
252
266
  timeout = setTimeout(() => {
253
- typeof fn === 'function' && fn()
254
- }, wait)
267
+ typeof fn === "function" && fn();
268
+ }, wait);
255
269
  }
256
270
  }
257
271
 
258
- let timer: ReturnType<typeof setTimeout> | null = null
259
- let flag: boolean | undefined
272
+ let timer: ReturnType<typeof setTimeout> | null = null;
273
+ let flag: boolean | undefined;
260
274
  /**
261
275
  * 函数节流: 一段时间执行一次
262
276
  * @param {void} fn 回调函数
@@ -265,25 +279,29 @@ let flag: boolean | undefined
265
279
  * @returns {void}
266
280
  * @constructor
267
281
  */
268
- const throttle = (fn: Function, wait: number = 500, immediate: boolean = true): void => {
282
+ const throttle = (
283
+ fn: Function,
284
+ wait: number = 500,
285
+ immediate: boolean = true,
286
+ ): void => {
269
287
  if (immediate) {
270
288
  if (!flag) {
271
- flag = true
289
+ flag = true;
272
290
  // 如果是立即执行,则在wait毫秒内开始时执行
273
- typeof fn === 'function' && fn()
291
+ typeof fn === "function" && fn();
274
292
  timer = setTimeout(() => {
275
- flag = false
276
- }, wait)
293
+ flag = false;
294
+ }, wait);
277
295
  }
278
296
  } else if (!flag) {
279
- flag = true
297
+ flag = true;
280
298
  // 如果是非立即执行,则在wait毫秒内的结束处执行
281
299
  timer = setTimeout(() => {
282
- flag = false
283
- typeof fn === 'function' && fn()
284
- }, wait)
300
+ flag = false;
301
+ typeof fn === "function" && fn();
302
+ }, wait);
285
303
  }
286
- }
304
+ };
287
305
 
288
306
  /**
289
307
  * 递归拷贝对象
@@ -291,19 +309,19 @@ const throttle = (fn: Function, wait: number = 500, immediate: boolean = true):
291
309
  * @returns 深拷贝的数组和对象
292
310
  * */
293
311
  const deepClone = (source: any) => {
294
- if (!source && typeof source !== 'object') {
295
- throw new Error('该值不存在或者不是个对象')
312
+ if (!source && typeof source !== "object") {
313
+ throw new Error("该值不存在或者不是个对象");
296
314
  }
297
- const targetObj: any = source.constructor === Array ? [] : {}
315
+ const targetObj: any = source.constructor === Array ? [] : {};
298
316
  Object.keys(source).forEach((keys) => {
299
- if (source[keys] && typeof source[keys] === 'object') {
300
- targetObj[keys] = deepClone(source[keys])
317
+ if (source[keys] && typeof source[keys] === "object") {
318
+ targetObj[keys] = deepClone(source[keys]);
301
319
  } else {
302
- targetObj[keys] = source[keys]
320
+ targetObj[keys] = source[keys];
303
321
  }
304
- })
305
- return targetObj
306
- }
322
+ });
323
+ return targetObj;
324
+ };
307
325
 
308
326
  /**
309
327
  * 字节转化(b/KB/MB/GB)单位
@@ -311,16 +329,16 @@ const deepClone = (source: any) => {
311
329
  * @returns {string} 返回单位大小
312
330
  * */
313
331
  const bytesToSize = (bytes: number) => {
314
- const sizes = ['b', 'KB', 'MB', 'GB', 'TB']
332
+ const sizes = ["b", "KB", "MB", "GB", "TB"];
315
333
  if (bytes === 0) {
316
- return '0b'
334
+ return "0b";
317
335
  }
318
- const i = Math.floor(Math.log(bytes) / Math.log(1024))
336
+ const i = Math.floor(Math.log(bytes) / Math.log(1024));
319
337
  if (i === 0) {
320
- return `${bytes}${sizes[i]}`
338
+ return `${bytes}${sizes[i]}`;
321
339
  }
322
- return `${(bytes / 1024 ** i).toFixed(1)}${sizes[i]}`
323
- }
340
+ return `${(bytes / 1024 ** i).toFixed(1)}${sizes[i]}`;
341
+ };
324
342
 
325
343
  /**
326
344
  * @description 将对象转换为 URL 查询参数字符串
@@ -332,10 +350,10 @@ const objectToUrlParams = (params: Record<string, any>): string => {
332
350
  .filter(([key, value]) => value !== undefined && value !== null) // 过滤掉值为 undefined 或 null 的项
333
351
  .map(([key, value]) => {
334
352
  // 对值进行编码以确保 URL 安全
335
- return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`
353
+ return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
336
354
  })
337
- .join('&') // 使用 & 拼接所有参数
338
- }
355
+ .join("&"); // 使用 & 拼接所有参数
356
+ };
339
357
 
340
358
  /**
341
359
  * 获取 [min,max]的随机数
@@ -345,23 +363,25 @@ const objectToUrlParams = (params: Record<string, any>): string => {
345
363
  * @returns {Number} string 随机数
346
364
  */
347
365
  const random = (min: number | string, max: number | string): number => {
348
- min = Number(min)
349
- max = Number(max)
350
- return Math.floor(Math.random() * (max - min + 1) + min) ?? 0
351
- }
366
+ min = Number(min);
367
+ max = Number(max);
368
+ return Math.floor(Math.random() * (max - min + 1) + min) || 0;
369
+ };
352
370
 
353
371
  const range = (min = 0, max = 0, value = 0) => {
354
- return Math.max(min, Math.min(max, Number(value)))
355
- }
372
+ return Math.max(min, Math.min(max, Number(value)));
373
+ };
356
374
 
357
- export type RectResultType<T extends boolean> = T extends true ? UniApp.NodeInfo[] : UniApp.NodeInfo
375
+ export type RectResultType<T extends boolean> = T extends true
376
+ ? UniApp.NodeInfo[]
377
+ : UniApp.NodeInfo;
358
378
  /**
359
379
  * 查询节点信息
360
380
  * 目前此方法在支付宝小程序中无法获取组件跟接点的尺寸,为支付宝的bug(2020-07-21)
361
381
  * 解决办法为在组件根部再套一个没有任何作用的view元素
362
382
  * @param selector 元素类名或id
363
383
  * @param all 是否获取多个相同元素数值
364
- * @param ins 在微信小程序里,因为utils文件里面获取不到instance值所以必须通过ins这个传过来
384
+ * @param ins 在微信小程序里,因为utils文件里面获取不到instance值所以必须通过ins这个传过来【注意,(支付宝小程序无效)】
365
385
  * @param useFields 是否使用 fields 方法获取节点信息
366
386
  */
367
387
  const getRect = <T extends boolean>(
@@ -371,32 +391,35 @@ const getRect = <T extends boolean>(
371
391
  useFields?: boolean,
372
392
  ): Promise<RectResultType<T>> => {
373
393
  return new Promise<RectResultType<T>>((resolve, reject) => {
374
- let query: UniNamespace.SelectorQuery | null = null
375
- // TODO: 在微信小程序里,因为utils文件里面获取不到instance值所以必须通过ins这个传过来
394
+ let query: UniNamespace.SelectorQuery | null = null;
376
395
  if (ins) {
377
- query = uni.createSelectorQuery().in(ins)
396
+ // TODO: 在微信小程序里,因为utils文件里面获取不到instance值所以必须通过ins这个传过来
397
+ query = uni.createSelectorQuery().in(ins);
378
398
  } else {
379
- query = uni.createSelectorQuery()
399
+ query = uni.createSelectorQuery();
380
400
  }
381
- const method = all ? 'selectAll' : 'select'
401
+ const method = all ? "selectAll" : "select";
382
402
 
383
403
  const callback = (rect: UniApp.NodeInfo | UniApp.NodeInfo[]) => {
384
404
  if (all && Array.isArray(rect) && rect.length > 0) {
385
- resolve(rect as RectResultType<T>)
405
+ resolve(rect as RectResultType<T>);
386
406
  } else if (!all && rect) {
387
- resolve(rect as RectResultType<T>)
407
+ resolve(rect as RectResultType<T>);
388
408
  } else {
389
- reject(new Error('No nodes found'))
409
+ error("没找到模块");
410
+ reject(new Error("No nodes found"));
390
411
  }
391
- }
412
+ };
392
413
 
393
414
  if (useFields) {
394
- query[method](selector).fields({ size: true, node: true }, callback).exec()
415
+ query[method](selector)
416
+ .fields({ size: true, node: true }, callback)
417
+ .exec();
395
418
  } else {
396
- query[method](selector).boundingClientRect(callback).exec()
419
+ query[method](selector).boundingClientRect(callback).exec();
397
420
  }
398
- })
399
- }
421
+ });
422
+ };
400
423
 
401
424
  /**
402
425
  * @description 用于获取用户传递值的px值 如果用户传递了"xxpx"或者"xxrpx",取出其数值部分,如果是"xxxrpx"还需要用过uni.rpx2px进行转换
@@ -404,19 +427,21 @@ const getRect = <T extends boolean>(
404
427
  * @param {boolean} unit
405
428
  * @returns {number|string}
406
429
  */
407
- function getPx(value: string | number, unit: true): string
408
- function getPx(value: string | number, unit?: false): number
430
+ function getPx(value: string | number, unit: true): string;
431
+ function getPx(value: string | number, unit?: false): number;
409
432
  function getPx(value: string | number, unit: boolean = false): string | number {
410
- if (isNumber(value) || typeof value === 'number') {
411
- return unit ? `${value}px` : Number(value)
433
+ if (isNumber(value) || typeof value === "number") {
434
+ return unit ? `${value}px` : Number(value);
412
435
  }
413
436
  // 如果带有rpx,先取出其数值部分,再转为px值
414
437
  if (/(rpx|upx)$/.test(value)) {
415
- return unit ? `${uni.rpx2px(parseInt(value))}px` : Number(uni.rpx2px(parseInt(value)))
438
+ return unit
439
+ ? `${uni.rpx2px(parseInt(value))}px`
440
+ : Number(uni.rpx2px(parseInt(value)));
416
441
  } else if (/(px)$/.test(value)) {
417
- return unit ? value : Number(value.replace('px', ''))
442
+ return unit ? value : Number(value.replace("px", ""));
418
443
  } else {
419
- return unit ? `${parseInt(value)}px` : Number(value)
444
+ return unit ? `${parseInt(value)}px` : Number(value);
420
445
  }
421
446
  }
422
447
 
@@ -426,8 +451,8 @@ function getPx(value: string | number, unit: boolean = false): string | number {
426
451
  const formatObject = (obj: CSSProperties) => {
427
452
  return Object.entries(obj)
428
453
  .map(([key, value]) => `${key.toUpperCase()}: ${value}`)
429
- .join('; ')
430
- }
454
+ .join("; ");
455
+ };
431
456
 
432
457
  export {
433
458
  encryptData,
@@ -448,4 +473,4 @@ export {
448
473
  getRect,
449
474
  getPx,
450
475
  formatObject,
451
- }
476
+ };