import { base } from "@/service/base"; import { Pages } from "@/util/model/interface"; import { message } from "antd"; import { action, makeObservable, observable, runInAction } from "mobx"; interface BaseStoreInterface { list: Array item: T | null | string getlist(url: string): any deleteItem(id: number): any putItem(url: string, param: any): any add(url: string, param: any): any total: number page: Pages setPages(pages: Pages): any listStatus: boolean | null | undefined isRefresh: boolean } class BaseStore implements BaseStoreInterface { urlConfig: { DELETE: string; ADD: string; EDIT: string; LIST: string; } constructor(urlConfig) { makeObservable(this, { getlist: action, deleteItem: action, putItem: action, setPages: action, list: observable, item: observable, total: observable, page: observable, add: action, listStatus: observable, }) this.urlConfig = urlConfig; } // 删除 async deleteItem(id: number) { try { await base.delete(this.urlConfig.DELETE + "/" + id, {}) this.getlist() } catch (error) { console.error(error); } } // 分页 setPages(page: Pages) { this.page = page this.getlist() } // 添加 async add(param: any, listParam?: any) { try { let res = await base.post(this.urlConfig.ADD, param) if (res.code !== 200) { message.error(res.msg) return false } this.isRefresh = true this.getlist(listParam) return true; } catch (error) { console.error(error); } } // 更新 async putItem(id: string, param: any, listParam?: any) { try { let res = await base.put(this.urlConfig.EDIT + "/" + id, param) if (res.code !== 200) { message.error(res.msg) return false } this.isRefresh = true this.getlist(listParam) return true; } catch (error) { console.error(error); } } // 获取列表 async getlist(params?: any) { this.listStatus = true; try { let res = await base.get(this.urlConfig.LIST, { size: this.page?.Size ?? 20, offset: this.page?.Offset ?? 1, ...params }); let data: Array = [] if (!res?.data?.record) { runInAction(() => { this.list = data; this.total = res.data.count }) this.listStatus = false; return; } for (let i = 0; i < res.data.record.length; i++) { data.push({ key: res.data.record[i].id, ...res.data.record[i] }) } runInAction(() => { this.list = data; this.total = res.data.count }) this.listStatus = false; } catch (error) { this.listStatus = false; } } list!: Array; item!: B | null | string total!: number; page!: Pages; listStatus: boolean | null | undefined; isRefresh!: boolean; } export default BaseStore