index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. <template>
  2. <BasicLayout>
  3. <template #wrapper>
  4. <el-card class="box-card">
  5. <el-form ref="queryForm" :model="queryParams" :inline="true" label-position="left">
  6. <el-form-item label="表名称" prop="tableName">
  7. <el-input
  8. v-model="queryParams.tableName"
  9. placeholder="请输入表名称"
  10. clearable
  11. size="small"
  12. @keyup.enter.native="handleQuery"
  13. />
  14. </el-form-item>
  15. <el-form-item label="菜单名称" prop="tableComment">
  16. <el-input
  17. v-model="queryParams.tableComment"
  18. placeholder="请输入菜单名称"
  19. clearable
  20. size="small"
  21. @keyup.enter.native="handleQuery"
  22. />
  23. </el-form-item>
  24. <el-form-item>
  25. <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
  26. <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
  27. </el-form-item>
  28. </el-form>
  29. <el-row :gutter="10" class="mb8">
  30. <el-col :span="1.5">
  31. <!-- <el-button
  32. type="primary"
  33. icon="el-icon-download"
  34. size="mini"
  35. @click="handleGenTable"
  36. >生成</el-button> -->
  37. </el-col>
  38. <el-col :span="1.5">
  39. <el-button
  40. type="info"
  41. icon="el-icon-upload"
  42. size="mini"
  43. @click="openImportTable"
  44. >导入</el-button>
  45. </el-col>
  46. <el-col :span="1.5">
  47. <el-button
  48. type="success"
  49. icon="el-icon-edit"
  50. size="mini"
  51. :disabled="single"
  52. @click="handleEditTable"
  53. >修改</el-button>
  54. </el-col>
  55. <el-col :span="1.5">
  56. <el-button
  57. type="danger"
  58. icon="el-icon-delete"
  59. size="mini"
  60. :disabled="multiple"
  61. @click="handleDelete"
  62. >删除</el-button>
  63. </el-col>
  64. </el-row>
  65. <el-table v-loading="loading" :data="tableList" @selection-change="handleSelectionChange">
  66. <el-table-column type="selection" width="55" />
  67. <el-table-column label="序号" align="center" prop="tableId" width="50px" />
  68. <el-table-column
  69. label="表名称"
  70. align="center"
  71. prop="tableName"
  72. :show-overflow-tooltip="true"
  73. width="130"
  74. />
  75. <el-table-column
  76. label="菜单名称"
  77. align="center"
  78. prop="tableComment"
  79. :show-overflow-tooltip="true"
  80. width="130"
  81. />
  82. <el-table-column
  83. label="模型名称"
  84. align="center"
  85. prop="className"
  86. :show-overflow-tooltip="true"
  87. width="130"
  88. />
  89. <el-table-column label="创建时间" align="center" prop="createdAt" width="165">
  90. <template slot-scope="scope">
  91. <span>{{ parseTime(scope.row.createdAt) }}</span>
  92. </template>
  93. </el-table-column>
  94. <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
  95. <template slot-scope="scope">
  96. <el-button
  97. type="text"
  98. size="small"
  99. icon="el-icon-edit"
  100. @click="handleEditTable(scope.row)"
  101. >编辑</el-button>
  102. <el-button
  103. type="text"
  104. size="small"
  105. icon="el-icon-view"
  106. @click="handlePreview(scope.row)"
  107. >预览</el-button>
  108. <el-popconfirm
  109. class="delete-popconfirm"
  110. title="正在使用代码生成请确认?"
  111. confirm-button-text="生成"
  112. @onConfirm="handleToProject(scope.row)"
  113. >
  114. <el-button
  115. slot="reference"
  116. type="text"
  117. size="small"
  118. icon="el-icon-view"
  119. >代码生成</el-button>
  120. </el-popconfirm>
  121. <el-popconfirm
  122. class="delete-popconfirm"
  123. title="正在使用【菜单以及API生成到数据库】请确认?"
  124. confirm-button-text="写入DB"
  125. @onConfirm="handleToDB(scope.row)"
  126. >
  127. <el-button
  128. slot="reference"
  129. type="text"
  130. size="small"
  131. icon="el-icon-view"
  132. >生成配置</el-button>
  133. </el-popconfirm>
  134. <el-popconfirm
  135. class="delete-popconfirm"
  136. title="正在使用代码生成配置迁移脚本请确认?"
  137. confirm-button-text="生成"
  138. @onConfirm="handleToApiFile(scope.row)"
  139. >
  140. <el-button
  141. slot="reference"
  142. type="text"
  143. size="small"
  144. icon="el-icon-view"
  145. >生成迁移脚本</el-button>
  146. </el-popconfirm>
  147. <el-popconfirm
  148. class="delete-popconfirm"
  149. title="确认删除数据项?"
  150. @onConfirm="handleSingleDelete(scope.row)"
  151. >
  152. <el-button
  153. slot="reference"
  154. type="text"
  155. size="small"
  156. icon="el-icon-delete"
  157. >删除</el-button>
  158. </el-popconfirm>
  159. </template>
  160. </el-table-column>
  161. </el-table>
  162. <pagination
  163. v-show="total>0"
  164. :total="total"
  165. :page.sync="queryParams.pageIndex"
  166. :limit.sync="queryParams.pageSize"
  167. @pagination="getList"
  168. />
  169. </el-card>
  170. <!-- 预览界面 -->
  171. <el-dialog class="preview" :title="preview.title" :visible.sync="preview.open" fullscreen>
  172. <div class="el-dialog-container">
  173. <div class="tag-group">
  174. <!-- eslint-disable-next-line vue/valid-v-for -->
  175. <el-tag v-for="(value, key) in preview.data" @click="codeChange(key)">
  176. <template>
  177. {{ key.substring(key.lastIndexOf('/')+1,key.indexOf('.go.template')) }}
  178. </template>
  179. </el-tag>
  180. </div>
  181. <div id="codemirror">
  182. <codemirror ref="cmEditor" :value="codestr" :options="cmOptions" />
  183. </div>
  184. <!-- <el-tabs v-model="preview.activeName" tab-position="left">
  185. <el-tab-pane
  186. v-for="(value, key) in preview.data"
  187. :key="key"
  188. :label="key.substring(key.lastIndexOf('/')+1,key.indexOf('.template'))"
  189. :name="key.substring(key.lastIndexOf('/')+1,key.indexOf('.template'))"
  190. >
  191. <pre class="pre"/>
  192. </el-tab-pane>
  193. </el-tabs> -->
  194. </div>
  195. </el-dialog>
  196. <import-table ref="importTB" @ok="handleQuery" />
  197. </template>
  198. </BasicLayout>
  199. </template>
  200. <script>
  201. import { listTable, previewTable, delTable, toDBTable, toProjectTableCheckRole, apiToFile } from '@/api/tools/gen'
  202. import importTable from './importTable'
  203. import { downLoadFile } from '@/utils/zipdownload'
  204. import { codemirror } from 'vue-codemirror'
  205. import 'codemirror/theme/material-palenight.css'
  206. require('codemirror/mode/javascript/javascript')
  207. import 'codemirror/mode/javascript/javascript'
  208. import 'codemirror/mode/go/go'
  209. import 'codemirror/mode/vue/vue'
  210. export default {
  211. name: 'Gen',
  212. components: { importTable, codemirror },
  213. data() {
  214. return {
  215. cmOptions: {
  216. tabSize: 4,
  217. theme: 'material-palenight',
  218. mode: 'text/javascript',
  219. lineNumbers: true,
  220. line: true
  221. // more CodeMirror options...
  222. },
  223. codestr: '',
  224. // 遮罩层
  225. loading: true,
  226. // 唯一标识符
  227. uniqueId: '',
  228. // 选中数组
  229. ids: [],
  230. // 选中表数组
  231. tableNames: [],
  232. // 非单个禁用
  233. single: true,
  234. // 非多个禁用
  235. multiple: true,
  236. // 总条数
  237. total: 0,
  238. // 表数据
  239. tableList: [],
  240. // 日期范围
  241. dateRange: '',
  242. // 查询参数
  243. queryParams: {
  244. pageIndex: 1,
  245. pageSize: 10,
  246. tableName: undefined,
  247. tableComment: undefined
  248. },
  249. // 预览参数
  250. preview: {
  251. open: false,
  252. title: '代码预览',
  253. data: {},
  254. activeName: 'api.go'
  255. }
  256. }
  257. },
  258. created() {
  259. this.getList()
  260. },
  261. activated() {
  262. const time = this.$route.query.t
  263. if (time !== null && time !== this.uniqueId) {
  264. this.uniqueId = time
  265. this.resetQuery()
  266. }
  267. },
  268. methods: {
  269. /** 查询表集合 */
  270. getList() {
  271. this.loading = true
  272. listTable(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
  273. this.tableList = response.data.list
  274. this.total = response.data.count
  275. this.loading = false
  276. }
  277. )
  278. },
  279. codeChange(e) {
  280. if (e.indexOf('js') > -1) {
  281. this.cmOptions.mode = 'text/javascript'
  282. }
  283. if (e.indexOf('model') > -1 || e.indexOf('router') > -1 || e.indexOf('api') > -1 || e.indexOf('service') > -1 || e.indexOf('dto') > -1) {
  284. this.cmOptions.mode = 'text/x-go'
  285. }
  286. if (e.indexOf('vue') > -1) {
  287. this.cmOptions.mode = 'text/x-vue'
  288. }
  289. this.codestr = this.preview.data[e]
  290. },
  291. /** 搜索按钮操作 */
  292. handleQuery() {
  293. this.queryParams.pageIndex = 1
  294. this.getList()
  295. },
  296. /** 生成代码操作 */
  297. handleGenTable(row) {
  298. const ids = row.tableId || this.ids
  299. if (ids === '') {
  300. this.msgError('请选择要生成的数据')
  301. return
  302. }
  303. downLoadFile('/api/v1/gen/gencode/' + ids)
  304. },
  305. /** 打开导入表弹窗 */
  306. openImportTable() {
  307. this.$refs.importTB.show()
  308. },
  309. /** 重置按钮操作 */
  310. resetQuery() {
  311. this.dateRange = []
  312. this.resetForm('queryForm')
  313. this.handleQuery()
  314. },
  315. /** 预览按钮 */
  316. handlePreview(row) {
  317. previewTable(row.tableId).then(response => {
  318. this.preview.data = response.data
  319. this.preview.open = true
  320. this.codeChange('template/api.go.template')
  321. })
  322. },
  323. handleToProject(row) {
  324. toProjectTableCheckRole(row.tableId, false).then((response) => {
  325. this.msgSuccess(response.msg)
  326. }).catch(function() {})
  327. },
  328. handleToApiFile(row) {
  329. apiToFile(row.tableId, true).then((response) => {
  330. this.msgSuccess(response.msg)
  331. }).catch(function() {})
  332. },
  333. handleToDB(row) {
  334. toDBTable(row.tableId).then((response) => {
  335. this.msgSuccess(response.msg)
  336. }).catch(function() {})
  337. },
  338. // 多选框选中数据
  339. handleSelectionChange(selection) {
  340. this.ids = selection.map(item => item.tableId)
  341. this.tableNames = selection.map(item => item.tableName)
  342. this.single = selection.length !== 1
  343. this.multiple = !selection.length
  344. },
  345. /** 修改按钮操作 */
  346. handleEditTable(row) {
  347. const tableId = row.tableId || this.ids[0]
  348. this.$router.push({ path: '/dev-tools/editTable', query: { tableId: tableId }})
  349. },
  350. /** 删除按钮操作 */
  351. handleDelete(row) {
  352. const tableIds = row.tableId || this.ids
  353. this.$confirm('是否确认删除表编号为"' + tableIds + '"的数据项?', '警告', {
  354. confirmButtonText: '确定',
  355. cancelButtonText: '取消',
  356. type: 'warning'
  357. }).then(function() {
  358. return delTable(tableIds)
  359. }).then((response) => {
  360. if (response.code === 200) {
  361. this.msgSuccess(response.msg)
  362. this.open = false
  363. this.getList()
  364. } else {
  365. this.msgError(response.msg)
  366. }
  367. }).catch(function() {})
  368. },
  369. handleSingleDelete(row) {
  370. const tableIds = row.tableId || this.ids
  371. delTable(tableIds).then((response) => {
  372. if (response.code === 200) {
  373. this.msgSuccess(response.msg)
  374. this.open = false
  375. this.getList()
  376. } else {
  377. this.msgError(response.msg)
  378. }
  379. }).catch(function() {})
  380. }
  381. }
  382. }
  383. </script>
  384. <style lang="scss" scoped>
  385. .el-dialog-container ::v-deep{
  386. overflow: hidden;
  387. .el-scrollbar__view{
  388. height: 100%;
  389. }
  390. .pre{
  391. height: 546px;
  392. overflow: hidden;
  393. .el-scrollbar{
  394. height: 100%;
  395. }
  396. }
  397. .el-scrollbar__wrap::-webkit-scrollbar{
  398. display: none;
  399. }
  400. }
  401. ::v-deep .el-dialog__body{
  402. padding: 0 20px;
  403. margin:0;
  404. }
  405. .tag-group {
  406. margin: 0 0 10px -10px;
  407. }
  408. .tag-group .el-tag{
  409. margin-left: 10px;
  410. }
  411. .el-tag {
  412. cursor: pointer;
  413. }
  414. </style>
  415. <style lang="scss">
  416. #codemirror {
  417. height: auto;
  418. margin: 0;
  419. overflow: auto;
  420. }
  421. .CodeMirror {
  422. border: 1px solid #eee;
  423. height: 600px;
  424. }
  425. </style>