index.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. <template>
  2. <div class="form-designer">
  3. <el-container>
  4. <!-- 左侧字段 -->
  5. <el-aside :width="leftWidth">
  6. <div class="fields-list">
  7. <template v-if="customFields && customFields.length > 0">
  8. <template v-if="customFields[0].title && customFields[0].list && customFields[0].list.length > 0">
  9. <template v-for="(field, index) in customFields">
  10. <div class="field-title"
  11. :key="'f_' + index">{{field.title}}</div>
  12. <draggable tag="ul"
  13. :list="field.list"
  14. :group="{ name: 'form', pull: 'clone', put: false }"
  15. ghost-class="ghost"
  16. :sort="false"
  17. :key="'d_' + index">
  18. <template v-for="(item, cIndex) in field.list">
  19. <li class="field-label"
  20. :key="'c_' + cIndex">
  21. <a @click="handleFieldClick(item)">
  22. <i class="icon iconfont"
  23. :class="item.icon"></i>
  24. <span>{{item.title || item.label}}</span>
  25. </a>
  26. </li>
  27. </template>
  28. </draggable>
  29. </template>
  30. </template>
  31. <template v-else>
  32. <el-link class="field-title"
  33. :underline="false"
  34. href="https://github.com/sscfaith/avue-form-design/blob/master/CHANGELOG.md#2020-09-22"
  35. target="_blank">自定义字段 <i class="el-icon-question"></i></el-link>
  36. <draggable tag="ul"
  37. :list="customFields"
  38. :group="{ name: 'form', pull: 'clone', put: false }"
  39. ghost-class="ghost"
  40. :sort="false">
  41. <template v-for="(item, index) in customFields">
  42. <el-tooltip v-if="item.tips"
  43. effect="dark"
  44. :content="item.tips"
  45. :key="index">
  46. <li class="field-label"
  47. :key="index">
  48. <a style="padding: 0 5px;"
  49. @click="handleFieldClick(item)">
  50. <i :class="item.icon"></i>
  51. <span style="margin-left: 5px;">{{item.title || item.label}}</span>
  52. </a>
  53. </li>
  54. </el-tooltip>
  55. <li v-else
  56. class="field-label"
  57. :key="index">
  58. <a style="padding: 0 5px;"
  59. @click="handleFieldClick(item)">
  60. <i :class="item.icon"></i>
  61. <span style="margin-left: 5px;">{{item.title || item.label}}</span>
  62. </a>
  63. </li>
  64. </template>
  65. </draggable>
  66. </template>
  67. </template>
  68. <div v-for="(field, index) in fields"
  69. :key="index">
  70. <template v-if="field.list.find(f => includeFields.includes(f.type))">
  71. <div class="field-title">{{field.title}}</div>
  72. <draggable tag="ul"
  73. :list="field.list"
  74. :group="{ name: 'form', pull: 'clone', put: false }"
  75. ghost-class="ghost"
  76. :sort="false">
  77. <template v-for="(item, cIndex) in field.list">
  78. <li class="field-label"
  79. v-if="includeFields.includes(item.type)"
  80. :key="'c_' + cIndex">
  81. <a @click="handleFieldClick(item)">
  82. <i class="icon iconfont"
  83. :class="item.icon"></i>
  84. <span>{{item.title || item.label}}</span>
  85. </a>
  86. </li>
  87. </template>
  88. </draggable>
  89. </template>
  90. </div>
  91. </div>
  92. </el-aside>
  93. <!-- 中间主布局 -->
  94. <el-container class="widget-container"
  95. direction="vertical">
  96. <el-header class="widget-container-header">
  97. <div>
  98. <template v-if="undoRedo">
  99. <el-button type="text"
  100. size="medium"
  101. icon="el-icon-refresh-left"
  102. :disabled="historySteps.index == 0"
  103. @click="widgetForm = handleUndo()">撤销</el-button>
  104. <el-button type="text"
  105. size="medium"
  106. icon="el-icon-refresh-right"
  107. :disabled="historySteps.index == historySteps.steps.length - 1"
  108. @click="widgetForm = handleRedo()">重做</el-button>
  109. </template>
  110. </div>
  111. <div style="display: flex; align-items: center;">
  112. <iframe src="https://ghbtns.com/github-btn.html?user=sscfaith&repo=avue-form-design&type=star&count=true"
  113. frameborder="0"
  114. scrolling="0"
  115. width="100"
  116. height="20"
  117. title="GitHub"
  118. style="margin-left: 10px;"
  119. v-if="showGithubStar"></iframe>
  120. <slot name="toolbar-left"></slot>
  121. <el-button v-if="toolbar.includes('avue-doc')"
  122. type="text"
  123. size="medium"
  124. icon="el-icon-document"
  125. @click="handleAvueDoc">Avue文档</el-button>
  126. <el-button v-if="toolbar.includes('import')"
  127. type="text"
  128. size="medium"
  129. icon="el-icon-upload2"
  130. @click="importJsonVisible = true">导入JSON</el-button>
  131. <el-button v-if="toolbar.includes('generate')"
  132. type="text"
  133. size="medium"
  134. icon="el-icon-download"
  135. @click="handleGenerateJson">生成JSON</el-button>
  136. <el-button v-if="toolbar.includes('preview')"
  137. type="text"
  138. size="medium"
  139. icon="el-icon-view"
  140. @click="handlePreview">预览</el-button>
  141. <el-button v-if="toolbar.includes('clear')"
  142. class="danger"
  143. type="text"
  144. size="medium"
  145. icon="el-icon-delete"
  146. @click="handleClear">清空</el-button>
  147. <slot name="toolbar"></slot>
  148. </div>
  149. </el-header>
  150. <el-main :style="{background: widgetForm.column.length == 0 ? `url(${widgetEmpty}) no-repeat 50%`: ''}">
  151. <widget-form ref="widgetForm"
  152. :data="widgetForm"
  153. :select.sync="widgetFormSelect"
  154. @change="handleHistoryChange(widgetForm)"></widget-form>
  155. </el-main>
  156. </el-container>
  157. <!-- 右侧配置 -->
  158. <el-aside class="widget-config-container"
  159. :width="rightWidth">
  160. <el-tabs v-model="configTab"
  161. stretch>
  162. <el-tab-pane label="字段属性"
  163. name="widget"
  164. style="padding: 0 10px;">
  165. <widget-config :data="widgetFormSelect"
  166. :default-values="defaultValues"></widget-config>
  167. </el-tab-pane>
  168. <el-tab-pane label="表单属性"
  169. name="form"
  170. lazy
  171. style="padding: 0 10px;">
  172. <form-config :data="widgetForm"></form-config>
  173. </el-tab-pane>
  174. </el-tabs>
  175. </el-aside>
  176. <!-- 弹窗 -->
  177. <!-- 导入JSON -->
  178. <el-drawer title="导入JSON"
  179. :visible.sync="importJsonVisible"
  180. size="50%"
  181. append-to-body
  182. destroy-on-close>
  183. <monaco-editor v-model="importJson"
  184. keyIndex="import"
  185. height="82%"></monaco-editor>
  186. <div class="drawer-foot">
  187. <el-button size="medium"
  188. type="primary"
  189. @click="handleImportJsonSubmit">确定</el-button>
  190. <el-button size="medium"
  191. type="danger"
  192. @click="importJsonVisible = false">取消</el-button>
  193. </div>
  194. </el-drawer>
  195. <!-- 生成JSON -->
  196. <el-drawer title="生成JSON"
  197. :visible.sync="generateJsonVisible"
  198. size="50%"
  199. append-to-body
  200. destroy-on-close>
  201. <monaco-editor v-model="option"
  202. keyIndex="generate"
  203. height="82%"
  204. :read-only="true"></monaco-editor>
  205. <div class="drawer-foot">
  206. <el-button size="medium"
  207. type="primary"
  208. @click="handleGenerate">生成</el-button>
  209. <el-popover placement="top"
  210. trigger="hover"
  211. width="350px">
  212. <el-form v-model="configOption"
  213. style="padding: 0 20px"
  214. label-suffix=":"
  215. label-width="180px"
  216. label-position="left">
  217. <el-form-item label="类型">
  218. <el-popover placement="top-start"
  219. trigger="hover"
  220. content="复制json对象"
  221. style="margin-right: 15px;">
  222. <el-radio slot="reference"
  223. v-model="configOption.generateType"
  224. label="json">json</el-radio>
  225. </el-popover>
  226. <el-popover placement="top-start"
  227. trigger="hover"
  228. content="复制string字符串,可直接用于后端保存无需再次处理。">
  229. <el-radio slot="reference"
  230. v-model="configOption.generateType"
  231. label="string">string</el-radio>
  232. </el-popover>
  233. </el-form-item>
  234. <el-form-item label="缩进长度-空格数量">
  235. <el-slider v-model="configOption.space"
  236. show-stops
  237. :marks="{ 1: '1', 2: '2', 3: '3', 4: '4' }"
  238. :min="1"
  239. :max="4"
  240. :step="1"></el-slider>
  241. </el-form-item>
  242. <el-form-item label="引号类型">
  243. <el-switch v-model="configOption.quoteType"
  244. active-value="single"
  245. inactive-value="double"
  246. active-text="单引号"
  247. inactive-text="双引号"></el-switch>
  248. </el-form-item>
  249. <el-form-item label="移除key的引号">
  250. <el-switch v-model="configOption.dropQuotesOnKeys"></el-switch>
  251. </el-form-item>
  252. <el-form-item label="移除数字字符串的引号">
  253. <el-switch v-model="configOption.dropQuotesOnNumbers"></el-switch>
  254. </el-form-item>
  255. </el-form>
  256. <el-button size="medium"
  257. type="primary"
  258. @click="handleCopy"
  259. slot="reference"
  260. style="margin-left: 10px;">复制</el-button>
  261. </el-popover>
  262. </div>
  263. </el-drawer>
  264. <!-- 预览 -->
  265. <el-drawer title="预览"
  266. :visible.sync="previewVisible"
  267. size="60%"
  268. append-to-body
  269. :before-close="handleBeforeClose">
  270. <avue-form v-if="previewVisible"
  271. ref="form"
  272. class="preview-form"
  273. :option="option"
  274. v-model="form"
  275. @submit="handlePreviewSubmit"></avue-form>
  276. <div class="drawer-foot">
  277. <el-button size="medium"
  278. type="primary"
  279. @click="handlePreviewSubmit">确定</el-button>
  280. <el-button size="medium"
  281. type="danger"
  282. @click="handleBeforeClose">取消</el-button>
  283. </div>
  284. </el-drawer>
  285. </el-container>
  286. </div>
  287. </template>
  288. <script>
  289. import fields from './fieldsConfig.js'
  290. import beautifier from './utils/json-beautifier'
  291. import MonacoEditor from './utils/monaco-editor'
  292. import widgetEmpty from './assets/widget-empty.png'
  293. import history from './mixins/history'
  294. import Draggable from 'vuedraggable'
  295. import WidgetForm from './WidgetForm'
  296. import FormConfig from './FormConfig'
  297. import WidgetConfig from './WidgetConfig'
  298. export default {
  299. name: "FormDesign",
  300. components: { Draggable, MonacoEditor, WidgetForm, FormConfig, WidgetConfig },
  301. mixins: [history],
  302. props: {
  303. options: {
  304. type: [Object, String],
  305. default: () => {
  306. return {
  307. column: []
  308. }
  309. }
  310. },
  311. storage: {
  312. type: Boolean,
  313. default: false
  314. },
  315. asideLeftWidth: {
  316. type: [String, Number],
  317. default: '270px'
  318. },
  319. asideRightWidth: {
  320. type: [String, Number],
  321. default: '380px'
  322. },
  323. showGithubStar: {
  324. type: Boolean,
  325. default: true
  326. },
  327. toolbar: {
  328. type: Array,
  329. default: () => {
  330. return ['import', 'generate', 'preview', 'clear']
  331. }
  332. },
  333. undoRedo: {
  334. type: Boolean,
  335. default: true
  336. },
  337. includeFields: {
  338. type: Array,
  339. default: () => {
  340. const arr = []
  341. fields.forEach(f => {
  342. f.list.forEach(c => {
  343. arr.push(c.type)
  344. })
  345. })
  346. return arr
  347. }
  348. },
  349. customFields: {
  350. type: Array,
  351. },
  352. defaultValues: {
  353. type: Object
  354. },
  355. },
  356. watch: {
  357. options: {
  358. handler(val) {
  359. let options = val
  360. if (typeof options == 'string') {
  361. try {
  362. options = eval('(' + options + ')')
  363. } catch (e) {
  364. console.error('非法配置')
  365. options = { column: [] }
  366. }
  367. }
  368. this.transAvueOptionsToFormDesigner(options).then(res => {
  369. this.widgetForm = { ...this.widgetForm, ...res }
  370. })
  371. },
  372. deep: true
  373. }
  374. },
  375. computed: {
  376. leftWidth() {
  377. if (typeof this.asideLeftWidth == 'string') {
  378. return this.asideLeftWidth
  379. } else {
  380. return `${this.asideLeftWidth}px`
  381. }
  382. },
  383. rightWidth() {
  384. if (typeof this.asideRightWidth == 'string') {
  385. return this.asideRightWidth
  386. } else {
  387. return `${this.asideRightWidth}px`
  388. }
  389. }
  390. },
  391. data() {
  392. return {
  393. widgetEmpty,
  394. fields,
  395. widgetForm: {
  396. column: [],
  397. labelPosition: 'left',
  398. labelSuffix: ':',
  399. labelWidth: 120,
  400. gutter: 0,
  401. menuBtn: true,
  402. submitBtn: true,
  403. submitText: '提交',
  404. emptyBtn: true,
  405. emptyText: '清空',
  406. menuPosition: 'center'
  407. },
  408. option: {},
  409. configTab: 'widget',
  410. widgetFormSelect: {},
  411. previewVisible: false,
  412. generateJsonVisible: false,
  413. importJsonVisible: false,
  414. importJson: {},
  415. form: {},
  416. configOption: {
  417. generateType: 'json',
  418. space: 2,
  419. quoteType: 'single',
  420. dropQuotesOnKeys: true
  421. },
  422. history: {
  423. index: 0, // 当前下标
  424. maxStep: 20, // 最大记录步数
  425. steps: [], // 历史步数
  426. }
  427. }
  428. },
  429. mounted() {
  430. this.handleLoadStorage()
  431. this.handleLoadCss()
  432. },
  433. methods: {
  434. // 组件初始化时加载本地存储中的options(需开启storage),若不存在则读取用户配置的options
  435. async handleLoadStorage() {
  436. let options = this.options
  437. if (typeof options == 'string') {
  438. try {
  439. options = eval('(' + options + ')')
  440. } catch (e) {
  441. console.error('非法配置')
  442. options = { column: [] }
  443. }
  444. }
  445. if (!options.column) options.column = []
  446. this.widgetForm = this.initHistory({
  447. index: 0,
  448. maxStep: 20,
  449. steps: [await this.transAvueOptionsToFormDesigner({ ...this.widgetForm, ...options })],
  450. storage: this.storage
  451. })
  452. if (this.undoRedo) {
  453. window.addEventListener('keydown', (evt) => {
  454. // 监听 cmd + z / ctrl + z 撤销
  455. if ((evt.metaKey && !evt.shiftKey && evt.keyCode == 90) || (evt.ctrlKey && !evt.shiftKey && evt.keyCode == 90)) {
  456. this.widgetForm = this.handleUndo()
  457. }
  458. // 监听 cmd + shift + z / ctrl + shift + z / ctrl + y 重做
  459. if ((evt.metaKey && evt.shiftKey && evt.keyCode == 90) || (evt.ctrlKey && evt.shiftKey && evt.keyCode == 90) || (evt.ctrlKey && evt.keyCode == 89)) {
  460. this.widgetForm = this.handleRedo()
  461. }
  462. }, false)
  463. }
  464. },
  465. // 加载icon
  466. handleLoadCss() {
  467. const head = document.getElementsByTagName('head')[0]
  468. const script = document.createElement('link')
  469. script.rel = 'stylesheet'
  470. script.type = 'text/css'
  471. // script.href = 'https://at.alicdn.com/t/font_1254447_zc9iezc230c.css'
  472. head.appendChild(script)
  473. // this.loadScript('css', 'https://at.alicdn.com/t/font_1254447_zc9iezc230c.css')
  474. },
  475. // Avue文档链接
  476. handleAvueDoc() {
  477. // window.open('https://avuejs.com/doc/form/form-doc', '_blank')
  478. },
  479. // 左侧字段点击
  480. handleFieldClick(item) {
  481. const activeIndex = this.widgetForm.column.findIndex(c => c.prop == this.widgetFormSelect.prop) + 1
  482. let newIndex = 0
  483. if (activeIndex == -1) {
  484. this.widgetForm.column.push(item)
  485. newIndex = this.widgetForm.column.length - 1
  486. } else {
  487. this.widgetForm.column.splice(activeIndex, 0, item)
  488. newIndex = activeIndex
  489. }
  490. this.$refs.widgetForm.handleWidgetAdd({ newIndex })
  491. },
  492. // 预览 - 弹窗
  493. handlePreview() {
  494. if (!this.widgetForm.column || this.widgetForm.column.length == 0) this.$message.error("没有需要展示的内容")
  495. else {
  496. this.transformToAvueOptions(this.widgetForm, true).then(data => {
  497. this.option = data
  498. this.previewVisible = true
  499. })
  500. }
  501. },
  502. // 导入JSON - 弹窗 - 确定
  503. handleImportJsonSubmit() {
  504. try {
  505. this.transAvueOptionsToFormDesigner(this.importJson).then(res => {
  506. this.widgetForm = res
  507. this.importJsonVisible = false
  508. this.handleHistoryChange(this.widgetForm)
  509. })
  510. } catch (e) {
  511. this.$message.error(e.message)
  512. }
  513. },
  514. // 生成JSON - 弹窗
  515. handleGenerateJson() {
  516. this.transformToAvueOptions(this.widgetForm).then(data => {
  517. this.option = data
  518. this.generateJsonVisible = true
  519. })
  520. },
  521. // 生成JSON - 弹窗 - 确定
  522. handleGenerate() {
  523. this.transformToAvueOptions(this.widgetForm).then(data => {
  524. if (this.configOption.generateType && this.configOption.generateType == 'string') this.$emit('submit', beautifier(data, {
  525. minify: true,
  526. ...this.configOption
  527. }))
  528. else this.$emit('submit', data)
  529. })
  530. },
  531. // 生成JSON - 弹窗 - 拷贝
  532. handleCopy() {
  533. this.transformToAvueOptions(this.widgetForm).then(data => {
  534. this.$Clipboard({
  535. text: beautifier(data, {
  536. minify: true,
  537. ...this.configOption
  538. })
  539. }).then(() => {
  540. this.$message.success('复制成功')
  541. }).catch(() => {
  542. this.$message.error('复制失败')
  543. })
  544. })
  545. },
  546. // 预览 - 弹窗 - 确定
  547. handlePreviewSubmit(form, done) {
  548. if (done) {
  549. this.$alert(this.form).then(() => {
  550. done()
  551. }).catch(() => {
  552. done()
  553. })
  554. } else {
  555. this.$refs.form.validate((valid, done) => {
  556. if (valid) this.$alert(this.form).then(() => {
  557. done()
  558. }).catch(() => {
  559. done()
  560. })
  561. })
  562. }
  563. },
  564. // 预览 - 弹窗 - 关闭前
  565. handleBeforeClose() {
  566. this.$refs.form.resetForm()
  567. this.form = {}
  568. this.previewVisible = false
  569. },
  570. // 清空
  571. handleClear() {
  572. if (this.widgetForm && this.widgetForm.column && this.widgetForm.column.length > 0) {
  573. this.$confirm('确定要清空吗?', '警告', {
  574. type: 'warning'
  575. }).then(() => {
  576. this.$set(this.widgetForm, 'column', [])
  577. this.$set(this, 'form', {})
  578. this.$set(this, 'widgetFormSelect', {})
  579. this.handleHistoryChange(this.widgetForm)
  580. }).catch(() => {
  581. })
  582. } else this.$message.error("没有需要清空的内容")
  583. },
  584. // 表单设计器配置项 转化为 Avue配置项
  585. transformToAvueOptions(obj, isPreview = false) {
  586. const _this = this
  587. return new Promise((resolve, reject) => {
  588. try {
  589. const data = _this.deepClone(obj)
  590. for (let i = 0; i < data.column.length; i++) {
  591. const col = data.column[i]
  592. if (isPreview) { // 预览调整事件中的this指向
  593. let event = ['change', 'blur', 'click', 'focus']
  594. event.forEach(e => {
  595. if (col[e]) col[e] = eval((col[e] + '').replace(/this/g, '_this'))
  596. })
  597. if (col.event) Object.keys(col.event).forEach(key => col.event[key] = eval((col.event[key] + '').replace(/this/g, '_this')))
  598. }
  599. if (col.type == 'dynamic' && col.children && col.children.column && col.children.column.length > 0) {
  600. const c = col.children.column;
  601. c.forEach(item => {
  602. delete item.subfield
  603. })
  604. this.transformToAvueOptions(col.children, isPreview).then(res => {
  605. col.children = res
  606. })
  607. } else if (col.type == 'group') {
  608. if (!data.group) data.group = []
  609. const group = {
  610. label: col.label,
  611. icon: col.icon,
  612. prop: col.prop,
  613. arrow: col.arrow,
  614. collapse: col.collapse,
  615. display: col.display
  616. }
  617. this.transformToAvueOptions(col.children, isPreview).then(res => {
  618. group.column = res.column
  619. data.group.push(group)
  620. })
  621. data.column.splice(i, 1)
  622. i--
  623. } else if (['checkbox', 'radio', 'tree', 'cascader', 'select'].includes(col.type)) {
  624. if (col.dicOption == 'static') {
  625. delete col.dicUrl
  626. delete col.dicMethod
  627. delete col.dicQuery
  628. delete col.dicQueryConfig
  629. } else if (col.dicOption == 'remote') {
  630. delete col.dicData
  631. if (col.dicQueryConfig && col.dicQueryConfig.length > 0) {
  632. const query = {}
  633. col.dicQueryConfig.forEach(q => {
  634. if (q.key && q.value) query[q.key] = q.value
  635. })
  636. col.dicQuery = query
  637. delete col.dicQueryConfig
  638. } else delete col.dicQueryConfig
  639. }
  640. delete col.dicOption
  641. } else if (['upload'].includes(col.type)) {
  642. if (col.headersConfig && col.headersConfig.length > 0) {
  643. const headers = {}
  644. col.headersConfig.forEach(h => {
  645. if (h.key && h.value) headers[h.key] = h.value
  646. })
  647. col.headers = headers
  648. } else delete col.headers
  649. delete col.headersConfig
  650. if (col.dataConfig && col.dataConfig.length > 0) {
  651. const data = {}
  652. col.dataConfig.forEach(h => {
  653. if (h.key && h.value) data[h.key] = h.value
  654. })
  655. col.data = data
  656. } else delete col.data
  657. delete col.dataConfig
  658. }
  659. }
  660. resolve(data)
  661. } catch (e) {
  662. reject(e)
  663. }
  664. })
  665. },
  666. // Avue配置项 转化为 表单设计器配置项
  667. transAvueOptionsToFormDesigner(obj) {
  668. if (typeof obj == 'string') obj = eval('(' + obj + ')')
  669. const data = this.deepClone(obj)
  670. return new Promise((resolve, reject) => {
  671. try {
  672. if (data.column && data.column.length > 0) {
  673. data.column.forEach(col => {
  674. if (col.type == 'dynamic' && col.children && col.children.column && col.children.column.length > 0) {
  675. const c = col.children.column;
  676. c.forEach(item => {
  677. item.subfield = true
  678. })
  679. this.transAvueOptionsToFormDesigner(col.children).then(res => {
  680. col.children = res
  681. })
  682. } else if (['checkbox', 'radio', 'tree', 'cascader', 'select'].includes(col.type)) {
  683. if (!col.dicData && col.dicQuery && typeof col.dicQuery == 'object') {
  684. const arr = []
  685. for (let key in col.dicQuery) {
  686. arr.push({
  687. key,
  688. value: col.dicQuery[key],
  689. $cellEdit: true
  690. })
  691. }
  692. col.dicQueryConfig = arr
  693. }
  694. if (col.dicUrl) col.dicOption = 'remote'
  695. else col.dicOption = 'static'
  696. if (!col.dicData) col.dicData = []
  697. } else if (['upload'].includes(col.type)) {
  698. if (col.headers && typeof col.headers == 'object') {
  699. const arr = []
  700. for (let key in col.headers) {
  701. arr.push({
  702. key,
  703. value: col.headers[key],
  704. $cellEdit: true
  705. })
  706. }
  707. col.headersConfig = arr
  708. } else col.headersConfig = []
  709. if (col.data && typeof col.data == 'object') {
  710. const arr = []
  711. for (let key in col.data) {
  712. arr.push({
  713. key,
  714. value: col.data[key],
  715. $cellEdit: true
  716. })
  717. }
  718. col.dataConfig = arr
  719. } else col.dataConfig = []
  720. }
  721. })
  722. }
  723. if (data.group && data.group.length > 0) {
  724. for (let i = 0; i < data.group.length; i++) {
  725. if (!data.column) data.column = []
  726. const col = data.group[i]
  727. const group = {
  728. type: 'group',
  729. label: col.label,
  730. icon: col.icon,
  731. prop: col.prop,
  732. arrow: col.arrow,
  733. collapse: col.collapse,
  734. display: col.display
  735. }
  736. this.transAvueOptionsToFormDesigner(col).then(res => {
  737. group.children = res
  738. data.column.push(group)
  739. })
  740. }
  741. delete data.group
  742. }
  743. resolve(data)
  744. } catch (e) {
  745. reject(e)
  746. }
  747. })
  748. },
  749. async getData(type = 'json', option = {}) {
  750. if (type == 'string') return beautifier(await this.transformToAvueOptions(this.widgetForm), {
  751. minify: true,
  752. ...option
  753. })
  754. else return await this.transformToAvueOptions(this.widgetForm)
  755. }
  756. }
  757. }
  758. </script>
  759. <style lang="scss">
  760. @import './styles/index.scss';
  761. </style>