🍉 feat(sysDict): 增加自定义数据源功能

This commit is contained in:
喵你个汪呀 2025-08-29 21:15:13 +08:00
parent 6557895033
commit 47ebee8f3e

View File

@ -91,6 +91,7 @@ function isMultipleModel(value: any): value is MultipleModel {
<script setup lang="ts"> <script setup lang="ts">
import { reactive, watch, computed, PropType } from 'vue'; import { reactive, watch, computed, PropType } from 'vue';
import { useUserInfo } from '/@/stores/userInfo'; import { useUserInfo } from '/@/stores/userInfo';
import {Nullable} from "/@/types/global";
const userStore = useUserInfo(); const userStore = useUserInfo();
const emit = defineEmits(['update:modelValue', 'change']); const emit = defineEmits(['update:modelValue', 'change']);
@ -126,7 +127,16 @@ const props = defineProps({
*/ */
code: { code: {
type: String, type: String,
required: true, required: false,
},
/**
* 直接传入的字典数据源优先级高于code
* @type {DictItem[]}
* @example [{ label: '选项1', value: '1' }, { label: '选项2', value: '2' }]
*/
data: {
type: Array as PropType<DictItem[]>,
default: () => [],
}, },
/** /**
* 是否为常量字典true从常量列表获取false从字典列表获取 * 是否为常量字典true从常量列表获取false从字典列表获取
@ -286,8 +296,17 @@ const currentDictItems = computed(() => {
*/ */
const getDataList = (): DictItem[] => { const getDataList = (): DictItem[] => {
try { try {
// data 使
if (props.data && props.data.length > 0) {
return props.data.map((item: any) => ({
...item,
label: item[props.propLabel] ?? [item.name, item.desc].filter((x) => x).join('-'),
value: item[props.propValue] ?? item.code,
}));
}
if (!props.code ) { if (!props.code ) {
console.error('[g-sys-dict] 字典编码不能为空'); console.error('[g-sys-dict] code和data不能同时为空');
return []; return [];
} }
@ -475,6 +494,14 @@ const getDisplayText = (dict?: DictItem): string => {
* @function * @function
*/ */
const initData = () => { const initData = () => {
// code data
if (!props.code && (!props.data || props.data.length === 0)) {
console.error('[g-sys-dict] code和data不能同时为空');
state.dictData = [];
state.value = props.multiple ? [] : null;
return;
}
state.dictData = getDataList(); state.dictData = getDataList();
processNumericValues(props.modelValue); processNumericValues(props.modelValue);
const initialValue = parseMultipleValue(props.modelValue); const initialValue = parseMultipleValue(props.modelValue);
@ -522,7 +549,7 @@ watch(
validateInitialValue(); validateInitialValue();
} }
); );
watch(() => [userStore.dictList, userStore.constList, state], initData, { immediate: true }); watch(() => [userStore.dictList, userStore.constList, props.data, state], initData, { immediate: true });
</script> </script>
<template> <template>