From 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期二, 30 九月 2025 17:38:04 +0800
Subject: [PATCH] feat: 完善比赛晋级功能并清理测试文件

---
 web/src/api/role.ts |  156 +++++++++++++--------------------------------------
 1 files changed, 41 insertions(+), 115 deletions(-)

diff --git a/web/src/api/role.ts b/web/src/api/role.ts
index 46aed90..434ee33 100644
--- a/web/src/api/role.ts
+++ b/web/src/api/role.ts
@@ -1,5 +1,7 @@
 // 瑙掕壊绠$悊API
 
+import { API_CONFIG, graphqlRequest } from '@/config/api'
+
 // 瑙掕壊鐩稿叧鐨� GraphQL 鏌ヨ
 export const ROLE_QUERIES = {
   // 鑾峰彇鎵�鏈夎鑹�
@@ -78,19 +80,19 @@
   `,
   
   // 鏍规嵁鍚嶇О妯$硦鏌ヨ瑙掕壊
-  GET_ROLES_BY_NAME: `
-    query GetRolesByName($name: String!) {
-      rolesByName(name: $name) {
-        id
-        code
-        name
-        description
-        state
-        createTime
-        updateTime
-      }
-    }
-  `
+   SEARCH_ROLES_BY_NAME: `
+     query SearchRolesByName($name: String!) {
+       searchRolesByName(name: $name) {
+         id
+         code
+         name
+         description
+         state
+         createTime
+         updateTime
+       }
+     }
+   `
 }
 
 // 绫诲瀷瀹氫箟
@@ -109,136 +111,60 @@
   // 鑾峰彇鎵�鏈夎鑹�
   async getRoles(): Promise<Role[]> {
     try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ROLES
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.roles || []
+      const data = await graphqlRequest(ROLE_QUERIES.GET_ROLES)
+      return data?.roles || []
     } catch (error: any) {
       throw new Error(error.message || '鑾峰彇瑙掕壊鍒楄〃澶辫触')
     }
   },
 
-  // 鑾峰彇鎵�鏈夊惎鐢ㄧ姸鎬佺殑瑙掕壊
+  // 鑾峰彇婵�娲荤姸鎬佺殑瑙掕壊
   async getActiveRoles(): Promise<Role[]> {
     try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ACTIVE_ROLES
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.activeRoles || []
+      const data = await graphqlRequest(ROLE_QUERIES.GET_ACTIVE_ROLES)
+      return data?.activeRoles || []
     } catch (error: any) {
-      throw new Error(error.message || '鑾峰彇鍚敤瑙掕壊鍒楄〃澶辫触')
+      throw new Error(error.message || '鑾峰彇婵�娲昏鑹插垪琛ㄥけ璐�')
     }
   },
 
-  // 鏍规嵁ID鑾峰彇瑙掕壊璇︽儏
-  async getRoleById(id: string): Promise<Role | null> {
-    try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ROLE,
-          variables: { id }
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.role || null
-    } catch (error: any) {
-      throw new Error(error.message || '鑾峰彇瑙掕壊璇︽儏澶辫触')
-    }
-  },
+  // 鏍规嵁ID鑾峰彇瑙掕壊
+   async getRoleById(id: string): Promise<Role | null> {
+     try {
+       const data = await graphqlRequest(ROLE_QUERIES.GET_ROLE, { id })
+       return data?.role || null
+     } catch (error: any) {
+       throw new Error(error.message || '鑾峰彇瑙掕壊璇︽儏澶辫触')
+     }
+   },
 
-  // 鏍规嵁瑙掕壊浠g爜鑾峰彇瑙掕壊
+  // 鏍规嵁浠g爜鑾峰彇瑙掕壊
   async getRoleByCode(code: string): Promise<Role | null> {
     try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ROLE_BY_CODE,
-          variables: { code }
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.roleByCode || null
+      const data = await graphqlRequest(ROLE_QUERIES.GET_ROLE_BY_CODE, { code })
+      return data?.roleByCode || null
     } catch (error: any) {
-      throw new Error(error.message || '鏍规嵁浠g爜鑾峰彇瑙掕壊澶辫触')
+      throw new Error(error.message || '鑾峰彇瑙掕壊璇︽儏澶辫触')
     }
   },
 
   // 鏍规嵁鐘舵�佽幏鍙栬鑹�
   async getRolesByState(state: number): Promise<Role[]> {
     try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ROLES_BY_STATE,
-          variables: { state }
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.rolesByState || []
+      const data = await graphqlRequest(ROLE_QUERIES.GET_ROLES_BY_STATE, { state })
+      return data?.rolesByState || []
     } catch (error: any) {
-      throw new Error(error.message || '鏍规嵁鐘舵�佽幏鍙栬鑹插け璐�')
+      throw new Error(error.message || '鑾峰彇瑙掕壊鍒楄〃澶辫触')
     }
   },
 
-  // 鏍规嵁鍚嶇О妯$硦鏌ヨ瑙掕壊
+  // 鏍规嵁鍚嶇О鎼滅储瑙掕壊
   async searchRolesByName(name: string): Promise<Role[]> {
     try {
-      const response = await fetch('http://localhost:8080/api/graphql', {
-        method: 'POST',
-        headers: {
-          'Content-Type': 'application/json',
-        },
-        body: JSON.stringify({
-          query: ROLE_QUERIES.GET_ROLES_BY_NAME,
-          variables: { name }
-        })
-      })
-      const result = await response.json()
-      if (result.errors) {
-        throw new Error(result.errors[0].message)
-      }
-      return result.data?.rolesByName || []
+      const data = await graphqlRequest(ROLE_QUERIES.SEARCH_ROLES_BY_NAME, { name })
+      return data?.searchRolesByName || []
     } catch (error: any) {
-      throw new Error(error.message || '鏍规嵁鍚嶇О鎼滅储瑙掕壊澶辫触')
+      throw new Error(error.message || '鎼滅储瑙掕壊澶辫触')
     }
   }
 }
\ No newline at end of file

--
Gitblit v1.8.0