You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
3.8 KiB
137 lines
3.8 KiB
import { request, AxiosRequestConfig } from '@/request.ts'
|
|
import { IApiResult, IPage, IPageResult } from '@/global'
|
|
|
|
|
|
export const createCURD = <TParams, TResult>(api: string, options?: AxiosRequestConfig) => {
|
|
|
|
return {
|
|
list: (params?: TParams & IPage) => {
|
|
return request.post<IPageResult<TResult>>(`${api}/list`, { ...params }, options)
|
|
},
|
|
add: (data: TParams) => {
|
|
return request.post<TResult>(`${api}/add`, { ...data }, options)
|
|
},
|
|
update: (data: TParams) => {
|
|
return request.post<TResult>(`${api}/edit`, { ...data }, options)
|
|
},
|
|
delete: (id: number) => {
|
|
return request.post<TResult>(`${api}/delete`, { id }, options)
|
|
},
|
|
batchDelete: (ids: number[]) => {
|
|
return request.post<TResult>(`${api}/deletes`, { ids }, options)
|
|
},
|
|
info: (id: number) => {
|
|
return request.post<TResult>(`${api}/get`, { id }, options)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
// 模拟的数据结果
|
|
|
|
export const createMockCURD = <TParams, TResult>(api: string, options?: AxiosRequestConfig) => {
|
|
console.log(api, options)
|
|
return {
|
|
list: (params?: TParams & IPage): Promise<IApiResult<IPageResult<TResult>>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
rows: [
|
|
{
|
|
id: 1,
|
|
name: 'Item 1',
|
|
description: 'Description 1',
|
|
} as unknown as TResult,
|
|
{
|
|
id: 2,
|
|
name: 'Item 2',
|
|
description: 'Description 2',
|
|
} as unknown as TResult,
|
|
],
|
|
total: 2,
|
|
pageSize: params?.pageSize || 10,
|
|
page: params?.page || 1,
|
|
},
|
|
message: 'success',
|
|
})
|
|
}, 500) // 模拟网络延迟
|
|
})
|
|
},
|
|
|
|
add: (data: TParams): Promise<IApiResult<TResult>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
...data,
|
|
id: Math.floor(Math.random() * 1000) + 1, // 随机生成一个ID
|
|
} as unknown as TResult,
|
|
message: 'Item added successfully',
|
|
})
|
|
}, 500)
|
|
})
|
|
},
|
|
|
|
update: (data: TParams): Promise<IApiResult<TResult>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
...data,
|
|
updatedAt: new Date().toISOString(), // 更新的时间戳
|
|
} as unknown as TResult,
|
|
message: 'Item updated successfully',
|
|
})
|
|
}, 500)
|
|
})
|
|
},
|
|
|
|
delete: (id: number): Promise<IApiResult<TResult>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
success: true,
|
|
} as unknown as TResult,
|
|
message: `Item with id ${id} deleted successfully.`,
|
|
})
|
|
}, 500)
|
|
})
|
|
},
|
|
|
|
batchDelete: (ids: number[]): Promise<IApiResult<TResult>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
success: true,
|
|
} as unknown as TResult,
|
|
message: `Items with ids ${ids.join(', ')} deleted successfully.`,
|
|
})
|
|
}, 500)
|
|
})
|
|
},
|
|
|
|
info: (id: number): Promise<IApiResult<TResult>> => {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve({
|
|
code: 200,
|
|
data: {
|
|
id: id,
|
|
name: `Item ${id}`,
|
|
description: `Description for item ${id}`,
|
|
} as unknown as TResult,
|
|
message: 'Item info retrieved successfully',
|
|
})
|
|
}, 500)
|
|
})
|
|
},
|
|
}
|
|
}
|