Skip to content

Commit

Permalink
Merge pull request #215 from eleliauk/main
Browse files Browse the repository at this point in the history
merge
eleliauk authored Dec 22, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 6f3d2b2 + 5dc7100 commit a48524e
Showing 13 changed files with 200 additions and 112 deletions.
16 changes: 11 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -13,16 +13,22 @@ const interceptor: Taro.interceptor = function (chain: Taro.Chain) {
return chain.proceed(requestParams).then((res) => {
console.log(Taro.getStorageSync('shortToken'));

if (res.statusCode === 401 && Taro.getStorageSync('visitor') !== false) {
void Taro.reLaunch({ url: '/pages/login/index' });
Taro.showToast({
title: '登录过期,请重新登录',
icon: 'none',
if (res.statusCode === 401 && Taro.getStorageSync('visitor') !== true) {
console.log(res, Taro.getStorageSync('visitor'));

void Taro.reLaunch({ url: '/pages/login/index' }).then(() => {
Taro.showToast({
title: '登录过期,请重新登录',
icon: 'none',
});
});
}
return res;
}) as Taro.Chain;
};
Taro.onAppShow(() => {
Taro.setStorageSync('visitor', false);
});
Taro.addInterceptor(interceptor);
class App extends Component<PropsWithChildren> {
//TODO 写成加interceptor 但是我还没写明白 别急
5 changes: 3 additions & 2 deletions src/common/components/AnswerToStudent/index.tsx
Original file line number Diff line number Diff line change
@@ -6,13 +6,14 @@ interface Question {
content: string;
preview_answers: Array<Answerv1Answer>;
}

const AnswerToStudent: React.FC<Question> = (props) => {
const { content, preview_answers } = props;
return (
<View className="m-auto flex h-[4vh] w-[80vw] items-center justify-center gap-2">
<Image src={Ask as string} className="h-6 w-6" />
<View className="text-base font-medium text-gray-800">{content}</View>
<View className="w-[50vw] overflow-hidden text-ellipsis whitespace-nowrap text-base font-medium text-gray-800">
{content}
</View>
<View className="ml-auto text-sm text-gray-500">
{preview_answers?.length ?? 0} 个回答
</View>
4 changes: 2 additions & 2 deletions src/common/components/Comment/index.tsx
Original file line number Diff line number Diff line change
@@ -63,7 +63,7 @@ const CommentHeader: React.FC<CommentProps> = memo((props) => {
<>
{!course_info ? (
<>
<View className="classTitle">pending</View>
<View className="italic text-gray-400">加载中 ...</View>
</>
) : (
<>
@@ -75,7 +75,7 @@ const CommentHeader: React.FC<CommentProps> = memo((props) => {
<View className="comment">
{!publisher_info ? (
<>
<View>pending</View>
<View className="italic text-gray-400">加载中 ...</View>
</>
) : (
<>
14 changes: 8 additions & 6 deletions src/common/utils/fetch.ts
Original file line number Diff line number Diff line change
@@ -12,15 +12,15 @@ const header = {
const getToken = async () => {
const res = await Taro.getStorage({ key: 'shortToken' });
if (res.data) return res.data;
void Taro.navigateTo({ url: '/pages/login/index' });
void Taro.reLaunch({ url: '/pages/login/index' });
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' });
void Taro.reLaunch({ url: '/pages/login/index' });
throw new Error('没longToken');
}

@@ -42,11 +42,13 @@ const refreshToken = async () => {
}
throw new Error('刷新token失败');
} catch (error) {
void Taro.showToast({
title: '登录过期 请刷新小程序重新登录',
icon: 'error',
if (Taro.getStorageSync('visitor') === true) return;
void Taro.reLaunch({ url: '/pages/login/index' }).then(() => {
void Taro.showToast({
title: '登录过期 请刷新小程序重新登录',
icon: 'none',
});
});
void Taro.navigateTo({ url: '/pages/login/index' });
throw error;
}
};
21 changes: 18 additions & 3 deletions src/modules/notification/components/Notification.tsx
Original file line number Diff line number Diff line change
@@ -22,7 +22,14 @@ const Notification: React.FC = memo(() => {
const [supportMessage, setSupportMessage] = useState<MessageType[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const currentMessage = useMemo(() => {
return tab === '提问' ? commentMessage : tab === '点赞' ? supportMessage : [];
const msg = tab === '提问' ? commentMessage : tab === '点赞' ? supportMessage : [];
if (!msg || !msg.length) {
void Taro.showToast({
title: '暂时没有消息',
icon: 'none',
});
}
return msg;
}, [tab, commentMessage, supportMessage]);
const [ctime, setCtime] = useState<number>(0);
const [end, setEnd] = useState(false);
@@ -43,8 +50,9 @@ const Notification: React.FC = memo(() => {
if (itemType === 'Comment') {
detailRes = await get(`/comments/${item.Ext.commentId}/detail`);
parentRes = await get(
`/comments/${detailRes.data.parent_comment_id}/detail`
`/comments/${detailRes.data?.parent_comment_id ?? 0}/detail`
);
console.log('tag');
user = await getUserInfo(item.Ext.commentator);
} else if (itemType === 'Support') {
detailRes =
@@ -164,7 +172,14 @@ const Notification: React.FC = memo(() => {
</View>
) : (
<View className="flex h-screen w-full flex-col items-center gap-4 overflow-y-scroll pb-[13vh]">
<TabBar tab={tab} setTab={setTab} />
<TabBar
tab={tab}
//eslint-disable-next-line @typescript-eslint/no-shadow
setTab={(tab) => {
setTab(tab);
setCtime(0);
}}
/>
<VirtualList
height="70%"
width="100%"
107 changes: 61 additions & 46 deletions src/pages/classInfo/index.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,8 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable import/first */
import { Image, Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';

import Taro, { useDidShow } from '@tarojs/taro';
import { useCallback, useEffect, useMemo, useState } from 'react';
import { AtIcon } from 'taro-ui';

@@ -96,7 +97,7 @@ export default function Index() {
getParams();
}, []);
//获取问题个数
useEffect(() => {
const initData = () => {
// eslint-disable-next-line @typescript-eslint/require-await
const getCourseData = async () => {
try {
@@ -127,52 +128,66 @@ export default function Index() {
};

if (courseId) void getCommentData();
};
const fetchAnswer = () => {
try {
void get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=${0}&limit=${3}`
).then((res) => {
console.log(res);

console.log('questionlist1', res.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
const questionsWithAnswers = res.data.map((item) => ({
...item,
preview_answers: item.preview_answers || [],
}));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionlist(res.data);
Taro.hideLoading();
});
} catch (e) {
console.error('Failed to fetch course data:', e);
}
};
const fetchGrades = async () => {
try {
await get(`/grades/courses/${courseId}`).then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setGrade(res.data); // 设置 grade 数据
!res.data && bailout();
Taro.hideLoading();
});
} catch (err) {
console.error('Failed to fetch grades data', err);
}
};
const getNumData = () => {
try {
void get(`/questions/count?biz=Course&biz_id=${courseId}`).then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionNum(res.data);
Taro.hideLoading();
});
} catch (e) {
console.error(e);
}
};
useEffect(() => {
initData();
}, [courseId]);
useDidShow(() => {
initData();
if (courseId) {
void Taro.showLoading({
title: '加载中',
});
void fetchGrades();
void getNumData();
void fetchAnswer();
}
});
useEffect(() => {
const fetchGrades = async () => {
try {
await get(`/grades/courses/${courseId}`).then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setGrade(res.data); // 设置 grade 数据
!res.data && bailout();
Taro.hideLoading();
});
} catch (err) {
console.error('Failed to fetch grades data', err);
}
};
const getNumData = () => {
try {
void get(`/questions/count?biz=Course&biz_id=${courseId}`).then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionNum(res.data);
Taro.hideLoading();
});
} catch (e) {
console.error(e);
}
};
const fetchAnswer = () => {
try {
void get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=${0}&limit=${3}`
).then((res) => {
console.log(res);

console.log('questionlist1', res.data);
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-return
const questionsWithAnswers = res.data.map((item) => ({
...item,
preview_answers: item.preview_answers || [],
}));
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
setQuestionlist(res.data);
Taro.hideLoading();
});
} catch (e) {
console.error('Failed to fetch course data:', e);
}
};
if (courseId) {
void Taro.showLoading({
title: '加载中',
25 changes: 23 additions & 2 deletions src/pages/evaluate/evaluate.tsx
Original file line number Diff line number Diff line change
@@ -134,6 +134,9 @@ export default function evaluate() {
});
return;
}
void Taro.showLoading({
title: '提交中',
});
const evaluationobj = {
star_rating: selectedStarIndex,
content: comment,
@@ -145,16 +148,34 @@ export default function evaluate() {
is_anonymous: isAnonymous,
};
console.log(evaluationobj);
if (!comment) {
void Taro.showToast({
title: '内容不能为空',
icon: 'none',
});
return;
}
post(`/evaluations/save`, evaluationobj)
.then((res) => {
if (res.code === 0) {
void Taro.switchTab({
url: '/pages/main/index', // 页面路径
void Taro.navigateBack().then(() => {
void Taro.showToast({
title: '课评发布成功',
icon: 'none',
});
});
} else {
void Taro.showToast({
title: res.msg,
icon: 'none',
});
}
})
.catch((error) => {
console.error('发布课评请求失败:', error);
})
.finally(() => {
void Taro.hideLoading();
});
};
const [selectedStarIndex, setSelectedStarIndex] = useState(-1);
21 changes: 17 additions & 4 deletions src/pages/main/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* eslint-disable react-hooks/exhaustive-deps */
/* eslint-disable import/first */
import { Image, ScrollView, Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';

import Taro, { useDidShow } from '@tarojs/taro';
import { useCallback, useEffect, useMemo, useState } from 'react';

import './index.scss';
@@ -16,9 +17,9 @@ import { useCourseStore } from './store/store';
import { COURSE_TYPE } from './store/types';

const COURSE_NAME_MAP = {
[COURSE_TYPE.ANY]: '全部',
[COURSE_TYPE.MAJOR]: '专业',
[COURSE_TYPE.GENERAL_ELECT]: '通选',
[COURSE_TYPE.GENERAL_REQUIRED]: '通必',
[COURSE_TYPE.GENERAL_ELECT]: '个性',
[COURSE_TYPE.GENERAL_CORE]: '通核',
};

@@ -67,6 +68,19 @@ export default function Index() {
}
}, [classType]);

useDidShow(() => {
void Taro.showLoading({ title: '加载中' });
void dispatch
.refershComments()
.then(() => {
Taro.hideLoading();
})
.catch(() => {
Taro.hideLoading();
void Taro.showToast({ title: '加载失败', icon: 'none' });
});
});

const handleSearch = (searchText: string) => {
console.log('搜索文本:', searchText);
};
@@ -77,7 +91,6 @@ export default function Index() {
const res = (await postBool('/checkStatus', {
name: 'kestack',
})) as StatusResponse;

setTest(res.data.status);
} catch (error) {
console.error('Error fetching status:', error);
4 changes: 2 additions & 2 deletions src/pages/main/store/commentInfoSlice.ts
Original file line number Diff line number Diff line change
@@ -16,13 +16,13 @@ export const CreateCommentInfo: StateCreator<
comments: {
CoursePropertyGeneralCore: [],
CoursePropertyGeneralElective: [],
CoursePropertyGeneralRequired: [],
CoursePropertyAny: [],
CoursePropertyMajorCore: [],
},
currentId: 0,
pageSize: 10,
loading: true,
classType: COURSE_TYPE.GENERAL_CORE,
classType: COURSE_TYPE.ANY,
async refershComments() {
return await get().updateComments(0);
},
6 changes: 3 additions & 3 deletions src/pages/main/store/types.ts
Original file line number Diff line number Diff line change
@@ -2,21 +2,21 @@ type CourseType = {
MAJOR: 'CoursePropertyMajorCore';
GENERAL_ELECT: 'CoursePropertyGeneralElective';
GENERAL_CORE: 'CoursePropertyGeneralCore';
GENERAL_REQUIRED: 'CoursePropertyGeneralRequired';
ANY: 'CoursePropertyAny';
};

export const COURSE_TYPE: CourseType = {
MAJOR: 'CoursePropertyMajorCore',
GENERAL_ELECT: 'CoursePropertyGeneralElective',
GENERAL_CORE: 'CoursePropertyGeneralCore',
GENERAL_REQUIRED: 'CoursePropertyGeneralRequired',
ANY: 'CoursePropertyAny',
};
/** 课程类别 */
export type classType =
| CourseType['MAJOR']
| CourseType['GENERAL_CORE']
| CourseType['GENERAL_ELECT']
| CourseType['GENERAL_REQUIRED'];
| CourseType['ANY'];

export type CourseDetailsType = {
/** 课程名 */
45 changes: 25 additions & 20 deletions src/pages/myclass/myclass.tsx
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

import { Picker, Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import Taro, { useDidShow } from '@tarojs/taro';
import { useEffect, useState } from 'react';

import './myclass.scss';
@@ -38,28 +38,27 @@ export default function Myclass() {
setYear(yearSelector[yearIndex]);
setSem(semSelector[semIndex]);
};

useEffect(() => {
async function fetchClasses() {
try {
const yearValue = year.split('-')[0];
const semValue =
sem === '第一学期'
? '1'
: sem === '第二学期'
? '2'
: sem === '第三学期'
? '3'
: '0';
const classes: Array<CouresProps> = await getUserCourses(yearValue, semValue);
setMyclasses(classes);
} catch (error) {
console.error('Error fetching user courses:', error);
}
}
const fetchCourses = () => {
void Taro.showLoading({
title: '加载中',
});
async function fetchClasses() {
try {
const yearValue = year.split('-')[0];
const semValue =
sem === '第一学期'
? '1'
: sem === '第二学期'
? '2'
: sem === '第三学期'
? '3'
: '0';
const classes: Array<CouresProps> = await getUserCourses(yearValue, semValue);
setMyclasses(classes);
} catch (error) {
console.error('Error fetching user courses:', error);
}
}
void fetchClasses()
.then(() => {
Taro.hideLoading();
@@ -71,7 +70,13 @@ export default function Myclass() {
title: '加载失败',
});
});
};
useEffect(() => {
fetchCourses();
}, [year, sem]);
useDidShow(() => {
fetchCourses();
});

const handleClassClick = (item: CouresProps) => {
// 拼接查询字符串参数
8 changes: 8 additions & 0 deletions src/pages/publishQuestion/index.tsx
Original file line number Diff line number Diff line change
@@ -154,13 +154,21 @@ export default function Index() {
biz_id: Number(courseId),
content: question,
};
if (question.length === 0) {
void Taro.showToast({
title: '内容不能为空',
icon: 'none',
});
return;
}
post(`/questions/publish`, questionobj)
.then((res) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (res?.code === 0) {
// 打印成功信息,但最好使用其他日志记录方式,而不是 console.log
// 例如:this.setState({ message: '发布课评成功' });
void Taro.showToast({ title: '发布问题成功', icon: 'success' });
void Taro.navigateBack();
// console.log('发布课评成功');
// 使用 redirectTo 跳转
// void Taro.redirectTo({
36 changes: 19 additions & 17 deletions src/pages/questionList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-console */
import { Button, Image, Text, View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import Taro, { useDidShow } from '@tarojs/taro';
import { useEffect, useState } from 'react';

// import './index.scss';
@@ -64,6 +64,21 @@ const App = () => {

getParams();
}, []);
// eslint-disable-next-line @typescript-eslint/require-await
const getQuestionList = async () => {
try {
void get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=0&limit=100`
).then((res) => {
// 检查 res 是否有 data 属性,并且断言其类型
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
setQuestions(res?.data as IQuestion[]);
});
} catch (error) {
// 错误处理,例如弹出提示
console.error('Failed to fetch course data:', error);
}
};

useEffect(() => {
// eslint-disable-next-line @typescript-eslint/require-await
@@ -82,24 +97,11 @@ const App = () => {

if (courseId) void getCourseData();

// eslint-disable-next-line @typescript-eslint/require-await
const getQuestionList = async () => {
try {
void get(
`/questions/list?biz=Course&biz_id=${courseId}&cur_question_id=0&limit=100`
).then((res) => {
// 检查 res 是否有 data 属性,并且断言其类型
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
setQuestions(res?.data as IQuestion[]);
});
} catch (error) {
// 错误处理,例如弹出提示
console.error('Failed to fetch course data:', error);
}
};

if (courseId) void getQuestionList().then((r) => console.log(r));
}, [courseId]);
useDidShow(() => {
if (courseId) void getQuestionList().then((r) => console.log(r));
});

const handleAsk = () => {
void Taro.navigateTo({

0 comments on commit a48524e

Please sign in to comment.