refactor(dict): 优化字典数据查询逻辑和接口

- 移除不必要的 ISysDictTypeService 依赖,简化 SysDictDataController
- 新增 selectDictDataByTypeRealtime 方法,支持实时查询字典数据,避免缓存问题
- 更新 SysDictDataController 中的字典数据查询逻辑,使用新方法
- 在 SysDictTypeController 中添加按字典类型编码精确查询的接口
- 更新前端组件以支持新的字典查询接口,优化字典选择器的加载逻辑
This commit is contained in:
王文昊
2026-04-28 19:12:50 +08:00
parent dde947516d
commit 5a56094e4f
11 changed files with 541 additions and 100 deletions

View File

@@ -1,27 +1,49 @@
<template>
<div class="spec-page">
<!-- 规程类型 tabs -->
<div class="type-tab-bar">
<span
v-for="t in specTypeTab"
:key="t.value"
:class="['type-tab', { active: activeSpecType === t.value }]"
@click="switchSpecType(t.value)"
>{{ t.label }}</span>
<!-- 规程类型 -->
<div class="dict-toolbar-row">
<div class="type-tab-bar">
<span
v-for="t in specTypeTab"
:key="'stype-' + (t.value === '' ? 'all' : t.value)"
:class="['type-tab', { active: activeSpecType === t.value }]"
@click="switchSpecType(t.value)"
>{{ t.label }}</span>
</div>
<dict-select
toolbar-only
:kisv="false"
:editable="true"
:refresh="false"
:dict-type="DICT_SPEC_TYPE"
panel-title="规程工艺类型字典"
@dict-updated="loadSpecTypeDict"
/>
</div>
<!-- 产线 tabs -->
<div class="line-tab-bar">
<span
:class="['line-tab', { active: activeLineId === '' }]"
@click="switchLine('')"
>全部</span>
<span
v-for="line in lineOptions"
:key="line.lineId"
:class="['line-tab', { active: activeLineId === line.lineId }]"
@click="switchLine(line.lineId)"
>{{ line.lineName }}</span>
<!-- 产线 -->
<div class="dict-toolbar-row line-row">
<div class="line-tab-bar">
<span
:class="['line-tab', { active: activeLineId === '' }]"
@click="switchLine('')"
>全部</span>
<span
v-for="line in lineOptions"
:key="'ln-' + line.lineId"
:class="['line-tab', { active: lineTabActive(line) }]"
@click="switchLine(line.lineId)"
>{{ line.lineName }}</span>
</div>
<dict-select
toolbar-only
:kisv="false"
:editable="true"
:refresh="false"
:dict-type="DICT_LINE"
panel-title="规程产线筛选项字典值为产线 line_id"
@dict-updated="loadLineOptions"
/>
</div>
<!-- 工具栏 -->
@@ -95,15 +117,20 @@
</el-form-item>
<el-form-item label="规程类型" prop="specType">
<el-select v-model="form.specType" style="width:100%">
<el-option v-for="t in specTypeOptions" :key="t.value" :label="t.label" :value="t.value" />
<el-option
v-for="t in specTypeOptionsForForm"
:key="t.dictValue"
:label="t.dictLabel"
:value="t.dictValue"
/>
</el-select>
</el-form-item>
<el-form-item label="产线" prop="lineId">
<el-select v-model="form.lineId" filterable placeholder="请选择" style="width:100%">
<el-option
v-for="line in lineOptions"
v-for="line in lineOptionsForForm"
:key="line.lineId"
:label="line.lineCode ? line.lineName + '' + line.lineCode + '' : line.lineName"
:label="lineOptLabel(line)"
:value="line.lineId"
/>
</el-select>
@@ -127,18 +154,24 @@
</template>
<script>
import { getDicts } from '@/api/system/dict/data'
import { listProcessSpec, getProcessSpec, delProcessSpec, updateProcessSpec, addProcessSpec } from '@/api/wms/processSpec'
import { listProductionLine } from '@/api/wms/productionLine'
const SPEC_TYPES = [
{ label: '工艺规程', value: 'PROCESS' },
{ label: '标准', value: 'STANDARD' }
const DICT_SPEC_TYPE = 'wms_process_spec_type'
const DICT_LINE = 'wms_process_spec_line'
const DEFAULT_SPEC_TYPES = [
{ dictLabel: '工艺规程', dictValue: 'PROCESS', dictSort: 10 },
{ dictLabel: '标准', dictValue: 'STANDARD', dictSort: 20 }
]
export default {
name: 'ProcessSpec',
data() {
return {
DICT_SPEC_TYPE,
DICT_LINE,
loading: false,
btnLoading: false,
total: 0,
@@ -148,9 +181,8 @@ export default {
multiple: true,
open: false,
dialogTitle: '',
specTypeRows: [],
lineOptions: [],
specTypeTab: [{ label: '全部', value: '' }, ...SPEC_TYPES],
specTypeOptions: SPEC_TYPES,
activeSpecType: '',
activeLineId: '',
queryParams: {
@@ -169,13 +201,159 @@ export default {
}
}
},
computed: {
specTypeTab() {
const rows = this.mergeSpecTypeRowsWithDefaults()
const sorted = [...rows].sort((a, b) => (Number(a.dictSort) || 0) - (Number(b.dictSort) || 0))
return [{ label: '全部', value: '' }, ...sorted.map(r => ({ label: r.dictLabel, value: r.dictValue }))]
},
specTypeOptionsForForm() {
const rows = this.mergeSpecTypeRowsWithDefaults()
return [...rows].sort((a, b) => (Number(a.dictSort) || 0) - (Number(b.dictSort) || 0))
},
lineOptionsForForm() {
return this.lineOptions
}
},
created() {
listProductionLine({ pageNum: 1, pageSize: 500 }).then(res => {
this.lineOptions = res.rows || []
Promise.all([this.loadSpecTypeDict(), this.loadLineOptions()]).finally(() => {
this.getList()
})
this.getList()
},
methods: {
/**
* 工艺类型:默认 PROCESS/STANDARD 与字典合并。若仅有字典中存在的新项,仍可保留两行基础兜底;同 dict_value 以字典为准(可改名、调 sort
*/
mergeSpecTypeRowsWithDefaults() {
const byVal = new Map()
DEFAULT_SPEC_TYPES.forEach(row => {
byVal.set(row.dictValue, { ...row })
})
for (const row of this.specTypeRows || []) {
if (!row || row.dictValue === undefined || row.dictValue === null || row.dictValue === '') continue
const v = String(row.dictValue)
const prev = byVal.get(v)
const sort = row.dictSort != null && row.dictSort !== ''
? Number(row.dictSort)
: (prev && prev.dictSort != null ? prev.dictSort : 999)
const label = (row.dictLabel != null && String(row.dictLabel).trim() !== '')
? row.dictLabel
: (prev && prev.dictLabel)
byVal.set(v, {
dictLabel: label,
dictValue: v,
dictSort: sort
})
}
return Array.from(byVal.values())
},
/**
* 产线 ID 与字典值:用数字字符串,避免超过 Number.MAX_SAFE_INTEGER 时精度丢失(雪花 id
*/
normalizeLineIdString(value) {
if (value === undefined || value === null || value === '') return ''
const s = String(value).trim()
return /^\d+$/.test(s) ? s : ''
},
/** Tab 高亮:雪花 id 一律按字符串比较 */
lineTabActive(line) {
if (this.activeLineId === '' || this.activeLineId === undefined) return false
return String(this.activeLineId) === String(line.lineId)
},
parseDictRows(res) {
const rows = (res.data || []).filter(d => d.status === '0' || d.status === undefined)
rows.sort((a, b) => (Number(a.dictSort) || 0) - (Number(b.dictSort) || 0))
return rows
},
async loadSpecTypeDict() {
try {
const res = await getDicts(DICT_SPEC_TYPE)
this.specTypeRows = this.parseDictRows(res)
} catch (err) {
console.error('规程工艺类型字典加载失败', err)
this.specTypeRows = []
}
},
/**
* 产线 Tab产线主表 字典中合法数字 line_id字典独有也会显示 Tab
* line_id 全程用数字字符串,避免超过 Number.MAX_SAFE_INTEGER 时精度丢失;字典值须为数字 line_id。
*/
async loadLineOptions() {
const previousOptions = Array.isArray(this.lineOptions) && this.lineOptions.length
? this.lineOptions.map(o => ({ ...o }))
: []
let dictRows = []
try {
const res = await getDicts(DICT_LINE)
dictRows = this.parseDictRows(res)
} catch (err) {
console.error('规程产线字典加载失败', err)
}
const dictMeta = new Map()
for (const d of dictRows) {
const idStr = this.normalizeLineIdString(d.dictValue)
if (!idStr) continue
const sort = Number(d.dictSort) || 0
const label = (d.dictLabel != null && String(d.dictLabel).trim() !== '')
? String(d.dictLabel).trim()
: idStr
dictMeta.set(idStr, { label, sort })
}
let tableLines = []
try {
const res = await listProductionLine({ pageNum: 1, pageSize: 500 })
tableLines = (res.rows || []).map(p => {
const idStr = this.normalizeLineIdString(p.lineId) || (p.lineId != null ? String(p.lineId).trim() : '')
return {
lineId: idStr,
lineName: p.lineName,
lineCode: p.lineCode
}
}).filter(p => p.lineId)
} catch (e2) {
console.error('产线列表加载失败', e2)
}
const tableIdSet = new Set(tableLines.map(l => String(l.lineId)))
const next = []
for (const line of tableLines) {
const idStr = String(line.lineId)
const dm = dictMeta.get(idStr)
next.push({
lineId: idStr,
lineName: dm ? dm.label : line.lineName,
lineCode: line.lineCode
})
}
const dictOnly = []
for (const [idStr, meta] of dictMeta) {
if (!tableIdSet.has(idStr)) {
dictOnly.push({ lineId: idStr, lineName: meta.label, lineCode: undefined, _sort: meta.sort })
}
}
dictOnly.sort((a, b) => a._sort - b._sort)
dictOnly.forEach(d => {
const { _sort, ...rest } = d
next.push(rest)
})
if (next.length === 0 && previousOptions.length > 0) {
console.warn('[规程产线] 本次未解析出有效筛选项(可能字典值非数字或产线接口异常),保留上一版 Tab')
return
}
this.lineOptions = next
},
lineOptLabel(line) {
if (line.lineCode) {
return `${line.lineName}${line.lineCode}`
}
return line.lineName
},
getList() {
this.loading = true
listProcessSpec(this.queryParams).then(res => {
@@ -190,8 +368,13 @@ export default {
this.getList()
},
switchLine(lineId) {
this.activeLineId = lineId
this.queryParams.lineId = lineId || undefined
if (lineId === '' || lineId === undefined || lineId === null) {
this.activeLineId = ''
} else {
const s = this.normalizeLineIdString(lineId)
this.activeLineId = s || String(lineId).trim()
}
this.queryParams.lineId = this.activeLineId === '' ? undefined : this.activeLineId
this.queryParams.pageNum = 1
this.getList()
},
@@ -208,8 +391,21 @@ export default {
this.single = sel.length !== 1
this.multiple = !sel.length
},
defaultSpecType() {
const first = this.specTypeOptionsForForm[0]
return first ? first.dictValue : 'PROCESS'
},
reset() {
this.form = { specId: undefined, specCode: undefined, specName: undefined, specType: 'PROCESS', lineId: undefined, productType: undefined, isEnabled: 1, remark: undefined }
this.form = {
specId: undefined,
specCode: undefined,
specName: undefined,
specType: this.defaultSpecType(),
lineId: undefined,
productType: undefined,
isEnabled: 1,
remark: undefined
}
this.$refs.form && this.$refs.form.clearValidate()
},
handleAdd() {
@@ -222,6 +418,10 @@ export default {
const specId = row ? row.specId : this.ids[0]
getProcessSpec(specId).then(res => {
this.form = res.data || {}
if (this.form.lineId != null && this.form.lineId !== '') {
const sid = this.normalizeLineIdString(this.form.lineId)
this.form.lineId = sid || String(this.form.lineId).trim()
}
this.dialogTitle = '修改规程'
this.open = true
})
@@ -273,11 +473,23 @@ export default {
/* ── 双色主题:默认=白底灰边,激活/主操作=深藏青 #5F7BA0 ── */
.dict-toolbar-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px 10px;
margin-bottom: 6px;
}
.dict-toolbar-row.line-row {
margin-bottom: 12px;
}
.type-tab-bar {
display: flex;
flex: 0 1 auto;
flex-wrap: wrap;
gap: 0;
margin-bottom: 10px;
width: fit-content;
border-radius: 4px;
overflow: hidden;
border: 1px solid #dcdfe6;
@@ -307,9 +519,11 @@ export default {
.line-tab-bar {
display: flex;
flex-wrap: wrap;
flex: 0 1 auto;
align-items: center;
gap: 6px;
margin-bottom: 12px;
padding: 10px 0;
min-width: 0;
}
.line-tab {
@@ -381,4 +595,9 @@ export default {
::v-deep .el-button--text.btn-danger { color: #f56c6c !important; }
.btn-danger { color: #f56c6c; }
/* 与 Tab 同一行时,将齿轮框配色贴近规程主题 */
::v-deep .dict-toolbar-row .el-icon-setting {
color: #5F7BA0;
}
</style>