cl-text.uvue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. // 最大行数,仅在ellipsis时生效
  114. lines: {
  115. type: Number,
  116. default: 1
  117. },
  118. // 是否可选择
  119. selectable: {
  120. type: Boolean,
  121. default: false
  122. },
  123. // 显示连续空格
  124. space: {
  125. type: String as PropType<"ensp" | "emsp" | "nbsp">,
  126. default: ""
  127. },
  128. // 是否解码 (app平台如需解析字符实体,需要配置为 true)
  129. decode: {
  130. type: Boolean,
  131. default: false
  132. },
  133. // 是否保留单词
  134. preWrap: {
  135. type: Boolean,
  136. default: false
  137. }
  138. });
  139. // 缓存
  140. const { cache } = useCache(() => [props.color, props]);
  141. // 透传样式类型
  142. type PassThrough = {
  143. className?: string;
  144. };
  145. // 解析透传样式
  146. const pt = computed(() => parsePt<PassThrough>(props.pt));
  147. // 文本大小
  148. const { getSize, getLineHeight, ptClassName } = useSize(() => pt.value.className ?? "");
  149. // 文本颜色
  150. const color = computed(() => {
  151. if (props.color != "") {
  152. switch (props.color) {
  153. case "primary":
  154. return ctx.color["primary-500"] as string;
  155. case "success":
  156. return "#22c55e";
  157. case "warn":
  158. return "#eab308";
  159. case "error":
  160. return "#ef4444";
  161. case "info":
  162. return isDark.value
  163. ? (ctx.color["surface-300"] as string)
  164. : (ctx.color["surface-500"] as string);
  165. case "dark":
  166. return ctx.color["surface-700"] as string;
  167. case "light":
  168. return ctx.color["surface-50"] as string;
  169. case "disabled":
  170. return ctx.color["surface-400"] as string;
  171. default:
  172. return props.color;
  173. }
  174. }
  175. return isDark.value ? "white" : (ctx.color["surface-700"] as string);
  176. });
  177. // 是否默认大小
  178. const isDefaultSize = computed(() => !hasTextSize(pt.value.className ?? ""));
  179. // 文本样式
  180. const textStyle = computed(() => {
  181. const style = {};
  182. // 省略号
  183. if (props.ellipsis) {
  184. style["lines"] = props.lines;
  185. }
  186. // 判断是不是有颜色样式
  187. if (!hasTextColor(ptClassName.value)) {
  188. style["color"] = color.value;
  189. }
  190. // 字号
  191. const fontSize = getSize(props.size);
  192. if (fontSize != null) {
  193. style["fontSize"] = fontSize;
  194. }
  195. // 行高
  196. const lineHeight = getLineHeight();
  197. if (lineHeight != null) {
  198. style["lineHeight"] = lineHeight;
  199. }
  200. return style;
  201. });
  202. /**
  203. * 手机号脱敏处理
  204. * 保留前3位和后4位,中间4位替换为掩码
  205. */
  206. function formatPhone(phone: string): string {
  207. if (phone.length != 11 || !props.mask) return phone;
  208. return phone.replace(/(\d{3})\d{4}(\d{4})/, `$1${props.maskChar.repeat(4)}$2`);
  209. }
  210. /**
  211. * 姓名脱敏处理
  212. * 2个字时保留第1个字
  213. * 大于2个字时保留首尾字
  214. */
  215. function formatName(name: string): string {
  216. if (name.length <= 1 || !props.mask) return name;
  217. if (name.length == 2) {
  218. return name[0] + props.maskChar;
  219. }
  220. return name[0] + props.maskChar.repeat(name.length - 2) + name[name.length - 1];
  221. }
  222. /**
  223. * 金额格式化
  224. * 1. 处理小数位数
  225. * 2. 添加千分位分隔符
  226. * 3. 添加货币符号
  227. */
  228. function formatAmount(amount: string | number): string {
  229. let num: number;
  230. if (typeof amount == "number") {
  231. num = amount;
  232. } else {
  233. num = parseFloat(amount);
  234. }
  235. const formatted = num.toFixed(props.precision);
  236. const parts = formatted.split(".");
  237. parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  238. return props.currency + parts.join(".");
  239. }
  240. /**
  241. * 银行卡号脱敏
  242. * 保留开头和结尾指定位数,中间用掩码替换
  243. */
  244. function formatCard(card: string): string {
  245. if (card.length < 8 || !props.mask) return card;
  246. const start = card.substring(0, props.maskStart);
  247. const end = card.substring(card.length - props.maskEnd);
  248. const middle = props.maskChar.repeat(card.length - props.maskStart - props.maskEnd);
  249. return start + middle + end;
  250. }
  251. /**
  252. * 邮箱脱敏处理
  253. * 保留用户名首尾字符和完整域名
  254. */
  255. function formatEmail(email: string): string {
  256. if (!props.mask) return email;
  257. const atIndex = email.indexOf("@");
  258. if (atIndex == -1) return email;
  259. const username = email.substring(0, atIndex);
  260. const domain = email.substring(atIndex);
  261. if (username.length <= 2) return email;
  262. const maskedUsername =
  263. username[0] + props.maskChar.repeat(username.length - 2) + username[username.length - 1];
  264. return maskedUsername + domain;
  265. }
  266. /**
  267. * 根据不同类型格式化显示
  268. */
  269. const content = computed(() => {
  270. const val = props.value ?? "";
  271. switch (props.type) {
  272. case "phone":
  273. return formatPhone(val as string);
  274. case "name":
  275. return formatName(val as string);
  276. case "amount":
  277. return formatAmount(val as number);
  278. case "card":
  279. return formatCard(val as string);
  280. case "email":
  281. return formatEmail(val as string);
  282. default:
  283. return val;
  284. }
  285. });
  286. </script>
  287. <style lang="scss" scoped>
  288. .cl-text {
  289. // #ifndef APP
  290. flex-shrink: unset;
  291. // #endif
  292. &--pre-wrap {
  293. // #ifndef APP
  294. white-space: pre-wrap;
  295. // #endif
  296. }
  297. &--ellipsis {
  298. text-overflow: ellipsis;
  299. // #ifndef APP
  300. display: -webkit-box;
  301. overflow: hidden;
  302. text-overflow: ellipsis;
  303. -webkit-line-clamp: v-bind(lines);
  304. line-break: anywhere;
  305. -webkit-box-orient: vertical;
  306. // #endif
  307. }
  308. &--default-size {
  309. @apply text-md;
  310. }
  311. }
  312. </style>