import baseHttp from "@/service/base"; import { Pages } from "@/util/model/interface"; 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 } 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 baseHttp.delete(this.urlConfig.DELETE + "/" + id, {}) this.getlist() } catch (error) { console.log(error); } } // 分页 setPages(page: Pages) { this.page = page this.getlist() } // 添加 async add(param: any,listParam?:any) { try { await baseHttp.post(this.urlConfig.ADD, param) this.getlist(listParam) } catch (error) { console.log(error); } } // 更新 async putItem(id: string, param: any,listParam?:any) { try { await baseHttp.put(this.urlConfig.EDIT + "/" + id, param) this.getlist(listParam) } catch (error) { } } // 获取列表 async getlist(params?: any) { this.listStatus = true; try { let res = await baseHttp.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.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; console.log(error); } } list!: Array; item!: B | null | string total!: number; page!: Pages; listStatus: boolean | null | undefined; } export default BaseStore