formCreate.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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,适用 FcDesigner 场景
  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,适用 form-create 场景
  35. export const setConfAndFields2 = (
  36. detailPreview: object,
  37. conf: string,
  38. fields: string[],
  39. value?: object
  40. ) => {
  41. if (isRef(detailPreview)) {
  42. // @ts-ignore
  43. detailPreview = detailPreview.value
  44. }
  45. // @ts-ignore
  46. detailPreview.option = JSON.parse(conf)
  47. // @ts-ignore
  48. detailPreview.rule = decodeFields(fields)
  49. if (value) {
  50. // @ts-ignore
  51. detailPreview.value = value
  52. }
  53. }