crud-page-react 0.0.6 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -812,13 +812,6 @@ function DynamicForm({ schema, mode, visible, initialValues, onSubmit, onCancel,
812
812
  }
813
813
 
814
814
  const { Title } = antd.Typography;
815
- /** 动态替换 URL 模板中的占位符 */
816
- function buildUrl(template, record) {
817
- return template.replace(/:(\w+)/g, (match, fieldName) => {
818
- const value = record[fieldName];
819
- return value !== undefined ? String(value) : match;
820
- });
821
- }
822
815
  /** 处理模板数据,支持 {{fieldName}} 格式的变量替换 */
823
816
  function processTemplateData(data, record) {
824
817
  const result = {};
@@ -889,14 +882,34 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
889
882
  initializeData();
890
883
  return;
891
884
  }
885
+ const listApiConfig = schema.api.list;
892
886
  setLoading(true);
893
887
  try {
888
+ // 构建查询参数
894
889
  const query = new URLSearchParams({ page: String(p), pageSize: String(ps) });
895
890
  Object.entries(params).forEach(([k, v]) => {
896
891
  if (v !== undefined && v !== null && v !== '')
897
892
  query.set(k, String(v));
898
893
  });
899
- const json = await request(`${schema.api.list}?${query}`);
894
+ // 构建请求选项
895
+ const options = {
896
+ method: listApiConfig.method || 'GET',
897
+ headers: Object.assign({ 'Content-Type': 'application/json' }, listApiConfig.headers)
898
+ };
899
+ // 如果是 POST 请求,将查询参数放到请求体中
900
+ let url = listApiConfig.url;
901
+ if (listApiConfig.method === 'POST') {
902
+ const queryParams = {};
903
+ query.forEach((value, key) => {
904
+ queryParams[key] = value;
905
+ });
906
+ const requestData = Object.assign(Object.assign({}, queryParams), listApiConfig.data);
907
+ options.body = JSON.stringify(requestData);
908
+ }
909
+ else {
910
+ url = `${url}?${query}`;
911
+ }
912
+ const json = await request(url, options);
900
913
  const { list, total: tot } = extractListResponse(json);
901
914
  setData(list);
902
915
  setTotal(tot);
@@ -938,8 +951,25 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
938
951
  messageApi.error('删除功能未配置');
939
952
  return;
940
953
  }
954
+ const deleteApiConfig = schema.api.delete;
941
955
  try {
942
- await request(buildUrl(schema.api.delete, record), { method: 'DELETE' });
956
+ // 构建 URL,动态替换占位符
957
+ let url = deleteApiConfig.url;
958
+ url = url.replace(/:(\w+)/g, (match, fieldName) => {
959
+ const value = record[fieldName];
960
+ return value !== undefined ? String(value) : match;
961
+ });
962
+ // 构建请求选项
963
+ const options = {
964
+ method: deleteApiConfig.method || 'DELETE',
965
+ headers: Object.assign({ 'Content-Type': 'application/json' }, deleteApiConfig.headers)
966
+ };
967
+ // 处理请求体数据
968
+ if (deleteApiConfig.data && ['POST', 'PUT', 'PATCH', 'DELETE'].includes(deleteApiConfig.method || 'DELETE')) {
969
+ const processedData = processTemplateData(deleteApiConfig.data, record);
970
+ options.body = JSON.stringify(Object.assign(Object.assign({}, processedData), { recordId: record[rowKey], timestamp: new Date().toISOString() }));
971
+ }
972
+ await request(url, options);
943
973
  messageApi.success('删除成功');
944
974
  fetchList();
945
975
  }
@@ -947,7 +977,7 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
947
977
  console.error('Delete failed:', error);
948
978
  messageApi.error('删除失败,请稍后重试');
949
979
  }
950
- }, [request, schema.api.delete, fetchList, messageApi]);
980
+ }, [request, schema.api.delete, fetchList, messageApi, rowKey]);
951
981
  // ---------- 操作列点击 ----------
952
982
  const handleAction = react.useCallback(async (action, record) => {
953
983
  if (action.type === 'view') {
@@ -1045,11 +1075,21 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
1045
1075
  messageApi.error('新增功能未配置');
1046
1076
  return;
1047
1077
  }
1078
+ const createApiConfig = schema.api.create;
1048
1079
  try {
1049
- await request(schema.api.create, {
1050
- method: 'POST',
1051
- body: JSON.stringify(values),
1052
- });
1080
+ // 构建请求选项
1081
+ const options = {
1082
+ method: createApiConfig.method || 'POST',
1083
+ headers: Object.assign({ 'Content-Type': 'application/json' }, createApiConfig.headers)
1084
+ };
1085
+ // 处理请求体数据
1086
+ let requestData = Object.assign({}, values);
1087
+ if (createApiConfig.data) {
1088
+ const processedData = processTemplateData(createApiConfig.data, values);
1089
+ requestData = Object.assign(Object.assign(Object.assign({}, requestData), processedData), { timestamp: new Date().toISOString() });
1090
+ }
1091
+ options.body = JSON.stringify(requestData);
1092
+ await request(createApiConfig.url, options);
1053
1093
  messageApi.success('新增成功');
1054
1094
  setModalState({ open: false, mode: 'create' });
1055
1095
  fetchList();
@@ -1064,11 +1104,27 @@ const CrudPage = ({ schema, initialData = [], apiRequest: customApiRequest }) =>
1064
1104
  messageApi.error('编辑功能未配置');
1065
1105
  return;
1066
1106
  }
1107
+ const updateApiConfig = schema.api.update;
1067
1108
  try {
1068
- await request(buildUrl(schema.api.update, values), {
1069
- method: 'PUT',
1070
- body: JSON.stringify(values),
1109
+ // 构建 URL,动态替换占位符
1110
+ let url = updateApiConfig.url;
1111
+ url = url.replace(/:(\w+)/g, (match, fieldName) => {
1112
+ const value = values[fieldName];
1113
+ return value !== undefined ? String(value) : match;
1071
1114
  });
1115
+ // 构建请求选项
1116
+ const options = {
1117
+ method: updateApiConfig.method || 'PUT',
1118
+ headers: Object.assign({ 'Content-Type': 'application/json' }, updateApiConfig.headers)
1119
+ };
1120
+ // 处理请求体数据
1121
+ let requestData = Object.assign({}, values);
1122
+ if (updateApiConfig.data) {
1123
+ const processedData = processTemplateData(updateApiConfig.data, values);
1124
+ requestData = Object.assign(Object.assign(Object.assign({}, requestData), processedData), { timestamp: new Date().toISOString() });
1125
+ }
1126
+ options.body = JSON.stringify(requestData);
1127
+ await request(url, options);
1072
1128
  messageApi.success('编辑成功');
1073
1129
  setModalState({ open: false, mode: 'create' });
1074
1130
  fetchList();