init.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * @param {any} prop - description
  3. * @param {string} key - description
  4. * @param {string} value - description
  5. */
  6. export const initParams = (prop, key = "key", value = "value") => {
  7. // get params
  8. const object1 = Object.fromEntries(
  9. prop.map(({ item, attr }) => [item[key], attr[value]])
  10. );
  11. // get mapping params
  12. const object2 = {};
  13. prop
  14. .filter((item) => item.attr.dataMapping)
  15. .map(({ attr: { dataMapping } }) => {
  16. for (let key in dataMapping) {
  17. object2[key] = null;
  18. }
  19. });
  20. return { ...object1, ...object2 };
  21. };
  22. /**
  23. * @param {any} prop - description
  24. */
  25. export const initRules = (prop) => {
  26. const rules = {};
  27. prop
  28. .filter(({ item }) => item.required)
  29. .forEach(({ item, attr }) => {
  30. const message = `${item.title}不能为空`;
  31. rules[item.key] = attr.rules ?
  32. [
  33. { required: true, message: message, trigger: "change" },
  34. ...attr.rules
  35. ]
  36. : [{ required: true, message: message, trigger: "change" },];
  37. });
  38. return rules;
  39. };
  40. /**
  41. * @param {any} prop - description
  42. */
  43. export const initDicts = (prop) => {
  44. return Array.from(
  45. new Set(
  46. prop
  47. .filter((item) => item.attr.dictName)
  48. .map((item) => item.attr.dictName)
  49. )
  50. );
  51. };
  52. export const initPage = () => {
  53. return { pageNum: 1, pageSize: 50, total: 0 };
  54. };