formCreate.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * 针对 https://github.com/xaboy/form-create-designer 封装的工具类
  3. */
  4. // 编码表单 Conf
  5. export const encodeConf = (designerRef: object) => {
  6. // @ts-ignore
  7. return JSON.stringify(designerRef.value.getOption())
  8. }
  9. // 编码表单 Fields
  10. export const encodeFields = (designerRef: object) => {
  11. // @ts-ignore
  12. const rule = designerRef.value.getRule()
  13. const fields: string[] = []
  14. rule.forEach((item) => {
  15. fields.push(JSON.stringify(item))
  16. })
  17. return fields
  18. }
  19. // 解码表单 Fields
  20. export const decodeFields = (fields, type: string) => {
  21. const rule: object[] = []
  22. if (type == '1') {
  23. fields.forEach((item) => {
  24. rule.push(JSON.parse(item))
  25. })
  26. } else {
  27. JSON.parse(fields).forEach((item) => {
  28. rule.push(item)
  29. })
  30. }
  31. return rule
  32. }
  33. // 设置表单的 Conf 和 Fields,适用 FcDesigner 场景
  34. export const setConfAndFields = (designerRef: object, conf: string, fields, type: string = '1') => {
  35. // @ts-ignore
  36. designerRef.value.setOption(JSON.parse(conf))
  37. // @ts-ignore
  38. designerRef.value.setRule(decodeFields(fields, type))
  39. }
  40. // 设置表单的 Conf 和 Fields,适用 form-create 场景
  41. export const setConfAndFields2 = (
  42. detailPreview: object,
  43. conf: string,
  44. fields: string[],
  45. value?: string
  46. ) => {
  47. if (isRef(detailPreview)) {
  48. // @ts-ignore
  49. detailPreview = detailPreview.value
  50. }
  51. // @ts-ignore
  52. detailPreview.option = JSON.parse(conf)
  53. // @ts-ignore
  54. detailPreview.rule = decodeFields(fields)
  55. if (value) {
  56. // @ts-ignore
  57. detailPreview.value = value
  58. }
  59. }