import { Ref } from 'vue'; import { OptionsRow } from 'yc-pro-components/es/types'; /** * 字典项数据结构 * @description 标准化的字典数据项,兼容 OptionsRow */ export interface DictItem extends OptionsRow { /** 字典项标签 */ label: string; /** 字典项值 */ value: string | number; /** 字典项编码(可选) */ code?: string; /** 排序(可选) */ sort?: number; /** 是否启用(可选) */ enabled?: boolean; /** 其他扩展字段 */ [key: string]: unknown; } /** * 字典数据映射 * @description key 为字典编码(如 "FEE_TYPE"),value 为字典项数组 */ export interface DictDataMap { [dictField: string]: DictItem[]; } /** * 字典加载状态映射 */ export interface DictLoadingMap { [dictField: string]: boolean; } /** * 字典原始数据结构(后端 API 返回) * @description 后端 /opa/api/dict/search-dict 接口的返回格式 */ export interface DictRawItem { id?: number; dictCode: string; dictType?: number; dictName?: string; dictJson: Array<{ key: number | string; value: string; }>; tenantId?: number; [key: string]: unknown; } /** * 字典 API 响应类型(泛型) * @description 统一的 API 响应格式 */ export interface DictApiResponse { code: string; data: T; message?: string; [key: string]: unknown; } /** * createDictStore 工厂函数配置 * @description 通过配置创建定制化的字典 Store * @example * ```ts * import { createDictStore } from 'yc-pro-components' * import { getDictList } from '@/api/dict' * * export const useDictStore = createDictStore({ * fetchDictList: (dictCodes) => getDictList({ dictCodeList: dictCodes }), * isSuccess: (res) => res?.code === '00000', * extractData: (res) => res.data * }) * ``` */ export interface CreateDictStoreConfig { /** * 字典 API 调用函数(必填) * @description 传入字典编码数组,返回 API 响应 * @param dictCodes 字典编码数组(如 ["FEE_TYPE", "ORDER_STATUS"]) * @returns API 响应(Promise) */ fetchDictList: (dictCodes: string[]) => Promise; /** * 判断 API 响应是否成功(可选) * @description 默认判断 response?.code === '00000' * @param response API 原始响应 * @returns 是否成功 */ isSuccess?: (response: unknown) => boolean; /** * 从 API 响应中提取字典原始数据(可选) * @description 默认取 response.data * @param response API 原始响应 * @returns 字典原始数据数组 */ extractData?: (response: unknown) => DictRawItem[]; /** * 需要交换 label 和 value 的字典编码列表(可选) * @description 部分后端数据 label/value 是反转的,需要前端修正 * @default [] */ swapLabelValueDicts?: string[]; /** * 是否自动将纯数字字符串转换为 number 类型(可选) * @description 如 "0" → 0, "1" → 1,避免类型不匹配 * @default true */ autoConvertNumericValue?: boolean; /** * Pinia Store ID(可选) * @description 自定义 Store 的 ID,避免多实例冲突 * @default 'dict' */ storeId?: string; } /** * createDictStore 返回的 Store 类型 * @description 包含完整的字典管理能力 */ export interface DictStoreReturn { /** 字典数据存储 */ dictData: Ref; /** 字典加载状态 */ loadingMap: Ref; /** 字典加载错误记录 */ errorMap: Ref>; /** 获取指定字典的数据 */ getDictData: (dictField: string) => DictItem[]; /** 检查字典是否正在加载 */ isLoading: (dictField: string) => boolean; /** 检查字典是否已加载 */ isLoaded: (dictField: string) => boolean; /** 获取字典加载错误信息 */ getError: (dictField: string) => string | undefined; /** 加载单个字典 */ loadDict: (dictField: string, force?: boolean) => Promise; /** 批量加载多个字典 */ loadDicts: (dictFields: string[], force?: boolean) => Promise; /** 清除指定字典的缓存 */ clearDict: (dictField?: string) => void; /** 重新加载指定字典 */ reloadDict: (dictField: string) => Promise; /** 根据字典值获取字典标签 */ getDictLabel: (dictField: string, value: string | number) => string; /** 根据字典标签获取字典值 */ getDictValue: (dictField: string, label: string) => string | number | undefined; /** 检查字段是否有字典映射 */ hasDict: (field: string) => boolean; }