index.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <template>
  2. <!-- <doc-alert title="代码生成(单表)" url="https://doc.iocoder.cn/new-feature/" />
  3. <doc-alert title="代码生成(树表)" url="https://doc.iocoder.cn/new-feature/tree/" />
  4. <doc-alert title="代码生成(主子表)" url="https://doc.iocoder.cn/new-feature/master-sub/" />
  5. <doc-alert title="单元测试" url="https://doc.iocoder.cn/unit-test/" /> -->
  6. <!-- 搜索 -->
  7. <ContentWrap>
  8. <el-form
  9. ref="queryFormRef"
  10. :inline="true"
  11. :model="queryParams"
  12. class="-mb-15px"
  13. label-width="68px"
  14. >
  15. <el-form-item label="表名称" prop="tableName">
  16. <el-input
  17. v-model="queryParams.tableName"
  18. class="!w-240px"
  19. clearable
  20. placeholder="请输入表名称"
  21. @keyup.enter="handleQuery"
  22. />
  23. </el-form-item>
  24. <el-form-item label="表描述" prop="tableComment">
  25. <el-input
  26. v-model="queryParams.tableComment"
  27. class="!w-240px"
  28. clearable
  29. placeholder="请输入表描述"
  30. @keyup.enter="handleQuery"
  31. />
  32. </el-form-item>
  33. <el-form-item label="创建时间" prop="createTime">
  34. <el-date-picker
  35. v-model="queryParams.createTime"
  36. :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
  37. class="!w-240px"
  38. end-placeholder="结束日期"
  39. start-placeholder="开始日期"
  40. type="daterange"
  41. value-format="YYYY-MM-DD HH:mm:ss"
  42. />
  43. </el-form-item>
  44. <el-form-item>
  45. <el-button @click="handleQuery">
  46. <Icon class="mr-5px" icon="ep:search" />
  47. 搜索
  48. </el-button>
  49. <el-button @click="resetQuery">
  50. <Icon class="mr-5px" icon="ep:refresh" />
  51. 重置
  52. </el-button>
  53. <el-button v-hasPermi="['infra:codegen:create']" type="primary" @click="openImportTable()">
  54. <Icon class="mr-5px" icon="ep:zoom-in" />
  55. 导入
  56. </el-button>
  57. </el-form-item>
  58. </el-form>
  59. </ContentWrap>
  60. <!-- 列表 -->
  61. <ContentWrap>
  62. <el-table v-loading="loading" :data="list">
  63. <el-table-column align="center" label="数据源">
  64. <template #default="scope">
  65. {{
  66. dataSourceConfigList.find((config) => config.id === scope.row.dataSourceConfigId)?.name
  67. }}
  68. </template>
  69. </el-table-column>
  70. <el-table-column align="center" label="表名称" prop="tableName" width="200" />
  71. <el-table-column
  72. :show-overflow-tooltip="true"
  73. align="center"
  74. label="表描述"
  75. prop="tableComment"
  76. width="200"
  77. />
  78. <el-table-column align="center" label="实体" prop="className" width="200" />
  79. <el-table-column
  80. :formatter="dateFormatter"
  81. align="center"
  82. label="创建时间"
  83. prop="createTime"
  84. width="180"
  85. />
  86. <el-table-column
  87. :formatter="dateFormatter"
  88. align="center"
  89. label="更新时间"
  90. prop="createTime"
  91. width="180"
  92. />
  93. <el-table-column align="center" fixed="right" label="操作" width="300px">
  94. <template #default="scope">
  95. <el-button
  96. v-hasPermi="['infra:codegen:preview']"
  97. link
  98. type="primary"
  99. @click="handlePreview(scope.row)"
  100. >
  101. 预览
  102. </el-button>
  103. <el-button
  104. v-hasPermi="['infra:codegen:update']"
  105. link
  106. type="primary"
  107. @click="handleUpdate(scope.row.id)"
  108. >
  109. 编辑
  110. </el-button>
  111. <el-button
  112. v-hasPermi="['infra:codegen:delete']"
  113. link
  114. type="danger"
  115. @click="handleDelete(scope.row.id)"
  116. >
  117. 删除
  118. </el-button>
  119. <el-button
  120. v-hasPermi="['infra:codegen:update']"
  121. link
  122. type="primary"
  123. @click="handleSyncDB(scope.row)"
  124. >
  125. 同步
  126. </el-button>
  127. <el-button
  128. v-hasPermi="['infra:codegen:download']"
  129. link
  130. type="primary"
  131. @click="handleGenTable(scope.row)"
  132. >
  133. 生成代码
  134. </el-button>
  135. </template>
  136. </el-table-column>
  137. </el-table>
  138. <!-- 分页 -->
  139. <Pagination
  140. v-model:limit="queryParams.pageSize"
  141. v-model:page="queryParams.pageNo"
  142. :total="total"
  143. @pagination="getList"
  144. />
  145. </ContentWrap>
  146. <!-- 弹窗:导入表 -->
  147. <ImportTable ref="importRef" @success="getList" />
  148. <!-- 弹窗:预览代码 -->
  149. <PreviewCode ref="previewRef" />
  150. </template>
  151. <script lang="ts" setup>
  152. import { dateFormatter } from '@/utils/formatTime'
  153. import download from '@/utils/download'
  154. import * as CodegenApi from '@/api/infra/codegen'
  155. import * as DataSourceConfigApi from '@/api/infra/dataSourceConfig'
  156. import ImportTable from './ImportTable.vue'
  157. import PreviewCode from './PreviewCode.vue'
  158. defineOptions({ name: 'InfraCodegen' })
  159. const message = useMessage() // 消息弹窗
  160. const { t } = useI18n() // 国际化
  161. const { push } = useRouter() // 路由跳转
  162. const loading = ref(true) // 列表的加载中
  163. const total = ref(0) // 列表的总页数
  164. const list = ref([]) // 列表的数据
  165. const queryParams = reactive({
  166. pageNo: 1,
  167. pageSize: 10,
  168. tableName: undefined,
  169. tableComment: undefined,
  170. createTime: []
  171. })
  172. const queryFormRef = ref() // 搜索的表单
  173. const dataSourceConfigList = ref<DataSourceConfigApi.DataSourceConfigVO[]>([]) // 数据源列表
  174. /** 查询列表 */
  175. const getList = async () => {
  176. loading.value = true
  177. try {
  178. const data = await CodegenApi.getCodegenTablePage(queryParams)
  179. list.value = data.list
  180. total.value = data.total
  181. } finally {
  182. loading.value = false
  183. }
  184. }
  185. /** 搜索按钮操作 */
  186. const handleQuery = () => {
  187. queryParams.pageNo = 1
  188. getList()
  189. }
  190. /** 重置按钮操作 */
  191. const resetQuery = () => {
  192. queryFormRef.value.resetFields()
  193. handleQuery()
  194. }
  195. /** 导入操作 */
  196. const importRef = ref()
  197. const openImportTable = () => {
  198. importRef.value.open()
  199. }
  200. /** 编辑操作 */
  201. const handleUpdate = (id: number) => {
  202. push('/codegen/edit?id=' + id)
  203. }
  204. /** 预览操作 */
  205. const previewRef = ref()
  206. const handlePreview = (row: CodegenApi.CodegenTableVO) => {
  207. previewRef.value.open(row.id)
  208. }
  209. /** 删除按钮操作 */
  210. const handleDelete = async (id: number) => {
  211. try {
  212. // 删除的二次确认
  213. await message.delConfirm()
  214. // 发起删除
  215. await CodegenApi.deleteCodegenTable(id)
  216. message.success(t('common.delSuccess'))
  217. // 刷新列表
  218. await getList()
  219. } catch {}
  220. }
  221. /** 同步操作 */
  222. const handleSyncDB = async (row: CodegenApi.CodegenTableVO) => {
  223. // 基于 DB 同步
  224. const tableName = row.tableName
  225. try {
  226. await message.confirm('确认要强制同步' + tableName + '表结构吗?', t('common.reminder'))
  227. await CodegenApi.syncCodegenFromDB(row.id)
  228. message.success('同步成功')
  229. } catch {}
  230. }
  231. /** 生成代码操作 */
  232. const handleGenTable = async (row: CodegenApi.CodegenTableVO) => {
  233. const res = await CodegenApi.downloadCodegen(row.id)
  234. download.zip(res, 'codegen-' + row.className + '.zip')
  235. }
  236. /** 初始化 **/
  237. onMounted(async () => {
  238. await getList()
  239. // 加载数据源列表
  240. dataSourceConfigList.value = await DataSourceConfigApi.getDataSourceConfigList()
  241. })
  242. </script>