cl-text.uvue 6.5 KB

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