代码修改
This commit is contained in:
1 parent
8f5af46ec2
commit
8f75e12871
168 files changed
+76575
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import service from "@/utils/request";
|
||||
export const requestCrudService = {
|
||||
get: async (endpoint, params = {}) => {
|
||||
try {
|
||||
const response = await service.get(endpoint, { params });
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`GET request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
post: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.post(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`POST request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
put: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.put(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`PUT request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
update: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.patch(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`patch request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
delete: async (endpoint) => {
|
||||
try {
|
||||
const response = await service.delete(endpoint);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`DELETE request failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import toolsService from "@/utils/tools-request";
|
||||
const service = toolsService;
|
||||
export const toolsCrudService = {
|
||||
get: async (endpoint, params = {}) => {
|
||||
try {
|
||||
const response = await service.get(endpoint, { params });
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`GET request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
post: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.post(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`POST request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
put: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.put(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`PUT request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
patch: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.patch(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`PATCH request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
update: async (endpoint, data = {}) => {
|
||||
try {
|
||||
const response = await service.patch(endpoint, data);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`patch request failed: ${error.message}`);
|
||||
}
|
||||
},
|
||||
|
||||
delete: async (endpoint) => {
|
||||
try {
|
||||
const response = await service.delete(endpoint);
|
||||
return response;
|
||||
} catch (error) {
|
||||
throw new Error(`DELETE request failed: ${error.message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user