From 5828f70dce0d3fe83eeddf5048ad162fe1909dd1 Mon Sep 17 00:00:00 2001 From: eleliauk <2831336720@qq.com> Date: Fri, 20 Dec 2024 15:27:42 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(token):=E8=A7=A3=E5=86=B3=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E8=BF=87=E6=9C=9F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/api/handleLogin.ts | 2 +- .../QuestionDetail/QuestionDetail.tsx | 2 +- src/common/utils/fetch.ts | 55 ++++++++++++++++++- 3 files changed, 55 insertions(+), 4 deletions(-) diff --git a/src/common/api/handleLogin.ts b/src/common/api/handleLogin.ts index dce3441..185e6e5 100644 --- a/src/common/api/handleLogin.ts +++ b/src/common/api/handleLogin.ts @@ -2,7 +2,7 @@ import Taro from '@tarojs/taro'; const preUrl = 'https://kstack.muxixyz.com'; -type LoginResponseHeaders = { +export type LoginResponseHeaders = { 'X-Jwt-Token'?: string; 'X-Refresh-Token'?: string; }; diff --git a/src/common/components/QuestionDetail/QuestionDetail.tsx b/src/common/components/QuestionDetail/QuestionDetail.tsx index 75fc075..fc3ebff 100644 --- a/src/common/components/QuestionDetail/QuestionDetail.tsx +++ b/src/common/components/QuestionDetail/QuestionDetail.tsx @@ -1,5 +1,6 @@ import '@/common/components/QuestionDetail/QuestionDetail'; import { Button, Image, Text, Textarea, View } from '@tarojs/components'; +import Taro from '@tarojs/taro'; import React, { useEffect, useState } from 'react'; import answericon from '@/common/assets/img/publishQuestion/answer.png'; @@ -8,7 +9,6 @@ import IconFont from '@/common/components/iconfont'; import PublishHeader from '@/common/components/PublishHeader/PublishHeader'; import { get, post } from '@/common/utils'; import { useCourseStore } from '@/pages/main/store/store'; -import Taro from '@tarojs/taro'; interface IUser { avatar: string; diff --git a/src/common/utils/fetch.ts b/src/common/utils/fetch.ts index df1bc52..daf1b0b 100644 --- a/src/common/utils/fetch.ts +++ b/src/common/utils/fetch.ts @@ -14,13 +14,46 @@ const getToken = async () => { throw new Error(`Failed to get token: ${res.errMsg as unknown as string}`); }; +const refreshToken = async () => { + try { + const longToken = await Taro.getStorage({ key: 'longToken' }); + if (!longToken.data) { + void Taro.navigateTo({ url: '/pages/login/index' }); + throw new Error('No long token found'); + } + + const response = await Taro.request({ + url: `${preUrl}/users/refresh_token`, + method: 'GET', + header: { + ...header, + Authorization: `Bearer ${longToken.data}`, + }, + }); + + if (response.statusCode.toString().startsWith('2')) { + 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 }); + } + } + throw new Error('Failed to refresh token'); + } catch (error) { + void Taro.navigateTo({ url: '/pages/login/index' }); + throw error; + } +}; + const request = async ( url = '', method: 'GET' | 'POST' = 'GET', data = {}, isToken = true ) => { - const token = isToken ? `Bearer ${await getToken()}` : ''; + let token = isToken ? `Bearer ${await getToken()}` : ''; header['Authorization'] = token ? `${token}` : ''; try { @@ -33,9 +66,27 @@ 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}`; + header['Authorization'] = token; + + // 使用新 token 重试请求 + const retryResponse = await Taro.request({ + url: `${preUrl}${url}`, + method, + header, + data: method === 'POST' ? JSON.stringify(data) : data, + }); + + if (retryResponse.statusCode.toString().startsWith('2')) { + return retryResponse.data; + } + throw new Error(`${retryResponse.statusCode}`); } else { const errorData = response.data as { code: number; msg: string }; - throw new Error(response.statusCode === 401 ? '401' : `${errorData.code}`); + throw new Error(`${errorData.code}`); } } catch (error) { // eslint-disable-next-line no-console 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 2/2] =?UTF-8?q?feat(magic=EF=BC=89:=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E9=97=AE=E9=97=AE=E5=90=8C=E5=AD=A6=E5=AE=A1=E6=A0=B8=E5=A4=A7?= =?UTF-8?q?=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;