Files
wecom_it_smart_desk/frontend-admin/src/stores/config.ts
T

125 lines
3.8 KiB
TypeScript
Raw Normal View History

// =============================================================================
// 企微IT智能服务台 — 配置项状态管理(Pinia Store
// =============================================================================
// 说明:管理功能开关/参数配置数据,支持读取、更新、变更历史查询
import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import type { ConfigGroup, ConfigItem } from '@/types'
import { getConfigGroups, updateConfig as apiUpdateConfig, getConfigHistory } from '@/api/admin'
// --------------------------------------------------------------------------
// Store 定义
// --------------------------------------------------------------------------
export const useConfigStore = defineStore('config', () => {
// ==========================================================================
// 响应式状态
// ==========================================================================
/** 配置分组列表 */
const groups = ref<ConfigGroup[]>([])
/** 是否正在加载 */
const loading = ref<boolean>(false)
/** 当前查看变更历史的配置项 key */
const historyKey = ref<string>('')
/** 变更历史列表 */
const historyItems = ref<{ id: string; config_key: string; old_value: string; new_value: string; changed_by: string; changed_by_name: string; changed_at: string }[]>([])
// ==========================================================================
// 计算属性
// ==========================================================================
/** 所有配置项扁平列表 */
const allConfigs = computed<ConfigItem[]>(() => {
return groups.value.flatMap((g) => g.items)
})
/** 按 key 获取配置项 */
function getConfigByKey(key: string): ConfigItem | undefined {
return allConfigs.value.find((item) => item.key === key)
}
// ==========================================================================
// 方法
// ==========================================================================
/**
* 加载全部配置分组
*/
async function loadConfigs(): Promise<void> {
loading.value = true
try {
const response = await getConfigGroups()
groups.value = response.data.data.groups
} catch (error) {
console.error('加载配置失败:', error)
} finally {
loading.value = false
}
}
/**
* 更新单个配置项
* @param key - 配置键
* @param value - 新值
*/
async function updateConfigValue(key: string, value: string): Promise<boolean> {
try {
const response = await apiUpdateConfig(key, value)
const result = response.data.data
// 更新本地缓存中的值
for (const group of groups.value) {
const item = group.items.find((i) => i.key === key)
if (item) {
item.value = value
break
}
}
return true
} catch (error) {
console.error('更新配置失败:', error)
return false
}
}
/**
* 加载指定配置项的变更历史
* @param key - 配置键
* @param limit - 最大返回条数
*/
async function loadHistory(key: string, limit: number = 20): Promise<void> {
historyKey.value = key
try {
const response = await getConfigHistory(key, limit)
historyItems.value = response.data.data.items
} catch (error) {
console.error('加载变更历史失败:', error)
}
}
// ==========================================================================
// 返回
// ==========================================================================
return {
// 状态
groups,
loading,
historyKey,
historyItems,
// 计算属性
allConfigs,
// 方法
getConfigByKey,
loadConfigs,
updateConfigValue,
loadHistory,
}
})