import { defineStore } from 'pinia' import { ref } from 'vue' import * as configApi from '@/api/config' import type { DemandType, Brand, Platform, Category } from '@/api/config' /** * 全局配置数据Store * 用于管理需求类型、品牌、平台、类目等配置数据 * 确保数据在整个项目中同步 */ export const useConfigStore = defineStore('config', () => { // 需求类型(只存储启用的,用于下拉选择) const demandTypes = ref([]) // 品牌(只存储启用的) const brands = ref([]) // 平台(只存储启用的) const platforms = ref([]) // 类目(只存储叶子类目,用于下拉选择) const categories = ref([]) /** * 加载启用的需求类型 */ const loadDemandTypes = async () => { try { const response = await configApi.getEnabledDemandTypes() if (response && response.data) { demandTypes.value = response.data } } catch (error) { console.error('加载需求类型失败:', error) } } /** * 加载启用的品牌 */ const loadBrands = async () => { try { const response = await configApi.getEnabledBrands() if (response && response.data) { brands.value = response.data } } catch (error) { console.error('加载品牌失败:', error) } } /** * 加载启用的平台 */ const loadPlatforms = async () => { try { const response = await configApi.getEnabledPlatforms() if (response && response.data) { platforms.value = response.data } } catch (error) { console.error('加载平台失败:', error) } } /** * 加载启用的叶子类目 */ const loadCategories = async () => { try { const response = await configApi.getEnabledLeafCategories() if (response && response.data) { categories.value = response.data } } catch (error) { console.error('加载类目失败:', error) } } /** * 加载所有配置数据 */ const loadAllConfigs = async () => { await Promise.all([ loadDemandTypes(), loadBrands(), loadPlatforms(), loadCategories() ]) } /** * 刷新配置数据(当系统设置页面更新后调用) */ const refreshConfigs = async (type?: 'demandType' | 'brand' | 'platform' | 'category') => { switch (type) { case 'demandType': await loadDemandTypes() break case 'brand': await loadBrands() break case 'platform': await loadPlatforms() break case 'category': await loadCategories() break default: await loadAllConfigs() } // 触发自定义事件,通知其他组件更新 window.dispatchEvent(new CustomEvent('configUpdated', { detail: { type } })) } return { demandTypes, brands, platforms, categories, loadDemandTypes, loadBrands, loadPlatforms, loadCategories, loadAllConfigs, refreshConfigs } })