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..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,42 @@ 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 () => { + try { + const longToken = await Taro.getStorage({ key: 'longToken' }); + if (!longToken.data) { + void Taro.navigateTo({ url: '/pages/login/index' }); + throw new Error('没longToken'); + } + + 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']; + if (shortToken) { + await Taro.setStorage({ key: 'shortToken', data: shortToken.toString() }); + } + } + throw new Error('刷新token失败'); + } catch (error) { + void Taro.showToast({ + title: '登录过期 请刷新小程序重新登录', + icon: 'error', + }); + void Taro.navigateTo({ url: '/pages/login/index' }); + throw error; + } }; const request = async ( @@ -20,7 +57,7 @@ const request = async ( data = {}, isToken = true ) => { - const token = isToken ? `Bearer ${await getToken()}` : ''; + let token = isToken ? `Bearer ${await getToken()}` : ''; header['Authorization'] = token ? `${token}` : ''; try { @@ -33,9 +70,27 @@ const request = async ( if (response.statusCode.toString().startsWith('2')) { return response.data; + } else if (response.statusCode === 401) { + await refreshToken(); + const newToken = await Taro.getStorage({ key: 'shortToken' }); + token = `Bearer ${newToken.data}`; + 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.toString()); } else { const errorData = response.data as { code: number; msg: string }; - throw new Error(response.statusCode === 401 ? '401' : `${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;