You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
3.9 KiB

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