result.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <template>
  2. <div class="app-container">
  3. <el-backtop />
  4. <div v-if="loading">生成中...</div>
  5. <div v-else>
  6. <el-container>
  7. <el-aside>
  8. <el-button
  9. icon="el-icon-download"
  10. type="text"
  11. @click="downloadAll"
  12. >下载全部</el-button>
  13. <el-input
  14. v-show="treeData.length > 0"
  15. v-model="filterText"
  16. prefix-icon="el-icon-search"
  17. placeholder="搜索"
  18. size="mini"
  19. clearable
  20. style="margin-bottom: 10px;"
  21. />
  22. <el-tree
  23. ref="tree"
  24. :data="treeData"
  25. :props="defaultProps"
  26. :filter-node-method="filterNode"
  27. node-key="id"
  28. default-expand-all
  29. highlight-current
  30. @current-change="onTreeSelect"
  31. />
  32. </el-aside>
  33. <el-main v-show="fileInfo.content.length > 0">
  34. <el-button
  35. type="text"
  36. icon="el-icon-document-copy"
  37. :data-clipboard-text="fileInfo.content"
  38. class="copyBtn">复制代码</el-button>
  39. <el-button
  40. icon="el-icon-download"
  41. type="text"
  42. @click="downloadText(fileInfo.filename, fileInfo.content)"
  43. >下载当前文件</el-button>
  44. <codemirror
  45. v-model="fileInfo.content"
  46. :options="cmOptions"
  47. />
  48. </el-main>
  49. </el-container>
  50. </div>
  51. </div>
  52. </template>
  53. <script>
  54. import JSZip from 'jszip'
  55. import { saveAs } from 'file-saver'
  56. import { codemirror } from 'vue-codemirror'
  57. import 'codemirror/theme/neat.css'
  58. require('codemirror/mode/javascript/javascript')
  59. require('codemirror/mode/clike/clike')
  60. require('codemirror/mode/htmlembedded/htmlembedded')
  61. require('codemirror/mode/htmlmixed/htmlmixed')
  62. require('codemirror/mode/xml/xml')
  63. const mode_map = {
  64. 'js': 'text/javascript',
  65. 'java': 'text/x-java',
  66. 'jsp': 'application/x-jsp',
  67. 'html': 'text/html',
  68. 'aspx': 'application/x-aspx',
  69. 'xml': 'xml'
  70. }
  71. export default {
  72. components: { codemirror },
  73. data() {
  74. return {
  75. loading: true,
  76. clientParam: {
  77. datasourceConfigId: '',
  78. tableNames: [],
  79. templateConfigIdList: [],
  80. packageName: ''
  81. },
  82. treeData: [],
  83. filterText: '',
  84. defaultProps: {
  85. children: 'children',
  86. label: 'filename'
  87. },
  88. content: '',
  89. fileInfo: {
  90. content: '',
  91. filename: ''
  92. },
  93. cmOptions: {
  94. value: '',
  95. mode: 'text/x-java',
  96. theme: 'neat',
  97. readOnly: true
  98. }
  99. }
  100. },
  101. watch: {
  102. filterText(val) {
  103. this.$refs.tree.filter(val)
  104. }
  105. },
  106. created() {
  107. const config = this.$route.params.config || ''
  108. if (config) {
  109. this.clientParam = JSON.parse(config)
  110. this.onGenerate()
  111. }
  112. this.initCopy()
  113. },
  114. destroyed() {
  115. this.cleanCopy()
  116. },
  117. methods: {
  118. onGenerate() {
  119. if (this.clientParam.tableNames.length === 0) {
  120. this.tip('请勾选表', 'error')
  121. return
  122. }
  123. if (this.clientParam.templateConfigIdList.length === 0) {
  124. this.tip('请勾选模板', 'error')
  125. return
  126. }
  127. this.post('/generate/code', this.clientParam, resp => {
  128. this.loading = false
  129. const rows = resp.data
  130. this.treeData = this.buildTreeData(rows)
  131. })
  132. },
  133. // 树搜索
  134. filterNode(value, data) {
  135. if (!value) return true
  136. return data.text.indexOf(value) !== -1
  137. },
  138. buildTreeData(rows) {
  139. const treeData = []
  140. const codeMap = {}
  141. // 把列表数据转换到map中,key为表名
  142. // value是个List
  143. for (let i = 0, len = rows.length; i < len; i++) {
  144. const row = rows[i]
  145. let list = codeMap[row.tableName]
  146. if (!list) {
  147. list = []
  148. codeMap[row.tableName] = list
  149. }
  150. list.push({ templateName: row.templateName, content: row.content })
  151. }
  152. // 把这个map对象转成tree格式数据
  153. for (const tableName in codeMap) {
  154. const codeFileArr = codeMap[tableName]
  155. const treeElement = {
  156. filename: tableName,
  157. children: this.buildChildren(codeFileArr)
  158. }
  159. treeData.push(treeElement)
  160. }
  161. return treeData
  162. },
  163. buildChildren(codeFileArr) {
  164. const children = []
  165. for (let i = 0, len = codeFileArr.length; i < len; i++) {
  166. const codeFile = codeFileArr[i]
  167. const child = {
  168. filename: codeFile.templateName,
  169. content: codeFile.content
  170. }
  171. children.push(child)
  172. }
  173. return children
  174. },
  175. onTreeSelect(data) {
  176. if (data.children && data.children.length > 0) {
  177. return
  178. }
  179. this.fileInfo = data
  180. this.changeMod(data.filename)
  181. },
  182. changeMod(fileName) {
  183. const suffix = this.getSuffix(fileName)
  184. this.cmOptions.mode = mode_map[suffix] || 'text/javascript'
  185. },
  186. getSuffix(fileName) {
  187. const index = fileName.lastIndexOf('.')
  188. if (index === -1) {
  189. return 'js'
  190. }
  191. return fileName.substring(index + 1, fileName.length)
  192. },
  193. downloadAll() {
  194. const data = this.treeData
  195. const zip = new JSZip()
  196. data.forEach(row => {
  197. const children = row.children
  198. const isFolder = children.length > 0
  199. if (isFolder) {
  200. // 创建文件夹
  201. const folderZip = zip.folder(row.filename)
  202. children.forEach(child => {
  203. // 文件放入文件夹中
  204. folderZip.file(child.filename, child.content)
  205. })
  206. }
  207. })
  208. // 下载
  209. zip.generateAsync({ type: 'blob' }).then(function(content) {
  210. // see FileSaver.js
  211. saveAs(content, `code-${new Date().getTime()}.zip`);
  212. })
  213. }
  214. }
  215. }
  216. </script>