App.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!-- eslint-disable @typescript-eslint/ban-ts-comment -->
  2. <!-- eslint-disable @typescript-eslint/no-explicit-any -->
  3. <script setup lang="ts">
  4. import { ref, onMounted } from 'vue'
  5. import type { Config, Handle } from '@form-create/designer'
  6. import { formConfig, formSubmitSave } from './form.config.ts'
  7. import FcDesigner from '@form-create/designer'
  8. // 组件
  9. import { RcSelect } from './plugins/components/rc-select.config.ts'
  10. import { RcRadio } from './plugins/components/rc-radio.config.ts'
  11. import { RcCheckbox } from './plugins/components/rc-checkbox.config.ts'
  12. // 菜单
  13. import { MenuOptions } from './plugins/menu/options.ts'
  14. // @ts-expect-error
  15. import type { FcDesignerInstance } from '@form-create/designer' // 注册组件
  16. import type { Options, Rule } from '@form-create/element-ui'
  17. import type { Option } from './components/rc-options/createOptions.ts'
  18. import formCreate from '@form-create/element-ui'
  19. /**
  20. * @see https://view.form-create.com/methods
  21. */
  22. declare global {
  23. interface Window {
  24. __QIANKUN__EVENT__: {
  25. on: (name: string, fn: (...arg: any[]) => void) => void
  26. emit: (name: string, ...arg: any[]) => boolean
  27. off: (name: string, fn?: (...arg: any[]) => void) => void
  28. } // 事件中心
  29. __QIANKUN__REQUSET__: any // 请求API接口
  30. __QIANKUN__GUID__: () => string // 获取唯一标识
  31. __QIANKUN__OPEN__TAG__: (tags: Option['tags']) => void // 打开标签页
  32. __QIANKUN__QUITE__: () => void // 返回页面
  33. }
  34. }
  35. // 表单实例
  36. const designer = ref<FcDesignerInstance | null>(null)
  37. const api = ref(null)
  38. // 表单模板元素
  39. // 判断是独立运行的还是qiankun子应用
  40. const height = ref('100vh')
  41. const config = ref<Config>(formConfig)
  42. // 顶部更多操作按钮
  43. const handle = ref<Handle>([])
  44. let id = ''
  45. const onSave = async (config: { options: string; rule: string }) => {
  46. const rule = config.rule
  47. const options = config.options
  48. formSubmitSave({
  49. rule: JSON.parse(rule),
  50. options: JSON.parse(options),
  51. original: config,
  52. id,
  53. onSuccess() {
  54. window.__QIANKUN__QUITE__()
  55. },
  56. })
  57. }
  58. onMounted(async () => {
  59. // 注册拖拽规则
  60. designer.value?.addMenu(MenuOptions)
  61. designer.value?.addComponent(RcSelect)
  62. designer.value?.addComponent(RcRadio)
  63. designer.value?.addComponent(RcCheckbox)
  64. console.log(designer.value)
  65. const des = designer.value
  66. window.__QIANKUN__EVENT__.on('formCoverChange', (cover: string) => {
  67. const newRule = des?.getRule() as Rule[]
  68. if (!cover) {
  69. newRule.shift()
  70. des?.setRule(newRule)
  71. return
  72. }
  73. const firstType = newRule[0]?.type
  74. if (firstType === 'rc-image') {
  75. newRule[0]!.props!.cover = cover
  76. } else {
  77. newRule.unshift({
  78. type: 'rc-image',
  79. props: {
  80. cover,
  81. },
  82. } as Rule)
  83. }
  84. des?.setRule(JSON.stringify(newRule)) // 重新赋值
  85. })
  86. id = location.search?.substring(1).split('=')[1] || ''
  87. if (!id) return
  88. window.__QIANKUN__REQUSET__.CustomrForm._getDetail({ id }).then(
  89. (res: { data: { rule: string; options: string } }) => {
  90. if (!res) return
  91. // 设置拖拽规则
  92. designer.value?.setRule(res.data.rule)
  93. designer.value?.setOptions(res.data.options as Options) // 重新赋值
  94. },
  95. )
  96. })
  97. </script>
  98. <template>
  99. <fc-designer
  100. ref="designer"
  101. name="form"
  102. :height="height"
  103. :handle="handle"
  104. :config="config"
  105. v-model:api="api"
  106. @save="onSave"
  107. />
  108. </template>