cl-text.uvue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <template>
  2. <!-- #ifdef MP -->
  3. <view
  4. class="cl-text"
  5. :class="[
  6. isDark ? 'text-surface-50' : 'text-surface-700',
  7. {
  8. 'truncate w-full': ellipsis,
  9. 'cl-text--pre-wrap': preWrap
  10. },
  11. {
  12. '!text-primary-500': color == 'primary',
  13. '!text-green-500': color == 'success',
  14. '!text-yellow-500': color == 'warn',
  15. '!text-red-500': color == 'error',
  16. [isDark ? '!text-surface-300' : '!text-surface-500']: color == 'info',
  17. '!text-surface-700': color == 'dark',
  18. '!text-surface-50': color == 'light',
  19. '!text-surface-400': color == 'disabled'
  20. },
  21. pt.className
  22. ]"
  23. :style="textStyle"
  24. :selectable="selectable"
  25. :space="space"
  26. :decode="decode"
  27. :key="cache.key"
  28. >
  29. <slot>{{ content }}</slot>
  30. </view>
  31. <!-- #endif -->
  32. <!-- #ifndef MP -->
  33. <text
  34. class="cl-text"
  35. :class="[
  36. isDark ? 'text-surface-50' : 'text-surface-700',
  37. {
  38. 'truncate w-full': ellipsis,
  39. 'cl-text--pre-wrap': preWrap
  40. },
  41. {
  42. '!text-primary-500': color == 'primary',
  43. '!text-green-500': color == 'success',
  44. '!text-yellow-500': color == 'warn',
  45. '!text-red-500': color == 'error',
  46. [isDark ? '!text-surface-300' : '!text-surface-500']: color == 'info',
  47. '!text-surface-700': color == 'dark',
  48. '!text-surface-50': color == 'light',
  49. '!text-surface-400': color == 'disabled'
  50. },
  51. pt.className
  52. ]"
  53. :style="textStyle"
  54. :selectable="selectable"
  55. :space="space"
  56. :decode="decode"
  57. :key="cache.key"
  58. >
  59. <slot>{{ content }}</slot>
  60. </text>
  61. <!-- #endif -->
  62. </template>
  63. <script setup lang="ts">
  64. import { computed, type PropType } from "vue";
  65. import { isDark, parsePt, useCache } from "@/cool";
  66. import type { ClTextType } from "../../types";
  67. defineOptions({
  68. name: "cl-text"
  69. });
  70. // 组件属性定义
  71. const props = defineProps({
  72. // 透传样式
  73. pt: {
  74. type: Object,
  75. default: () => ({})
  76. },
  77. // 显示的值
  78. value: {
  79. type: [String, Number] as PropType<string | number | null>,
  80. default: null
  81. },
  82. // 文本颜色
  83. color: {
  84. type: String,
  85. default: ""
  86. },
  87. // 文本类型
  88. type: {
  89. type: String as PropType<ClTextType>,
  90. default: "default"
  91. },
  92. // 是否开启脱敏/加密
  93. mask: {
  94. type: Boolean,
  95. default: false
  96. },
  97. // 金额货币符号
  98. currency: {
  99. type: String,
  100. default: "¥"
  101. },
  102. // 金额小数位数
  103. precision: {
  104. type: Number,
  105. default: 2
  106. },
  107. // 脱敏起始位置
  108. maskStart: {
  109. type: Number,
  110. default: 3
  111. },
  112. // 脱敏结束位置
  113. maskEnd: {
  114. type: Number,
  115. default: 4
  116. },
  117. // 脱敏替换字符
  118. maskChar: {
  119. type: String,
  120. default: "*"
  121. },
  122. // 是否省略号
  123. ellipsis: {
  124. type: Boolean,
  125. default: false
  126. },
  127. // 是否可选择
  128. selectable: {
  129. type: Boolean,
  130. default: false
  131. },
  132. // 显示连续空格
  133. space: {
  134. type: String as PropType<"ensp" | "emsp" | "nbsp">,
  135. default: ""
  136. },
  137. // 是否解码 (app平台如需解析字符实体,需要配置为 true)
  138. decode: {
  139. type: Boolean,
  140. default: false
  141. },
  142. // 是否保留单词
  143. preWrap: {
  144. type: Boolean,
  145. default: false
  146. }
  147. });
  148. // 缓存
  149. const { cache } = useCache(() => [props.color, props]);
  150. // 透传样式类型
  151. type PassThrough = {
  152. className?: string;
  153. };
  154. // 解析透传样式
  155. const pt = computed(() => parsePt<PassThrough>(props.pt));
  156. // 文本样式
  157. const textStyle = computed(() => {
  158. const style = {};
  159. if (props.color != "") {
  160. style["color"] = props.color;
  161. }
  162. return style;
  163. });
  164. /**
  165. * 手机号脱敏处理
  166. * 保留前3位和后4位,中间4位替换为掩码
  167. */
  168. function formatPhone(phone: string): string {
  169. if (phone.length != 11 || !props.mask) return phone;
  170. return phone.replace(/(\d{3})\d{4}(\d{4})/, `$1${props.maskChar.repeat(4)}$2`);
  171. }
  172. /**
  173. * 姓名脱敏处理
  174. * 2个字时保留第1个字
  175. * 大于2个字时保留首尾字
  176. */
  177. function formatName(name: string): string {
  178. if (name.length <= 1 || !props.mask) return name;
  179. if (name.length == 2) {
  180. return name[0] + props.maskChar;
  181. }
  182. return name[0] + props.maskChar.repeat(name.length - 2) + name[name.length - 1];
  183. }
  184. /**
  185. * 金额格式化
  186. * 1. 处理小数位数
  187. * 2. 添加千分位分隔符
  188. * 3. 添加货币符号
  189. */
  190. function formatAmount(amount: string | number): string {
  191. let num: number;
  192. if (typeof amount == "number") {
  193. num = amount;
  194. } else {
  195. num = parseFloat(amount);
  196. }
  197. const formatted = num.toFixed(props.precision);
  198. const parts = formatted.split(".");
  199. parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  200. return props.currency + parts.join(".");
  201. }
  202. /**
  203. * 银行卡号脱敏
  204. * 保留开头和结尾指定位数,中间用掩码替换
  205. */
  206. function formatCard(card: string): string {
  207. if (card.length < 8 || !props.mask) return card;
  208. const start = card.substring(0, props.maskStart);
  209. const end = card.substring(card.length - props.maskEnd);
  210. const middle = props.maskChar.repeat(card.length - props.maskStart - props.maskEnd);
  211. return start + middle + end;
  212. }
  213. /**
  214. * 邮箱脱敏处理
  215. * 保留用户名首尾字符和完整域名
  216. */
  217. function formatEmail(email: string): string {
  218. if (!props.mask) return email;
  219. const atIndex = email.indexOf("@");
  220. if (atIndex == -1) return email;
  221. const username = email.substring(0, atIndex);
  222. const domain = email.substring(atIndex);
  223. if (username.length <= 2) return email;
  224. const maskedUsername =
  225. username[0] + props.maskChar.repeat(username.length - 2) + username[username.length - 1];
  226. return maskedUsername + domain;
  227. }
  228. /**
  229. * 根据不同类型格式化显示
  230. */
  231. const content = computed(() => {
  232. const val = props.value ?? "";
  233. switch (props.type) {
  234. case "phone":
  235. return formatPhone(val as string);
  236. case "name":
  237. return formatName(val as string);
  238. case "amount":
  239. return formatAmount(val as number);
  240. case "card":
  241. return formatCard(val as string);
  242. case "email":
  243. return formatEmail(val as string);
  244. default:
  245. return val;
  246. }
  247. });
  248. </script>
  249. <style lang="scss" scoped>
  250. .cl-text {
  251. @apply text-md;
  252. &--pre-wrap {
  253. // #ifdef H5
  254. white-space: pre-wrap;
  255. // #endif
  256. }
  257. }
  258. </style>