|
@@ -2,69 +2,161 @@ import { request } from './index';
|
|
|
import $storage from '@/sheep/utils/storage';
|
|
|
import { ElMessage } from 'element-plus';
|
|
|
|
|
|
-// 检测是否为mall前缀的接口
|
|
|
-const isMallAPI = (url) => {
|
|
|
- return url.startsWith('/mall') || url.startsWith('mall');
|
|
|
+// CRUD 配置常量
|
|
|
+const CRUD_VERSIONS = {
|
|
|
+ V1: 'v1', // 旧版本 CRUD
|
|
|
+ V2: 'v2', // 新版本 CRUD
|
|
|
};
|
|
|
|
|
|
-// 查看列表
|
|
|
-export const LIST = (url, data) => {
|
|
|
- const requestConfig = {
|
|
|
- url: url + `/list`,
|
|
|
- method: 'POST',
|
|
|
- };
|
|
|
- requestConfig.data = data;
|
|
|
- return request(requestConfig);
|
|
|
+// 默认 CRUD 配置
|
|
|
+const DEFAULT_CRUD_CONFIG = {
|
|
|
+ version: CRUD_VERSIONS.V1,
|
|
|
+ endpoints: {
|
|
|
+ list: '/list',
|
|
|
+ detail: '/detail',
|
|
|
+ add: '/add',
|
|
|
+ edit: '/update',
|
|
|
+ delete: '/delete',
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ list: 'POST',
|
|
|
+ detail: 'POST',
|
|
|
+ add: 'POST',
|
|
|
+ edit: 'POST',
|
|
|
+ delete: 'POST',
|
|
|
+ },
|
|
|
};
|
|
|
|
|
|
-// 查看详情
|
|
|
-export const DETAIL = (url, id) => {
|
|
|
- const isMall = isMallAPI(url);
|
|
|
+// 新版本 CRUD 配置
|
|
|
+const V2_CRUD_CONFIG = {
|
|
|
+ version: CRUD_VERSIONS.V2,
|
|
|
+ endpoints: {
|
|
|
+ list: '/page',
|
|
|
+ detail: '/info',
|
|
|
+ add: '/save',
|
|
|
+ edit: '/update',
|
|
|
+ delete: '/delete',
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ list: 'GET',
|
|
|
+ detail: 'GET',
|
|
|
+ add: 'POST',
|
|
|
+ edit: 'PATCH',
|
|
|
+ delete: 'DELETE',
|
|
|
+ },
|
|
|
+};
|
|
|
+
|
|
|
+// 获取 CRUD 配置
|
|
|
+const getCrudConfig = (options = {}) => {
|
|
|
+ const { version = CRUD_VERSIONS.V1 } = options;
|
|
|
+
|
|
|
+ if (version === CRUD_VERSIONS.V2) {
|
|
|
+ return V2_CRUD_CONFIG;
|
|
|
+ }
|
|
|
+
|
|
|
+ return DEFAULT_CRUD_CONFIG;
|
|
|
+};
|
|
|
+
|
|
|
+// 查看列表
|
|
|
+export const LIST = (url, data, options = {}) => {
|
|
|
+ const config = getCrudConfig(options);
|
|
|
+ const endpoint = url + config.endpoints.list;
|
|
|
+ const method = config.methods.list;
|
|
|
|
|
|
return request({
|
|
|
- url: url + `/detail`,
|
|
|
- method: isMall ? 'GET' : 'POST', // mall用GET,其他用POST
|
|
|
- params: { id },
|
|
|
+ url: endpoint,
|
|
|
+ method,
|
|
|
+ ...(method === 'GET' ? { params: data } : { data }),
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+// 查看详情
|
|
|
+export const DETAIL = (url, id, options = {}) => {
|
|
|
+ const config = getCrudConfig(options);
|
|
|
+ const method = config.methods.detail;
|
|
|
+
|
|
|
+ if (config.version === CRUD_VERSIONS.V2) {
|
|
|
+ // 新版本:使用 RESTful 风格的 URL
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.detail + `/${id}`,
|
|
|
+ method,
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 旧版本:使用参数传递 ID
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.detail,
|
|
|
+ method,
|
|
|
+ ...(method === 'GET' ? { params: { id } } : { data: { id } }),
|
|
|
+ });
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
// 新增
|
|
|
-export const ADD = (url, data) =>
|
|
|
- request({
|
|
|
- url: url + '/add',
|
|
|
- method: 'POST',
|
|
|
+export const ADD = (url, data, options = {}) => {
|
|
|
+ const config = getCrudConfig(options);
|
|
|
+
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.add,
|
|
|
+ method: config.methods.add,
|
|
|
data,
|
|
|
options: {
|
|
|
showSuccessMessage: false,
|
|
|
},
|
|
|
});
|
|
|
+};
|
|
|
|
|
|
// 编辑&更新
|
|
|
-export const EDIT = (url, data) => {
|
|
|
- const isMall = isMallAPI(url);
|
|
|
+export const EDIT = (url, id, data, options = {}) => {
|
|
|
+ const config = getCrudConfig(options);
|
|
|
|
|
|
- return request({
|
|
|
- url: url + `/update`,
|
|
|
- method: isMall ? 'PUT' : 'POST', // mall用PUT,其他用POST
|
|
|
- data,
|
|
|
- options: {
|
|
|
- showSuccessMessage: false,
|
|
|
- },
|
|
|
- });
|
|
|
+ if (config.version === CRUD_VERSIONS.V2) {
|
|
|
+ // 新版本:使用 RESTful 风格的 URL
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.edit + `/${id}`,
|
|
|
+ method: config.methods.edit,
|
|
|
+ data,
|
|
|
+ options: {
|
|
|
+ showSuccessMessage: false,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 旧版本:将 ID 包含在数据中
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.edit,
|
|
|
+ method: config.methods.edit,
|
|
|
+ data: { id, ...data },
|
|
|
+ options: {
|
|
|
+ showSuccessMessage: false,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
// 删除(软删除/真实删除)
|
|
|
-export const DELETE = (url, data) => {
|
|
|
- const isMall = isMallAPI(url);
|
|
|
+export const DELETE = (url, id, options = {}) => {
|
|
|
+ const config = getCrudConfig(options);
|
|
|
+ const method = config.methods.delete;
|
|
|
|
|
|
- return request({
|
|
|
- url: url + `/delete`,
|
|
|
- method: isMall ? 'DELETE' : 'POST', // mall用DELETE,其他用POST
|
|
|
- params: data,
|
|
|
- options: {
|
|
|
- showSuccessMessage: true,
|
|
|
- },
|
|
|
- });
|
|
|
+ if (config.version === CRUD_VERSIONS.V2) {
|
|
|
+ // 新版本:使用 RESTful 风格的 URL
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.delete + `/${id}`,
|
|
|
+ method,
|
|
|
+ options: {
|
|
|
+ showSuccessMessage: true,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ // 旧版本:使用参数传递 ID
|
|
|
+ return request({
|
|
|
+ url: url + config.endpoints.delete,
|
|
|
+ method,
|
|
|
+ ...(method === 'GET' ? { params: { id } } : { data: { id } }),
|
|
|
+ options: {
|
|
|
+ showSuccessMessage: true,
|
|
|
+ },
|
|
|
+ });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
// 选择
|
|
@@ -104,12 +196,14 @@ export const CRUD = (
|
|
|
options = {},
|
|
|
) => {
|
|
|
const apis = {};
|
|
|
- if (methods.includes('list'))
|
|
|
- apis.list = (params, pageInParams = true) => LIST(url, params, pageInParams);
|
|
|
- if (methods.includes('detail')) apis.detail = (id) => DETAIL(url, id);
|
|
|
- if (methods.includes('add')) apis.add = (data) => ADD(url, data);
|
|
|
- if (methods.includes('edit')) apis.edit = (id, data) => EDIT(url, id, data);
|
|
|
- if (methods.includes('delete')) apis.delete = (id) => DELETE(url, id);
|
|
|
+ const crudOptions = { version: options.version || CRUD_VERSIONS.V1 };
|
|
|
+
|
|
|
+ if (methods.includes('list')) apis.list = (params) => LIST(url, params, crudOptions);
|
|
|
+ if (methods.includes('detail')) apis.detail = (id) => DETAIL(url, id, crudOptions);
|
|
|
+ if (methods.includes('add')) apis.add = (data) => ADD(url, data, crudOptions);
|
|
|
+ if (methods.includes('edit')) apis.edit = (id, data) => EDIT(url, id, data, crudOptions);
|
|
|
+ if (methods.includes('delete')) apis.delete = (id) => DELETE(url, id, crudOptions);
|
|
|
+
|
|
|
if (methods.includes('report'))
|
|
|
apis.report = (params, filename) =>
|
|
|
REPORT(url, params, filename, 'report', options.reportMethod);
|
|
@@ -119,6 +213,15 @@ export const CRUD = (
|
|
|
return apis;
|
|
|
};
|
|
|
|
|
|
+// 新版本 CRUD 的便捷函数
|
|
|
+export const CRUD_V2 = (
|
|
|
+ url,
|
|
|
+ methods = ['list', 'detail', 'add', 'edit', 'delete', 'export'],
|
|
|
+ options = {},
|
|
|
+) => {
|
|
|
+ return CRUD(url, methods, { ...options, version: CRUD_VERSIONS.V2 });
|
|
|
+};
|
|
|
+
|
|
|
// 通用回收站
|
|
|
export const RECYCLE = (url) => {
|
|
|
return {
|
|
@@ -246,4 +349,26 @@ export const REPORT = async (
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-// add, list, delete, edit, detail, select, recyclebin, restore, destroy, report
|
|
|
+/**
|
|
|
+ * CRUD 使用说明:
|
|
|
+ *
|
|
|
+ * 1. 旧版本 CRUD(默认):
|
|
|
+ * ...CRUD('shop/admin/user', ['list', 'detail', 'add', 'edit', 'delete'])
|
|
|
+ *
|
|
|
+ * 2. 新版本 CRUD(通过参数指定):
|
|
|
+ * ...CRUD('mall/notice', ['list', 'detail', 'add', 'edit', 'delete'], { version: 'v2' })
|
|
|
+ *
|
|
|
+ * 3. 新版本 CRUD(便捷函数):
|
|
|
+ * ...CRUD_V2('mall/notice', ['list', 'detail', 'add', 'edit', 'delete'])
|
|
|
+ *
|
|
|
+ * 接口映射对照:
|
|
|
+ * 操作 | 旧版本(V1) | 新版本(V2)
|
|
|
+ * ------ | ---------------- | ------------------
|
|
|
+ * 列表 | POST /list | GET /page
|
|
|
+ * 详情 | POST /detail | GET /info/{id}
|
|
|
+ * 新增 | POST /add | POST /save
|
|
|
+ * 更新 | POST /update | PATCH /update/{id}
|
|
|
+ * 删除 | POST /delete | DELETE /delete/{id}
|
|
|
+ */
|
|
|
+
|
|
|
+// 支持的方法: add, list, delete, edit, detail, select, recyclebin, restore, destroy, report
|