127 lines
3.4 KiB
Vue
127 lines
3.4 KiB
Vue
<template>
|
||
<el-select
|
||
:model-value="modelValue"
|
||
:placeholder="placeholder"
|
||
:clearable="clearable"
|
||
:filterable="filterable"
|
||
:size="size"
|
||
:style="{ width }"
|
||
:disabled="disabled"
|
||
@change="handleChange"
|
||
>
|
||
<el-option
|
||
v-for="wh in warehouseList"
|
||
:key="wh.warehouse_id"
|
||
:label="wh.warehouse_name"
|
||
:value="wh.warehouse_id"
|
||
/>
|
||
</el-select>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, onMounted, watch } from 'vue'
|
||
import { loadWarehouses } from '@/api/index/index'
|
||
|
||
interface WarehouseItem {
|
||
warehouse_id: string | number
|
||
warehouse_name: string
|
||
}
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
/** v-model 绑定的仓库 ID */
|
||
modelValue?: string | number | null
|
||
/** localStorage 缓存 key,不传则全局共享同一个仓库选择 */
|
||
storageKey?: string
|
||
placeholder?: string
|
||
clearable?: boolean
|
||
filterable?: boolean
|
||
size?: 'small' | 'default' | 'large'
|
||
width?: string
|
||
disabled?: boolean
|
||
}>(),
|
||
{
|
||
modelValue: null,
|
||
storageKey: 'global',
|
||
placeholder: '全部仓库',
|
||
clearable: true,
|
||
filterable: true,
|
||
size: 'small',
|
||
width: '200px',
|
||
disabled: false,
|
||
},
|
||
)
|
||
|
||
const emit = defineEmits<{
|
||
'update:modelValue': [value: string | number | null]
|
||
/** 选择变更时触发(包括首次从 localStorage 恢复后自动触发) */
|
||
change: [value: string | number | null]
|
||
}>()
|
||
|
||
const storageKeyFull = `warehouse_select_${props.storageKey}`
|
||
|
||
const warehouseList = ref<WarehouseItem[]>([])
|
||
|
||
async function loadWarehouseList() {
|
||
try {
|
||
const res: any = await loadWarehouses()
|
||
const raw = res?.warehouses || res?.data?.list || res?.data || []
|
||
warehouseList.value = raw.map((item: any) => ({
|
||
warehouse_id: item.value ?? item.warehouse_id ?? item.id,
|
||
warehouse_name: item.label || item.warehouse_name || item.company_name || String(item.value ?? ''),
|
||
}))
|
||
|
||
// 列表加载完成后,尝试从 localStorage 恢复上次选择
|
||
restoreFromStorage()
|
||
} catch {
|
||
// 仓库列表加载失败不影响主流程
|
||
}
|
||
}
|
||
|
||
/** 从 localStorage 恢复选择,若有效则自动触发 change */
|
||
function restoreFromStorage() {
|
||
const cached = localStorage.getItem(storageKeyFull)
|
||
if (cached === null || cached === '') return
|
||
|
||
const cachedId: string | number = isNaN(Number(cached)) ? cached : Number(cached)
|
||
const exists = warehouseList.value.some((wh) => wh.warehouse_id === cachedId)
|
||
|
||
if (exists && props.modelValue !== cachedId) {
|
||
emit('update:modelValue', cachedId)
|
||
// 延迟一帧触发 change,确保父组件已完成初始化
|
||
requestAnimationFrame(() => {
|
||
emit('change', cachedId)
|
||
})
|
||
} else if (!exists) {
|
||
// 缓存的仓库已不存在,清除
|
||
localStorage.removeItem(storageKeyFull)
|
||
}
|
||
}
|
||
|
||
function handleChange(value: string | number | null) {
|
||
if (value !== null && value !== undefined && value !== '') {
|
||
localStorage.setItem(storageKeyFull, String(value))
|
||
} else {
|
||
localStorage.removeItem(storageKeyFull)
|
||
}
|
||
emit('update:modelValue', value)
|
||
emit('change', value)
|
||
}
|
||
|
||
// 外部通过 v-model 变更时同步到 localStorage(但不再重复触发 change)
|
||
watch(
|
||
() => props.modelValue,
|
||
(newVal) => {
|
||
if (newVal !== null && newVal !== undefined && newVal !== '') {
|
||
localStorage.setItem(storageKeyFull, String(newVal))
|
||
} else {
|
||
localStorage.removeItem(storageKeyFull)
|
||
}
|
||
},
|
||
)
|
||
|
||
onMounted(() => {
|
||
loadWarehouseList()
|
||
})
|
||
</script>
|