index.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <div class="app-container code-gen">
  3. <el-form ref="genForm" class="gen-form" :model="clientParam" size="mini" label-width="150px">
  4. <el-form-item label="选择数据源" prop="datasourceConfigId" :rules="{required: true, message: '请选择数据源'}">
  5. <el-select
  6. v-model="clientParam.datasourceConfigId"
  7. placeholder="选择数据源"
  8. @change="onDataSourceChange"
  9. >
  10. <el-option
  11. v-for="item in datasourceConfigList"
  12. :key="item.id"
  13. :label="`${item.dbName}(${item.host})`"
  14. :value="item.id"
  15. >
  16. <span style="float: left">{{ `${item.dbName}(${item.host})` }} </span>
  17. <span style="float: right; color: #8492a6; font-size: 13px">
  18. <el-tooltip placement="top" content="Duplicate">
  19. <el-link type="primary" icon="el-icon-document-copy" style="margin-right: 20px;" @click.stop="onDataSourceDuplicate(item)"></el-link>
  20. </el-tooltip>
  21. <el-link type="primary" icon="el-icon-edit" style="margin-right: 20px;" @click.stop="onDataSourceUpdate(item)"></el-link>
  22. <el-link type="danger" icon="el-icon-delete" @click.stop="onDataSourceDelete(item)"></el-link>
  23. </span>
  24. </el-option>
  25. </el-select>
  26. <el-button type="text" @click="onDataSourceAdd">新建数据源</el-button>
  27. </el-form-item>
  28. <el-form-item v-show="showTable" label="包名(package)">
  29. <el-input v-model="clientParam.packageName" placeholder="可选,如:com.gitee.xxx" show-word-limit maxlength="100" />
  30. </el-form-item>
  31. </el-form>
  32. <el-row v-show="showTable" :gutter="20">
  33. <el-col :span="12">
  34. <h4>选择表</h4>
  35. <el-input
  36. v-model="tableSearch"
  37. prefix-icon="el-icon-search"
  38. size="mini"
  39. placeholder="过滤表"
  40. style="margin-bottom: 10px;width: 100%;"
  41. />
  42. <el-table
  43. :data="tableListData"
  44. border
  45. :cell-style="cellStyleSmall()"
  46. :header-cell-style="headCellStyleSmall()"
  47. :row-class-name="tableRowClassName"
  48. @selection-change="onTableListSelect"
  49. >
  50. <el-table-column
  51. type="selection"
  52. />
  53. <el-table-column
  54. prop="tableName"
  55. label="表名"
  56. />
  57. </el-table>
  58. </el-col>
  59. <el-col :span="12">
  60. <h4>选择模板</h4>
  61. <el-table
  62. :data="templateListData"
  63. border
  64. :cell-style="cellStyleSmall()"
  65. :header-cell-style="headCellStyleSmall()"
  66. @selection-change="onTemplateListSelect"
  67. >
  68. <el-table-column
  69. type="selection"
  70. />
  71. <el-table-column
  72. prop="name"
  73. label="模板名称"
  74. />
  75. </el-table>
  76. <el-button v-show="showTable" type="primary" @click="onGenerate">生成代码</el-button>
  77. </el-col>
  78. </el-row>
  79. <el-dialog
  80. :title="datasourceTitle"
  81. :visible.sync="datasourceDlgShow"
  82. >
  83. <el-form
  84. ref="datasourceForm"
  85. :model="datasourceFormData"
  86. :rules="datasourceRule"
  87. size="mini"
  88. label-width="120px"
  89. >
  90. <el-form-item label="数据库类型">
  91. <el-select
  92. v-model="datasourceFormData.dbType"
  93. filterable
  94. default-first-option
  95. >
  96. <el-option
  97. v-for="item in dbTypeConfig"
  98. :key="item.dbType"
  99. :label="item.label"
  100. :value="item.dbType"
  101. />
  102. </el-select>
  103. </el-form-item>
  104. <el-form-item label="Host" prop="host">
  105. <el-input v-model="datasourceFormData.host" placeholder="地址" show-word-limit maxlength="100" />
  106. </el-form-item>
  107. <el-form-item label="Port" prop="port">
  108. <el-input v-model="datasourceFormData.port" placeholder="端口" show-word-limit maxlength="10" />
  109. </el-form-item>
  110. <el-form-item label="Database" prop="dbName">
  111. <el-input v-model="datasourceFormData.dbName" placeholder="数据库" show-word-limit maxlength="64" />
  112. </el-form-item>
  113. <el-form-item label="Username" prop="username">
  114. <el-input v-model="datasourceFormData.username" placeholder="用户名" show-word-limit maxlength="100" />
  115. </el-form-item>
  116. <el-form-item label="Password" prop="password">
  117. <el-input v-model="datasourceFormData.password" type="password" placeholder="密码" show-word-limit maxlength="100" />
  118. </el-form-item>
  119. <el-form-item>
  120. <el-button type="success" @click="onDatasourceTest">测试连接</el-button>
  121. <el-button type="primary" @click="onDatasourceSave">保存</el-button>
  122. </el-form-item>
  123. </el-form>
  124. </el-dialog>
  125. </div>
  126. </template>
  127. <style lang="scss">
  128. .code-gen {
  129. margin: 0 auto;
  130. width: 70%;
  131. .el-input { width: 450px;}
  132. .el-row h4 {
  133. text-align: center;
  134. }
  135. .el-row .el-button {
  136. margin-top: 20px;
  137. }
  138. }
  139. .el-table .hidden-row {
  140. display: none;
  141. }
  142. </style>
  143. <script>
  144. export default {
  145. data() {
  146. return {
  147. showTable: false,
  148. clientParam: {
  149. datasourceConfigId: '',
  150. tableNames: [],
  151. templateConfigIdList: [],
  152. packageName: ''
  153. },
  154. tableSearch: '',
  155. datasourceConfigList: [],
  156. tableListData: [],
  157. templateListData: [],
  158. // add datasource
  159. datasourceTitle: '新建连接',
  160. datasourceDlgShow: false,
  161. datasourceFormData: {
  162. id: 0,
  163. dbType: 1,
  164. host: '',
  165. port: '',
  166. username: '',
  167. password: '',
  168. dbName: ''
  169. },
  170. dbTypeConfig: [],
  171. datasourceRule: {
  172. host: [
  173. { required: true, message: '不能为空', trigger: 'blur' }
  174. ],
  175. port: [
  176. { required: true, message: '不能为空', trigger: 'blur' }
  177. ],
  178. username: [
  179. { required: true, message: '不能为空', trigger: 'blur' }
  180. ],
  181. password: [
  182. { required: true, message: '不能为空', trigger: 'blur' }
  183. ],
  184. dbName: [
  185. { required: true, message: '不能为空', trigger: 'blur' }
  186. ]
  187. }
  188. }
  189. },
  190. created() {
  191. this.loadDataSource()
  192. this.loadTemplate()
  193. this.loadDbType()
  194. },
  195. methods: {
  196. tableRowClassName: function ({row, index}) {
  197. row.hidden = false
  198. if (this.tableSearch.length === 0) {
  199. return ''
  200. }
  201. if (!(row.tableName && row.tableName.indexOf(this.tableSearch) > -1)) {
  202. row.hidden = true
  203. return 'hidden-row';
  204. }
  205. return ''
  206. },
  207. loadDataSource() {
  208. this.post('/datasource/list', {}, resp => {
  209. this.datasourceConfigList = resp.data
  210. })
  211. },
  212. loadTemplate() {
  213. this.post('/template/list', {}, resp => {
  214. this.templateListData = resp.data
  215. })
  216. },
  217. loadDbType() {
  218. this.post('/datasource/dbtype', {}, resp => {
  219. this.dbTypeConfig = resp.data
  220. })
  221. },
  222. onDataSourceAdd() {
  223. this.datasourceTitle = '新建连接'
  224. this.datasourceFormData.id = 0
  225. this.datasourceDlgShow = true
  226. },
  227. onTableListSelect(selectedRows) {
  228. this.clientParam.tableNames = selectedRows
  229. .filter(row => row.hidden === undefined || row.hidden === false)
  230. .map(row => row.tableName)
  231. },
  232. onTemplateListSelect(selectedRows) {
  233. this.clientParam.templateConfigIdList = selectedRows.map(row => row.id)
  234. },
  235. onDataSourceChange(datasourceConfigId) {
  236. this.clientParam.datasourceConfigId = datasourceConfigId
  237. this.post(`/datasource/table/${datasourceConfigId}`, {}, resp => {
  238. this.showTable = true
  239. this.tableListData = resp.data
  240. })
  241. },
  242. onDataSourceUpdate(item) {
  243. this.datasourceTitle = '修改连接'
  244. Object.assign(this.datasourceFormData, item)
  245. this.datasourceDlgShow = true
  246. },
  247. onDataSourceDuplicate(item) {
  248. this.datasourceTitle = `${item.host} Copy`
  249. Object.assign(this.datasourceFormData, item)
  250. this.datasourceFormData.id = 0
  251. this.datasourceDlgShow = true
  252. },
  253. onDataSourceDelete(row) {
  254. this.confirm(`确认要删除 ${row.dbName} 吗?`, function(done) {
  255. const data = {
  256. id: row.id
  257. }
  258. this.post('/datasource/del', data, function() {
  259. done()
  260. location.reload()
  261. })
  262. })
  263. },
  264. onGenerate() {
  265. this.$refs.genForm.validate((valid) => {
  266. if (valid) {
  267. if (this.clientParam.tableNames.length === 0) {
  268. this.tip('请勾选表', 'error')
  269. return
  270. }
  271. if (this.clientParam.templateConfigIdList.length === 0) {
  272. this.tip('请勾选模板', 'error')
  273. return
  274. }
  275. const config = JSON.stringify(this.clientParam)
  276. this.goRoute(`result/${config}`)
  277. }
  278. })
  279. },
  280. onDatasourceTest() {
  281. this.$refs.datasourceForm.validate((valid) => {
  282. if (valid) {
  283. this.post('/datasource/test', this.datasourceFormData, resp => {
  284. this.tip('连接成功')
  285. })
  286. }
  287. })
  288. },
  289. onDatasourceSave() {
  290. this.$refs.datasourceForm.validate((valid) => {
  291. if (valid) {
  292. this.post('/datasource/test', this.datasourceFormData, resp => {
  293. if (this.datasourceFormData.id) {
  294. this.post('/datasource/update', this.datasourceFormData, resp => {
  295. location.reload()
  296. })
  297. } else {
  298. this.post('/datasource/add', this.datasourceFormData, resp => {
  299. this.tip('添加成功')
  300. this.loadDataSource()
  301. this.datasourceDlgShow = false
  302. })
  303. }
  304. })
  305. }
  306. })
  307. }
  308. }
  309. }
  310. </script>