util.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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
  46. .replace(/\[/g, '.')
  47. .replace(/\]/g, '')
  48. .split('.')
  49. .filter(Boolean);
  50. }
  51. const { length } = paths;
  52. let index = 0;
  53. while (source != null && index < length) {
  54. source = source[paths[index++]];
  55. }
  56. return source === undefined || index === 0 ? defaultValue : source;
  57. };
  58. let systemWidth = 0;
  59. /** 获取系统宽度,为了减少启动消耗所以在函数里边做初始化 */
  60. export const loadSystemWidth = () => {
  61. if (systemWidth) {
  62. return systemWidth;
  63. }
  64. try {
  65. ({ screenWidth: systemWidth, pixelRatio } = wx.getSystemInfoSync());
  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 =
  98. '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$';
  99. /**
  100. * 手机号正则校验
  101. * @param phone 手机号
  102. * @param phoneReg 正则字符串
  103. * @returns true - 校验通过 false - 校验失败
  104. */
  105. const phoneRegCheck = (phone) => {
  106. const phoneRegExp = new RegExp(innerPhoneReg);
  107. return phoneRegExp.test(phone);
  108. };
  109. module.exports = {
  110. formatTime,
  111. priceFormat,
  112. cosThumb,
  113. get,
  114. rpx2px,
  115. phoneEncryption,
  116. phoneRegCheck,
  117. };