Skip to content

Commit 3d2b6e3

Browse files
committed
feat: 添加缓存校验以避免使用过期缓存
1 parent 211b56e commit 3d2b6e3

3 files changed

Lines changed: 31 additions & 24 deletions

File tree

src/common/final.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// 记得用斜杠,否则IOS会不正常
22
export const START_TIME = new Date("2025/2/17").getTime();
33
export const END_TIME = new Date("2025/6/14").getTime();
4-
export const TOTAL_WEEK = 19;
4+
export const TOTAL_WEEK = 20;
55
export const BASE_URL = "";
66
export const VERSION = 0.41;
77
export const WEB_VERSION = "1.0"
8-
export const NOTICE = "(有点重要的) 姑且做了点安全升级,建议更新一下APP防止用不了,反馈QQ群:706437676 "
8+
export const NOTICE = "如有课程错误请尝试右上角关于清除缓存,反馈QQ群:706437676 "

src/js/CourseUtils.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {getExpCourse, getNormCourse} from "@/api/getCourse";
2+
import {START_TIME} from "@/common/final";
23

34
class Lesson {
45
constructor(jw_course_code, base_teacher_name, base_room_name, week, jw_task_book_no, jw_course_name, section_end, week_day, section, base_teacher_no, section_start) {
@@ -124,6 +125,7 @@ export function refreshNormalCourse(callback) {
124125
getNormCourse().then((response) => {
125126
if (response.status === 200 && response.data.code === 0) {
126127
localStorage.setItem("norm", JSON.stringify(response.data.data));
128+
localStorage.setItem("normStamp", START_TIME.toString());
127129
}
128130
callback(response);
129131
});
@@ -136,6 +138,7 @@ export function refreshExpCourse(callback) {
136138
getExpCourse().then((response) => {
137139
if (response.status === 200 && response.data.code === 0) {
138140
localStorage.setItem("exp", JSON.stringify(response.data.data));
141+
localStorage.setItem("expStamp", START_TIME.toString());
139142
}
140143
callback(response);
141144
});

src/views/course.vue

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,8 @@ export default {
9696
for (let i = 0; i <= this.totalWeek; i++) {
9797
this.lessonsList.push([]);
9898
}
99-
// 强制清理一下缓存
100-
if (localStorage.getItem("refresh") === null) {
101-
this.clearLocalCache();
102-
localStorage.setItem("refresh", "true");
103-
console.log("清理缓存");
104-
} else if (this.curWeek > this.totalWeek || this.curWeek === 0) {
105-
localStorage.removeItem("refresh");
106-
}
99+
// 缓存验证
100+
this.validateLocalCache();
107101
refreshExpCourse();
108102
refreshNormalCourse();
109103
this.setActiveDay();
@@ -160,19 +154,19 @@ export default {
160154
if (Number(this.curWeek) > Number(this.totalWeek)) {
161155
localStorage.setItem("cur", this.totalWeek.toString())
162156
localStorage.setItem("lessons", "[]");
163-
return;
157+
} else {
158+
localStorage.setItem("cur", this.curWeek.toString());
159+
this.getRound(this.curWeek);
160+
localStorage.setItem("lessons", JSON.stringify(this.lessonsList[this.curWeek]));
164161
}
165-
localStorage.setItem("cur", this.curWeek.toString());
166-
this.getRound(this.curWeek);
167-
localStorage.setItem("lessons", JSON.stringify(this.lessonsList[this.curWeek]));
168162
},
169163
// 获取所选周前后一周的课程
170164
getRound: function (index) {
171-
let exp = localStorage.getItem("exp");
172-
let norm = localStorage.getItem("norm");
173165
let setCourse = () => {
174-
exp = (exp == null ? [] : JSON.parse(exp));
175-
norm = (norm == null ? [] : JSON.parse(norm));
166+
let expStr = localStorage.getItem("exp");
167+
let normStr = localStorage.getItem("norm");
168+
let exp = (expStr == null ? [] : JSON.parse(expStr));
169+
let norm = (normStr == null ? [] : JSON.parse(normStr));
176170
const fillCourse = async (w) => {
177171
if (this.lessonsList[w].length === 0) {
178172
this.lessonsList[w] = simpleSelectWeek(w, exp.concat(norm));
@@ -186,7 +180,7 @@ export default {
186180
}, 500);
187181
}
188182
189-
if (exp == null) {
183+
if (localStorage.getItem("exp") == null) {
190184
this.show = true;
191185
refreshExpCourse((response) => {
192186
if (response.status !== 200) {
@@ -198,7 +192,7 @@ export default {
198192
setCourse();
199193
});
200194
}
201-
if (norm == null) {
195+
if (localStorage.getItem("norm") == null) {
202196
this.show = true;
203197
refreshNormalCourse((response) => {
204198
if (response.status !== 200) {
@@ -248,10 +242,20 @@ export default {
248242
return colors[num % colors.length];
249243
},
250244
251-
clearLocalCache: function () {
252-
localStorage.removeItem("lessons");
253-
localStorage.removeItem("norm");
254-
localStorage.removeItem("exp");
245+
validateLocalCache: function () {
246+
let normStamp = localStorage.getItem("normStamp");
247+
let expStamp = localStorage.getItem("expStamp");
248+
if (normStamp !== START_TIME.toString() || expStamp !== START_TIME.toString()) {
249+
localStorage.removeItem("refresh");
250+
localStorage.removeItem("lessons");
251+
this.show = true;
252+
}
253+
if (normStamp !== START_TIME.toString()) {
254+
localStorage.removeItem("norm");
255+
}
256+
if (expStamp !== START_TIME.toString()) {
257+
localStorage.removeItem("exp");
258+
}
255259
},
256260
}
257261
}

0 commit comments

Comments
 (0)