value.js 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. export default {
  2. computed: {
  3. // 经处理后需要显示的值
  4. value() {
  5. const {
  6. text,
  7. mode,
  8. format,
  9. href
  10. } = this
  11. // 价格类型
  12. if (mode === 'price') {
  13. // 如果text不为金额进行提示
  14. if (!/^\d+(\.\d+)?$/.test(text)) {
  15. uni.$u.error('金额模式下,text参数需要为金额格式');
  16. }
  17. // 进行格式化,判断用户传入的format参数为正则,或者函数,如果没有传入format,则使用默认的金额格式化处理
  18. if (uni.$u.test.func(format)) {
  19. // 如果用户传入的是函数,使用函数格式化
  20. return format(text)
  21. }
  22. // 如果format非正则,非函数,则使用默认的金额格式化方法进行操作
  23. return uni.$u.priceFormat(text, 2)
  24. }
  25. if (mode === 'date') {
  26. // 判断是否合法的日期或者时间戳
  27. !uni.$u.test.date(text) && uni.$u.error('日期模式下,text参数需要为日期或时间戳格式')
  28. // 进行格式化,判断用户传入的format参数为正则,或者函数,如果没有传入format,则使用默认的格式化处理
  29. if (uni.$u.test.func(format)) {
  30. // 如果用户传入的是函数,使用函数格式化
  31. return format(text)
  32. }
  33. if (this.formart) {
  34. // 如果format非正则,非函数,则使用默认的时间格式化方法进行操作
  35. return uni.$u.timeFormat(text, format)
  36. }
  37. // 如果没有设置format,则设置为默认的时间格式化形式
  38. return uni.$u.timeFormat(text, 'yyyy-mm-dd')
  39. }
  40. if (mode === 'phone') {
  41. // 判断是否合法的手机号
  42. !uni.$u.test.mobile(text) && uni.$u.error('手机号模式下,text参数需要为手机号码格式')
  43. if (uni.$u.test.func(format)) {
  44. // 如果用户传入的是函数,使用函数格式化
  45. return format(text)
  46. }
  47. if (format === 'encrypt') {
  48. // 如果format为encrypt,则将手机号进行星号加密处理
  49. return `${text.substr(0, 3)}****${text.substr(7)}`
  50. }
  51. return text
  52. }
  53. if (mode === 'name') {
  54. // 判断是否合法的字符粗
  55. !(typeof (text) === 'string') && uni.$u.error('姓名模式下,text参数需要为字符串格式')
  56. if (uni.$u.test.func(format)) {
  57. // 如果用户传入的是函数,使用函数格式化
  58. return format(text)
  59. }
  60. if (format === 'encrypt') {
  61. // 如果format为encrypt,则将姓名进行星号加密处理
  62. return this.formatName(text)
  63. }
  64. return text
  65. }
  66. if (mode === 'link') {
  67. // 判断是否合法的字符粗
  68. !uni.$u.test.url(href) && uni.$u.error('超链接模式下,href参数需要为URL格式')
  69. return text
  70. }
  71. return text
  72. }
  73. },
  74. methods: {
  75. // 默认的姓名脱敏规则
  76. formatName(name) {
  77. let value = ''
  78. if (name.length === 2) {
  79. value = name.substr(0, 1) + '*'
  80. } else if (name.length > 2) {
  81. let char = ''
  82. for (let i = 0, len = name.length - 2; i < len; i++) {
  83. char += '*'
  84. }
  85. value = name.substr(0, 1) + char + name.substr(-1, 1)
  86. } else {
  87. value = name
  88. }
  89. return value
  90. }
  91. }
  92. }