index.vue 9.8 KB

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