// ============================================================================= // 企微IT智能服务台 — 坐席管理状态管理(Pinia Store) // ============================================================================= // 说明:管理后台坐席管理数据,支持列表查询、筛选、增删改 import { defineStore } from 'pinia' import { ref, computed } from 'vue' import type { Agent, AgentFilterStatus, CreateAgentRequest, UpdateAgentRequest } from '@/types' import { getAgents as apiGetAgents, createAgent as apiCreateAgent, updateAgent as apiUpdateAgent, deleteAgent as apiDeleteAgent } from '@/api/admin' // -------------------------------------------------------------------------- // Store 定义 // -------------------------------------------------------------------------- export const useAgentStore = defineStore('agent', () => { // ========================================================================== // 响应式状态 // ========================================================================== /** 坐席列表 */ const agents = ref([]) /** 当前筛选状态 */ const filterStatus = ref('all') /** 是否正在加载 */ const loading = ref(false) /** 总坐席数 */ const totalCount = computed(() => agents.value.length) // ========================================================================== // 计算属性 // ========================================================================== /** 按状态筛选后的坐席列表 */ const filteredAgents = computed(() => { if (filterStatus.value === 'all') { return agents.value } return agents.value.filter((a) => a.status === filterStatus.value) }) /** 各状态计数 */ const statusCounts = computed(() => { const counts: Record = { all: agents.value.length, online: 0, busy: 0, offline: 0, } for (const a of agents.value) { if (a.status === 'online') counts.online++ else if (a.status === 'busy') counts.busy++ else counts.offline++ } return counts }) // ========================================================================== // 方法 // ========================================================================== /** * 加载坐席列表 * @param status - 可选筛选状态 */ async function loadAgents(status?: string): Promise { loading.value = true try { const response = await apiGetAgents(status) agents.value = response.items } catch (error) { console.error('加载坐席列表失败:', error) } finally { loading.value = false } } /** * 设置筛选状态并重新加载 * @param status - 筛选状态 */ function setFilter(status: AgentFilterStatus): void { filterStatus.value = status } /** * 添加坐席 * @param data - 坐席创建数据 */ async function addAgent(data: CreateAgentRequest): Promise { try { await apiCreateAgent(data) // 重新加载列表 await loadAgents() return true } catch (error) { console.error('添加坐席失败:', error) return false } } /** * 编辑坐席 * @param id - 坐席ID * @param data - 坐席更新数据 */ async function editAgent(id: string, data: UpdateAgentRequest): Promise { try { await apiUpdateAgent(id, data) // 重新加载列表 await loadAgents() return true } catch (error) { console.error('编辑坐席失败:', error) return false } } /** * 移除坐席 * @param id - 坐席ID */ async function removeAgent(id: string): Promise { try { await apiDeleteAgent(id) // 重新加载列表 await loadAgents() return true } catch (error) { console.error('移除坐席失败:', error) return false } } // ========================================================================== // 返回 // ========================================================================== return { // 状态 agents, filterStatus, loading, // 计算属性 filteredAgents, statusCounts, totalCount, // 方法 loadAgents, setFilter, addAgent, editAgent, removeAgent, } })