From 031eef964343c37585bff308d83d241230122c3e Mon Sep 17 00:00:00 2001 From: nepiedg <806669289@qq.com> Date: Mon, 23 Feb 2026 22:24:29 +0800 Subject: [PATCH] aaa --- api/request.js | 22 +- .../smoke-record-dialog.vue | 7 +- config/index.js | 2 +- pages/logs/index.vue | 15 +- pages/stats/index.vue | 584 ++++++++++++++---- stores/logs.js | 7 +- utils/time.js | 5 +- 7 files changed, 494 insertions(+), 148 deletions(-) diff --git a/api/request.js b/api/request.js index 757439d..4c0e420 100644 --- a/api/request.js +++ b/api/request.js @@ -1,9 +1,19 @@ import { BASE_URL } from '@/config' import { storage, SESSION_KEY } from '@/utils/storage' +import { login as authLogin } from './auth' + +// 是否为 token 失效(HTTP 401 或 body code 401,如 invalid token) +function isInvalidToken(res) { + if (res.statusCode === 401) return true + const body = res.data + if (body && body.code === 401) return true + return false +} export const request = { async request(options) { const sessionKey = storage.get(SESSION_KEY) + const isRetryAfter401 = options._retryAfter401 === true return new Promise((resolve, reject) => { uni.request({ @@ -15,11 +25,15 @@ export const request = { 'Authorization': sessionKey ? `Bearer ${sessionKey}` : '' }, success: async (res) => { - if (res.statusCode === 401) { - const { login } = await import('./auth') + if (isInvalidToken(res)) { + if (isRetryAfter401) { + reject(new Error(res.data?.message || 'invalid token')) + return + } try { - await login() - resolve(this.request(options)) + await authLogin() + const nextOpts = { ...options, _retryAfter401: true } + resolve(this.request(nextOpts)) } catch (e) { reject(e) } diff --git a/components/smoke-record-dialog/smoke-record-dialog.vue b/components/smoke-record-dialog/smoke-record-dialog.vue index fd3e4d2..bd30fa9 100644 --- a/components/smoke-record-dialog/smoke-record-dialog.vue +++ b/components/smoke-record-dialog/smoke-record-dialog.vue @@ -147,9 +147,12 @@ export default { num: this.initialData.num ?? 1 } } else { - // 新建模式,使用当前时间 + // 新建模式,使用当前本地时间(不用 toISOString,避免 UTC 导致日期差一天) const now = new Date() - const dateStr = now.toISOString().split('T')[0] + const y = now.getFullYear() + const m = String(now.getMonth() + 1).padStart(2, '0') + const d = String(now.getDate()).padStart(2, '0') + const dateStr = `${y}-${m}-${d}` const timeStr = `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}` const datetimeStr = `${dateStr} ${timeStr}:00` diff --git a/config/index.js b/config/index.js index c625986..0488f6b 100644 --- a/config/index.js +++ b/config/index.js @@ -1,6 +1,6 @@ const ENV = { development: { - BASE_URL: ' http://192.168.31.132:8080/api/v1', + BASE_URL: 'http://192.168.31.46:8080/api/v1', MINI_PROGRAM_ID: 2 }, production: { diff --git a/pages/logs/index.vue b/pages/logs/index.vue index 614e92d..382d59d 100644 --- a/pages/logs/index.vue +++ b/pages/logs/index.vue @@ -13,9 +13,6 @@ {{ tab.label }} - - 📅 - @@ -165,6 +162,14 @@ const groupedLogs = computed(() => { }, {}) }) +// 本地日期 YYYY-MM-DD(避免 toISOString 用 UTC 导致日期差一天) +function localDateStr(d) { + const y = d.getFullYear() + const m = String(d.getMonth() + 1).padStart(2, '0') + const day = String(d.getDate()).padStart(2, '0') + return `${y}-${m}-${day}` +} + // 格式化分组标题 function formatGroupTitle(dateStr) { if (!dateStr) return '' @@ -174,8 +179,8 @@ function formatGroupTitle(dateStr) { const yesterday = new Date(today) yesterday.setDate(yesterday.getDate() - 1) - const todayStr = today.toISOString().split('T')[0] - const yesterdayStr = yesterday.toISOString().split('T')[0] + const todayStr = localDateStr(today) + const yesterdayStr = localDateStr(yesterday) if (dateStr === todayStr) { return '今天' diff --git a/pages/stats/index.vue b/pages/stats/index.vue index 4468111..345e942 100644 --- a/pages/stats/index.vue +++ b/pages/stats/index.vue @@ -1,4 +1,4 @@ -