68 lines
2.0 KiB
JavaScript
68 lines
2.0 KiB
JavaScript
import { ref } from 'vue'
|
|
import { listProduct } from '@/api/price/product'
|
|
|
|
/**
|
|
* 产品列表模块级共享缓存:全应用只拉取一次
|
|
* 用于客户绑定产品的选择与已绑产品名称/明细回显
|
|
*/
|
|
const productList = ref([])
|
|
let loadingPromise = null
|
|
|
|
/** 逗号分隔 id 串 -> 字符串 id 数组 */
|
|
export function parseProductIds(value) {
|
|
if (Array.isArray(value)) return value.map(v => String(v)).filter(Boolean)
|
|
if (value === null || value === undefined) return []
|
|
return String(value).split(',').map(s => s.trim()).filter(Boolean)
|
|
}
|
|
|
|
/** id 数组 -> 逗号分隔串(去重,保持原顺序) */
|
|
export function joinProductIds(ids) {
|
|
const seen = new Set()
|
|
const out = []
|
|
;(ids || []).forEach(id => {
|
|
const key = String(id)
|
|
if (key && !seen.has(key)) {
|
|
seen.add(key)
|
|
out.push(key)
|
|
}
|
|
})
|
|
return out.join(',')
|
|
}
|
|
|
|
export function useProducts() {
|
|
/** 确保产品列表已加载(force=true 强制刷新),拉取全部状态以便回显已失效产品 */
|
|
function ensureProducts(force = false) {
|
|
if (!force && loadingPromise) return loadingPromise
|
|
if (!force && productList.value.length) return Promise.resolve(productList.value)
|
|
loadingPromise = listProduct({ pageNum: 1, pageSize: 1000 })
|
|
.then(res => {
|
|
productList.value = res.rows || []
|
|
return productList.value
|
|
})
|
|
.finally(() => {
|
|
loadingPromise = null
|
|
})
|
|
return loadingPromise
|
|
}
|
|
|
|
/** 按产品ID查产品对象 */
|
|
function findProduct(productId) {
|
|
if (productId === null || productId === undefined || productId === '') return null
|
|
return productList.value.find(p => String(p.productId) === String(productId)) || null
|
|
}
|
|
|
|
/** 产品展示名:代码 + 名称 */
|
|
function getProductLabel(productId) {
|
|
const p = findProduct(productId)
|
|
if (!p) return String(productId)
|
|
return p.productCode + ' ' + p.productName
|
|
}
|
|
|
|
return {
|
|
productList,
|
|
ensureProducts,
|
|
findProduct,
|
|
getProductLabel
|
|
}
|
|
}
|