From 4982e66973eb8dfa798fd451f0caa00d7e559531 Mon Sep 17 00:00:00 2001 From: eleliauk <2831336720@qq.com> Date: Fri, 20 Dec 2024 21:33:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(magic=EF=BC=89:=E6=96=B0=E5=A2=9E=E9=97=AE?= =?UTF-8?q?=E9=97=AE=E5=90=8C=E5=AD=A6=E5=AE=A1=E6=A0=B8=E5=A4=A7=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/utils/fetch.ts | 30 ++++++++++++++++------------- src/pages/classInfo/index.tsx | 34 ++++++++++++++++++++++++++++++++- src/pages/evaluate/evaluate.tsx | 2 +- 3 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/common/utils/fetch.ts b/src/common/utils/fetch.ts index daf1b0b..c06b14d 100644 --- a/src/common/utils/fetch.ts +++ b/src/common/utils/fetch.ts @@ -1,6 +1,8 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ import Taro from '@tarojs/taro'; +import { LoginResponseHeaders } from '../api/handleLogin'; + const preUrl = 'https://kstack.muxixyz.com'; const header = { @@ -11,7 +13,7 @@ const getToken = async () => { const res = await Taro.getStorage({ key: 'shortToken' }); if (res.data) return res.data; void Taro.navigateTo({ url: '/pages/login/index' }); - throw new Error(`Failed to get token: ${res.errMsg as unknown as string}`); + throw new Error(`没token: ${res.errMsg as unknown as string}`); }; const refreshToken = async () => { @@ -19,7 +21,7 @@ const refreshToken = async () => { const longToken = await Taro.getStorage({ key: 'longToken' }); if (!longToken.data) { void Taro.navigateTo({ url: '/pages/login/index' }); - throw new Error('No long token found'); + throw new Error('没longToken'); } const response = await Taro.request({ @@ -32,16 +34,18 @@ const refreshToken = async () => { }); if (response.statusCode.toString().startsWith('2')) { - const headers: LoginResponseHeaders = response.header || {}; + const headers: LoginResponseHeaders = response.header; const shortToken = headers['X-Jwt-Token']; - //const longToken = headers['X-Refresh-Token']; - if (shortToken && longToken) { - await Taro.setStorage({ key: 'shortToken', data: shortToken }); - // await Taro.setStorage({ key: 'longToken', data: longToken }); + if (shortToken) { + await Taro.setStorage({ key: 'shortToken', data: shortToken.toString() }); } } - throw new Error('Failed to refresh token'); + throw new Error('刷新token失败'); } catch (error) { + void Taro.showToast({ + title: '登录过期 请刷新小程序重新登录', + icon: 'error', + }); void Taro.navigateTo({ url: '/pages/login/index' }); throw error; } @@ -67,9 +71,9 @@ const request = async ( if (response.statusCode.toString().startsWith('2')) { return response.data; } else if (response.statusCode === 401) { - // Token 过期,尝试刷新 - const newToken = await refreshToken(); - token = `Bearer ${newToken}`; + await refreshToken(); + const newToken = await Taro.getStorage({ key: 'shortToken' }); + token = `Bearer ${newToken.data}`; header['Authorization'] = token; // 使用新 token 重试请求 @@ -83,10 +87,10 @@ const request = async ( if (retryResponse.statusCode.toString().startsWith('2')) { return retryResponse.data; } - throw new Error(`${retryResponse.statusCode}`); + throw new Error(retryResponse.statusCode.toString()); } else { const errorData = response.data as { code: number; msg: string }; - throw new Error(`${errorData.code}`); + throw new Error(errorData.code.toString()); } } catch (error) { // eslint-disable-next-line no-console diff --git a/src/pages/classInfo/index.tsx b/src/pages/classInfo/index.tsx index 326988c..72dbbd1 100644 --- a/src/pages/classInfo/index.tsx +++ b/src/pages/classInfo/index.tsx @@ -15,6 +15,9 @@ import LineChart from '@/common/components/chart'; import Label3 from '@/common/components/label3/label3'; import ShowStar from '@/common/components/showStar/showStar'; import { get, post } from '@/common/utils'; +import { postBool } from '@/common/utils/fetch'; + +import { StatusResponse } from '../evaluate/evaluate'; const coursePropertyMap = { CoursePropertyGeneralCore: '通识核心课', @@ -43,6 +46,33 @@ export default function Index() { const [questionNum, setQuestionNum] = useState(0); const [questionlist, setQuestionlist] = useState([]); const [collect, setCollect] = useState(course?.is_collected); + const [test, setTest] = useState(false); + useEffect(() => { + const getParams = async () => { + try { + const res = (await postBool('/checkStatus', { + name: 'kestack', + })) as StatusResponse; + + setTest(res.data.status); + + // const instance = Taro.getCurrentInstance(); + // const params = instance?.router?.params || {}; + + // setId(params.id ? Number(params.id) : null); + // setName( + // params.name ? decodeURIComponent(params.name) : '只能评价自己学过的课程哦' + // ); + } catch (error) { + console.error('Error fetching status:', error); + } + }; + + void getParams(); + }, []); + useEffect(() => { + console.log('test status updated:', test); + }, [test]); const getCommentData = async () => { try { await get( @@ -221,7 +251,9 @@ export default function Index() { <> - {questionlist.length > 0 ? ( + {!test ? ( + 因为政策原因暂不能发布课评 + ) : questionlist.length > 0 ? ( <> {questionlist.slice(0, 3).map((question, index) => ( diff --git a/src/pages/evaluate/evaluate.tsx b/src/pages/evaluate/evaluate.tsx index 4eb7253..7dfdce4 100644 --- a/src/pages/evaluate/evaluate.tsx +++ b/src/pages/evaluate/evaluate.tsx @@ -14,7 +14,7 @@ import Star from '@/common/components/star/star'; import { post } from '@/common/utils'; import { postBool } from '@/common/utils/fetch'; -interface StatusResponse { +export interface StatusResponse { code: number; data: { status: boolean;