From 38fd1546d3368366843d4b879fc25a453cfa49d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B6=85=E7=BA=A7=E6=97=A0=E6=95=8C=E6=95=B0=E7=A0=81?= =?UTF-8?q?=E6=9A=B4=E9=BE=99=E6=88=98=E5=A3=AB?= <1845519693@qq.com> Date: Sun, 22 Dec 2024 23:17:41 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E4=B8=8A=E7=BA=BF?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.ts | 16 ++- .../components/AnswerToStudent/index.tsx | 5 +- src/common/components/Comment/index.tsx | 4 +- src/common/utils/fetch.ts | 14 ++- .../notification/components/Notification.tsx | 21 +++- src/pages/classInfo/index.tsx | 106 ++++++++++-------- src/pages/evaluate/evaluate.tsx | 25 ++++- src/pages/main/index.tsx | 19 +++- src/pages/main/store/commentInfoSlice.ts | 4 +- src/pages/main/store/types.ts | 6 +- src/pages/myclass/myclass.tsx | 45 ++++---- src/pages/publishQuestion/index.tsx | 8 ++ src/pages/questionList/index.tsx | 36 +++--- 13 files changed, 198 insertions(+), 111 deletions(-) diff --git a/src/app.ts b/src/app.ts index 7112ab8..b9820d1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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 { //TODO 写成加interceptor 但是我还没写明白 别急 diff --git a/src/common/components/AnswerToStudent/index.tsx b/src/common/components/AnswerToStudent/index.tsx index f365971..d14a351 100644 --- a/src/common/components/AnswerToStudent/index.tsx +++ b/src/common/components/AnswerToStudent/index.tsx @@ -6,13 +6,14 @@ interface Question { content: string; preview_answers: Array; } - const AnswerToStudent: React.FC = (props) => { const { content, preview_answers } = props; return ( - {content} + + {content} + {preview_answers?.length ?? 0} 个回答 diff --git a/src/common/components/Comment/index.tsx b/src/common/components/Comment/index.tsx index d47e952..c965e55 100644 --- a/src/common/components/Comment/index.tsx +++ b/src/common/components/Comment/index.tsx @@ -63,7 +63,7 @@ const CommentHeader: React.FC = memo((props) => { <> {!course_info ? ( <> - pending + 加载中 ... ) : ( <> @@ -75,7 +75,7 @@ const CommentHeader: React.FC = memo((props) => { {!publisher_info ? ( <> - pending + 加载中 ... ) : ( <> diff --git a/src/common/utils/fetch.ts b/src/common/utils/fetch.ts index c06b14d..071d33f 100644 --- a/src/common/utils/fetch.ts +++ b/src/common/utils/fetch.ts @@ -12,7 +12,7 @@ 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}`); }; @@ -20,7 +20,7 @@ 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; } }; diff --git a/src/modules/notification/components/Notification.tsx b/src/modules/notification/components/Notification.tsx index b6b374c..c1c66a6 100644 --- a/src/modules/notification/components/Notification.tsx +++ b/src/modules/notification/components/Notification.tsx @@ -20,7 +20,14 @@ const Notification: React.FC = memo(() => { const [supportMessage, setSupportMessage] = useState([]); const [loading, setLoading] = useState(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(0); const [end, setEnd] = useState(false); @@ -41,8 +48,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 = @@ -121,7 +129,14 @@ const Notification: React.FC = memo(() => { return ( - + { + setTab(tab); + setCtime(0); + }} + /> { + const initData = () => { // eslint-disable-next-line @typescript-eslint/require-await const getCourseData = async () => { try { @@ -126,52 +126,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: '加载中', diff --git a/src/pages/evaluate/evaluate.tsx b/src/pages/evaluate/evaluate.tsx index 7dfdce4..e4ff7cd 100644 --- a/src/pages/evaluate/evaluate.tsx +++ b/src/pages/evaluate/evaluate.tsx @@ -124,6 +124,9 @@ export default function evaluate() { }); return; } + void Taro.showLoading({ + title: '提交中', + }); const evaluationobj = { star_rating: selectedStarIndex, content: comment, @@ -135,16 +138,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); diff --git a/src/pages/main/index.tsx b/src/pages/main/index.tsx index def78a8..5c1bc89 100644 --- a/src/pages/main/index.tsx +++ b/src/pages/main/index.tsx @@ -1,7 +1,7 @@ /* eslint-disable react-hooks/exhaustive-deps */ /* eslint-disable import/first */ import { ScrollView, 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'; @@ -13,9 +13,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]: '通核', }; @@ -64,6 +64,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); }; diff --git a/src/pages/main/store/commentInfoSlice.ts b/src/pages/main/store/commentInfoSlice.ts index 1050bff..9c6b88f 100644 --- a/src/pages/main/store/commentInfoSlice.ts +++ b/src/pages/main/store/commentInfoSlice.ts @@ -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); }, diff --git a/src/pages/main/store/types.ts b/src/pages/main/store/types.ts index 97621e0..9f4850b 100644 --- a/src/pages/main/store/types.ts +++ b/src/pages/main/store/types.ts @@ -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 = { /** 课程名 */ diff --git a/src/pages/myclass/myclass.tsx b/src/pages/myclass/myclass.tsx index 65dbb95..6fae6b5 100644 --- a/src/pages/myclass/myclass.tsx +++ b/src/pages/myclass/myclass.tsx @@ -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 = 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 = 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) => { // 拼接查询字符串参数 diff --git a/src/pages/publishQuestion/index.tsx b/src/pages/publishQuestion/index.tsx index fa20cae..b693965 100644 --- a/src/pages/publishQuestion/index.tsx +++ b/src/pages/publishQuestion/index.tsx @@ -132,6 +132,13 @@ 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 @@ -139,6 +146,7 @@ export default function Index() { // 打印成功信息,但最好使用其他日志记录方式,而不是 console.log // 例如:this.setState({ message: '发布课评成功' }); void Taro.showToast({ title: '发布问题成功', icon: 'success' }); + void Taro.navigateBack(); // console.log('发布课评成功'); // 使用 redirectTo 跳转 // void Taro.redirectTo({ diff --git a/src/pages/questionList/index.tsx b/src/pages/questionList/index.tsx index 5f93038..916a468 100644 --- a/src/pages/questionList/index.tsx +++ b/src/pages/questionList/index.tsx @@ -1,6 +1,6 @@ /* eslint-disable no-console */ import { Button, View } from '@tarojs/components'; -import Taro from '@tarojs/taro'; +import Taro, { useDidShow } from '@tarojs/taro'; import { useEffect, useState } from 'react'; // import './index.scss'; @@ -41,6 +41,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 @@ -59,24 +74,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({