优化资产编辑和分类管理功能,完善虚拟资产配置

This commit is contained in:
WUSIJIAN
2025-12-22 14:08:17 +08:00
parent 1a904388ef
commit ac0b790363
3 changed files with 334 additions and 109 deletions

View File

@@ -20,6 +20,14 @@ export function getCategory(id: string) {
}); });
} }
// 获取属性类型选项
export function getCategoryAttrTypeOptions() {
return newService({
url: '/assets/category/getCategoryAttrTypeOptions',
method: 'get',
});
}
// 新增分类 // 新增分类
export function addCategory(data: object) { export function addCategory(data: object) {
return newService({ return newService({

View File

@@ -215,8 +215,42 @@
<el-divider content-position="left">虚拟资产配置</el-divider> <el-divider content-position="left">虚拟资产配置</el-divider>
<el-row :gutter="24"> <el-row :gutter="24">
<el-col :span="12"> <el-col :span="12">
<el-form-item label="虚拟资产ID"> <el-form-item label="HTTP方法">
<el-input v-model="ruleForm.virtualAssetConfig.virtualAssetId" placeholder="请输入虚拟资产ID" /> <el-select v-model="ruleForm.virtualAssetConfig.method" placeholder="请选择HTTP方法" class="w100">
<el-option label="GET" value="GET" />
<el-option label="POST" value="POST" />
<el-option label="PUT" value="PUT" />
<el-option label="DELETE" value="DELETE" />
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24">
<el-col :span="12">
<el-form-item label="请求地址">
<el-input v-model="ruleForm.virtualAssetConfig.requestURL" placeholder="请输入请求地址" />
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="认证类型">
<el-select v-model="ruleForm.virtualAssetConfig.authType" placeholder="请选择认证类型" class="w100">
<el-option label="无认证" value="none" />
<el-option label="API Key" value="apikey" />
<el-option label="Bearer Token" value="bearer" />
<el-option label="OAuth" value="oauth" />
</el-select>
</el-form-item>
</el-col>
</el-row>
<el-row :gutter="24" v-if="ruleForm.virtualAssetConfig.authType && ruleForm.virtualAssetConfig.authType !== 'none'">
<el-col :span="24">
<el-form-item label="认证配置">
<el-input
v-model="ruleForm.virtualAssetConfig.authConfig"
type="textarea"
:rows="4"
placeholder='请输入认证配置JSON例如{"api_key": "your_api_key"}'
/>
</el-form-item> </el-form-item>
</el-col> </el-col>
</el-row> </el-row>
@@ -275,8 +309,12 @@
<el-form-item label="例外日期"> <el-form-item label="例外日期">
<div class="config-list-container"> <div class="config-list-container">
<div v-for="(exc, index) in ruleForm.serviceAssetConfig.schedule.exceptions" :key="index" class="config-list-item"> <div v-for="(exc, index) in ruleForm.serviceAssetConfig.schedule.exceptions" :key="index" class="config-list-item">
<el-date-picker v-model="exc.date" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="日期" style="width: 130px" /> <el-select v-model="exc.exceptionType" placeholder="类型" style="width: 100px" @change="onExceptionTypeChange(exc)">
<el-select v-model="exc.dayOfWeek" placeholder="星期" style="width: 90px"> <el-option label="指定日期" value="date" />
<el-option label="指定星期" value="dayOfWeek" />
</el-select>
<el-date-picker v-if="exc.exceptionType === 'date'" v-model="exc.date" type="date" format="YYYY-MM-DD" value-format="YYYY-MM-DD" placeholder="日期" style="width: 130px" />
<el-select v-if="exc.exceptionType === 'dayOfWeek'" v-model="exc.dayOfWeek" placeholder="星期" style="width: 100px">
<el-option v-for="d in 7" :key="d" :label="'周' + ['一','二','三','四','五','六','日'][d-1]" :value="String(d)" /> <el-option v-for="d in 7" :key="d" :label="'周' + ['一','二','三','四','五','六','日'][d-1]" :value="String(d)" />
</el-select> </el-select>
<el-select v-model="exc.status" placeholder="状态" style="width: 90px"> <el-select v-model="exc.status" placeholder="状态" style="width: 90px">
@@ -330,6 +368,7 @@ interface TimeSlot {
} }
interface Exception { interface Exception {
exceptionType: 'date' | 'dayOfWeek';
date: string; date: string;
status: number; status: number;
reason: string; reason: string;
@@ -360,7 +399,10 @@ interface RuleForm {
}; };
}; };
virtualAssetConfig: { virtualAssetConfig: {
virtualAssetId: string; method: string;
requestURL: string;
authType: string;
authConfig: string;
}; };
serviceAssetConfig: { serviceAssetConfig: {
schedule: { schedule: {
@@ -424,7 +466,10 @@ const getInitialForm = (): RuleForm => ({
}, },
}, },
virtualAssetConfig: { virtualAssetConfig: {
virtualAssetId: '', method: 'GET',
requestURL: '',
authType: 'none',
authConfig: '',
}, },
serviceAssetConfig: { serviceAssetConfig: {
schedule: { schedule: {
@@ -490,6 +535,7 @@ const removeTimeSlot = (index: number) => {
// 例外日期操作 // 例外日期操作
const addException = () => { const addException = () => {
ruleForm.serviceAssetConfig.schedule.exceptions.push({ ruleForm.serviceAssetConfig.schedule.exceptions.push({
exceptionType: 'date',
date: '', date: '',
status: 1, status: 1,
reason: '', reason: '',
@@ -501,6 +547,15 @@ const removeException = (index: number) => {
ruleForm.serviceAssetConfig.schedule.exceptions.splice(index, 1); ruleForm.serviceAssetConfig.schedule.exceptions.splice(index, 1);
}; };
// 例外类型切换时清空对应字段
const onExceptionTypeChange = (exc: Exception) => {
if (exc.exceptionType === 'date') {
exc.dayOfWeek = '';
} else {
exc.date = '';
}
};
// 重置表单 // 重置表单
const resetForm = () => { const resetForm = () => {
const initial = getInitialForm(); const initial = getInitialForm();

View File

@@ -1,6 +1,6 @@
<template> <template>
<div class="assets-edit-category-container"> <div class="assets-edit-category-container">
<el-dialog :title="isEdit ? '修改分类' : '添加分类'" v-model="isShowDialog" width="700px"> <el-dialog :title="isEdit ? '修改分类' : '添加分类'" v-model="isShowDialog" width="940px">
<el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="90px"> <el-form ref="formRef" :model="ruleForm" :rules="rules" size="default" label-width="90px">
<el-form-item label="上级分类"> <el-form-item label="上级分类">
<el-cascader <el-cascader
@@ -17,74 +17,85 @@
<el-input v-model="ruleForm.name" placeholder="请输入分类名称" clearable /> <el-input v-model="ruleForm.name" placeholder="请输入分类名称" clearable />
</el-form-item> </el-form-item>
<el-form-item label="排序"> <el-form-item label="排序">
<el-input-number v-model="ruleForm.sort" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" /> <el-input-number v-model="ruleForm.sort" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w10" />
</el-form-item> </el-form-item>
<!-- <el-form-item label="状态" v-if="!isEdit">
<el-switch
v-model="ruleForm.status"
active-value="1"
inactive-value="0"
inline-prompt
active-text="显示"
inactive-text="隐藏"
/>
</el-form-item> -->
<!-- 自定义属性 -->
<el-form-item label="自定义属性"> <el-form-item label="自定义属性">
<div class="custom-attrs-container"> <div class="custom-attrs-table">
<div v-for="(attr, index) in ruleForm.attrs" :key="index" class="custom-attr-item"> <div class="table-head">
<el-input <span class="col col-name">属性名称</span>
v-if="attr.type !== 'select' && attr.type !== 'multi_select'" <span class="col col-type">属性类型</span>
v-model="attr.name" <span class="col col-dict">字典</span>
placeholder="属性名称" <span class="col col-dict-value">字典值</span>
style="width: 120px" <span class="col col-ops">操作</span>
/> </div>
<el-select v-model="attr.type" placeholder="属性类型" style="width: 120px" @change="onAttrTypeChange(attr)"> <div class="table-body">
<el-option label="文本" value="text" /> <div v-for="(attr, index) in ruleForm.attrs" :key="index" class="table-row">
<el-option label="数字" value="number" /> <div class="col col-name">
<el-option label="日期" value="date" /> <el-input
<el-option label="单选" value="select" /> v-if="!isDictType(attr.type)"
<el-option label="多选" value="multi_select" /> v-model="attr.name"
<el-option label="布尔" value="boolean" /> placeholder="请输入属性名称"
<el-option label="图片" value="image" /> clearable
</el-select> />
<!-- 单选/多选时显示字典类型选择 --> <span v-else class="dict-name">{{ attr.description || '请选择字典' }}</span>
<el-select </div>
v-if="attr.type === 'select' || attr.type === 'multi_select'" <div class="col col-type">
v-model="attr.description" <el-select v-model="attr.type" placeholder="属性类型" class="w100" @change="onAttrTypeChange(attr)">
placeholder="选择字典类型" <el-option
style="width: 120px" v-for="item in attrTypeOptions"
:loading="dictLoading" :key="item.key"
@change="onDictKeyChange(attr)" :label="item.value"
> :value="item.key"
<el-option />
v-for="(item, idx) in dictTypeOptions" </el-select>
:key="idx" </div>
:label="item.name || ''" <div class="col col-dict">
:value="item.name || ''" <el-select
/> v-model="attr.description"
</el-select> placeholder="选择字典"
<!-- 选择字典类型后显示字典值选择 --> class="w100"
<el-select :disabled="!isDictType(attr.type)"
v-if="attr.description && (attr.type === 'select' || attr.type === 'multi_select')" :loading="dictLoading"
v-model="attr.options" clearable
multiple @change="onDictKeyChange(attr)"
placeholder="选择字典值" >
style="width: 150px" <el-option
collapse-tags v-for="(item, idx) in dictTypeOptions"
collapse-tags-tooltip :key="idx"
:max-collapse-tags="2" :label="item.name || ''"
> :value="item.name || ''"
<el-option />
v-for="(item, idx) in getDictValuesByType(attr.description)" </el-select>
:key="idx" </div>
:label="item.value" <div class="col col-dict-value">
:value="item.key" <el-select
/> v-if="isDictType(attr.type) && attr.description"
</el-select> v-model="attr.options"
<el-button type="danger" :icon="Delete" circle @click="removeAttr(index)" /> multiple
placeholder="选择字典值"
class="w100"
collapse-tags
collapse-tags-tooltip
:max-collapse-tags="2"
>
<el-option
v-for="(item, idx) in getDictValuesByType(attr.description)"
:key="idx"
:label="item.value"
:value="item.key"
/>
</el-select>
<span v-else class="dict-placeholder">-</span>
</div>
<div class="col col-ops">
<el-button type="danger" :icon="Delete" circle @click="removeAttr(index)" />
</div>
</div>
<el-empty v-if="ruleForm.attrs.length === 0" description="暂无自定义属性" />
</div>
<div class="table-footer">
<el-button type="primary" :icon="Plus" plain @click="addAttr">添加属性</el-button>
</div> </div>
<el-button type="primary" :icon="Plus" @click="addAttr">添加属性</el-button>
</div> </div>
</el-form-item> </el-form-item>
</el-form> </el-form>
@@ -105,10 +116,10 @@ export default {
</script> </script>
<script setup lang="ts"> <script setup lang="ts">
import { ref, reactive } from 'vue'; import { ref, reactive, onMounted } from 'vue';
import { ElMessage } from 'element-plus'; import { ElMessage } from 'element-plus';
import { Plus, Delete } from '@element-plus/icons-vue'; import { Plus, Delete } from '@element-plus/icons-vue';
import { getCategoryTree, getCategory, addCategory, updateCategory } from '/@/api/assets/category'; import { getCategoryTree, getCategory, addCategory, updateCategory, getCategoryAttrTypeOptions } from '/@/api/assets/category';
import { getDicts } from '/@/api/system/dict/data'; import { getDicts } from '/@/api/system/dict/data';
interface CategoryRow { interface CategoryRow {
@@ -162,6 +173,7 @@ const isEdit = ref(false);
const isAddChild = ref(false); const isAddChild = ref(false);
const submitLoading = ref(false); const submitLoading = ref(false);
const categoryData = ref<CategoryRow[]>([]); const categoryData = ref<CategoryRow[]>([]);
const attrTypeOptions = ref<{ key: string; value: string }[]>([]);
const dictTypeOptions = ref<DictInfo[]>([]); const dictTypeOptions = ref<DictInfo[]>([]);
const dictValueOptions = ref<any[]>([]); const dictValueOptions = ref<any[]>([]);
const dictLoading = ref(false); const dictLoading = ref(false);
@@ -179,6 +191,21 @@ const rules = {
name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }], name: [{ required: true, message: '分类名称不能为空', trigger: 'blur' }],
}; };
// 获取属性类型
const fetchAttrTypeOptions = () => {
getCategoryAttrTypeOptions()
.then((res: any) => {
const list = res.data?.options ?? [];
attrTypeOptions.value = list.map((item: any) => ({
key: item.key ?? item.Key ?? '',
value: item.value ?? item.Value ?? '',
}));
})
.catch(() => {
attrTypeOptions.value = [];
});
};
// 获取字典类型数据 // 获取字典类型数据
const fetchDictTypeOptions = () => { const fetchDictTypeOptions = () => {
dictLoading.value = true; dictLoading.value = true;
@@ -213,6 +240,9 @@ const getDictValuesByType = (dictKey: string) => {
return dictItem?.values ?? []; return dictItem?.values ?? [];
}; };
// 判断是否为字典类型
const isDictType = (type?: string) => type === 'select' || type === 'multi_select';
// 字典类型选择变化时 // 字典类型选择变化时
const onDictKeyChange = (attr: CustomAttr) => { const onDictKeyChange = (attr: CustomAttr) => {
// 清空已选的字典值 // 清空已选的字典值
@@ -224,7 +254,8 @@ const addAttr = () => {
ruleForm.attrs.push({ ruleForm.attrs.push({
name: '', name: '',
type: 'text', type: 'text',
dictKey: '', description: '',
options: [],
}); });
}; };
@@ -235,17 +266,23 @@ const removeAttr = (index: number) => {
// 属性类型变化时 // 属性类型变化时
const onAttrTypeChange = (attr: CustomAttr) => { const onAttrTypeChange = (attr: CustomAttr) => {
if (attr.type === 'select' || attr.type === 'multi_select') { if (isDictType(attr.type)) {
// 如果字典类型数据还没加载,则加载 // 如果字典类型数据还没加载,则加载
if (dictTypeOptions.value.length === 0) { if (dictTypeOptions.value.length === 0) {
fetchDictTypeOptions(); fetchDictTypeOptions();
} }
attr.dictKey = ''; attr.description = '';
attr.options = [];
} else { } else {
attr.dictKey = undefined; attr.description = '';
attr.options = [];
} }
}; };
onMounted(() => {
fetchAttrTypeOptions();
});
// 重置表单 // 重置表单
const resetForm = () => { const resetForm = () => {
ruleForm.id = ''; ruleForm.id = '';
@@ -318,36 +355,53 @@ const onCancel = () => {
closeDialog(); closeDialog();
}; };
// 将字典值转换为 {label,value}
const formatDictOptions = (attr: CustomAttr) => {
const options = attr.options || [];
if (options.length > 0 && typeof options[0] === 'object') {
return options.map((opt: any) => ({
label: opt.label ?? opt.value ?? '',
value: opt.value ?? opt.key ?? '',
}));
}
const dictValues = getDictValuesByType(attr.description || '');
return options.map((optValue: string) => {
const dictItem = dictValues.find((d: any) => d.key === optValue);
return {
label: dictItem?.value || optValue,
value: optValue,
};
});
};
// 提交 // 提交
const onSubmit = () => { const onSubmit = () => {
formRef.value.validate((valid: boolean) => { formRef.value.validate((valid: boolean) => {
if (valid) { if (valid) {
submitLoading.value = true; submitLoading.value = true;
// 处理 attrs单选/多选类型不传递 name并转换 options 格式 // 处理 attrs统一清理脏数据
const processedAttrs = ruleForm.attrs.map((attr) => { const processedAttrs = ruleForm.attrs.map((attr) => {
if (attr.type === 'select' || attr.type === 'multi_select') { const base = {
const { name, ...rest } = attr; type: attr.type,
const options = attr.options || []; description: attr.description || '',
let formattedOptions: any[] = []; required: attr.required ?? false,
multiple: attr.type === 'multi_select',
// 判断 options 是否已经是对象数组格式 sort: attr.sort ?? 0,
if (options.length > 0 && typeof options[0] === 'object') { };
// 已经是对象数组格式,直接使用
formattedOptions = options; if (isDictType(attr.type)) {
} else { return {
// 是字符串数组,需要转换为 [{label, value}] 格式 ...base,
const dictValues = getDictValuesByType(attr.description || ''); name: '',
formattedOptions = options.map((optValue: string) => { options: formatDictOptions(attr),
const dictItem = dictValues.find((d: any) => d.key === optValue); };
return {
label: dictItem?.value || optValue,
value: optValue,
};
});
}
return { ...rest, options: formattedOptions };
} }
return attr;
return {
...base,
name: (attr.name || '').trim(),
options: [],
};
}); });
const submitData = { ...ruleForm, attrs: processedAttrs }; const submitData = { ...ruleForm, attrs: processedAttrs };
@@ -391,20 +445,128 @@ defineExpose({
width: 100%; width: 100%;
} }
.custom-attrs-container { .custom-attrs-table {
width: 100%; width: 100%;
box-sizing: border-box;
border: 1px solid #ebeef5;
border-radius: 6px;
overflow: hidden;
background: #fff;
padding: 4px 0 0;
.custom-attr-item { .table-head,
.table-row {
display: grid;
grid-template-columns: 1.4fr 1fr 1fr 1.4fr 60px;
align-items: center;
column-gap: 12px;
padding: 12px 16px;
}
.table-head {
background: #f7f9fc;
font-weight: 600;
color: #4b4f58;
.col {
justify-content: flex-start;
white-space: nowrap;
}
border-bottom: 1px solid #ebeef5;
}
.table-body {
max-height: 260px;
overflow-y: auto;
padding: 0 16px 12px;
}
.table-row + .table-row {
border-top: 1px solid #f2f2f2;
}
.col {
display: flex; display: flex;
align-items: center; align-items: center;
gap: 10px; justify-content: flex-start;
margin-bottom: 10px; }
.el-button.is-circle { .col-ops {
:deep(.el-icon) { justify-content: center;
margin-right: 0 !important;
} :deep(.el-button) {
display: inline-flex;
align-items: center;
justify-content: center;
margin: 0 auto;
} }
:deep(.el-button.is-circle) {
width: 32px;
height: 32px;
padding: 0;
}
:deep(.el-button .el-icon) {
margin: 0;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
}
:deep(.el-button .el-icon svg) {
margin: 0;
}
}
.col-name {
.dict-name {
display: inline-block;
width: 100%;
padding: 0 12px;
height: 32px;
line-height: 32px;
border: 1px solid #dcdfe6;
border-radius: 4px;
color: #606266;
background-color: #f5f7fa;
}
}
.col-type,
.col-dict,
.col-dict-value {
:deep(.el-select),
:deep(.el-input) {
width: 100%;
}
:deep(.el-input__wrapper) {
width: 100%;
box-sizing: border-box;
}
}
.col-dict-value {
min-height: 40px;
.dict-placeholder {
color: #c0c4cc;
}
}
:deep(.el-empty) {
padding: 24px 0;
}
.table-footer {
padding: 12px 16px;
border-top: 1px solid #ebeef5;
text-align: left;
background: #fafafa;
display: flex;
justify-content: flex-start;
} }
} }
</style> </style>