index.vue 7.0 KB

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