zhangwencui
39 分钟以前 f08a81f01bc4a4104dd9ef04e071bb0bd0eb8afe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<script setup lang="ts">
import { computed } from 'vue';
 
import { getDictObj } from '@vben/hooks';
import { isValidColor, TinyColor } from '@vben/utils';
 
import { Tag } from 'ant-design-vue';
 
interface DictTagProps {
  type: string; // 字典类型
  value: any; // 字典值
  icon?: string; // 图标
}
 
const props = defineProps<DictTagProps>();
 
/** 获取字典标签 */
const dictTag = computed(() => {
  // 校验参数有效性
  if (!props.type || props.value === undefined || props.value === null) {
    return null;
  }
 
  // 获取字典对象
  const dict = getDictObj(props.type, String(props.value));
  if (!dict) {
    return null;
  }
 
  // 处理颜色类型
  let colorType = dict.colorType;
  switch (colorType) {
    case 'danger': {
      colorType = 'error';
      break;
    }
    case 'info': {
      colorType = 'default';
      break;
    }
    case 'primary': {
      colorType = 'processing';
      break;
    }
    default: {
      if (!colorType) {
        colorType = 'default';
      }
    }
  }
 
  if (isValidColor(dict.cssClass)) {
    colorType = new TinyColor(dict.cssClass).toHexString();
  }
 
  return {
    label: dict.label || '',
    colorType,
    cssClass: dict.cssClass,
  };
});
</script>
 
<template>
  <Tag
    v-if="dictTag"
    :color="dictTag.colorType ? dictTag.colorType : dictTag.cssClass"
  >
    {{ dictTag.label }}
  </Tag>
</template>