import { defineStore } from 'pinia' import { ref, computed } from 'vue' import { pluginService } from '../services/pluginService' /** * Plugins Store * 管理插件列表和状态 */ export const usePluginsStore = defineStore('plugins', () => { // ==================== State ==================== const plugins = ref([]) const loading = ref(false) // ==================== Getters ==================== const enabledCount = computed(() => plugins.value.filter(p => p.enabled).length) const totalCount = computed(() => plugins.value.length) const isEmpty = computed(() => !loading.value && plugins.value.length === 0) // ==================== Actions ==================== /** * 获取插件列表 * @returns {Promise} */ async function fetchPlugins() { loading.value = true try { const response = await pluginService.getPlugins() if (response.code === 200) { plugins.value = response.data.plugins || [] return true } } catch (error) { console.error('获取插件列表失败:', error) } finally { loading.value = false } return false } /** * 切换插件启用状态 * @param {string|number} pluginId * @param {boolean} enabled * @returns {Promise} */ async function togglePlugin(pluginId, enabled) { try { const response = await pluginService.togglePlugin(pluginId, enabled) if (response.code === 200) { const plugin = plugins.value.find(p => p.id === pluginId) if (plugin) { plugin.enabled = enabled } return true } } catch (error) { console.error('切换插件状态失败:', error) } return false } /** * 触发插件爬取 * @param {string|number} pluginId * @returns {Promise} */ async function crawlPlugin(pluginId) { try { const response = await pluginService.crawlPlugin(pluginId) return response.code === 200 } catch (error) { console.error('触发插件爬取失败:', error) return false } } /** * 根据 ID 获取插件 * @param {string|number} id * @returns {object|undefined} */ function getPluginById(id) { return plugins.value.find(p => p.id === id) } /** * 重置状态 */ function reset() { plugins.value = [] } return { // State plugins, loading, // Getters enabledCount, totalCount, isEmpty, // Actions fetchPlugins, togglePlugin, crawlPlugin, getPluginById, reset } })