-
zhangwei
昨天 2f5177b8a553bdc468cc68a20029916f59d1b4f1
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<!-- 组件使用文档: https://gitee.com/zuohuaijun/非政采招标采购交易管理平台/pulls/1559  -->
<script setup lang="ts">
import { reactive, watch, PropType } from 'vue';
import { useUserInfo } from '/@/stores/userInfo';
 
type DictItem = {
  [key: string]: any;
  tagType?: string;
  styleSetting?: string;
  classSetting?: string;
};
 
const userStore = useUserInfo();
const emit = defineEmits(['update:modelValue']);
const props = defineProps({
  /**
   * 绑定的值,支持多种类型
   * @example
   * <g-sys-dict v-model="selectedValue" code="xxxx" />
   */
  modelValue: {
    type: [String, Number, Boolean, Array, null] as PropType<string | number | boolean | any[] | null>,
    default: null,
    required: true,
  },
  /**
   * 字典编码,用于获取字典项
   * @example 'gender'
   */
  code: {
    type: String,
    required: true,
  },
  /**
   * 是否是常量
   * @default false
   */
  isConst: {
    type: Boolean,
    default: false,
  },
  /**
   * 字典项中用于显示的字段名
   * @default 'label'
   */
  propLabel: {
    type: String,
    default: 'label',
  },
  /**
   * 字典项中用于取值的字段名
   * @default 'value'
   */
  propValue: {
    type: String,
    default: 'value',
  },
  /**
   * 字典项过滤函数
   * @param dict - 字典项
   * @returns 是否保留该项
   * @default (dict) => true
   */
  onItemFilter: {
    type: Function as PropType<(dict: DictItem) => boolean>,
    default: (dict: DictItem) => true,
  },
  /**
   * 字典项显示内容格式化函数
   * @param dict - 字典项
   * @returns 格式化后的显示内容
   * @default () => undefined
   */
  onItemFormatter: {
    type: Function as PropType<(dict: DictItem) => string | undefined | null>,
    default: () => undefined,
  },
  /**
   * 组件渲染方式
   * @values 'tag', 'select', 'radio', 'checkbox'
   * @default 'tag'
   */
  renderAs: {
    type: String as PropType<'tag' | 'select' | 'radio' | 'checkbox'>,
    default: 'tag',
    validator(value: string) {
      return ['tag', 'select', 'radio', 'checkbox'].includes(value);
    },
  },
  /**
   * 是否多选
   * @default false
   */
  multiple: {
    type: Boolean,
    default: false,
  },
});
 
const state = reactive({
  dict: undefined as DictItem | DictItem[] | undefined,
  dictData: [] as DictItem[],
  value: undefined as any,
});
 
// 获取数据集
const getDataList = () => {
  if (props.isConst) {
    const data = userStore.constList?.find((x: any) => x.code === props.code)?.data?.result ?? [];
    // 与字典的显示文本、值保持一致,方便渲染
    data?.forEach((item: any) => {
      item.label = item.name;
      item.value = item.code;
      delete item.name;
    });
    return data;
  } else {
    return userStore.dictList[props.code];
  }
}
 
// 设置字典数据
const setDictData = () => {
  state.dictData = getDataList()?.filter(props.onItemFilter) ?? [];
  processNumericValues(props.modelValue);
};
 
// 处理数字类型的值
const processNumericValues = (value: any) => {
  if (typeof value === 'number' || (Array.isArray(value) && typeof value[0] === 'number')) {
    state.dictData.forEach((item) => {
      item[props.propValue] = Number(item[props.propValue]);
    });
  }
};
 
// 设置多选值
const trySetMultipleValue = (value: any) => {
  let newValue = value;
  if (typeof value === 'string') {
    const trimmedValue = value.trim();
    if (trimmedValue.startsWith('[') && trimmedValue.endsWith(']')) {
      try {
        newValue = JSON.parse(trimmedValue);
      } catch (error) {
        console.warn('[g-sys-dict]解析多选值失败, 异常信息:', error);
      }
    }
  } else if (props.multiple && !value) {
    newValue = [];
  }
  if (newValue != value) updateValue(newValue);
 
  setDictData();
  return newValue;
}
 
// 设置字典值
const setDictValue = (value: any) => {
  value = trySetMultipleValue(value);
  if (Array.isArray(value)) {
    state.dict = state.dictData?.filter((x) => value.find(y => y == x[props.propValue]));
    state.dict?.forEach(ensureTagType);
  } else {
    state.dict = state.dictData?.find((x) => x[props.propValue] == value);
    if (state.dict) ensureTagType(state.dict);
  }
  state.value = value;
};
 
// 确保标签类型存在
const ensureTagType = (item: DictItem) => {
  if (!['success', 'warning', 'info', 'primary', 'danger'].includes(item.tagType ?? '')) {
    item.tagType = 'primary';
  }
};
 
// 更新绑定值
const updateValue = (newValue: any) => {
  emit('update:modelValue', newValue);
};
 
// 计算显示的文本
const getDisplayText = (dict: DictItem | undefined = undefined) => {
  if (dict) return props.onItemFormatter?.(dict) ?? dict[props.propLabel];
  return state.value;
}
 
watch(
    () => props.modelValue,
    (newValue) => setDictValue(newValue),
    { immediate: true }
);
</script>
 
<template>
  <!-- 渲染标签 -->
  <template v-if="props.renderAs === 'tag'">
    <template v-if="Array.isArray(state.dict)">
      <el-tag v-for="(item, index) in state.dict" :key="index" v-bind="$attrs" :type="item.tagType" :style="item.styleSetting" :class="item.classSetting" class="mr2">
        {{ getDisplayText(item) }}
      </el-tag>
    </template>
    <template v-else>
      <el-tag v-if="state.dict" v-bind="$attrs" :type="state.dict.tagType" :style="state.dict.styleSetting" :class="state.dict.classSetting">
        {{ getDisplayText(state.dict) }}
      </el-tag>
      <span v-else>{{ getDisplayText() }}</span>
    </template>
  </template>
 
  <!-- 渲染选择器 -->
  <template v-if="props.renderAs === 'select'">
    <el-select v-model="state.value" v-bind="$attrs" :multiple="props.multiple" @change="updateValue" clearable>
      <el-option v-for="(item, index) in state.dictData" :key="index" :label="getDisplayText(item)" :value="item[propValue]" />
    </el-select>
  </template>
 
  <!-- 渲染复选框(多选) -->
  <template v-if="props.renderAs === 'checkbox'">
    <el-checkbox-group v-model="state.value" v-bind="$attrs" @change="updateValue">
      <el-checkbox-button v-for="(item, index) in state.dictData" :key="index" :value="item[propValue]">
        {{ getDisplayText(item) }}
      </el-checkbox-button>
    </el-checkbox-group>
  </template>
 
  <!-- 渲染单选框 -->
  <template v-if="props.renderAs === 'radio'">
    <el-radio-group v-model="state.value" v-bind="$attrs" @change="updateValue">
      <el-radio v-for="(item, index) in state.dictData" :key="index" :value="item[propValue]">
        {{ getDisplayText(item) }}
      </el-radio>
    </el-radio-group>
  </template>
</template>
<style scoped lang="scss">
</style>