formCreate.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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: string[]) => {
  21. const rule: object[] = []
  22. fields.forEach((item) => {
  23. rule.push(JSON.parse(item))
  24. })
  25. return rule
  26. }
  27. // 设置表单的 Conf 和 Fields
  28. export const setConfAndFields = (designerRef: object, conf: string, fields: string) => {
  29. // @ts-ignore
  30. designerRef.value.setOption(JSON.parse(conf))
  31. // @ts-ignore
  32. designerRef.value.setRule(decodeFields(fields))
  33. }
  34. // 设置表单的 Conf 和 Fields
  35. export const setConfAndFields2 = (detailPreview: object, conf: string, fields: string) => {
  36. // @ts-ignore
  37. detailPreview.value.option = JSON.parse(conf)
  38. // @ts-ignore
  39. detailPreview.value.rule = decodeFields(fields)
  40. }