字典配置

This commit is contained in:
zhonghua.li
2026-04-24 12:13:21 +08:00
parent 8464645e74
commit 765b2cd47c
3 changed files with 460 additions and 0 deletions

46
src/api/dict-item.js Normal file
View File

@@ -0,0 +1,46 @@
import request from '@/utils/request'
export function getDictItemList(params) {
return request({
url: '/dictItem/list',
method: 'get',
params
})
}
export function getDictItemById(id, params) {
return request({
url: `/dictItem/get/${id}`,
method: 'get',
params
})
}
// 获取企微配置:后端会在 name=qiwei_config 且记录不存在时返回默认配置 JSON
export function getQiWeiConfigDictItem(id = '0') {
return getDictItemById(id, { name: 'qiwei_config' })
}
export function addDictItem(data) {
return request({
url: '/dictItem/add',
method: 'post',
data
})
}
export function updateDictItem(data) {
return request({
url: '/dictItem/update',
method: 'put',
data
})
}
export function deleteDictItem(id) {
return request({
url: `/dictItem/delete/${id}`,
method: 'delete'
})
}

View File

@@ -398,6 +398,12 @@ export const constantRoutes = [
name: 'AiPrompts',
component: () => import('@/views/system/ai-prompts/index'),
meta: { title: '提示词管理', icon: 'el-icon-document' }
},
{
path: 'dict-item',
name: 'DictItem',
component: () => import('@/views/system/dict-item/index'),
meta: { title: '字典管理', icon: 'el-icon-collection' }
}
]
},

View File

@@ -0,0 +1,408 @@
<template>
<div class="app-container">
<el-card class="filter-container" shadow="never">
<el-form ref="queryForm" :model="queryParams" :inline="true" label-width="100px">
<el-form-item label="租户ID">
<el-input v-model="queryParams.tenantId" placeholder="租户ID" clearable style="width: 220px" />
</el-form-item>
<el-form-item label="名称">
<el-input v-model="queryParams.name" placeholder="名称" clearable style="width: 180px" />
</el-form-item>
<el-form-item label="类型">
<el-input v-model="queryParams.type" placeholder="类型" clearable style="width: 160px" />
</el-form-item>
<el-form-item label="微服务">
<el-input v-model="queryParams.microService" placeholder="微服务标识" clearable style="width: 180px" />
</el-form-item>
<el-form-item label="适用范围">
<el-input v-model="queryParams.valueRange" placeholder="范围/适用范围" clearable style="width: 200px" />
</el-form-item>
<el-form-item>
<el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
<el-button icon="el-icon-delete" @click="resetQuery">清空</el-button>
</el-form-item>
</el-form>
</el-card>
<el-card class="action-container" shadow="never">
<el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
<el-button icon="el-icon-setting" @click="handleQiweiConfig">配置企微</el-button>
</el-card>
<el-card class="table-container" shadow="never">
<el-table v-loading="loading" :data="list" style="width: 100%">
<el-table-column label="序号" type="index" width="50" />
<el-table-column label="名称" prop="name" min-width="140" show-overflow-tooltip />
<el-table-column label="值" prop="value" min-width="200" show-overflow-tooltip />
<el-table-column label="类型" prop="type" min-width="120" show-overflow-tooltip />
<el-table-column label="适用范围" prop="valueRange" min-width="140" show-overflow-tooltip />
<el-table-column label="微服务" prop="microService" min-width="140" show-overflow-tooltip />
<el-table-column label="描述" prop="description" min-width="160" show-overflow-tooltip />
<el-table-column label="租户ID" prop="tenantId" min-width="220" show-overflow-tooltip />
<el-table-column label="创建时间" prop="createdTime" min-width="160">
<template slot-scope="scope">
{{ formatDateTime(scope.row.createdTime) }}
</template>
</el-table-column>
<el-table-column label="更新时间" prop="updatedTime" min-width="160">
<template slot-scope="scope">
{{ formatDateTime(scope.row.updatedTime) }}
</template>
</el-table-column>
<el-table-column label="操作" width="140" fixed="right">
<template slot-scope="scope">
<el-button type="text" @click="handleEdit(scope.row)">编辑</el-button>
<el-button type="text" style="color: #F56C6C;" @click="handleDelete(scope.row)">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
:current-page="queryParams.current"
:page-sizes="[10, 20, 50, 100]"
:page-size="queryParams.size"
:total="total"
layout="total, sizes, prev, pager, next, jumper"
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
/>
</el-card>
<el-dialog
:title="dialogTitle"
:visible.sync="dialogVisible"
width="720px"
@close="resetForm"
>
<el-form
ref="dataForm"
:model="form"
:rules="formRules"
label-width="110px"
>
<el-form-item label="租户ID" prop="tenantId">
<el-input v-model="form.tenantId" placeholder="租户ID可选" />
</el-form-item>
<el-form-item label="名称" prop="name">
<el-input v-model="form.name" placeholder="名称" />
</el-form-item>
<el-form-item label="值" prop="value">
<el-input v-model="form.value" type="textarea" :rows="3" placeholder="值" />
</el-form-item>
<el-form-item label="类型" prop="type">
<el-input v-model="form.type" placeholder="类型" />
</el-form-item>
<el-form-item label="适用范围" prop="valueRange">
<el-input v-model="form.valueRange" placeholder="范围/适用范围" />
</el-form-item>
<el-form-item label="微服务" prop="microService">
<el-input v-model="form.microService" placeholder="所属微服务/服务标识" />
</el-form-item>
<el-form-item label="描述" prop="description">
<el-input v-model="form.description" type="textarea" :rows="2" placeholder="描述说明" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" @click="handleSubmit">确定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
import {
getDictItemList,
getQiWeiConfigDictItem,
addDictItem,
updateDictItem,
deleteDictItem
} from '@/api/dict-item'
export default {
name: 'DictItem',
data() {
return {
loading: false,
list: [],
total: 0,
dialogVisible: false,
dialogTitle: '',
isEdit: false,
queryParams: {
current: 1,
size: 10,
tenantId: '',
name: '',
type: '',
microService: '',
valueRange: ''
},
form: {
id: '',
tenantId: '',
name: '',
value: '',
type: '',
valueRange: '',
microService: '',
description: ''
},
formRules: {
name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
value: [{ required: true, message: '请输入值', trigger: 'blur' }],
type: [{ required: true, message: '请输入类型', trigger: 'blur' }]
}
}
},
created() {
this.fetchList()
},
methods: {
isPlainObject(val) {
return val !== null && typeof val === 'object' && !Array.isArray(val)
},
safeJsonStringify(val) {
try {
return JSON.stringify(val, null, 2)
} catch (e) {
return String(val)
}
},
buildListParams() {
const params = {
current: this.queryParams.current,
size: this.queryParams.size,
tenantId: this.queryParams.tenantId || undefined,
name: this.queryParams.name || undefined,
type: this.queryParams.type || undefined,
microService: this.queryParams.microService || undefined,
valueRange: this.queryParams.valueRange || undefined
}
Object.keys(params).forEach((key) => {
if (params[key] === undefined || params[key] === '') {
delete params[key]
}
})
return params
},
normalizeListData(response) {
const data = response ? response.data : null
if (Array.isArray(data)) return data
if (data && Array.isArray(data.records)) return data.records
if (data && Array.isArray(data.list)) return data.list
return []
},
normalizeTotal(response) {
if (!response) return 0
if (typeof response.total === 'number') return response.total
const data = response.data
if (data && typeof data.total === 'number') return data.total
if (data && typeof data.count === 'number') return data.count
return 0
},
fetchList() {
this.loading = true
getDictItemList(this.buildListParams())
.then((response) => {
if (response && (response.code === 20000 || response.code === 200)) {
this.list = this.normalizeListData(response)
this.total = this.normalizeTotal(response)
} else {
this.list = []
this.total = 0
}
this.loading = false
})
.catch(() => {
this.loading = false
})
},
handleQuery() {
this.queryParams.current = 1
this.fetchList()
},
resetQuery() {
this.queryParams = {
current: 1,
size: 10,
tenantId: '',
name: '',
type: '',
microService: '',
valueRange: ''
}
this.fetchList()
},
handleSizeChange(val) {
this.queryParams.size = val
this.fetchList()
},
handleCurrentChange(val) {
this.queryParams.current = val
this.fetchList()
},
handleAdd() {
this.dialogTitle = '新增字典项'
this.isEdit = false
this.resetFormData()
this.dialogVisible = true
this.$nextTick(() => {
this.$refs.dataForm && this.$refs.dataForm.clearValidate()
})
},
handleQiweiConfig() {
this.dialogTitle = '配置企微'
this.isEdit = false
this.resetFormData()
this.loading = true
getQiWeiConfigDictItem()
.then((res) => {
const data = res && (res.code === 20000 || res.code === 200) ? res.data : null
// 兼容:可能返回 DictItem也可能直接返回 QiWeiConfig
if (data && this.isPlainObject(data) && (data.name || data.type || data.value || data.id)) {
this.isEdit = Boolean(data.id)
this.form = {
id: data.id || '',
tenantId: data.tenantId || '',
name: data.name || 'qiwei_config',
value: data.value || '',
type: data.type || 'qiwei_config',
valueRange: data.valueRange || '',
microService: data.microService || '',
description: data.description || '企微配置'
}
} else {
this.isEdit = false
this.form = {
id: '',
tenantId: '',
name: 'qiwei_config',
value: this.safeJsonStringify(data || {}),
type: 'qiwei_config',
valueRange: '',
microService: '',
description: '企微配置'
}
}
this.dialogVisible = true
this.$nextTick(() => {
this.$refs.dataForm && this.$refs.dataForm.clearValidate()
})
})
.finally(() => {
this.loading = false
})
},
handleEdit(row) {
this.dialogTitle = '编辑字典项'
this.isEdit = true
this.form = {
id: row.id,
tenantId: row.tenantId || '',
name: row.name || '',
value: row.value || '',
type: row.type || '',
valueRange: row.valueRange || '',
microService: row.microService || '',
description: row.description || ''
}
this.dialogVisible = true
this.$nextTick(() => {
this.$refs.dataForm && this.$refs.dataForm.clearValidate()
})
},
handleDelete(row) {
this.$confirm('确定删除该字典项吗?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(() => {
deleteDictItem(row.id).then((res) => {
this.$message.success(res.message || '删除成功')
this.fetchList()
})
})
.catch(() => {})
},
handleSubmit() {
this.$refs.dataForm.validate((valid) => {
if (!valid) return
const payload = {
tenantId: this.form.tenantId || undefined,
name: this.form.name,
value: this.form.value,
type: this.form.type,
valueRange: this.form.valueRange || undefined,
microService: this.form.microService || undefined,
description: this.form.description || undefined
}
if (this.isEdit) {
payload.id = this.form.id
updateDictItem(payload).then((res) => {
this.$message.success(res.message || '更新成功')
this.fetchList()
this.dialogVisible = false
})
} else {
addDictItem(payload).then((res) => {
this.$message.success(res.message || '添加成功')
this.fetchList()
this.dialogVisible = false
})
}
})
},
resetFormData() {
this.form = {
id: '',
tenantId: '',
name: '',
value: '',
type: '',
valueRange: '',
microService: '',
description: ''
}
},
resetForm() {
this.$refs.dataForm && this.$refs.dataForm.resetFields()
this.resetFormData()
},
formatDateTime(dateTime) {
if (!dateTime) return '-'
const s = typeof dateTime === 'string' ? dateTime.replace('T', ' ') : dateTime
const date = new Date(s)
if (Number.isNaN(date.getTime())) return String(dateTime)
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
}
}
</script>
<style lang="scss" scoped>
.app-container {
.filter-container {
margin-bottom: 20px;
}
.action-container {
margin-bottom: 20px;
}
.table-container {
.el-pagination {
margin-top: 20px;
text-align: right;
}
}
}
</style>