Skip to content

Commit

Permalink
feat(magic):新增问问同学审核大法
Browse files Browse the repository at this point in the history
  • Loading branch information
eleliauk committed Dec 20, 2024
1 parent 5828f70 commit 4982e66
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
30 changes: 17 additions & 13 deletions src/common/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -11,15 +13,15 @@ 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('No long token found');
throw new Error('没longToken');
}

const response = await Taro.request({
Expand All @@ -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;
}
Expand All @@ -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 重试请求
Expand All @@ -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
Expand Down
34 changes: 33 additions & 1 deletion src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: '通识核心课',
Expand Down Expand Up @@ -43,6 +46,33 @@ export default function Index() {
const [questionNum, setQuestionNum] = useState<number>(0);
const [questionlist, setQuestionlist] = useState<WebQuestionVo[]>([]);
const [collect, setCollect] = useState<boolean | undefined>(course?.is_collected);
const [test, setTest] = useState<boolean>(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(
Expand Down Expand Up @@ -221,7 +251,9 @@ export default function Index() {
</View>
</View>
<>
{questionlist.length > 0 ? (
{!test ? (
<View>因为政策原因暂不能发布课评</View>
) : questionlist.length > 0 ? (
<>
<View className="relative">
{questionlist.slice(0, 3).map((question, index) => (
Expand Down
2 changes: 1 addition & 1 deletion src/pages/evaluate/evaluate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 4982e66

Please sign in to comment.