index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <div class="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="可选,如:cn.studyjava.xxx" show-word-limit maxlength="100" />
  30. </el-form-item>
  31. <el-form-item v-show="showTable" label="删除前缀">
  32. <el-input v-model="clientParam.delPrefix" placeholder="可选,如:sys_user对应Java类为User(多前缀逗号隔开)" show-word-limit maxlength="100" />
  33. </el-form-item>
  34. </el-form>
  35. <el-row v-show="showTable" :gutter="20">
  36. <el-col :span="12">
  37. <h4>选择表</h4>
  38. <el-input
  39. v-model="tableSearch"
  40. prefix-icon="el-icon-search"
  41. clearable
  42. size="mini"
  43. placeholder="过滤表"
  44. style="margin-bottom: 10px;width: 100%;"
  45. />
  46. <el-table
  47. :data="tableListData"
  48. border
  49. :cell-style="cellStyleSmall()"
  50. :header-cell-style="headCellStyleSmall()"
  51. :row-class-name="tableRowClassName"
  52. @selection-change="onTableListSelect"
  53. >
  54. <el-table-column
  55. type="selection"
  56. />
  57. <el-table-column
  58. prop="tableName"
  59. label="表名"
  60. />
  61. </el-table>
  62. </el-col>
  63. <el-col :span="12" id="templateSelect">
  64. <h4>选择模板</h4>
  65. <el-select
  66. v-model="clientParam.groupName"
  67. placeholder="选择模板所在组"
  68. size="mini"
  69. @change="onDataGroupChange"
  70. style="margin-bottom: 10px; width: 100%;"
  71. >
  72. <el-option
  73. v-for="item in groupData"
  74. :key="item.id"
  75. :label="`${item.groupName}`"
  76. :value="item.id"
  77. >
  78. </el-option>
  79. </el-select>
  80. <el-table
  81. :data="templateListData"
  82. border
  83. :cell-style="cellStyleSmall()"
  84. :header-cell-style="headCellStyleSmall()"
  85. :row-class-name="templateTableRowClassName"
  86. @selection-change="onTemplateListSelect"
  87. >
  88. <el-table-column
  89. type="selection"
  90. />
  91. <el-table-column
  92. prop="name"
  93. label="模板名称"
  94. >
  95. <span slot-scope="scope">
  96. <!-- {{scope.row.groupName}}-{{scope.row.name}}-->
  97. {{scope.row.name}}
  98. </span>
  99. </el-table-column>
  100. </el-table>
  101. <el-button v-show="showTable" type="primary" @click="onGenerate">生成代码</el-button>
  102. </el-col>
  103. </el-row>
  104. <el-dialog
  105. :title="datasourceTitle"
  106. :visible.sync="datasourceDlgShow"
  107. >
  108. <el-form
  109. ref="datasourceForm"
  110. :model="datasourceFormData"
  111. :rules="datasourceRule"
  112. size="mini"
  113. label-width="120px"
  114. >
  115. <el-form-item label="数据库类型">
  116. <el-select
  117. v-model="datasourceFormData.dbType"
  118. filterable
  119. default-first-option
  120. >
  121. <el-option
  122. v-for="item in dbTypeConfig"
  123. :key="item.dbType"
  124. :label="item.label"
  125. :value="item.dbType"
  126. />
  127. </el-select>
  128. </el-form-item>
  129. <el-form-item label="Host" prop="host">
  130. <el-input v-model="datasourceFormData.host" placeholder="地址" show-word-limit maxlength="100" />
  131. </el-form-item>
  132. <el-form-item label="Port" prop="port">
  133. <el-input v-model="datasourceFormData.port" placeholder="端口" show-word-limit maxlength="10" />
  134. </el-form-item>
  135. <el-form-item label="Database" prop="dbName">
  136. <el-input v-model="datasourceFormData.dbName" placeholder="数据库" show-word-limit maxlength="64" />
  137. </el-form-item>
  138. <el-form-item label="Username" prop="username">
  139. <el-input v-model="datasourceFormData.username" placeholder="用户名" show-word-limit maxlength="100" />
  140. </el-form-item>
  141. <el-form-item label="Password" prop="password">
  142. <el-input v-model="datasourceFormData.password" type="password" placeholder="密码" show-word-limit maxlength="100" />
  143. </el-form-item>
  144. <el-form-item label="包名" prop="packageName">
  145. <el-input v-model="datasourceFormData.packageName" placeholder="包名(package)" show-word-limit maxlength="100" />
  146. </el-form-item>
  147. <el-form-item label="删除前缀" prop="delPrefix">
  148. <el-input v-model="datasourceFormData.delPrefix" placeholder="删除前缀(表名sys_user删除前缀sys_对应bean为User)多前缀逗号隔开" show-word-limit maxlength="200" />
  149. </el-form-item>
  150. <el-form-item label="代码生成器模板" prop="delPrefix">
  151. <el-select
  152. v-model="datasourceFormData.groupName"
  153. placeholder="选择模板所在组"
  154. size="mini"
  155. @change="onDataGroupChange"
  156. style="margin-bottom: 10px; width: 100%;"
  157. >
  158. <el-option
  159. v-for="item in groupData"
  160. :key="item.id"
  161. :label="`${item.groupName}`"
  162. :value="item.id"
  163. >
  164. </el-option>
  165. </el-select>
  166. </el-form-item>
  167. <el-form-item>
  168. <el-button type="success" @click="onDatasourceTest">测试连接</el-button>
  169. <el-button type="primary" @click="onDatasourceSave">保存</el-button>
  170. </el-form-item>
  171. </el-form>
  172. </el-dialog>
  173. </div>
  174. </template>
  175. <style lang="scss">
  176. .code-gen {
  177. margin: 0 auto;
  178. width: 70%;
  179. .el-input { width: 450px;}
  180. .el-row h4 {
  181. text-align: center;
  182. }
  183. .el-row .el-button {
  184. margin-top: 20px;
  185. }
  186. }
  187. .el-table .hidden-row {
  188. display: none;
  189. }
  190. #templateSelect {
  191. .el-input { width: 100%;}
  192. }
  193. </style>
  194. <script>
  195. export default {
  196. name: 'GenerateConfig',
  197. data() {
  198. return {
  199. groupId: '',
  200. groupData: {},
  201. showTable: false,
  202. clientParam: {
  203. datasourceConfigId: '',
  204. tableNames: [],
  205. templateConfigIdList: [],
  206. packageName: null,
  207. delPrefix: null,
  208. groupId: '',
  209. groupName: ''
  210. },
  211. tableSearch: '',
  212. datasourceConfigList: [],
  213. tableListData: [],
  214. templateListData: [],
  215. // add datasource
  216. datasourceTitle: '新建连接',
  217. datasourceDlgShow: false,
  218. datasourceFormData: {
  219. id: 0,
  220. dbType: 1,
  221. host: '',
  222. port: '',
  223. username: '',
  224. password: '',
  225. dbName: '',
  226. packageName: '',
  227. delPrefix: '',
  228. groupId: '',
  229. groupName: ''
  230. },
  231. dbTypeConfig: [],
  232. datasourceRule: {
  233. host: [
  234. { required: true, message: '不能为空', trigger: 'blur' }
  235. ],
  236. port: [
  237. { required: true, message: '不能为空', trigger: 'blur' }
  238. ],
  239. username: [
  240. { required: true, message: '不能为空', trigger: 'blur' }
  241. ],
  242. password: [
  243. { required: true, message: '不能为空', trigger: 'blur' }
  244. ],
  245. dbName: [
  246. { required: true, message: '不能为空', trigger: 'blur' }
  247. ]
  248. }
  249. }
  250. },
  251. created() {
  252. this.loadDataSource()
  253. this.loadTemplate()
  254. this.loadDbType()
  255. this.loadGroups()
  256. },
  257. methods: {
  258. tableRowClassName: function ({row, index}) {
  259. // console.log("tablerow="+row.tableName+","+index)
  260. row.hidden = false
  261. if (this.tableSearch.length === 0) {
  262. return ''
  263. }
  264. // console.log("tablerow="+row.tableName +","+ row.tableName.indexOf(this.tableName)+","+(!(row.tableName && row.tableName.indexOf(this.tableSearch) > -1)))
  265. if (!(row.tableName && row.tableName.toLowerCase().indexOf(this.tableSearch.toLowerCase()) > -1)) {
  266. row.hidden = true
  267. return 'hidden-row';
  268. }
  269. return ''
  270. },
  271. templateTableRowClassName: function ({row, index}) {
  272. // console.log("temprow="+row.id+",rowGroupId="+row.groupId+", this.groupId="+ this.groupId)
  273. row.hidden = false
  274. if (this.groupId == '' || this.groupId <= 0) {
  275. return ''
  276. }
  277. // console.log("temprow="+row.groupId +","+(row.groupId && row.groupId == this.groupId))
  278. if (row.groupId && row.groupId == this.groupId) {
  279. return ''
  280. }
  281. row.hidden = true
  282. return 'hidden-row';
  283. },
  284. loadGroups() {
  285. this.post(`/group/list/`, {}, function(resp) {
  286. this.groupData = resp.data
  287. })
  288. },
  289. loadDataSource() {
  290. this.post('/datasource/list', {}, resp => {
  291. this.datasourceConfigList = resp.data
  292. })
  293. },
  294. loadTemplate() {
  295. this.post('/template/list', {}, resp => {
  296. this.templateListData = resp.data
  297. })
  298. },
  299. loadDbType() {
  300. this.post('/datasource/dbtype', {}, resp => {
  301. this.dbTypeConfig = resp.data
  302. })
  303. },
  304. onDataSourceAdd() {
  305. this.datasourceTitle = '新建连接'
  306. Object.keys(this.datasourceFormData).forEach(key=>{this.datasourceFormData[key]=''})
  307. this.datasourceFormData.id = 0
  308. this.datasourceDlgShow = true
  309. },
  310. onTableListSelect(selectedRows) {
  311. this.clientParam.tableNames = selectedRows
  312. .filter(row => row.hidden === undefined || row.hidden === false)
  313. .map(row => row.tableName)
  314. },
  315. onTemplateListSelect(selectedRows) {
  316. this.clientParam.templateConfigIdList = selectedRows
  317. .filter(row => row.hidden === undefined || row.hidden === false)
  318. .map(row => row.id)
  319. },
  320. onDataSourceChange(datasourceConfigId) {
  321. this.clientParam.datasourceConfigId = datasourceConfigId
  322. this.datasourceConfigList.find((item)=>{
  323. if(item.id === datasourceConfigId){
  324. this.clientParam.packageName = item.packageName;
  325. this.clientParam.delPrefix = item.delPrefix;
  326. this.groupId = item.groupId;
  327. this.groupData.find((gitem)=>{
  328. // console.log("gid="+gitem.id+",datasourceConfigGroupId="+item.groupId+","+(gitem.id == item.groupId))
  329. if(gitem.id == item.groupId){
  330. this.clientParam.groupName = gitem.groupName
  331. }
  332. });
  333. console.log("this.clientParam.groupName="+this.clientParam.groupName)
  334. }
  335. });
  336. this.post(`/datasource/table/${datasourceConfigId}`, {}, resp => {
  337. this.showTable = true
  338. this.tableListData = resp.data
  339. })
  340. },
  341. onDataSourceUpdate(item) {
  342. this.datasourceTitle = '修改连接'
  343. Object.assign(this.datasourceFormData, item)
  344. this.groupData.find((gitem)=>{
  345. // console.log("gid="+gitem.id+",datasourceConfigGroupId="+item.groupId+","+(gitem.id == this.datasourceFormData.groupId))
  346. if(gitem.id == item.groupId){
  347. this.datasourceFormData.groupName = gitem.groupName
  348. }
  349. });
  350. this.datasourceDlgShow = true
  351. },
  352. onDataSourceDuplicate(item) {
  353. this.datasourceTitle = `${item.host} Copy`
  354. Object.assign(this.datasourceFormData, item)
  355. this.datasourceFormData.id = 0
  356. this.datasourceDlgShow = true
  357. },
  358. onDataSourceDelete(row) {
  359. this.confirm(`确认要删除 ${row.dbName} 吗?`, function(done) {
  360. const data = {
  361. id: row.id
  362. }
  363. this.post('/datasource/del', data, function() {
  364. done()
  365. location.reload()
  366. })
  367. })
  368. },
  369. onGenerate() {
  370. this.$refs.genForm.validate((valid) => {
  371. if (valid) {
  372. if (this.clientParam.tableNames.length === 0) {
  373. this.tip('请勾选表', 'error')
  374. return
  375. }
  376. if (this.clientParam.templateConfigIdList.length === 0) {
  377. this.tip('请勾选模板', 'error')
  378. return
  379. }
  380. const config = JSON.stringify(this.clientParam)
  381. this.goRoute(`result/${config}`)
  382. }
  383. })
  384. },
  385. onDatasourceTest() {
  386. this.$refs.datasourceForm.validate((valid) => {
  387. if (valid) {
  388. this.post('/datasource/test', this.datasourceFormData, resp => {
  389. this.tip('连接成功')
  390. })
  391. }
  392. })
  393. },
  394. onDataGroupChange(groupId){
  395. // console.log(groupId)
  396. if(groupId != ''){
  397. this.groupId = groupId
  398. this.datasourceFormData.groupId = groupId
  399. }
  400. },
  401. onDatasourceSave() {
  402. this.$refs.datasourceForm.validate((valid) => {
  403. if (valid) {
  404. this.post('/datasource/test', this.datasourceFormData, resp => {
  405. if (this.datasourceFormData.id) {
  406. this.post('/datasource/update', this.datasourceFormData, resp => {
  407. location.reload()
  408. })
  409. } else {
  410. this.post('/datasource/add', this.datasourceFormData, resp => {
  411. this.tip('添加成功')
  412. this.loadDataSource()
  413. this.datasourceDlgShow = false
  414. })
  415. }
  416. })
  417. }
  418. })
  419. }
  420. }
  421. }
  422. </script>