index.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div style="padding: 0px 0px 8px 8px">
  3. <el-row :gutter="8" style="width: 100%">
  4. <el-col :span="24" :xs="24">
  5. <el-card shadow="never" :body-style="{ paddingBottom: '0' }" style="margin-top: 8px">
  6. <el-form :model="state.filterModel" :inline="true">
  7. <el-form-item label="接口名称" prop="name">
  8. <el-input v-model="state.filterModel.name" placeholder="接口名称" />
  9. </el-form-item>
  10. <el-form-item>
  11. <el-button type="primary" icon="ele-Search" @click="onQuery"> 查询 </el-button>
  12. <el-button v-auth="'api:admin:api:add'" type="primary" icon="ele-Plus" @click="onAdd"> 新增 </el-button>
  13. <el-popconfirm title="确定要同步接口" hide-icon width="180" hide-after="0" @confirm="onSync">
  14. <template #reference>
  15. <el-button v-auth="'api:admin:api:sync'" :loading="state.syncLoading" type="primary" icon="ele-Refresh"> 同步 </el-button>
  16. </template>
  17. </el-popconfirm>
  18. </el-form-item>
  19. </el-form>
  20. </el-card>
  21. <el-card shadow="never" style="margin-top: 8px">
  22. <el-table
  23. :data="state.apiTreeData"
  24. style="width: 100%"
  25. v-loading="state.loading"
  26. row-key="id"
  27. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  28. >
  29. <el-table-column prop="label" label="接口名称" min-width="120" show-overflow-tooltip />
  30. <el-table-column prop="path" label="接口地址" min-width="120" show-overflow-tooltip />
  31. <el-table-column prop="description" label="接口描述" min-width="120" show-overflow-tooltip />
  32. <el-table-column label="状态" width="80" align="center" show-overflow-tooltip>
  33. <template #default="{ row }">
  34. <el-tag type="success" v-if="row.enabled">启用</el-tag>
  35. <el-tag type="danger" v-else>禁用</el-tag>
  36. </template>
  37. </el-table-column>
  38. <el-table-column label="操作" width="160" fixed="right" header-align="center" align="center">
  39. <template #default="{ row }">
  40. <el-button v-auth="'api:admin:api:update'" icon="ele-EditPen" size="small" text type="primary" @click="onEdit(row)">编辑</el-button>
  41. <el-button v-auth="'api:admin:api:delete'" icon="ele-Delete" size="small" text type="danger" @click="onDelete(row)">删除</el-button>
  42. </template>
  43. </el-table-column>
  44. </el-table>
  45. </el-card>
  46. </el-col>
  47. </el-row>
  48. <api-form ref="apiFormRef" :title="state.apiFormTitle" :api-tree-data="state.formApiTreeData"></api-form>
  49. </div>
  50. </template>
  51. <script lang="ts" setup>
  52. import { ref, reactive, onMounted, getCurrentInstance, onUnmounted, defineAsyncComponent } from 'vue'
  53. import { ApiListOutput } from '/@/api/admin/data-contracts'
  54. import { ApiApi } from '/@/api/admin/Api'
  55. import { ApiApi as ApiExtApi } from '/@/api/admin.extend/Api'
  56. import { listToTree } from '/@/utils/tree'
  57. import { cloneDeep, isArray } from 'lodash-es'
  58. import eventBus from '/@/utils/mitt'
  59. // 引入组件
  60. const ApiForm = defineAsyncComponent(() => import('./components/api-form.vue'))
  61. const { proxy } = getCurrentInstance() as any
  62. const apiFormRef = ref()
  63. const state = reactive({
  64. loading: false,
  65. syncLoading: false,
  66. apiFormTitle: '',
  67. filterModel: {
  68. name: '',
  69. },
  70. apiTreeData: [] as Array<ApiListOutput>,
  71. formApiTreeData: [] as Array<ApiListOutput>,
  72. })
  73. onMounted(() => {
  74. onQuery()
  75. eventBus.on('refreshApi', async () => {
  76. onQuery()
  77. })
  78. })
  79. onUnmounted(() => {
  80. eventBus.off('refreshApi')
  81. })
  82. const onQuery = async () => {
  83. state.loading = true
  84. const res = await new ApiApi().getList()
  85. if (res.data && res.data.length > 0) {
  86. state.apiTreeData = listToTree(cloneDeep(res.data))
  87. state.formApiTreeData = listToTree(res.data.filter((a) => a.parentId === 0))
  88. } else {
  89. state.apiTreeData = []
  90. state.formApiTreeData = []
  91. }
  92. state.loading = false
  93. }
  94. const onAdd = () => {
  95. state.apiFormTitle = '新增接口'
  96. apiFormRef.value.open()
  97. }
  98. const onEdit = (row: ApiListOutput) => {
  99. state.apiFormTitle = '编辑接口'
  100. apiFormRef.value.open(row)
  101. }
  102. const onDelete = (row: ApiListOutput) => {
  103. proxy.$modal
  104. .confirmDelete(`确定要删除接口【${row.label}】?`, { type: 'info' })
  105. .then(async () => {
  106. await new ApiApi().delete({ id: row.id }, { loading: true })
  107. onQuery()
  108. })
  109. .catch(() => {})
  110. }
  111. const syncApi = async (swaggerResource: any) => {
  112. const res = await new ApiExtApi().getSwaggerJson(swaggerResource.url, { showErrorMessage: false })
  113. if (!res) {
  114. return
  115. }
  116. const tags = res.tags
  117. const paths = res.paths
  118. const apis = []
  119. const urls = swaggerResource.url.split('/')
  120. const code = urls.length >= 2 ? urls[urls.length - 2] : ''
  121. if (code === '') {
  122. return
  123. }
  124. apis[apis.length] = {
  125. label: swaggerResource.name,
  126. path: code,
  127. }
  128. // tags
  129. if (tags && tags.length > 0) {
  130. tags.forEach((t: any) => {
  131. apis[apis.length] = {
  132. label: t.description,
  133. path: t.name,
  134. parentPath: code,
  135. }
  136. })
  137. }
  138. // paths
  139. if (paths) {
  140. for (const [key, value] of Object.entries(paths)) {
  141. const keys = Object.keys(value as any)
  142. const values = Object.values(value as any)
  143. const v = values && values.length > 0 ? values[0] : ({} as any)
  144. const parentPath = v.tags && v.tags.length > 0 ? v.tags[0] : ''
  145. apis[apis.length] = {
  146. label: v.summary,
  147. path: key,
  148. parentPath,
  149. httpMethods: keys.join(','),
  150. }
  151. }
  152. }
  153. return await new ApiApi().sync({ apis })
  154. }
  155. const onSync = () => {
  156. state.syncLoading = true
  157. const swaggerResources = ['/swagger-resources']
  158. const lastSwaggerResourcesIndex = swaggerResources.length - 1
  159. swaggerResources.forEach(async (swaggerResource, swaggerResourcesIndex) => {
  160. const resSwaggerResources = await new ApiExtApi().getSwaggerResources(swaggerResource, { showErrorMessage: false }).catch(() => {
  161. state.syncLoading = false
  162. })
  163. if (isArray(resSwaggerResources) && (resSwaggerResources?.length as number) > 0) {
  164. for (let index = 0, len = resSwaggerResources.length, last = len - 1; index < len; index++) {
  165. const swaggerResource = resSwaggerResources[index]
  166. const resSyncApi = await syncApi(swaggerResource).catch(() => {
  167. proxy.$modal.msgSuccess(`同步${swaggerResource.name}失败`)
  168. })
  169. }
  170. }
  171. if (swaggerResourcesIndex === lastSwaggerResourcesIndex) {
  172. state.syncLoading = false
  173. proxy.$modal.msgSuccess(`同步完成`)
  174. onQuery()
  175. }
  176. })
  177. }
  178. </script>
  179. <script lang="ts">
  180. import { defineComponent } from 'vue'
  181. export default defineComponent({
  182. name: 'admin/api',
  183. })
  184. </script>
  185. <style scoped lang="scss"></style>