From d3340eb953aa65d7afa0151aae875143fc91b8f2 Mon Sep 17 00:00:00 2001 From: gyq <875626088@qq.com> Date: Mon, 1 Dec 2025 18:50:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=A1=B5=E9=9D=A2=E5=B0=8F?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=BC=98=E5=8C=96=E7=94=A8=E6=97=B6=E6=98=BE?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/src/App.vue | 2 - src/renderer/src/stores/user.js | 7 +- src/renderer/src/utils/index.js | 118 +++++++++++++++++++---------- src/renderer/src/utils/request.js | 8 +- src/renderer/src/views/home.vue | 119 ++++++++++++++++++++++-------- src/renderer/src/views/login.vue | 2 +- 6 files changed, 176 insertions(+), 80 deletions(-) diff --git a/src/renderer/src/App.vue b/src/renderer/src/App.vue index c698302..4804160 100644 --- a/src/renderer/src/App.vue +++ b/src/renderer/src/App.vue @@ -11,8 +11,6 @@ import { useSocketStore } from '@/stores/socket'; const pinia = createPinia() onMounted(() => { - console.log('WS URL:', import.meta.env.VITE_WS_URL); - // 重启或刷新防止ws丢失 const socketStore = useSocketStore(pinia) socketStore.restoreFromStorage() diff --git a/src/renderer/src/stores/user.js b/src/renderer/src/stores/user.js index 5a01c11..e7b1f1b 100644 --- a/src/renderer/src/stores/user.js +++ b/src/renderer/src/stores/user.js @@ -7,7 +7,8 @@ export const useUserStore = defineStore('user', { state: () => ({ token: '', shopInfo: '', - shopStaff: '' + shopStaff: '', + account: '' }), actions: { async login(params) { @@ -19,6 +20,8 @@ export const useUserStore = defineStore('user', { // 登录员工 if (res.loginType == 1) this.shopStaff = res.shopStaff // async 函数直接返回值即可 resolve + + this.account = params.username return res } else { ElMessage.error('先下单模式下不可登录,请联系管理员') @@ -68,7 +71,7 @@ export const useUserStore = defineStore('user', { { key: 'kitchen-user', storage: localStorage, - paths: ['token', 'shopInfo', 'shopStaff'] // 持久化指定状态 + paths: ['token', 'shopInfo', 'shopStaff', 'account'] // 持久化指定状态 } ] } diff --git a/src/renderer/src/utils/index.js b/src/renderer/src/utils/index.js index ef1d67a..b71a070 100644 --- a/src/renderer/src/utils/index.js +++ b/src/renderer/src/utils/index.js @@ -1,20 +1,24 @@ import dayjs from 'dayjs'; /** - * 计算当前时间与传入时间的时间差,并按规则格式化 + * 计算基准时间与传入目标时间的时间差,并按规则格式化 * @param {string|number|Date} targetTime - 传入的目标时间(支持字符串、时间戳、Date对象) - * @returns {string} 格式化后的时间差(≤1小时:MM:ss;>1小时:HH:MM:ss)| 错误提示 + * @param {string|number|Date} [currentTime=dayjs()] - 基准时间(默认当前时间),支持字符串、时间戳、Date对象 + * @returns {string} 格式化后的时间差(≤1小时:MM:ss;>1小时:HH:MM:ss)| 无效时间返回 "00:00" */ -export const formatTimeDiff = (targetTime) => { - // 1. 校验传入时间的有效性 +export const formatTimeDiff = (targetTime, currentTime = dayjs()) => { + // 1. 校验目标时间和基准时间的有效性 const target = dayjs(targetTime); - if (!target.isValid()) { - return '00:00'; // 无效时间返回提示 + const baseTime = dayjs(currentTime); // 解析基准时间(默认当前时间) + + // 任一时间无效,返回 "00:00" + if (!target.isValid() || !baseTime.isValid()) { + return '00:00'; } - // 2. 计算当前时间与目标时间的**秒级总差值**(当前时间 - 传入时间) - const diffSeconds = dayjs().diff(target, 'second'); - // 处理差值为负数的情况(传入时间在当前时间之后,差值为负) + // 2. 计算基准时间与目标时间的**秒级总差值**(基准时间 - 目标时间) + const diffSeconds = baseTime.diff(target, 'second'); + // 处理差值为负数的情况(目标时间在基准时间之后,取绝对值) const absDiffSeconds = Math.abs(diffSeconds); // 3. 按秒数分解为 小时、分钟、秒 @@ -36,68 +40,104 @@ export const formatTimeDiff = (targetTime) => { }; /** - * 判断时分秒字符串是否大于指定分钟数 - * @param {string} timeStr - 时分秒字符串,如 "26:30:40" + * 判断时间字符串是否大于指定分钟数(兼容分秒格式和时分秒格式) + * @param {string} timeStr - 时间字符串,支持两种格式: + * 1. 分秒格式:"MM:SS"(如 "12:34") + * 2. 时分秒格式:"HH:MM:SS"(如 "07:23:45") * @param {number} [thresholdMinutes=15] - 分钟阈值,默认15分钟 * @returns {boolean} 大于阈值返回true,否则false(格式错误返回false) */ export const isMoreThanSpecifiedMinutes = (timeStr, thresholdMinutes = 15) => { - // 1. 按冒号分割时分秒并转换为数字 - const [hours, minutes, seconds] = timeStr.split(':').map(Number); + console.log('isMoreThanSpecifiedMinutes===', timeStr); - // 2. 校验时分秒格式是否合法 + // 1. 按冒号分割时间字符串并转换为数字数组 + const timeParts = timeStr.split(':').map(Number); + + // 2. 校验格式合法性(仅允许 2 段【分秒】或 3 段【时分秒】,且所有部分为有效数字) if ( - isNaN(hours) || - isNaN(minutes) || - isNaN(seconds) || - hours < 0 || - minutes < 0 || minutes >= 60 || - seconds < 0 || seconds >= 60 + !([2, 3].includes(timeParts.length)) || // 仅支持 2 或 3 段 + timeParts.some(part => isNaN(part)) || // 存在非数字部分 + timeParts.some(part => part < 0) // 存在负数 ) { - console.error('时分秒格式错误,示例:"26:30:40"'); + console.error('时间格式错误,支持:分秒格式(如 "12:34")或时分秒格式(如 "07:23:45")'); return false; } - // 3. 转换为总秒数:小时*3600 + 分钟*60 + 秒 + // 3. 根据分割长度补全小时位,统一转换为 [小时, 分钟, 秒] + let hours = 0, minutes = 0, seconds = 0; + if (timeParts.length === 3) { + // 时分秒格式:HH:MM:SS + [hours, minutes, seconds] = timeParts; + // 额外校验:分钟和秒不能超过 59 + if (minutes >= 60 || seconds >= 60) { + console.error('时分秒格式错误:分钟和秒必须小于 60'); + return false; + } + } else if (timeParts.length === 2) { + // 分秒格式:MM:SS → 补全小时为 0 + [minutes, seconds] = timeParts; + // 额外校验:分钟和秒不能超过 59 + if (minutes >= 60 || seconds >= 60) { + console.error('分秒格式错误:分钟和秒必须小于 60'); + return false; + } + } + + // 4. 转换为总秒数:小时*3600 + 分钟*60 + 秒 const totalSeconds = hours * 3600 + minutes * 60 + seconds; - // 4. 将分钟阈值转换为总秒数,进行比较 + // 5. 将分钟阈值转换为总秒数,进行比较 const thresholdSeconds = thresholdMinutes * 60; return totalSeconds > thresholdSeconds; }; /** - * 计算时分秒字符串超出指定分钟阈值的时长(返回HH:MM:SS格式) - * @param {string} timeStr - 时分秒字符串,如 "26:30:40" + * 计算时间字符串超出指定分钟阈值的时长(返回HH:MM:SS格式,兼容分秒/时分秒输入) + * @param {string} timeStr - 时间字符串,支持两种格式: + * 1. 分秒格式:"MM:SS"(如 "12:34") + * 2. 时分秒格式:"HH:MM:SS"(如 "07:23:45") * @param {number} [thresholdMinutes=15] - 分钟阈值,默认15分钟 * @returns {string} 超时时长(HH:MM:SS),未超时返回"00:00:00",格式错误返回"格式错误" */ export const calculateTimeoutDuration = (timeStr, thresholdMinutes = 15) => { - // 1. 按冒号分割时分秒并转换为数字 - const [hours, minutes, seconds] = timeStr.split(':').map(Number); + // 新增:清理输入字符串(去除前后空格、替换中文冒号为英文冒号) + const cleanedTimeStr = timeStr.trim().replace(/:/g, ':'); + // 按英文冒号分割并转换为数字数组 + const timeParts = cleanedTimeStr.split(':').map(Number); - // 2. 校验时分秒格式合法性 + // 校验格式合法性(仅支持 2 段【分秒】或 3 段【时分秒】) if ( - isNaN(hours) || - isNaN(minutes) || - isNaN(seconds) || - hours < 0 || - minutes < 0 || minutes >= 60 || - seconds < 0 || seconds >= 60 + !([2, 3].includes(timeParts.length)) || // 仅允许 2/3 段分割结果 + timeParts.some(part => isNaN(part)) || // 存在非数字部分 + timeParts.some(part => part < 0) // 存在负数 ) { - console.error('时分秒格式错误,示例:"26:30:40"'); + console.error('时间格式错误,支持:分秒格式(如 "12:34")或时分秒格式(如 "07:23:45")'); return '格式错误'; } - // 3. 转换为总秒数 + // 根据分割长度补全小时位,统一转换为 [小时, 分钟, 秒] + let hours = 0, minutes = 0, seconds = 0; + if (timeParts.length === 3) { + [hours, minutes, seconds] = timeParts; + if (minutes >= 60 || seconds >= 60) { + console.error('时分秒格式错误:分钟和秒必须小于 60'); + return '格式错误'; + } + } else if (timeParts.length === 2) { + [minutes, seconds] = timeParts; + if (minutes >= 60 || seconds >= 60) { + console.error('分秒格式错误:分钟和秒必须小于 60'); + return '格式错误'; + } + } + + // 计算总秒数、阈值秒数、超时秒数 const totalSeconds = hours * 3600 + minutes * 60 + seconds; - // 4. 计算阈值对应的总秒数 const thresholdSeconds = thresholdMinutes * 60; - // 5. 计算超时秒数(差值≤0则未超时) const timeoutSeconds = Math.max(totalSeconds - thresholdSeconds, 0); - // 6. 将超时秒数转换回HH:MM:SS格式(补零处理) + // 格式化为 HH:MM:SS const padZero = (num) => num.toString().padStart(2, '0'); const timeoutHours = padZero(Math.floor(timeoutSeconds / 3600)); const timeoutMins = padZero(Math.floor((timeoutSeconds % 3600) / 60)); diff --git a/src/renderer/src/utils/request.js b/src/renderer/src/utils/request.js index 4218085..35586be 100644 --- a/src/renderer/src/utils/request.js +++ b/src/renderer/src/utils/request.js @@ -40,15 +40,15 @@ service.interceptors.request.use( // 响应拦截器 service.interceptors.response.use( (response) => { + const userStore = useUserStore() // 对响应数据做点什么 if (+response.status === 200) { if (+response.data.code == 200) { return response.data.data; } else if (+response.data.code == 501) { - useStorage.del("token"); - useStorage.del("userInfo"); - useStorage.del("shopInfo"); - useStorage.del("douyin"); + userStore.shopInfo = '' + userStore.token = '' + userStore.shopStaff = '' ElMessage.error("登录已过期,请重新登录"); window.location.reload(); return Promise.reject("登录已过期,请重新登录"); diff --git a/src/renderer/src/views/home.vue b/src/renderer/src/views/home.vue index 588fad4..c189cce 100644 --- a/src/renderer/src/views/home.vue +++ b/src/renderer/src/views/home.vue @@ -17,11 +17,9 @@
-
- - {{ item.label }} - +
+ {{ item.label }}
@@ -62,10 +60,10 @@ }})
-
+
@@ -74,21 +72,19 @@
{{ selectItem.tableName || '-' }} | {{ selectItem.areaName || '-' }} - 待出菜({{ selectItem.pendingDishCount || '-' }}) + 待出菜({{ selectItem.pendingDishCount }}) 员工名称:{{ selectItem.staffName || notStaff }} 下单时间:{{ selectItem.orderTime || '-' }}
- - + + +