From 69c67a7e52baadaf92a7dbf3e666fb2ac8970cf5 Mon Sep 17 00:00:00 2001 From: SunWuyuan Date: Sun, 16 Mar 2025 09:41:48 +0800 Subject: [PATCH] 1 --- src/pages/index.vue | 111 ++++++++++++++++++------------ src/styles/transitions.scss | 1 + src/utils/defaults/defaultData.js | 16 +++++ src/utils/providers/indexedDB.js | 11 +-- src/utils/settings.js | 10 +-- 5 files changed, 92 insertions(+), 57 deletions(-) create mode 100644 src/utils/defaults/defaultData.js diff --git a/src/pages/index.vue b/src/pages/index.vue index d10642a..5f14e15 100644 --- a/src/pages/index.vue +++ b/src/pages/index.vue @@ -5,7 +5,7 @@ - {{ state.classNumber }}班 - {{ titleText }} + {{ state.classNumber }} - {{ titleText }} @@ -25,14 +25,13 @@ - - + + value.content?.trim()) .map(([key, value]) => ({ key, - name: this.state.availableSubjects.find((s) => s.key === key)?.name || key, + name: + this.state.availableSubjects.find((s) => s.key === key)?.name || + key, content: value.content, order: this.state.subjectOrder.indexOf(key), rowSpan: Math.ceil( @@ -593,6 +591,32 @@ export default { }, methods: { + // 添加新的日期辅助方法 + ensureDate(dateInput) { + if (dateInput instanceof Date) { + return dateInput; + } + if (typeof dateInput === "string") { + const date = new Date(dateInput); + if (!isNaN(date.getTime())) { + return date; + } + } + return new Date(); // 如果无法解析,返回当前日期 + }, + + formatDate(dateInput) { + const date = this.ensureDate(dateInput); + const year = date.getFullYear(); + const month = String(date.getMonth() + 1).padStart(2, "0"); + const day = String(date.getDate()).padStart(2, "0"); + return `${year}-${month}-${day}`; + }, + + getToday() { + return new Date(); + }, + async initializeData() { this.provider = getSetting("server.provider"); const domain = getSetting("server.domain"); @@ -605,12 +629,14 @@ export default { // 从 URL 获取日期,如果没有则使用今天的日期 const urlParams = new URLSearchParams(window.location.search); const dateFromUrl = urlParams.get("date"); - const today = new Date().toISOString().split("T")[0]; + const today = this.getToday(); // 确保日期格式正确 - this.state.dateString = dateFromUrl || today; - this.state.selectedDate = this.state.dateString; // 添加这行,设置默认选中日期 - this.state.isToday = this.state.dateString === today; + const currentDate = dateFromUrl ? new Date(dateFromUrl) : today; + this.state.dateString = this.formatDate(currentDate); + this.state.selectedDate = this.state.dateString; + this.state.isToday = + this.formatDate(currentDate) === this.formatDate(today); await Promise.all([this.downloadData(), this.loadConfig()]); }, @@ -700,7 +726,8 @@ export default { if (!this.currentEditSubject) return; const content = this.state.textarea.trim(); - const originalContent = this.state.boardData.homework[this.currentEditSubject]?.content || ''; + const originalContent = + this.state.boardData.homework[this.currentEditSubject]?.content || ""; // 如果内容发生变化(包括清空),就视为修改 if (content !== originalContent.trim()) { @@ -785,9 +812,9 @@ export default { content: "", }; } - this.state.dialogTitle = this.state.availableSubjects.find( - (s) => s.key === subject - )?.name || subject; + this.state.dialogTitle = + this.state.availableSubjects.find((s) => s.key === subject)?.name || + subject; this.state.textarea = this.state.boardData.homework[subject].content; this.state.dialogVisible = true; this.$nextTick(() => { @@ -890,22 +917,16 @@ export default { if (!newDate) return; try { - // 确保日期格式正确 - const date = typeof newDate === 'string' ? new Date(newDate) : newDate; - if (!(date instanceof Date) || isNaN(date.getTime())) { - console.error('Invalid date:', newDate); - return; - } - - const year = date.getFullYear(); - const month = String(date.getMonth() + 1).padStart(2, "0"); - const day = String(date.getDate()).padStart(2, "0"); - const formattedDate = `${year}-${month}-${day}`; + const selectedDate = this.ensureDate(newDate); + const formattedDate = this.formatDate(selectedDate); // 只有当日期真正改变时才更新 if (this.state.dateString !== formattedDate) { this.state.dateString = formattedDate; this.state.selectedDate = formattedDate; + this.state.isToday = + formattedDate === this.formatDate(this.getToday()); + // 使用 replace 而不是 push 来避免创建新的历史记录 this.$router .replace({ @@ -915,8 +936,8 @@ export default { this.downloadData(); } } catch (error) { - console.error('Date processing error:', error); - this.$message.error('日期处理错误', '请重新选择日期'); + console.error("Date processing error:", error); + this.$message.error("日期处理错误", "请重新选择日期"); } }, diff --git a/src/styles/transitions.scss b/src/styles/transitions.scss index 18f77eb..e195579 100644 --- a/src/styles/transitions.scss +++ b/src/styles/transitions.scss @@ -136,6 +136,7 @@ $standard-accelerate: cubic-bezier(0.3, 0.0, 1.0, 1.0); transform 150ms $emphasized-decelerate; touch-action: manipulation; min-height: 40px; // 确保触摸目标足够大 + min-width: 40px; &:active { transform: scale(0.98); diff --git a/src/utils/defaults/defaultData.js b/src/utils/defaults/defaultData.js new file mode 100644 index 0000000..80f315a --- /dev/null +++ b/src/utils/defaults/defaultData.js @@ -0,0 +1,16 @@ +export const defaultConfig = { + studentList: [ + "Classworks可以管理学生列表", + '你可以点击设置,在其中找到"学生列表"', + "在添加学生处输入学生姓名,点击添加", + "或者点击高级编辑,从Excel表格中复制数据并粘贴进来", + ], +}; + +export const defaultHomework = { + homework: {}, + attendance: { + absent: [], + late: [], + }, +}; diff --git a/src/utils/providers/indexedDB.js b/src/utils/providers/indexedDB.js index be1572d..33c939f 100644 --- a/src/utils/providers/indexedDB.js +++ b/src/utils/providers/indexedDB.js @@ -1,5 +1,6 @@ import { openDB } from 'idb'; import { formatResponse, formatError } from '../dataProvider'; +import { defaultConfig, defaultHomework } from '../defaults/defaultData'; const DB_NAME = "HomeworkDB"; const DB_VERSION = 1; @@ -32,10 +33,7 @@ export const indexedDBProvider = { if (!data) { const today = new Date().toISOString().split("T")[0]; if (date === today) { - return formatResponse({ - homework: {}, - attendance: { absent: [], late: [] }, - }); + return formatResponse(defaultHomework); } return formatError("数据不存在", "NOT_FOUND"); } @@ -74,10 +72,7 @@ export const indexedDBProvider = { const config = await db.get("config", storageKey); if (!config) { - return formatResponse({ - studentList: [], - displayOptions: {}, - }); + return formatResponse(defaultConfig); } return formatResponse(JSON.parse(config)); diff --git a/src/utils/settings.js b/src/utils/settings.js index e8ece11..0bc4fc1 100644 --- a/src/utils/settings.js +++ b/src/utils/settings.js @@ -61,7 +61,7 @@ window.addEventListener("load", initializeStorage); */ // 存储所有设置的localStorage键名 -const SETTINGS_STORAGE_KEY = "classworks_settings"; +const SETTINGS_STORAGE_KEY = "Classworks_settings"; /** * 所有配置项的定义 @@ -107,9 +107,11 @@ const settingsDefinitions = { }, "server.classNumber": { type: "string", - default: "", - validate: (value) => /^[A-Za-z0-9]*$/.test(value), - description: "班级编号(无论使用哪种存储方式都需要设置)", + default: "高三八班", + //validate: (value) => /^[A-Za-z0-9]*$/.test(value), + validate: (value) => /.*/.test(value), + + description: "班级编号", }, "server.provider": { type: "string",