98 lines
2.6 KiB
TypeScript
98 lines
2.6 KiB
TypeScript
import baseHttp from "@/service/base";
|
|
import { Pages } from "@/util/model/interface";
|
|
import { action, makeObservable, observable, runInAction } from "mobx";
|
|
interface BaseStoreInterface<T> {
|
|
list: Array<T>
|
|
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<B> implements BaseStoreInterface<B> {
|
|
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) {
|
|
await baseHttp.delete(this.urlConfig.DELETE + "/" + id, {})
|
|
this.getlist()
|
|
}
|
|
// 分页
|
|
setPages(page: Pages) {
|
|
this.page = page
|
|
this.getlist()
|
|
}
|
|
// 添加
|
|
async add(param: any) {
|
|
await baseHttp.post(this.urlConfig.ADD, param)
|
|
this.getlist();
|
|
}
|
|
|
|
// 更新
|
|
async putItem(id: string, param: any) {
|
|
await baseHttp.put(this.urlConfig.EDIT + "/" + id, param)
|
|
this.getlist()
|
|
}
|
|
|
|
// 获取列表
|
|
async getlist() {
|
|
this.listStatus = true;
|
|
let res = await baseHttp.get(this.urlConfig.LIST, {
|
|
size: this.page?.Size | 1,
|
|
offset: this.page?.Offset | 20,
|
|
isSelf:0
|
|
});
|
|
|
|
this.page = {
|
|
Offset: res.data?.pageNum,
|
|
Size: res.data?.pageSize
|
|
}
|
|
let data: Array<B> = []
|
|
if (!res.data.list) {
|
|
runInAction(() => {
|
|
this.list = data;
|
|
})
|
|
this.listStatus = false;
|
|
return;
|
|
}
|
|
for (let i = 0; i < res.data.list.length; i++) {
|
|
data.push({
|
|
key: res.data.list[i].id,
|
|
...res.data.list[i]
|
|
})
|
|
}
|
|
|
|
runInAction(() => {
|
|
this.list = data;
|
|
this.total = res.data.total | res.data.totals
|
|
})
|
|
this.listStatus = false;
|
|
}
|
|
list!: Array<B>;
|
|
item!: B | null | string
|
|
total!: number;
|
|
page!: Pages;
|
|
listStatus: boolean | null | undefined;
|
|
}
|
|
|
|
export default BaseStore |