@zamlia/mini-ui 0.0.12 → 0.0.13

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 (53) hide show
  1. package/es/index.d.ts +1 -0
  2. package/es/index.d.ts.map +1 -1
  3. package/es/index.js +762 -33
  4. package/es/index.js.map +1 -1
  5. package/es/types/components.d.ts +13 -0
  6. package/es/types/components.d.ts.map +1 -0
  7. package/es/types/index.d.ts +8 -0
  8. package/es/types/index.d.ts.map +1 -0
  9. package/es/types/utils.d.ts +70 -0
  10. package/es/types/utils.d.ts.map +1 -0
  11. package/es/utils/cache.d.ts +6 -0
  12. package/es/utils/cache.d.ts.map +1 -0
  13. package/es/utils/index.d.ts +73 -33
  14. package/es/utils/index.d.ts.map +1 -1
  15. package/es/utils/lib/format.utils.d.ts +29 -0
  16. package/es/utils/lib/format.utils.d.ts.map +1 -0
  17. package/es/utils/lib/is.utils.d.ts +128 -0
  18. package/es/utils/lib/is.utils.d.ts.map +1 -0
  19. package/es/utils/lib/mini.utils.d.ts +78 -0
  20. package/es/utils/lib/mini.utils.d.ts.map +1 -0
  21. package/es/utils/lib/object.utils.d.ts +40 -0
  22. package/es/utils/lib/object.utils.d.ts.map +1 -0
  23. package/es/utils/lib/pay.utils.d.ts +25 -0
  24. package/es/utils/lib/pay.utils.d.ts.map +1 -0
  25. package/es/utils/lib/string.utils.d.ts +36 -0
  26. package/es/utils/lib/string.utils.d.ts.map +1 -0
  27. package/lib/index.d.ts +155 -34
  28. package/lib/index.d.ts.map +1 -1
  29. package/lib/index.js +762 -33
  30. package/lib/index.js.map +1 -1
  31. package/lib/types/components.d.ts +13 -0
  32. package/lib/types/components.d.ts.map +1 -0
  33. package/lib/types/index.d.ts +8 -0
  34. package/lib/types/index.d.ts.map +1 -0
  35. package/lib/types/utils.d.ts +70 -0
  36. package/lib/types/utils.d.ts.map +1 -0
  37. package/lib/utils/cache.d.ts +6 -0
  38. package/lib/utils/cache.d.ts.map +1 -0
  39. package/lib/utils/index.d.ts +73 -33
  40. package/lib/utils/index.d.ts.map +1 -1
  41. package/lib/utils/lib/format.utils.d.ts +29 -0
  42. package/lib/utils/lib/format.utils.d.ts.map +1 -0
  43. package/lib/utils/lib/is.utils.d.ts +128 -0
  44. package/lib/utils/lib/is.utils.d.ts.map +1 -0
  45. package/lib/utils/lib/mini.utils.d.ts +78 -0
  46. package/lib/utils/lib/mini.utils.d.ts.map +1 -0
  47. package/lib/utils/lib/object.utils.d.ts +40 -0
  48. package/lib/utils/lib/object.utils.d.ts.map +1 -0
  49. package/lib/utils/lib/pay.utils.d.ts +25 -0
  50. package/lib/utils/lib/pay.utils.d.ts.map +1 -0
  51. package/lib/utils/lib/string.utils.d.ts +36 -0
  52. package/lib/utils/lib/string.utils.d.ts.map +1 -0
  53. package/package.json +2 -2
package/lib/index.js CHANGED
@@ -227,15 +227,153 @@ const NavBar = (props) => {
227
227
  React.createElement(components.Text, null, title)));
228
228
  };
229
229
 
230
- /**
231
- * 工具函数
232
- */
233
- const Utils = {
230
+ var IsUtils = {
234
231
  /**
235
- * 检查值是否为空
236
- * @param value 要检查的值
237
- * @returns 如果值为空返回 true,否则返回 false
232
+ * 是否是阿里
233
+ * @returns {boolean}
234
+ */
235
+ isAlipay: () => {
236
+ return Utils.env() === 'alipay';
237
+ },
238
+ /**
239
+ * 是否是微信
240
+ * @returns {boolean}
241
+ */
242
+ isWeapp: () => {
243
+ return Utils.env() === 'weapp';
244
+ },
245
+ /**
246
+ * 纯数字
247
+ * @param {*} str
248
+ * @returns {boolean}
238
249
  */
250
+ isNumberStr(str) {
251
+ return /^\d+$/.test(str);
252
+ },
253
+ isText: (value) => {
254
+ if (!value) {
255
+ return false;
256
+ }
257
+ if (value.trim().length === 0) {
258
+ return false;
259
+ }
260
+ return true;
261
+ },
262
+ /**
263
+ * 验证是否是【16进制颜色】
264
+ * @public
265
+ * @param {string} text 验证字符串
266
+ * @returns {boolean}
267
+ * @example `
268
+ * 例如:isHexColor('#FF0000'); // true
269
+ * 例如:isHexColor('#f00'); // true
270
+ * 例如:isHexColor('#12345G'); // false
271
+ * `
272
+ */
273
+ isHexColor(text) {
274
+ const hexColorRegex = /^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/;
275
+ return hexColorRegex.test(text);
276
+ },
277
+ /**
278
+ * 验证是否是【rgb或者rgba颜色】
279
+ * @public
280
+ * @param {string} text 验证字符串
281
+ * @returns {boolean}
282
+ * @example `
283
+ * 匹配 rgb() 格式,例如:isRgbOrRgbaColor(rgb(255, 0, 0)); // true
284
+ * 匹配 rgba() 格式,例如:isRgbOrRgbaColor(rgba(255, 0, 0, 0.5)); // true
285
+ * `
286
+ */
287
+ isRgbOrRgbaColor(text) {
288
+ const rgbRegex = /^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/;
289
+ const rgbaRegex = /^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|0?\.\d+|1(\.0)?)\s*\)$/;
290
+ return rgbRegex.test(text) || rgbaRegex.test(text);
291
+ },
292
+ /**
293
+ * 验证是否是【手机号码 简版】
294
+ * @public
295
+ * @param {string} text 验证字符串
296
+ * @returns {boolean}
297
+ */
298
+ isMobile(text) {
299
+ const mobileRegex = /^1[0-9]{10}$/;
300
+ return mobileRegex.test(text);
301
+ },
302
+ /**
303
+ * 验证是否是【电话号码】
304
+ * @public
305
+ * @param {string} text 验证字符串
306
+ * @returns {boolean}
307
+ */
308
+ isPhone(text) {
309
+ const phoneRegex = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/;
310
+ return phoneRegex.test(text);
311
+ },
312
+ /**
313
+ * 验证是否是【邮箱】
314
+ * @public
315
+ * @param {string} text 验证字符串
316
+ * @returns {boolean}
317
+ */
318
+ isEmail(text) {
319
+ const emailRegex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/;
320
+ return emailRegex.test(text);
321
+ },
322
+ /**
323
+ * 验证是否是【URL地址】
324
+ * @public
325
+ * @param {string} text 验证字符串
326
+ * @returns {boolean}
327
+ */
328
+ isURL(text) {
329
+ const urlRegex = /^http[s]?:\/\/.*/;
330
+ return urlRegex.test(text);
331
+ },
332
+ /**
333
+ * 验证是否是【身份证】
334
+ * @public
335
+ * @param {string} text 验证字符串
336
+ * @returns {boolean}
337
+ */
338
+ isIdCard(text) {
339
+ const idcardRegex = /^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$|^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/;
340
+ return idcardRegex.test(text);
341
+ },
342
+ /**
343
+ * 验证是否是【金钱】
344
+ * @public
345
+ * @param {string} text 验证字符串
346
+ * @returns {boolean}
347
+ */
348
+ isMoney(text) {
349
+ const moneyRegex = /([1-9]([0-9]+)?(.[0-9]{1,2})?$)|((0){1}$)|(^[0-9].0-9?$)/;
350
+ return moneyRegex.test(text);
351
+ },
352
+ /**
353
+ * 验证是否是【姓名】
354
+ * @public
355
+ * @param {string} text 验证字符串
356
+ * @returns {boolean}
357
+ */
358
+ isName(text) {
359
+ const nameRegex = /^[\u4e00-\u9fa5]{1,20}(·[\u4e00-\u9fa5]{1,20}){0,20}([,,][\u4e00-\u9fa5]{1,20}(·[\u4e00-\u9fa5]{1,20}){0,20})*$/;
360
+ return nameRegex.test(text);
361
+ },
362
+ /**
363
+ * 验证是否是【社会统一代码】
364
+ * @public
365
+ * @param {string} text 验证字符串
366
+ * @returns {boolean}
367
+ */
368
+ isCompanyCode(text) {
369
+ const codeRegex = /^[^_IOZSVa-z\W]{2}\d{6}[^_IOZSVa-z\W]{10}$/;
370
+ return codeRegex.test(text);
371
+ },
372
+ /**
373
+ * 检查值是否为空
374
+ * @param value 要检查的值
375
+ * @returns 如果值为空返回 true,否则返回 false
376
+ */
239
377
  isEmpty(value) {
240
378
  if (value == null) {
241
379
  return true;
@@ -251,32 +389,6 @@ const Utils = {
251
389
  }
252
390
  return false;
253
391
  },
254
- /**
255
- * 深拷贝对象
256
- * @param obj 要拷贝的对象
257
- * @returns 深拷贝后的新对象
258
- */
259
- cloneDeep(obj) {
260
- if (obj === null || typeof obj !== 'object') {
261
- return obj;
262
- }
263
- if (obj instanceof Date) {
264
- return new Date(obj.getTime());
265
- }
266
- if (obj instanceof Array) {
267
- return obj.map(item => Utils.cloneDeep(item));
268
- }
269
- if (typeof obj === 'object') {
270
- const clonedObj = {};
271
- for (const key in obj) {
272
- if (obj.hasOwnProperty(key)) {
273
- clonedObj[key] = Utils.cloneDeep(obj[key]);
274
- }
275
- }
276
- return clonedObj;
277
- }
278
- return obj;
279
- },
280
392
  /**
281
393
  * 判断 URL 是否是图片
282
394
  * @param url 要检查的 URL
@@ -331,6 +443,9 @@ const Utils = {
331
443
  return videoExtensions.test(url.toLowerCase());
332
444
  }
333
445
  },
446
+ };
447
+
448
+ var StringUtils = {
334
449
  /**
335
450
  * 将字符串多余长度转化为...
336
451
  * @param str 字符串
@@ -361,6 +476,620 @@ const Utils = {
361
476
  return '';
362
477
  }
363
478
  },
479
+ /**
480
+ * 生成随机字符串
481
+ * @param {number} len 长度
482
+ * @returns {string}
483
+ */
484
+ generateNonceStr: (len) => {
485
+ let data = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678";
486
+ let str = "";
487
+ for (let i = 0; i < len; i++) {
488
+ str += data.charAt(Math.floor(Math.random() * data.length));
489
+ }
490
+ return str;
491
+ },
492
+ /**
493
+ * ecode
494
+ * @param url 加密链接
495
+ * @returns 处理好的链接
496
+ */
497
+ urlEcode: (url) => {
498
+ return encodeURIComponent(url);
499
+ },
500
+ /**
501
+ * 解密
502
+ * @param url 解密链接
503
+ * @returns 处理好的链接
504
+ */
505
+ urlDecode: (url) => {
506
+ return decodeURIComponent(url);
507
+ },
508
+ /**
509
+ * 颜色16进制转rgb
510
+ * @public
511
+ * @param {string} colorCode 16进制的颜色
512
+ * @param {number} opacity 透明度 0 < colorCode < 1
513
+ * @returns {string} rgb格式的颜色
514
+ */
515
+ hexToRgbaColor(colorCode, opacity = 1) {
516
+ // 去除颜色码前面的 #
517
+ const cleanedColorCode = colorCode.replace('#', '');
518
+ // 将颜色码转换为十进制数
519
+ const decimalValue = parseInt(cleanedColorCode, 16);
520
+ // 分离红、绿、蓝三个分量
521
+ const red = (decimalValue >> 16) & 255;
522
+ const green = (decimalValue >> 8) & 255;
523
+ const blue = decimalValue & 255;
524
+ return `rgba(${red}, ${green}, ${blue}, ${opacity})`;
525
+ }
526
+ };
527
+
528
+ /**
529
+ * 方法命名规则format**
530
+ */
531
+ var FormatUtils = {
532
+ /**
533
+ * 金额格式化
534
+ * @public
535
+ * @param {string | number} amount 金额
536
+ * @returns {string}
537
+ */
538
+ formatAmount(amount) {
539
+ if (typeof amount === 'number')
540
+ return amount.toFixed(2);
541
+ if (typeof amount !== 'string')
542
+ return amount || 0;
543
+ return parseFloat(amount).toFixed(2);
544
+ },
545
+ /**
546
+ * 格式化时间
547
+ * @public
548
+ * @param {string} time 要格式化的时间(支持能被 Date 解析的字符串,如 ISO 字符串、时间戳字符串等)
549
+ * @param {string} format 时间格式,默认 'YYYY-MM-DD HH:mm:ss'
550
+ * @returns {string} 格式化后的时间字符串
551
+ */
552
+ formatDate(time, format) {
553
+ // 入参校验:无时间值直接返回空字符串
554
+ if (!time)
555
+ return '';
556
+ // 初始化 Date 对象(兼容时间戳字符串/普通时间字符串)
557
+ let date;
558
+ // 处理时间戳字符串(如 "1735689600000")
559
+ if (/^\d+$/.test(time)) {
560
+ date = new Date(Number(time));
561
+ }
562
+ else {
563
+ date = new Date(time);
564
+ }
565
+ // 校验 Date 对象是否有效
566
+ if (isNaN(date.getTime())) {
567
+ console.warn('formatDate: 传入的时间格式无效 →', time);
568
+ return '';
569
+ }
570
+ // 默认格式化模板
571
+ const fmt = format || 'YYYY-MM-DD HH:mm:ss';
572
+ // 定义时间补零函数(单个数字补 0,如 5 → "05")
573
+ const padZero = (num) => num.toString().padStart(2, '0');
574
+ // 🌟 关键修复:所有值都转为 string 类型,避免 number 混入
575
+ const timeMap = {
576
+ YYYY: date.getFullYear().toString(), // 4位年份(转为字符串)
577
+ MM: padZero(date.getMonth() + 1), // 月份(本身已是字符串)
578
+ DD: padZero(date.getDate()), // 日期(本身已是字符串)
579
+ HH: padZero(date.getHours()), // 小时(本身已是字符串)
580
+ mm: padZero(date.getMinutes()), // 分钟(本身已是字符串)
581
+ ss: padZero(date.getSeconds()), // 秒(本身已是字符串)
582
+ // 可选扩展的字段也统一为字符串
583
+ D: date.getDate().toString(),
584
+ M: (date.getMonth() + 1).toString(),
585
+ h: padZero(date.getHours() % 12 || 12),
586
+ };
587
+ // 🌟 修复 replace 回调的返回值类型:确保只返回 string
588
+ return fmt.replace(/YYYY|MM|DD|HH|mm|ss|D|M|h/g, (match) => {
589
+ // 找不到匹配的占位符时,返回原字符串(避免返回 undefined)
590
+ return timeMap[match] || match;
591
+ });
592
+ },
593
+ /**
594
+ * 中文日期格式转换
595
+ * @param {string} dateStr 字符串日期
596
+ * @return {string}
597
+ * @description YYYY-MM-DD -> YYYY年MM月DD日
598
+ */
599
+ formatDateCn(dateStr) {
600
+ // 通过正则表达式提取年、月、日
601
+ const regex = /(\d{4})年(\d{1,2})月(\d{1,2})日/;
602
+ const match = dateStr.match(regex);
603
+ if (match) {
604
+ const year = match[1];
605
+ const month = String(match[2]).padStart(2, '0'); // 确保月是两位数
606
+ const day = String(match[3]).padStart(2, '0'); // 确保日是两位数
607
+ return `${year}年${month}月${day}日`;
608
+ }
609
+ else {
610
+ return dateStr;
611
+ }
612
+ }
613
+ };
614
+
615
+ var ObjectUtils = {
616
+ /**
617
+ * json字符串转对象
618
+ * @param {string} jsonStr JSON字符串
619
+ * @return {Record<string, any>}
620
+ */
621
+ jsonStrToObj(jsonStr) {
622
+ if (!jsonStr)
623
+ return {};
624
+ try {
625
+ const obj = JSON.parse(jsonStr);
626
+ return obj;
627
+ }
628
+ catch (error) {
629
+ return {};
630
+ }
631
+ },
632
+ /**
633
+ * 对象转化成url params需要的字符串
634
+ * @public
635
+ * @param {Record<string, any>} params
636
+ * @returns {string}
637
+ */
638
+ objectToParams(params) {
639
+ // 1. 校验入参:非对象/空对象直接返回空字符串(和lodash.isObject行为对齐)
640
+ if (!params ||
641
+ typeof params !== 'object' ||
642
+ Array.isArray(params) || // 排除数组(lodash.isObject认为数组是对象,这里按需调整)
643
+ Object.keys(params).length === 0) {
644
+ return '';
645
+ }
646
+ // 2. 原生遍历对象,拼接key=value(替代lodash.map)
647
+ return Object.entries(params)
648
+ // 过滤值为undefined/null的参数(可选,和lodash行为一致)
649
+ .filter(([_, value]) => value !== undefined && value !== null)
650
+ // 拼接key=value(注意:若需要URL编码,可加encodeURIComponent)
651
+ .map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`)
652
+ // 用&连接所有参数
653
+ .join('&');
654
+ },
655
+ /**
656
+ * 清除空字段
657
+ * @param {Record<string, any>} obj
658
+ * @returns {Record<string, any>}
659
+ */
660
+ objectCleanEmpty(obj) {
661
+ if (!obj)
662
+ return obj;
663
+ // 使用 Object.entries 来遍历对象,过滤掉空数组、null、undefined
664
+ Object.keys(obj).forEach(key => {
665
+ const value = obj[key];
666
+ // 如果值是 null、undefined 或 空数组,则删除该属性
667
+ if (value === null || value === undefined || (Array.isArray(value) && value.length === 0)) {
668
+ delete obj[key];
669
+ }
670
+ });
671
+ return obj;
672
+ },
673
+ /**
674
+ * 洗牌算法
675
+ * @public
676
+ * @param {(Record<string, any> | string | number)[]} arr 需要洗牌的数组
677
+ * @returns {(Record<string, any> | string | number)[]}
678
+ */
679
+ shuffle(arr) {
680
+ const newArr = arr;
681
+ for (let i = 0; i < newArr.length; i++) {
682
+ let j = Utils.getRandom(0, i);
683
+ const temp = newArr[i];
684
+ newArr[i] = newArr[j];
685
+ newArr[j] = temp;
686
+ }
687
+ return newArr;
688
+ },
689
+ /**
690
+ * 将数组转换为树结构数组
691
+ * @public
692
+ * @param {array} array 需要转换的数组
693
+ * @param {string} [id='id'] 数组中对象的唯一ID的别名
694
+ * @param {string} [parentId='pid'] 数组中对象的父ID的别名
695
+ * @param {string} [children='children'] 数组中对象的子级的别名
696
+ * @return {array} 返回一个树结构数组
697
+ */
698
+ arrayToTree(array, id = 'id', parentId = 'pid', children = 'children') {
699
+ const result = [];
700
+ const hash = {};
701
+ const data = [...(array || [])];
702
+ // const newData: any[] = lodash.sortBy(data, (it: T & { sort: number }) => {
703
+ // return it?.sort;
704
+ // });
705
+ data.forEach((_item, index) => {
706
+ hash[data[index][id]] = data[index];
707
+ });
708
+ data.forEach((item) => {
709
+ // @ts-ignore
710
+ const hashParent = hash[item[parentId]];
711
+ if (hashParent) {
712
+ if (!hashParent[children]) {
713
+ hashParent[children] = [];
714
+ }
715
+ hashParent[children].push(item);
716
+ }
717
+ else {
718
+ result.push(item);
719
+ }
720
+ });
721
+ return result;
722
+ }
723
+ };
724
+
725
+ // 当前用户位置
726
+ const CURRENT_LOCATION_KEY = 'LOCATION';
727
+ const setCurrentLocation = (value) => {
728
+ return Taro.setStorageSync(CURRENT_LOCATION_KEY, value);
729
+ };
730
+
731
+ /** 页面参数存储缓存key */
732
+ const CACHE_PARAMS_KEY = 'params';
733
+ const ENV_TEXT = {
734
+ weapp: '微信',
735
+ swan: '百度',
736
+ alipay: '支付宝',
737
+ tt: '字节跳动',
738
+ qq: 'QQ',
739
+ h5: 'h5'
740
+ };
741
+ var miniUtils = {
742
+ /**
743
+ * 开发中
744
+ */
745
+ dev: () => {
746
+ Utils.showToast({ title: '开发中' });
747
+ },
748
+ /**
749
+ * 环境
750
+ * @param {string}isCapitalization 大写
751
+ * @return {string} weapp | alipay
752
+ */
753
+ env: (isCapitalization = false) => {
754
+ var _a, _b, _c, _d;
755
+ return isCapitalization ? ((_c = (_b = (_a = process === null || process === void 0 ? void 0 : process.env) === null || _a === void 0 ? void 0 : _a.TARO_ENV) === null || _b === void 0 ? void 0 : _b.toUpperCase) === null || _c === void 0 ? void 0 : _c.call(_b)) || '' : ((_d = process === null || process === void 0 ? void 0 : process.env) === null || _d === void 0 ? void 0 : _d.TARO_ENV) || '';
756
+ },
757
+ /**
758
+ * 环境中文
759
+ * @return {string} weapp | alipay
760
+ */
761
+ envText: () => {
762
+ return ENV_TEXT[Utils.env()];
763
+ },
764
+ showToast: (params) => {
765
+ const { delay, ...restParams } = params;
766
+ const toast = () => Taro.showToast({ icon: 'none', mask: true, duration: 1500, ...restParams });
767
+ if (typeof delay === 'number') {
768
+ Utils.delay(delay).then(() => toast());
769
+ return;
770
+ }
771
+ toast();
772
+ },
773
+ /**
774
+ * 设置拷贝内容到剪切盘
775
+ * @param content 拷贝内容
776
+ */
777
+ setCopyContent(content) {
778
+ Taro.setClipboardData({
779
+ data: content,
780
+ success: function () {
781
+ Utils.showToast({ title: '拷贝成功', icon: 'success' });
782
+ },
783
+ fail: function (err) {
784
+ Utils.showToast({ title: `拷贝失败-${err}` });
785
+ }
786
+ });
787
+ },
788
+ /**
789
+ * 扫码
790
+ * @param {boolean} isonlyFromCamera 是否只支持相机扫码
791
+ * @returns {Promise<string>}
792
+ */
793
+ scanCode: (isonlyFromCamera) => {
794
+ return new Promise((resolve) => {
795
+ Taro.scanCode({
796
+ onlyFromCamera: isonlyFromCamera || false,
797
+ scanType: 'QR_CODE',
798
+ success: (res) => {
799
+ let { result } = res;
800
+ if (!Utils.isText(result)) {
801
+ Utils.showToast({ title: '无法识别,请重试' });
802
+ return;
803
+ }
804
+ resolve(result);
805
+ },
806
+ fail: (e) => {
807
+ console.error(e);
808
+ }
809
+ });
810
+ });
811
+ },
812
+ /**
813
+ * 小程序登录
814
+ * @returns 获取的code
815
+ */
816
+ miniLogin: () => {
817
+ return new Promise((resolve) => {
818
+ Taro.login({
819
+ success: res => {
820
+ const { code } = res;
821
+ if (!code) {
822
+ console.error('登录失败.' + res.errMsg);
823
+ resolve();
824
+ }
825
+ else {
826
+ resolve(code);
827
+ }
828
+ },
829
+ fail: err => {
830
+ Utils.showToast({ title: err.errMsg });
831
+ console.error('登录失败.', err);
832
+ resolve();
833
+ },
834
+ });
835
+ });
836
+ },
837
+ /**
838
+ * 获取定位
839
+ * @returns {Promise<GeoLocation>}
840
+ */
841
+ getLocation() {
842
+ return new Promise((resolve) => {
843
+ {
844
+ Taro.getLocation({ type: 'gcj02' }).then(res => {
845
+ const { longitude, latitude } = res;
846
+ setCurrentLocation({ longitude, latitude });
847
+ resolve({ longitude, latitude });
848
+ }).catch((error) => {
849
+ if ((error === null || error === void 0 ? void 0 : error.errMsg) === 'getLocation:fail auth deny' || (error === null || error === void 0 ? void 0 : error.errMsg) === 'getLocation:fail auth denied') {
850
+ // 用户拒绝授权
851
+ Taro.showModal({
852
+ title: '提示',
853
+ content: '获取位置未授权,请到我的->个人信息->设置页面中打开',
854
+ showCancel: false
855
+ });
856
+ }
857
+ else {
858
+ // 其他错误处理
859
+ Taro.showToast({
860
+ title: '获取位置失败',
861
+ icon: 'none'
862
+ });
863
+ }
864
+ resolve(null);
865
+ });
866
+ }
867
+ });
868
+ },
869
+ /**
870
+ * 检查小程序版本更新
871
+ */
872
+ updateVersion() {
873
+ const updateManager = Taro.getUpdateManager();
874
+ updateManager.onCheckForUpdate(function (res) {
875
+ // 请求完新版本信息的回调
876
+ if (!res.hasUpdate)
877
+ Utils.showToast({ title: '已经是最新版本了' });
878
+ });
879
+ updateManager.onUpdateReady(function () {
880
+ Taro.showModal({
881
+ title: '更新提示',
882
+ content: '新版本已经准备好,是否重启应用?',
883
+ success: function (res) {
884
+ if (res.confirm) {
885
+ // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
886
+ updateManager.applyUpdate();
887
+ }
888
+ }
889
+ });
890
+ });
891
+ updateManager.onUpdateFailed(function () {
892
+ // 新的版本下载失败
893
+ });
894
+ },
895
+ /**
896
+ * 打开地图导航
897
+ * @param [经度,纬度]location 经纬度
898
+ * @param options
899
+ */
900
+ openLocation: (location, options) => {
901
+ if (location === undefined || Array.isArray(location)) {
902
+ Utils.showToast({ title: "定位信息不全" });
903
+ return;
904
+ }
905
+ // 经度,范围为-180~180
906
+ const longitude = location === null || location === void 0 ? void 0 : location[0];
907
+ // 纬度,范围为-90~90
908
+ const latitude = location === null || location === void 0 ? void 0 : location[1];
909
+ const scale = (options === null || options === void 0 ? void 0 : options.scale) || 18;
910
+ const name = options === null || options === void 0 ? void 0 : options.name;
911
+ const address = options === null || options === void 0 ? void 0 : options.address;
912
+ const data = {
913
+ longitude, latitude, scale, name, address
914
+ };
915
+ if (Utils.isAlipay()) {
916
+ // @ts-ignore
917
+ my.openLocation(data);
918
+ }
919
+ else {
920
+ Taro.openLocation(data);
921
+ }
922
+ },
923
+ /**
924
+ * 打开全屏预览图片
925
+ * @param {string} current 当前点击的图片url
926
+ * @param {string} urlArr 所有图片url的数组
927
+ */
928
+ previewImage(current, urlArr) {
929
+ if (!current)
930
+ return;
931
+ Taro.previewImage({
932
+ current,
933
+ urls: urlArr
934
+ });
935
+ },
936
+ /**
937
+ * 缓存页面参数存储
938
+ * @param params 存入页面的参数
939
+ */
940
+ setCacheParams: (params) => {
941
+ Taro.setStorageSync(CACHE_PARAMS_KEY, params);
942
+ },
943
+ /**
944
+ * 缓存页面参数获取
945
+ * @returns {Record<string, any>}
946
+ */
947
+ getCacheParams: () => {
948
+ return Taro.getStorageSync(CACHE_PARAMS_KEY);
949
+ },
950
+ /**
951
+ * 缓存页面参数删除
952
+ */
953
+ removeCacheParams: () => {
954
+ Taro.removeStorageSync(CACHE_PARAMS_KEY);
955
+ }
956
+ };
957
+
958
+ var payUtils = {
959
+ /**
960
+ * 支付
961
+ * @param {WechatPayParams} payParams 微信支付参数
962
+ * @param {string} tradeId
963
+ * @returns {Promise<boolean>}
964
+ */
965
+ onlinePay: (payParams, tradeId) => {
966
+ if (Utils.isWeapp()) {
967
+ return Utils.wechatPay(payParams, tradeId);
968
+ }
969
+ else if (Utils.isAlipay()) {
970
+ return Utils.aliPay(tradeId);
971
+ }
972
+ else {
973
+ console.log(`支付异常,未兼容${Utils.envText()}小程序`);
974
+ return Promise.resolve(false);
975
+ }
976
+ },
977
+ /**
978
+ * 微信支付
979
+ * @param {WechatPayParams} payParams 微信支付参数
980
+ * @param {string} tradeId
981
+ * @returns {Promise<boolean>}
982
+ */
983
+ wechatPay: (payParams, tradeId) => {
984
+ console.log('payParams', payParams, tradeId);
985
+ return new Promise(resolve => {
986
+ // 优惠券
987
+ // if (tradeId === CouponPayId) {
988
+ // resolve(true)
989
+ // return
990
+ // }
991
+ Taro.requestPayment({
992
+ ...payParams,
993
+ success: function (_res) {
994
+ console.log('支付结果', _res);
995
+ resolve(true);
996
+ },
997
+ fail: function (_res) {
998
+ console.log('支付失败', _res);
999
+ resolve(false);
1000
+ }
1001
+ });
1002
+ });
1003
+ },
1004
+ /**
1005
+ * 支付宝支付
1006
+ * @param {string} tradeId
1007
+ * @returns {Promise<boolean>}
1008
+ */
1009
+ aliPay: (tradeId) => {
1010
+ console.log('支付参数--->', tradeId);
1011
+ return new Promise(resolve => {
1012
+ // 优惠券
1013
+ // if (tradeId === CouponPayId) {
1014
+ // resolve(true)
1015
+ // return
1016
+ // }
1017
+ // @ts-ignore
1018
+ my.tradePay({
1019
+ tradeNO: tradeId, //从服务端获取
1020
+ success: (res) => {
1021
+ console.log('支付结果:' + JSON.stringify(res));
1022
+ // success:{"result":"","resultCode":"6001","callbackUrl":"","memo":"支付未完成","extendInfo":{}}
1023
+ if (res.resultCode !== '9000') {
1024
+ resolve(false);
1025
+ return;
1026
+ }
1027
+ else {
1028
+ resolve(true);
1029
+ }
1030
+ },
1031
+ });
1032
+ });
1033
+ },
1034
+ };
1035
+
1036
+ /**
1037
+ * 工具函数
1038
+ */
1039
+ const Utils = {
1040
+ ...IsUtils,
1041
+ ...StringUtils,
1042
+ ...FormatUtils,
1043
+ ...ObjectUtils,
1044
+ ...miniUtils,
1045
+ ...payUtils,
1046
+ /**
1047
+ * 获取随机数
1048
+ * @public
1049
+ * @param {number} min 最小值
1050
+ * @param {number} max 最大值
1051
+ * @returns {number}
1052
+ */
1053
+ getRandom(min, max) {
1054
+ return Math.floor(Math.random() * (max - min + 1) + min);
1055
+ },
1056
+ /**
1057
+ * base64转图片Url
1058
+ * @public
1059
+ * @param {string} base64Url base64 url
1060
+ * @returns {string}
1061
+ */
1062
+ base64ToImgUrl(base64Url) {
1063
+ if (!base64Url)
1064
+ return base64Url;
1065
+ return `data:image/png;base64,${base64Url}`;
1066
+ },
1067
+ /**
1068
+ * 深拷贝对象
1069
+ * @param obj 要拷贝的对象
1070
+ * @returns 深拷贝后的新对象
1071
+ */
1072
+ cloneDeep(obj) {
1073
+ if (obj === null || typeof obj !== 'object') {
1074
+ return obj;
1075
+ }
1076
+ if (obj instanceof Date) {
1077
+ return new Date(obj.getTime());
1078
+ }
1079
+ if (obj instanceof Array) {
1080
+ return obj.map(item => Utils.cloneDeep(item));
1081
+ }
1082
+ if (typeof obj === 'object') {
1083
+ const clonedObj = {};
1084
+ for (const key in obj) {
1085
+ if (obj.hasOwnProperty(key)) {
1086
+ clonedObj[key] = Utils.cloneDeep(obj[key]);
1087
+ }
1088
+ }
1089
+ return clonedObj;
1090
+ }
1091
+ return obj;
1092
+ },
364
1093
  /**
365
1094
  * 等待
366
1095
  */