generateCode.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. function findRemoteFunc (list, funcList, tokenFuncList, blankList, optionList) {
  2. for (let i = 0; i < list.length; i++) {
  3. if (list[i].type == 'grid') {
  4. list[i].columns.forEach(item => {
  5. findRemoteFunc(item.list, funcList, tokenFuncList, blankList, optionList)
  6. })
  7. } else if (list[i].type == 'table') {
  8. findRemoteFunc(list[i].tableColumns, funcList, tokenFuncList, blankList, optionList)
  9. } else if (list[i].type == 'subform') {
  10. findRemoteFunc(list[i].list, funcList, tokenFuncList, blankList, optionList)
  11. } else if (list[i].type == 'tabs') {
  12. list[i].tabs.forEach(item => {
  13. findRemoteFunc(item.list, funcList, tokenFuncList, blankList, optionList)
  14. })
  15. } else if (list[i].type == 'collapse') {
  16. list[i].tabs.forEach(item => {
  17. findRemoteFunc(item.list, funcList, tokenFuncList, blankList, optionList)
  18. })
  19. } else if (list[i].type == 'report') {
  20. list[i].rows.forEach(row => {
  21. row.columns.forEach(col => {
  22. findRemoteFunc(col.list, funcList, tokenFuncList, blankList, optionList)
  23. })
  24. })
  25. } else if (list[i].type == 'inline') {
  26. findRemoteFunc(list[i].list, funcList, tokenFuncList, blankList, optionList)
  27. } else if (list[i].type == 'dialog') {
  28. findRemoteFunc(list[i].list, funcList, tokenFuncList, blankList, optionList)
  29. } else if (list[i].type == 'card') {
  30. findRemoteFunc(list[i].list, funcList, tokenFuncList, blankList, optionList)
  31. } else if (list[i].type == 'group') {
  32. findRemoteFunc(list[i].list, funcList, tokenFuncList, blankList, optionList)
  33. } else {
  34. if (list[i].type == 'blank') {
  35. if (list[i].model && blankList.findIndex(item => item.name == list[i].model) < 0) {
  36. blankList.push({
  37. name: list[i].model,
  38. label: list[i].name,
  39. dataBind: list[i].options.dataBind
  40. })
  41. }
  42. } else if (list[i].type == 'imgupload') {
  43. if (list[i].options.tokenType == 'func' && list[i].options.tokenFunc) {
  44. tokenFuncList.push({
  45. func: list[i].options.tokenFunc,
  46. label: list[i].name,
  47. model: list[i].model
  48. })
  49. }
  50. } else {
  51. if (list[i].options.remote && list[i].options.remoteType == 'func' && list[i].options.remoteFunc) {
  52. funcList.push({
  53. func: list[i].options.remoteFunc,
  54. label: list[i].name,
  55. model: list[i].model
  56. })
  57. }
  58. if (list[i].options.remote && list[i].options.remoteType == 'option' && list[i].options.remoteOption) {
  59. optionList.push({
  60. option: list[i].options.remoteOption,
  61. label: list[i].name,
  62. model: list[i].model
  63. })
  64. }
  65. }
  66. }
  67. }
  68. }
  69. export default function (data, type = 'html', ui = 'element', isVant = false) {
  70. const isDark = document.querySelector('html').className.indexOf('dark')>-1
  71. const funcList = []
  72. const tokenFuncList = []
  73. const blankList = []
  74. const optionList = []
  75. let templateName = ui == 'element' ? 'fm-generate-form' : 'fm-generate-antd-form'
  76. const buttonName = ui == 'element' ? 'el-button' : 'a-button'
  77. let cdnjs = ui == 'element' ? 'https://unpkg.com/element-plus' : 'https://unpkg.com/ant-design-vue/dist/antd.min.js'
  78. const dayjs = `<script src="https://unpkg.com/dayjs/dayjs.min.js"></script>
  79. <script src="https://unpkg.com/dayjs/plugin/customParseFormat.js"></script>
  80. <script src="https://unpkg.com/dayjs/plugin/weekday.js"></script>
  81. <script src="https://unpkg.com/dayjs/plugin/localeData.js"></script>
  82. <script src="https://unpkg.com/dayjs/plugin/weekOfYear.js"></script>
  83. <script src="https://unpkg.com/dayjs/plugin/weekYear.js"></script>
  84. <script src="https://unpkg.com/dayjs/plugin/advancedFormat.js"></script>
  85. <script src="https://unpkg.com/dayjs/plugin/quarterOfYear.js"></script>`
  86. let cdncss = ''
  87. if (ui == 'element') {
  88. if (isDark) {
  89. cdncss = `<link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css">
  90. <link rel="stylesheet" href="https://unpkg.com/element-plus/theme-chalk/dark/css-vars.css">`
  91. } else {
  92. cdncss = `<link rel="stylesheet" href="https://unpkg.com/element-plus/dist/index.css">`
  93. }
  94. } else {
  95. cdncss = `<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/ant-design-vue/dist/reset.min.css">`
  96. }
  97. let useElemnt = `.use(${ui == 'element' ? 'ElementPlus' : 'antd'}).use(FormMakingV3)${ui == 'element' ? '' : '.provide("useInjectFormItemContext", antd.Form.useInjectFormItemContext)'}.mount('#app')`
  98. if (isVant) {
  99. templateName = 'fm-generate-vant-form'
  100. cdncss = `<link rel="stylesheet" href="https://fastly.jsdelivr.net/npm/vant@4.9.9/lib/index.css"/>`
  101. cdnjs = `https://fastly.jsdelivr.net/npm/vant@4.9.9/lib/vant.min.js`
  102. useElemnt = `.use(vant).use(FormMakingV3).mount('#app')`
  103. }
  104. findRemoteFunc(JSON.parse(data).list, funcList, tokenFuncList, blankList, optionList)
  105. let funcTemplate = ''
  106. let blankTemplate = ''
  107. let optionTemplate = ''
  108. for (let i = 0; i < optionList.length; i++) {
  109. optionTemplate += `
  110. ${optionList[i].option} : [], // ${optionList[i].label} option data
  111. `
  112. }
  113. for(let i = 0; i < funcList.length; i++) {
  114. funcTemplate += `
  115. ${funcList[i].func} (resolve) {
  116. // ${funcList[i].label} ${funcList[i].model}
  117. // resolve(data)
  118. },
  119. `
  120. }
  121. for(let i = 0; i < tokenFuncList.length; i++) {
  122. funcTemplate += `
  123. ${tokenFuncList[i].func} (resolve) {
  124. // ${tokenFuncList[i].label} ${tokenFuncList[i].model}
  125. // resolve(token)
  126. },
  127. `
  128. }
  129. for (let i = 0; i < blankList.length; i++) {
  130. if (blankList[i].dataBind) {
  131. blankTemplate += `
  132. <template v-slot:${blankList[i].name}="scope">
  133. <!-- ${blankList[i].label || blankList[i].name} -->
  134. <!-- use v-model="scope.model.${blankList[i].name}" to bind data -->
  135. </template>
  136. `
  137. } else {
  138. blankTemplate += `
  139. <template v-slot:${blankList[i].name}>
  140. <!-- ${blankList[i].label || blankList[i].name} -->
  141. </template>
  142. `
  143. }
  144. }
  145. if (type == 'html') {
  146. return `<!DOCTYPE html>
  147. <html>
  148. <head>
  149. <meta charset="UTF-8">
  150. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
  151. ${cdncss}
  152. <link rel="stylesheet" href="https://form.making.link/public/lib/form-making-v3/dist/index.css">
  153. </head>
  154. <body>
  155. <div id="app">
  156. <${templateName}
  157. :data="jsonData"
  158. >${blankTemplate}
  159. </${templateName}>
  160. </div>
  161. <script src="https://unpkg.com/vue/dist/vue.global.prod.js"></script>${ui == 'antd' ? `\n\t${dayjs}` : ''}
  162. <script src="${cdnjs}"></script>
  163. <script src="https://form.making.link/public/lib/form-making-v3/dist/form-making-v3.umd.js"></script>
  164. <script>
  165. Vue.createApp({
  166. data () {
  167. return {
  168. jsonData: ${data}
  169. }
  170. }
  171. })${useElemnt}
  172. </script>
  173. </body>
  174. </html>`
  175. } else {
  176. return `<template>
  177. <${templateName}
  178. :data="jsonData"
  179. >${blankTemplate}
  180. </${templateName}>
  181. </template>
  182. <script setup>
  183. const jsonData = ${data}
  184. </script>`
  185. }
  186. }