Files
sionrui/frontend/app/web-gold/src/stores/pointsConfig.js
2026-02-25 21:30:24 +08:00

163 lines
4.0 KiB
JavaScript

/**
* 积分配置 Store
* 管理AI模型积分消耗配置
*/
import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { getJSON, setJSON } from '@/utils/storage'
import ModelConfigService from '@/api/modelConfig'
const STORAGE_KEY = 'points_config_v1'
const CACHE_DURATION = 30 * 60 * 1000 // 30分钟缓存
export const usePointsConfigStore = defineStore('pointsConfig', () => {
// 模型配置映射(按平台分组)
const configMap = ref({})
// 是否已加载
const isLoaded = ref(false)
// 上次加载时间
const lastLoadTime = ref(0)
// 是否正在加载
const isLoading = ref(false)
// 所有平台列表
const platforms = computed(() => Object.keys(configMap.value))
// 获取所有模型列表(扁平化)
const allModels = computed(() => {
const models = []
for (const [platform, list] of Object.entries(configMap.value)) {
if (Array.isArray(list)) {
list.forEach(model => {
models.push({ ...model, platform })
})
}
}
return models
})
/**
* 根据模型代码获取积分消耗
* @param {string} modelCode - 模型代码
* @returns {number|null} 积分消耗
*/
const getConsumePoints = (modelCode) => {
if (!modelCode) return null
return ModelConfigService.getConsumePoints(configMap.value, modelCode)
}
/**
* 根据模型代码获取模型名称
* @param {string} modelCode - 模型代码
* @returns {string|null} 模型名称
*/
const getModelName = (modelCode) => {
if (!modelCode) return null
return ModelConfigService.getModelName(configMap.value, modelCode)
}
/**
* 获取模型完整信息
* @param {string} modelCode - 模型代码
* @returns {Object|null} 模型信息 { modelCode, modelName, consumePoints, platform }
*/
const getModelInfo = (modelCode) => {
if (!modelCode) return null
for (const [platform, list] of Object.entries(configMap.value)) {
if (Array.isArray(list)) {
const model = list.find(m => m.modelCode === modelCode)
if (model) {
return { ...model, platform }
}
}
}
return null
}
/**
* 从本地存储恢复
*/
const hydrateFromStorage = async () => {
const saved = await getJSON(STORAGE_KEY)
if (saved && saved.configMap) {
configMap.value = saved.configMap
lastLoadTime.value = saved.lastLoadTime || 0
isLoaded.value = true
}
}
/**
* 持久化到本地存储
*/
const persistToStorage = async () => {
await setJSON(STORAGE_KEY, {
configMap: configMap.value,
lastLoadTime: lastLoadTime.value,
})
}
/**
* 加载模型配置(从服务器)
* @param {boolean} force - 是否强制刷新
*/
const loadConfig = async (force = false) => {
// 检查缓存是否有效
const now = Date.now()
if (!force && isLoaded.value && (now - lastLoadTime.value) < CACHE_DURATION) {
return configMap.value
}
// 防止重复加载
if (isLoading.value) {
return configMap.value
}
isLoading.value = true
try {
const data = await ModelConfigService.getEnabledModelConfigList()
configMap.value = data || {}
lastLoadTime.value = now
isLoaded.value = true
await persistToStorage()
return configMap.value
} catch (error) {
console.error('[pointsConfig] 加载模型配置失败:', error)
throw error
} finally {
isLoading.value = false
}
}
/**
* 格式化积分显示
* @param {number} points - 积分数
* @returns {string} 格式化后的字符串
*/
const formatPoints = (points) => {
if (points === null || points === undefined) return ''
if (points === 0) return '免费'
return `${points}积分`
}
// 初始化时从本地恢复
hydrateFromStorage()
return {
// 状态
configMap,
isLoaded,
isLoading,
platforms,
allModels,
// 方法
getConsumePoints,
getModelName,
getModelInfo,
loadConfig,
formatPoints,
}
})
export default usePointsConfigStore