소스 검색

同步接口支持同步多微服务接口

zhontai 2 년 전
부모
커밋
ac861786eb
2개의 변경된 파일34개의 추가작업 그리고 24개의 파일을 삭제
  1. 2 2
      src/api/admin.extend/Api.ts
  2. 32 22
      src/views/admin/api/index.vue

+ 2 - 2
src/api/admin.extend/Api.ts

@@ -22,9 +22,9 @@ export class ApiApi<SecurityDataType = unknown> extends HttpClient<SecurityDataT
    * @request GET:/swagger-resources
    * @secure
    */
-  getSwaggerResources = (params: RequestParams = {}) =>
+  getSwaggerResources = (path: string, params: RequestParams = {}) =>
     this.request<AxiosResponse, any>({
-      path: `/swagger-resources`,
+      path: path,
       method: 'GET',
       secure: true,
       format: 'json',

+ 32 - 22
src/views/admin/api/index.vue

@@ -122,22 +122,31 @@ const onDelete = (row: ApiListOutput) => {
     .catch(() => {})
 }
 
-const syncApi = async (url: string) => {
-  const res = await new ApiExtApi().getSwaggerJson(url, { showErrorMessage: false })
+const syncApi = async (swaggerResource: any) => {
+  const res = await new ApiExtApi().getSwaggerJson(swaggerResource.url, { showErrorMessage: false })
   if (!res) {
     return
   }
 
   const tags = res.tags
   const paths = res.paths
-
   const apis = []
+  const urls = swaggerResource.url.split('/')
+  const code = urls.length >= 2 ? urls[urls.length - 2] : ''
+  if (code === '') {
+    return
+  }
+  apis[apis.length] = {
+    label: swaggerResource.name,
+    path: code,
+  }
   // tags
   if (tags && tags.length > 0) {
     tags.forEach((t: any) => {
       apis[apis.length] = {
         label: t.description,
         path: t.name,
+        parentPath: code,
       }
     })
   }
@@ -160,28 +169,29 @@ const syncApi = async (url: string) => {
   return await new ApiApi().sync({ apis })
 }
 
-const onSync = async () => {
+const onSync = () => {
   state.syncLoading = true
-  const resSwaggerResources = await new ApiExtApi().getSwaggerResources({ showErrorMessage: false }).catch(() => {
-    state.syncLoading = false
-  })
-  if (isArray(resSwaggerResources) && (resSwaggerResources?.length as number) > 0) {
-    for (let index = 0, len = resSwaggerResources.length, last = len - 1; index < len; index++) {
-      const swaggerResource = resSwaggerResources[index]
-      const resSyncApi = await syncApi(swaggerResource.url)
-      if (index === last) {
-        state.syncLoading = false
-        if (resSyncApi?.success) {
-          proxy.$modal.msgSuccess('同步成功')
-          onQuery()
-        } else {
-          proxy.$modal.msgError('同步失败')
-        }
+  const swaggerResources = ['/swagger-resources']
+  const lastSwaggerResourcesIndex = swaggerResources.length - 1
+  swaggerResources.forEach(async (swaggerResource, swaggerResourcesIndex) => {
+    const resSwaggerResources = await new ApiExtApi().getSwaggerResources(swaggerResource, { showErrorMessage: false }).catch(() => {
+      state.syncLoading = false
+    })
+    if (isArray(resSwaggerResources) && (resSwaggerResources?.length as number) > 0) {
+      for (let index = 0, len = resSwaggerResources.length, last = len - 1; index < len; index++) {
+        const swaggerResource = resSwaggerResources[index]
+        const resSyncApi = await syncApi(swaggerResource).catch(() => {
+          proxy.$modal.msgSuccess(`同步${swaggerResource.name}失败`)
+        })
       }
     }
-  } else {
-    state.syncLoading = false
-  }
+
+    if (swaggerResourcesIndex === lastSwaggerResourcesIndex) {
+      state.syncLoading = false
+      proxy.$modal.msgSuccess(`同步完成`)
+      onQuery()
+    }
+  })
 }
 </script>