adapters.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import _ from 'lodash-es';
  2. import { ModuleData, ShowStyle } from './types';
  3. /** 将原本的 formData 格式化为 api.saveOrUpdate 需要的格式 */
  4. export function parseFormDataToParams(formData: Record<string, number | string | undefined>) {
  5. const params = {};
  6. _.forEach(formData, (v: string | undefined, k) => {
  7. if (!v) return;
  8. // 如果是以 moduleData/showStyle 打头的数据要特殊处理,因为这是配置的主要项目,表单配置见 ./configuration.data
  9. if (k.startsWith('moduleData')) {
  10. return _.set(params, k, JSON.parse(v));
  11. }
  12. return _.set(params, k, v);
  13. });
  14. return params;
  15. }
  16. /** 将 api.list 返回的 moduleData 格式化,格式化之后可以支持对应的表单以用,该方法会修改源数据 */
  17. export function parseModuleData(listData: { moduleData: ModuleData; showStyle: ShowStyle }) {
  18. _.forEach(listData.moduleData, (v, k) => {
  19. listData[`moduleData.${k}`] = JSON.stringify(v);
  20. });
  21. return listData;
  22. }
  23. // export function parseModuleDataToConfig(moduleData: ModuleData): {
  24. // chart: { label: string; prop: string }[];
  25. // list: { label: string; prop: string }[];
  26. // };
  27. // export function parseModuleDataToConfig(
  28. // moduleData: ModuleData,
  29. // data: any
  30. // ): {
  31. // chart: { label: string; value: string }[];
  32. // list: { label: string; value: string }[];
  33. // };
  34. /** 将 api.list 返回的 moduleData 格式化为配置对象,如果传入了 data,那么会返回带数据的配置,否则返回默认配置,这些配置通常可用于页面展示 */
  35. // export function parseModuleDataToConfig(moduleData: ModuleData): {
  36. // chart: { label: string; prop: string }[];
  37. // list: { label: string; prop: string }[];
  38. // } {
  39. // // if (data) {
  40. // // return {
  41. // // chart: _.map(_.get(moduleData, 'chart', []), (label, prop) => {
  42. // // return { label, value: _.get(data, prop, '/') };
  43. // // }),
  44. // // list: _.map(_.get(moduleData, 'list', []), (label, prop) => {
  45. // // return { label, value: _.get(data, prop, '/') };
  46. // // }),
  47. // // };
  48. // // }
  49. // return {
  50. // chart: _.map(_.get(moduleData, 'chart', []), (label, prop) => {
  51. // return { label, prop };
  52. // }),
  53. // list: _.map(_.get(moduleData, 'list', []), (label, prop) => {
  54. // return { label, prop };
  55. // }),
  56. // };
  57. // }