维护三个基础物件入库时候与数据库不匹配问题
This commit is contained in:
@ -161,6 +161,7 @@
|
||||
<span class="opt-name">{{ item.name }}</span>
|
||||
<span class="opt-spec">{{ item.spec }}</span>
|
||||
<el-tag v-if="item.isHistory" size="small" type="info" effect="plain">历史</el-tag>
|
||||
<el-tag v-else size="small" type="success" effect="plain">系统</el-tag>
|
||||
</div>
|
||||
</el-option>
|
||||
</el-select>
|
||||
@ -168,7 +169,7 @@
|
||||
</el-col>
|
||||
<el-col :span="14" style="display: flex; align-items: center;">
|
||||
<span class="search-tip">
|
||||
<el-icon><InfoFilled /></el-icon> 点击可查看最近使用的物料,或输入关键词从云端搜索。
|
||||
<el-icon><InfoFilled /></el-icon> 未输入时展示最新物料;输入关键词进行精确搜索。
|
||||
</span>
|
||||
</el-col>
|
||||
</el-row>
|
||||
@ -524,9 +525,8 @@ const saveMaterialHistory = (item: any) => {
|
||||
try {
|
||||
const existing = localStorage.getItem(key)
|
||||
let list = existing ? JSON.parse(existing) : []
|
||||
// 必须保存完整对象,因为下拉框需要显示 name, spec 等
|
||||
list = list.filter((i: any) => i.id !== item.id)
|
||||
list.unshift({ ...item, isHistory: true }) // 标记为历史
|
||||
list.unshift({ ...item, isHistory: true })
|
||||
if (list.length > 10) list = list.slice(0, 10)
|
||||
localStorage.setItem(key, JSON.stringify(list))
|
||||
} catch (e) {}
|
||||
@ -563,7 +563,7 @@ const mixedSearch = (queryString: string, tableField: string, storageKey: string
|
||||
// 合并去重
|
||||
const map = new Map()
|
||||
historyList.forEach(i => map.set(i.value, i))
|
||||
tableList.forEach(i => map.set(i.value, i)) // 表格数据优先(虽然value一样没区别)
|
||||
tableList.forEach(i => map.set(i.value, i))
|
||||
|
||||
const allList = Array.from(map.values())
|
||||
const results = queryString ? allList.filter(createFilter(queryString)) : allList
|
||||
@ -594,34 +594,39 @@ const querySearchCurrency = (queryString: string, cb: any) => {
|
||||
}
|
||||
|
||||
// ------------------------------------
|
||||
// 物料搜索逻辑 (支持历史回显 + API)
|
||||
// 物料搜索逻辑 (优化:支持空查询加载默认值)
|
||||
// ------------------------------------
|
||||
|
||||
// 下拉框展开时触发
|
||||
const handleMaterialDropdownVisible = (visible: boolean) => {
|
||||
if (visible) {
|
||||
// 只有当列表为空时(即没有进行API搜索时),才填充历史记录
|
||||
// 这样不会覆盖用户正在搜的结果
|
||||
// 【修改点】: 展开时,如果列表为空,直接调用 API 加载前 20 条数据
|
||||
// 同时也会把历史记录合并进去(在 handleSearchMaterial 内部处理或分开展示)
|
||||
// 这里简单处理:直接调用 handleSearchMaterial('') 让后端返回默认数据
|
||||
if (materialOptions.value.length === 0) {
|
||||
materialOptions.value = getMaterialHistory()
|
||||
handleSearchMaterial('')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const handleSearchMaterial = async (query: string) => {
|
||||
if (query) {
|
||||
searchLoading.value = true
|
||||
try {
|
||||
const res: any = await searchMaterialBase(query)
|
||||
// 给 API 返回的数据加个标记,区分历史
|
||||
const apiResults = (res.data || []).map((i: any) => ({ ...i, isHistory: false }))
|
||||
searchLoading.value = true
|
||||
try {
|
||||
// 即使 query 为空,后端现在也会返回数据
|
||||
const res: any = await searchMaterialBase(query)
|
||||
const apiResults = (res.data || []).map((i: any) => ({ ...i, isHistory: false }))
|
||||
|
||||
// 如果是空搜索,我们可以把本地历史记录插在前面,做个合并
|
||||
if (!query) {
|
||||
const history = getMaterialHistory()
|
||||
// 简单去重:如果 API 返回的 ID 在历史记录里有,就跳过 API 的那个(优先显示历史标记)
|
||||
const historyIds = new Set(history.map((h: any) => h.id))
|
||||
const filteredApi = apiResults.filter((apiItem: any) => !historyIds.has(apiItem.id))
|
||||
materialOptions.value = [...history, ...filteredApi]
|
||||
} else {
|
||||
materialOptions.value = apiResults
|
||||
} finally {
|
||||
searchLoading.value = false
|
||||
}
|
||||
} else {
|
||||
// 搜索词清空时,恢复历史记录
|
||||
materialOptions.value = getMaterialHistory()
|
||||
} finally {
|
||||
searchLoading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
@ -629,7 +634,6 @@ const handleSearchMaterial = async (query: string) => {
|
||||
const onMaterialSelected = (val: number) => {
|
||||
const item = materialOptions.value.find(i => i.id === val)
|
||||
if (item) {
|
||||
// 存入历史
|
||||
saveMaterialHistory(item)
|
||||
|
||||
form.material_name = item.name
|
||||
@ -832,7 +836,7 @@ const submitForm = async () => {
|
||||
ElMessage.success('更新成功')
|
||||
}
|
||||
|
||||
// 核心修改:提交成功时,保存供应商等信息到历史记录
|
||||
// 保存供应商等信息到历史记录
|
||||
saveToHistory(HISTORY_KEYS.SUPPLIER, form.supplier_name)
|
||||
saveToHistory(HISTORY_KEYS.PURCHASER, form.purchaser)
|
||||
saveToHistory(HISTORY_KEYS.EMAIL, form.purchaser_email)
|
||||
|
||||
Reference in New Issue
Block a user