value.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { round } from './libs/function/digit.js'
  2. const debug = process.env.NODE_ENV === 'development';
  3. /**
  4. * @description 数字格式化
  5. * @param {number|string} number 要格式化的数字
  6. * @param {number} decimals 保留几位小数
  7. * @param {string} decimalPoint 小数点符号
  8. * @param {string} thousandsSeparator 千分位符号
  9. * @returns {string} 格式化后的数字
  10. */
  11. function priceFormat(number, decimals = 0, decimalPoint = '.', thousandsSeparator = ',') {
  12. number = (`${number}`).replace(/[^0-9+-Ee.]/g, '');
  13. const n = !isFinite(+number) ? 0 : +number;
  14. const prec = !isFinite(+decimals) ? 0 : Math.abs(decimals);
  15. const sep = (typeof thousandsSeparator === 'undefined') ? ',' : thousandsSeparator;
  16. const dec = (typeof decimalPoint === 'undefined') ? '.' : decimalPoint;
  17. let s = '';
  18. s = (prec ? round(n, prec) + '' : `${Math.round(n)}`).split('.');
  19. const re = /(-?\d+)(\d{3})/;
  20. while (re.test(s[0])) {
  21. s[0] = s[0].replace(re, `$1${sep}$2`);
  22. }
  23. if ((s[1] || '').length < prec) {
  24. s[1] = s[1] || '';
  25. s[1] += new Array(prec - s[1].length + 1).join('0');
  26. }
  27. return s.join(dec);
  28. }
  29. export default {
  30. computed: {
  31. // 经处理后需要显示的值
  32. value() {
  33. const {
  34. text,
  35. mode,
  36. format,
  37. href
  38. } = this
  39. // 价格类型
  40. if (mode === 'price') {
  41. // 如果text不为金额进行提示
  42. if (!/^\d+(\.\d+)?$/.test(text)) {
  43. if (debug) console.error('金额模式下,text参数需要为金额格式');
  44. }
  45. // 进行格式化,判断用户传入的format参数为正则,或者函数,如果没有传入format,则使用默认的金额格式化处理
  46. if (uni.$u.test.func(format)) {
  47. // 如果用户传入的是函数,使用函数格式化
  48. return format(text)
  49. }
  50. // 如果format非正则,非函数,则使用默认的金额格式化方法进行操作
  51. return priceFormat(text, 2)
  52. }
  53. if (mode === 'date') {
  54. // 判断是否合法的日期或者时间戳
  55. if (!uni.$u.test.date(text)) {
  56. if (debug) console.error('日期模式下,text参数需要为日期或时间戳格式');
  57. }
  58. // 进行格式化,判断用户传入的format参数为正则,或者函数,如果没有传入format,则使用默认的格式化处理
  59. if (uni.$u.test.func(format)) {
  60. // 如果用户传入的是函数,使用函数格式化
  61. return format(text);
  62. }
  63. if (format) {
  64. // 如果format非正则,非函数,则使用默认的时间格式化方法进行操作
  65. return uni.$u.timeFormat(text, format);
  66. }
  67. // 如果没有设置format,则设置为默认的时间格式化形式
  68. return uni.$u.timeFormat(text, 'yyyy-mm-dd');
  69. }
  70. if (mode === 'phone') {
  71. // 判断是否合法的手机号
  72. if (uni.$u.test.func(format)) {
  73. // 如果用户传入的是函数,使用函数格式化
  74. return format(text);
  75. }
  76. if (format === 'encrypt') {
  77. // 如果format为encrypt,则将手机号进行星号加密处理
  78. return `${text.substr(0, 3)}****${text.substr(7)}`;
  79. }
  80. return text
  81. }
  82. if (mode === 'name') {
  83. // 判断是否合法的字符粗
  84. if (!(typeof(text) === 'string')) {
  85. if (debug) console.error('姓名模式下,text参数需要为字符串格式');
  86. }
  87. if (uni.$u.test.func(format)) {
  88. // 如果用户传入的是函数,使用函数格式化
  89. return format(text)
  90. }
  91. if (format === 'encrypt') {
  92. // 如果format为encrypt,则将姓名进行星号加密处理
  93. return this.formatName(text);
  94. }
  95. return text
  96. }
  97. if (mode === 'link') {
  98. // 判断是否合法的字符粗
  99. if (!uni.$u.test.url(href)) {
  100. if (debug) console.error('超链接模式下,href参数需要为URL格式');
  101. }
  102. return text;
  103. }
  104. return text;
  105. }
  106. },
  107. methods: {
  108. // 默认的姓名脱敏规则
  109. formatName(name) {
  110. let value = '';
  111. if (name.length === 2) {
  112. value = name.substr(0, 1) + '*';
  113. } else if (name.length > 2) {
  114. let char = '';
  115. for (let i = 0, len = name.length - 2; i < len; i++) {
  116. char += '*';
  117. }
  118. value = name.substr(0, 1) + char + name.substr(-1, 1);
  119. } else {
  120. value = name;
  121. }
  122. return value;
  123. }
  124. }
  125. }