util.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import dayjs from 'dayjs';
  2. const formatTime = (date, template) => dayjs(date).format(template);
  3. /**
  4. * 格式化价格数额为字符串
  5. * 可对小数部分进行填充,默认不填充
  6. * @param price 价格数额,以分为单位!
  7. * @param fill 是否填充小数部分 0-不填充 1-填充第一位小数 2-填充两位小数
  8. */
  9. function priceFormat(price, fill = 0) {
  10. if (isNaN(price) || price === null || price === Infinity) {
  11. return price;
  12. }
  13. let priceFormatValue = Math.round(parseFloat(`${price}`) * 10 ** 8) / 10 ** 8; // 恢复精度丢失
  14. priceFormatValue = `${Math.ceil(priceFormatValue) / 100}`; // 向上取整,单位转换为元,转换为字符串
  15. if (fill > 0) {
  16. // 补充小数位数
  17. if (priceFormatValue.indexOf('.') === -1) {
  18. priceFormatValue = `${priceFormatValue}.`;
  19. }
  20. const n = fill - priceFormatValue.split('.')[1]?.length;
  21. for (let i = 0; i < n; i++) {
  22. priceFormatValue = `${priceFormatValue}0`;
  23. }
  24. }
  25. return priceFormatValue;
  26. }
  27. /**
  28. * 获取cdn裁剪后链接
  29. *
  30. * @param {string} url 基础链接
  31. * @param {number} width 宽度,单位px
  32. * @param {number} [height] 可选,高度,不填时与width同值
  33. */
  34. const cosThumb = (url, width, height = width) => {
  35. if (url.indexOf('?') > -1) {
  36. return url;
  37. }
  38. if (url.indexOf('http://') === 0) {
  39. url = url.replace('http://', 'https://');
  40. }
  41. return `${url}?imageMogr2/thumbnail/${~~width}x${~~height}`;
  42. };
  43. const get = (source, paths, defaultValue) => {
  44. if (typeof paths === 'string') {
  45. paths = paths.replace(/\[/g, '.').replace(/\]/g, '').split('.').filter(Boolean);
  46. }
  47. const { length } = paths;
  48. let index = 0;
  49. while (source != null && index < length) {
  50. source = source[paths[index++]];
  51. }
  52. return source === undefined || index === 0 ? defaultValue : source;
  53. };
  54. let systemWidth = 0;
  55. /** 获取系统宽度,为了减少启动消耗所以在函数里边做初始化 */
  56. export const loadSystemWidth = () => {
  57. if (systemWidth) {
  58. return systemWidth;
  59. }
  60. try {
  61. // Replace deprecated wx.getSystemInfoSync()
  62. const windowInfo = wx.getWindowInfo();
  63. const deviceInfo = wx.getDeviceInfo();
  64. systemWidth = windowInfo.screenWidth;
  65. pixelRatio = deviceInfo.pixelRatio;
  66. } catch (e) {
  67. systemWidth = 0;
  68. }
  69. return systemWidth;
  70. };
  71. /**
  72. * 转换rpx为px
  73. *
  74. * @description
  75. * 什么时候用?
  76. * - 布局(width: 172rpx)已经写好, 某些组件只接受px作为style或者prop指定
  77. *
  78. */
  79. const rpx2px = (rpx, round = false) => {
  80. loadSystemWidth();
  81. // px / systemWidth = rpx / 750
  82. const result = (rpx * systemWidth) / 750;
  83. if (round) {
  84. return Math.floor(result);
  85. }
  86. return result;
  87. };
  88. /**
  89. * 手机号码*加密函数
  90. * @param {string} phone 电话号
  91. * @returns
  92. */
  93. const phoneEncryption = (phone) => {
  94. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  95. };
  96. // 内置手机号正则字符串
  97. const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$';
  98. /**
  99. * 手机号正则校验
  100. * @param phone 手机号
  101. * @param phoneReg 正则字符串
  102. * @returns true - 校验通过 false - 校验失败
  103. */
  104. const phoneRegCheck = (phone) => {
  105. const phoneRegExp = new RegExp(innerPhoneReg);
  106. return phoneRegExp.test(phone);
  107. };
  108. function objectToParamString(obj) {
  109. return Object.entries(obj)
  110. .map(([key, val]) => `${key}=${val}`)
  111. .join('&');
  112. }
  113. module.exports = {
  114. formatTime,
  115. priceFormat,
  116. cosThumb,
  117. get,
  118. rpx2px,
  119. phoneEncryption,
  120. phoneRegCheck,
  121. objectToParamString,
  122. };