142 lines
4.4 KiB
TypeScript
142 lines
4.4 KiB
TypeScript
import { environment } from '@/environments/environment';
|
||
import { requestCrudService } from '@/service/request-axios-service';
|
||
|
||
export interface GetOneParam {
|
||
fields?: string[]; // 指定返回的字段 id,name
|
||
join?: string[]; // 指定关联表,如 ['user||id,name', 'relation||id']
|
||
cache?: 0 | 1; // 0直接DB,1缓存,默认1缓存
|
||
}
|
||
export interface GetAllParam {
|
||
fields?: string[] | string; // 指定返回的字段: 多个字段如 'id', 'name'
|
||
filter?: string[]; // 指定过滤条件,如 ['name||$eq||batman', 'id||$in||1,2,3', 'field||$isnull', 'relation.id||$eq||1']
|
||
or?: string[]; // 同filter
|
||
join?: string[]; // 指定关联表,如 ['user||id,name', 'relation||id']
|
||
cache?: 0 | 1; // 0直接DB,1缓存,默认1缓存
|
||
page?: number; // 页码(会被转换成offset)
|
||
offset?: number; // 偏移量
|
||
limit?: number; // 每页数量
|
||
sort?: string[]; // 排序,如 '['name,ASC', 'id,DESC]'
|
||
s?: string;
|
||
joinTaskAction?: boolean; //是否连行动表查询
|
||
joinTaskActionBoci?: boolean; //策略分析列表使用
|
||
}
|
||
|
||
export interface GetAllResponse {
|
||
data: any[];
|
||
page: number;
|
||
pageCount: number;
|
||
count: number;
|
||
total: number;
|
||
errMsg?: string;
|
||
}
|
||
let deepCopy = (str: any) => {
|
||
return JSON.parse(JSON.stringify(str));
|
||
}
|
||
export class RequestCrudService {
|
||
|
||
/**
|
||
* 获取单个对象
|
||
* @param url 地址
|
||
* @param id ID
|
||
* @param params 参数
|
||
* @returns 单个对象
|
||
*/
|
||
async getOne(url: string, id: number, params?: GetOneParam): Promise<any> {
|
||
const getUrl = `${url}/${id}`;
|
||
const res = await requestCrudService.get(getUrl, params);
|
||
if (res) return res;
|
||
// this.notification.create('error', getUrl, `${res.data}`);
|
||
return {};
|
||
}
|
||
/**
|
||
* 获取多个对象
|
||
* @param url 地址
|
||
* @param params 参数
|
||
* @returns 多个对象数组
|
||
*/
|
||
async getAll(url: string, params: GetAllParam | null = null): Promise<GetAllResponse> {
|
||
const formattedParams = deepCopy(params || {});
|
||
// 分页默认值
|
||
if (!params || !params.limit) formattedParams.limit = 999;
|
||
if (!params || !params.page) formattedParams.page = 1;
|
||
// 计算offset
|
||
if (params && formattedParams.page) {
|
||
formattedParams.offset = (formattedParams.page - 1) * formattedParams.limit!;
|
||
delete formattedParams.page;
|
||
}
|
||
const res = await requestCrudService.get(url, formattedParams);
|
||
if (res.status === 200) return res.data;
|
||
// this.notification.create('error', url, `${res.data}`);
|
||
return {
|
||
count: 0,
|
||
data: [],
|
||
page: 1,
|
||
pageCount: 1,
|
||
total: 0
|
||
};
|
||
}
|
||
/**
|
||
* 创建单个对象
|
||
* @param url 地址
|
||
* @param data 单个对象
|
||
* @returns 被创建的单个对象
|
||
*/
|
||
async createOne(url: string, data: any) {
|
||
const res = await requestCrudService.post(url, data);
|
||
if (res.status === 201) return res.data;
|
||
// this.notification.create('error', url, `${res.data}`);
|
||
return {};
|
||
}
|
||
/**
|
||
* 创建多个对象
|
||
* @param url 地址
|
||
* @param data 多个对象
|
||
* @returns 被创建的多个对象
|
||
*/
|
||
async createMulti(url: string, data: any[]) {
|
||
const res = await requestCrudService.post(url, {
|
||
bulk: data
|
||
});
|
||
if (res.status === 201) return res.data;
|
||
// this.notification.create('error', url, `${res.data}`);
|
||
return [];
|
||
}
|
||
/**
|
||
* 更新单个对象
|
||
* @param url 地址
|
||
* @param id ID
|
||
* @param data 单个对象部分键值对
|
||
* @returns 被修改的单个对象
|
||
*/
|
||
async updateOne(url: string, id: number, data: any) {
|
||
const res = await requestCrudService.update(`${url}/${id}`, data);
|
||
if (res.status === 200) return res.data;
|
||
// this.notification.create('error', url, `${res.data}`);
|
||
return {};
|
||
}
|
||
/**
|
||
* 删除单个对象
|
||
* @param url 地址
|
||
* @param id ID
|
||
* @returns 操作结果
|
||
*/
|
||
async deleteOne(url: string, id: number) {
|
||
const res = await requestCrudService.delete(`${url}/${id}`);
|
||
if (res.status === 200) return res.data;
|
||
// this.notification.create('error', url, `${res.data}`);
|
||
return 'error';
|
||
}
|
||
// get 请求
|
||
async simpleGet(url: string, params?: any): Promise<any> {
|
||
const res = await requestCrudService.get(url, params);
|
||
if (res.status === 200) return res.data;
|
||
return res.data;
|
||
}
|
||
// post 请求
|
||
async simplePost(url: string, data: any) {
|
||
const res = await requestCrudService.post(url, data);
|
||
if (res.status === 200) return res.data;
|
||
return res.data;
|
||
}
|
||
}
|