import { create } from 'zustand'; import { query } from '@/modules'; import { isObjectNull } from '@/utils/is-null'; import { message } from '@/modules/message'; type AppVersionStore = { showEdit: boolean; setShowEdit: (showEdit: boolean) => void; formData: any; setFormData: (formData: any) => void; updateByFromData: () => void; loading: boolean; setLoading: (loading: boolean) => void; key: string; setKey: (key: string) => void; list: any[]; getList: () => Promise; app: any; getApp: (key: string, force?: boolean) => Promise; updateData: (data: any) => Promise; /** * 删除应用版本 * @param id 应用版本id * @param deleteFile 是否删除文件 * @returns */ deleteData: (id: string, deleteFile?: boolean) => Promise; publishVersion: (data: { id?: string; appKey?: string; version?: string }, opts?: { showToast?: boolean }) => Promise; detectVersionList: (data: { appKey: string; version: string }) => Promise; }; export const useAppVersionStore = create((set, get) => { return { showEdit: false, setShowEdit: (showEdit) => set({ showEdit }), formData: {}, setFormData: (formData) => set({ formData }), updateByFromData: () => { const { formData, list } = get(); const data = list.map((item) => { if (item.id === formData.id) { return formData; } return item; }); set({ list: data }); }, loading: false, setLoading: (loading) => set({ loading }), key: '', setKey: (key) => set({ key }), list: [], getList: async () => { set({ loading: true }); const key = get().key; const res = await query.post({ path: 'app', key: 'list', data: { key, }, }); get().getApp(key, true); set({ loading: false }); if (res.code === 200) { set({ list: res.data }); } else { message.error(res.message || 'Request failed'); } }, app: {}, getApp: async (key, force) => { const { app } = get(); if (!force && !isObjectNull(app)) { return; } const res = await query.post({ path: 'user-app', key: 'get', data: { key, }, }); if (res.code === 200) { set({ app: res.data }); } else { message.error(res.message || 'Request failed'); } }, updateData: async (data) => { const { getList } = get(); const res = await query.post({ path: 'app', key: 'update', data, }); if (res.code === 200) { message.success('Success'); set({ showEdit: false, formData: res.data }); getList(); } else { message.error(res.message || 'Request failed'); } }, deleteData: async (id, deleteFile = false) => { const { getList } = get(); const res = await query.post({ path: 'app', key: 'delete', payload: { id, deleteFile, }, }); if (res.code === 200) { getList(); message.success('Success'); } else { message.error(res.message || 'Request failed'); } }, publishVersion: async (data, opts) => { const showToast = opts?.showToast ?? true; const res = await query.post({ path: 'app', key: 'publish', data, }); if (res.code === 200) { if (showToast) { message.success('Success'); get().getApp(get().key, true); } } else { if (showToast) { message.error(res.message || 'Request failed'); } } return res; }, detectVersionList: async (data) => { const res = await query.post({ path: 'app', key: 'detectVersionList', data, }); return res; }, }; });