25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
import { computed, ref } from 'vue'
|
|
import { analyticsApi } from './api/analytics'
|
|
import { contextParams } from './state'
|
|
import type { OwnerOption, QueryContext } from './types'
|
|
|
|
export function useOwnerDirectory() {
|
|
const owners = ref<OwnerOption[]>([]), error = ref('')
|
|
let loaded = '', generation = 0
|
|
const options = computed(() => owners.value.map(owner => ({ value: owner.customerCode, label: owner.customerName ? `${owner.customerName} (${owner.customerCode})` : owner.customerCode })))
|
|
async function load(context: QueryContext, warehouseCode: string, signal?: AbortSignal) {
|
|
const key = `${warehouseCode}|${context.queryAsOf}`
|
|
if (key === loaded) return
|
|
const request = ++generation
|
|
try {
|
|
const result = await analyticsApi.ownerDirectory({ sourceAccount: 'lingxing-primary', warehouseCode, ...contextParams(context) }, signal)
|
|
if (request !== generation || signal?.aborted) return
|
|
owners.value = result; loaded = key; error.value = ''
|
|
} catch {
|
|
if (request !== generation || signal?.aborted) return
|
|
owners.value = []; loaded = ''; error.value = '客户名称选项读取失败,请刷新重试'
|
|
}
|
|
}
|
|
return { options, error, load }
|
|
}
|