formCreate.ts 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 = (
  36. detailPreview: object,
  37. conf: string,
  38. fields: string,
  39. value?: object
  40. ) => {
  41. // @ts-ignore
  42. detailPreview.value.option = JSON.parse(conf)
  43. // @ts-ignore
  44. detailPreview.value.rule = decodeFields(fields)
  45. if (value) {
  46. // @ts-ignore
  47. detailPreview.value.value = value
  48. }
  49. }