fs-text.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <text :class="[colorType, decoration, { block }]" :style="styleStr" @click="handleClick">{{ formatText(text) }}</text>
  3. </template>
  4. <script>
  5. /**
  6. * 文本组件
  7. * @description 文本组件
  8. * @property {String} text 显示的内容文本
  9. * @property {String} mode = [price | phone | name] 文本处理的匹配模式
  10. * @property {String} color 文本颜色
  11. * @property {String} colorType = [primary | danger | warning | info | success | default] 文本亮颜色类型
  12. * @property {String} link 跳转地址
  13. * @property {String} linkType 跳转类型
  14. * @property {Boolean} bolck 块状
  15. * @property {Boolean} encrypt 是否加密(仅对mode=phone有效)
  16. * @property {String} decoration = [underline | line-through] 文字装饰
  17. */
  18. export default {
  19. name: 'fs-text'
  20. }
  21. </script>
  22. <script setup>
  23. import { computed } from 'vue'
  24. const props = defineProps({
  25. text: String,
  26. mode: {
  27. type: String,
  28. validator(value) {
  29. return ['price', 'phone', 'name'].includes(value)
  30. }
  31. },
  32. colorType: {
  33. type: String,
  34. validator(value) {
  35. return ['primary', 'success', 'info', 'warning', 'danger'].includes(value)
  36. }
  37. },
  38. color: String,
  39. link: String,
  40. linkType: {
  41. type: String,
  42. default: 'navigateTo'
  43. },
  44. block: Boolean,
  45. encrypt: Boolean,
  46. decoration: {
  47. type: String,
  48. validator(value) {
  49. return ['underline', 'line-through'].includes(value)
  50. }
  51. }
  52. })
  53. const emits = defineEmits(['click'])
  54. const styleStr = computed(() => {
  55. return props.color ? `color: ${props.color};` : ''
  56. })
  57. const formatText = text => {
  58. if (props.mode === 'price') {
  59. return '¥' + text
  60. } else if (props.mode === 'name') {
  61. return text[0] + '**'
  62. } else if (props.mode === 'phone' && props.encrypt) {
  63. return text.slice(0, 3) + '****' + text.slice(-4)
  64. }
  65. return text
  66. }
  67. const handleClick = () => {
  68. if (props.mode === 'phone') {
  69. uni.makePhoneCall({
  70. phoneNumber: props.text
  71. })
  72. } else if (props.link) {
  73. uni[props.linkType]({
  74. url: props.link
  75. })
  76. }
  77. emits('click')
  78. }
  79. </script>
  80. <style lang="scss" scoped></style>