From 77c4055c72fc5d0e0becaf3c410ab700f26c220b Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期六, 04 十月 2025 18:57:09 +0800
Subject: [PATCH] fix(auth): 避免在 JWT 过滤器中消费原始 GraphQL 请求体,防止下游读取为空引发 400
---
web/src/api/rating.js | 227 ++++++++++++++++++++++++++------------------------------
1 files changed, 107 insertions(+), 120 deletions(-)
diff --git a/web/src/api/rating.js b/web/src/api/rating.js
index a2a5d72..5be5ccc 100644
--- a/web/src/api/rating.js
+++ b/web/src/api/rating.js
@@ -1,138 +1,125 @@
-const GRAPHQL_ENDPOINT = 'http://localhost:8080/api/graphql'
+import { API_CONFIG, graphqlRequest } from '@/config/api'
-// GraphQL璇锋眰鍑芥暟
-async function graphqlRequest(query, variables = {}) {
- const response = await fetch(GRAPHQL_ENDPOINT, {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- },
- body: JSON.stringify({
- query,
- variables,
- }),
- })
+// 浣跨敤缁熶竴鐨凣raphQL璇锋眰鍑芥暟
- if (!response.ok) {
- throw new Error(`HTTP error! status: ${response.status}`)
+// GraphQL鏌ヨ璇彞
+const GET_RATING_SCHEMES_QUERY = `
+ query GetRatingSchemes($page: Int!, $size: Int!, $name: String) {
+ ratingSchemes(page: $page, size: $size, name: $name) {
+ content {
+ id
+ name
+ description
+ totalScore
+ state
+ stateName
+ createTime
+ updateTime
+ }
+ totalElements
+ page
+ size
+ }
}
+`
- const result = await response.json()
-
- if (result.errors) {
- throw new Error(result.errors[0].message)
+const GET_ALL_RATING_SCHEMES_QUERY = `
+ query GetAllRatingSchemes {
+ allRatingSchemes {
+ id
+ name
+ description
+ totalScore
+ state
+ stateName
+ }
}
+`
- return result.data
-}
+const GET_RATING_SCHEME_QUERY = `
+ query GetRatingScheme($id: ID!) {
+ ratingScheme(id: $id) {
+ id
+ name
+ description
+ totalScore
+ state
+ stateName
+ createTime
+ updateTime
+ items {
+ id
+ name
+ maxScore
+ orderNo
+ }
+ }
+ }
+`
-// 鍒嗛〉鏌ヨ璇勫垎妯℃澘鍒楄〃
+const SAVE_RATING_SCHEME_MUTATION = `
+ mutation SaveRatingScheme($input: RatingSchemeInput!) {
+ saveRatingScheme(input: $input) {
+ id
+ name
+ description
+ totalScore
+ state
+ stateName
+ createTime
+ updateTime
+ }
+ }
+`
+
+const DELETE_RATING_SCHEME_MUTATION = `
+ mutation DeleteRatingScheme($id: ID!) {
+ deleteRatingScheme(id: $id)
+ }
+`
+
+// API鍑芥暟
export const getRatingSchemes = async (page = 0, size = 10, name = '') => {
- const query = `
- query GetRatingSchemes($page: Int, $size: Int, $name: String) {
- ratingSchemes(page: $page, size: $size, name: $name) {
- content {
- id
- name
- description
- totalScore
- createTime
- updateTime
- }
- totalElements
- totalPages
- number
- size
- first
- last
- }
- }
- `
-
- const variables = { page, size }
- if (name) {
- variables.name = name
+ try {
+ const result = await graphqlRequest(GET_RATING_SCHEMES_QUERY, { page, size, name })
+ return result.data.ratingSchemes
+ } catch (error) {
+ throw new Error(error.message || '鑾峰彇璇勫垎鏂规鍒楄〃澶辫触')
}
-
- const data = await graphqlRequest(query, variables)
- return data.ratingSchemes
}
-// 鏍规嵁ID鑾峰彇璇勫垎妯℃澘璇︽儏
-export const getRatingScheme = async (id) => {
- const query = `
- query GetRatingScheme($id: ID!) {
- ratingScheme(id: $id) {
- id
- name
- description
- totalScore
- items {
- id
- name
- maxScore
- orderNo
- }
- createTime
- updateTime
- }
- }
- `
-
- const data = await graphqlRequest(query, { id })
- return data.ratingScheme
-}
-
-// 鑾峰彇鎵�鏈夎瘎鍒嗘ā鏉匡紙鐢ㄤ簬涓嬫媺閫夋嫨锛�
export const getAllRatingSchemes = async () => {
- const query = `
- query GetAllRatingSchemes {
- allRatingSchemes {
- id
- name
- description
- totalScore
- }
- }
- `
-
- const data = await graphqlRequest(query)
- return data.allRatingSchemes
+ try {
+ const result = await graphqlRequest(GET_ALL_RATING_SCHEMES_QUERY)
+ return result.data.allRatingSchemes || []
+ } catch (error) {
+ throw new Error(error.message || '鑾峰彇鎵�鏈夎瘎鍒嗘柟妗堝け璐�')
+ }
}
-// 淇濆瓨璇勫垎妯℃澘
-export const saveRatingScheme = async (input) => {
- const mutation = `
- mutation SaveRatingScheme($input: RatingSchemeInput!) {
- saveRatingScheme(input: $input) {
- id
- name
- description
- totalScore
- items {
- id
- name
- maxScore
- orderNo
- }
- createTime
- updateTime
- }
- }
- `
-
- const data = await graphqlRequest(mutation, { input })
- return data.saveRatingScheme
+export const getRatingScheme = async (id) => {
+ try {
+ const result = await graphqlRequest(GET_RATING_SCHEME_QUERY, { id })
+ return result.data.ratingScheme
+ } catch (error) {
+ throw new Error(error.message || '鑾峰彇璇勫垎鏂规璇︽儏澶辫触')
+ }
}
-// 鍒犻櫎璇勫垎妯℃澘
+export const saveRatingScheme = async (ratingSchemeData) => {
+ try {
+ const result = await graphqlRequest(SAVE_RATING_SCHEME_MUTATION, { input: ratingSchemeData })
+ return result.data.saveRatingScheme
+ } catch (error) {
+ throw new Error(error.message || '淇濆瓨璇勫垎鏂规澶辫触')
+ }
+}
+
export const deleteRatingScheme = async (id) => {
- const mutation = `
- mutation DeleteRatingScheme($id: ID!) {
- deleteRatingScheme(id: $id)
- }
- `
-
- const data = await graphqlRequest(mutation, { id })
- return data.deleteRatingScheme
+ try {
+ const result = await graphqlRequest(DELETE_RATING_SCHEME_MUTATION, { id })
+ return result.data.deleteRatingScheme
+ } catch (error) {
+ throw new Error(error.message || '鍒犻櫎璇勫垎鏂规澶辫触')
+ }
}
\ No newline at end of file
--
Gitblit v1.8.0