fix: 日志时区修复 + 日期选择器

- logger.ts: 使用 UTC+8 时区记录日志
- logs.ts: 统计使用 UTC+8 今日日期
- logs.vue: 日期选择器改用 picker 组件
- 移除未使用的 showDatePicker 变量
This commit is contained in:
2026-06-08 15:35:50 +08:00
parent fc3491742f
commit 3ce5ff4b65
3 changed files with 32 additions and 11 deletions

View File

@@ -53,10 +53,12 @@
<view class="query-section"> <view class="query-section">
<view class="query-header"> <view class="query-header">
<text class="section-title">日志查询</text> <text class="section-title">日志查询</text>
<view class="date-picker" @tap="showDatePicker = true"> <picker mode="date" :value="selectedDate" @change="onDateChange">
<Icon name="calendar" :size="28" color="#8B7E7E" /> <view class="date-picker">
<text class="date-text">{{ selectedDate }}</text> <Icon name="calendar" :size="28" color="#8B7E7E" />
</view> <text class="date-text">{{ selectedDate }}</text>
</view>
</picker>
</view> </view>
<!-- 筛选条件 --> <!-- 筛选条件 -->
@@ -109,7 +111,6 @@ const logLines = ref<string[]>([])
const loading = ref(false) const loading = ref(false)
const page = ref(1) const page = ref(1)
const total = ref(0) const total = ref(0)
const showDatePicker = ref(false)
const noMore = computed(() => logLines.value.length >= total.value && total.value > 0) const noMore = computed(() => logLines.value.length >= total.value && total.value > 0)
@@ -178,6 +179,12 @@ function selectDate(date: string) {
loadLogs() loadLogs()
} }
function onDateChange(e: any) {
selectedDate.value = e.detail.value
page.value = 1
loadLogs()
}
function getBarWidth(count: number): number { function getBarWidth(count: number): number {
if (!stats.value) return 0 if (!stats.value) return 0
const max = Math.max(...stats.value.daily.map(d => d.total), 1) const max = Math.max(...stats.value.daily.map(d => d.total), 1)

View File

@@ -8,15 +8,22 @@ if (!fs.existsSync(LOG_DIR)) {
fs.mkdirSync(LOG_DIR, { recursive: true }) fs.mkdirSync(LOG_DIR, { recursive: true })
} }
/** 获取当前日期字符串 YYYY-MM-DD */ /** 获取 UTC+8 时间 */
function getChinaTime(date: Date = new Date()): Date {
const utc = date.getTime() + date.getTimezoneOffset() * 60000
return new Date(utc + 8 * 3600000)
}
/** 获取当前日期字符串 YYYY-MM-DDUTC+8 */
function getDateStr(): string { function getDateStr(): string {
const d = new Date() const d = getChinaTime()
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}` return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
} }
/** 格式化时间 HH:mm:ss.SSS */ /** 格式化时间 HH:mm:ss.SSSUTC+8 */
function formatTime(date: Date): string { function formatTime(date: Date): string {
return `${String(date.getHours()).padStart(2, '0')}:${String(date.getMinutes()).padStart(2, '0')}:${String(date.getSeconds()).padStart(2, '0')}.${String(date.getMilliseconds()).padStart(3, '0')}` const d = getChinaTime(date)
return `${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}:${String(d.getSeconds()).padStart(2, '0')}.${String(d.getMilliseconds()).padStart(3, '0')}`
} }
/** 写入日志文件 */ /** 写入日志文件 */

View File

@@ -7,6 +7,14 @@ import { requireAdmin } from '../middleware/requireAdmin'
const router = Router() const router = Router()
const LOG_DIR = path.resolve(process.env.LOG_DIR || './logs') const LOG_DIR = path.resolve(process.env.LOG_DIR || './logs')
/** 获取 UTC+8 今日日期字符串 */
function getTodayStr(): string {
const now = new Date()
const utc = now.getTime() + now.getTimezoneOffset() * 60000
const d = new Date(utc + 8 * 3600000)
return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`
}
/** 获取日志列表最近7天 */ /** 获取日志列表最近7天 */
router.get('/files', requireAdmin, (_req, res) => { router.get('/files', requireAdmin, (_req, res) => {
try { try {
@@ -45,8 +53,7 @@ router.get('/stats', requireAdmin, (_req, res) => {
return res.json({ code: 0, data: { today: { total: 0, errors: 0, slow: 0 }, files: [] } }) return res.json({ code: 0, data: { today: { total: 0, errors: 0, slow: 0 }, files: [] } })
} }
const today = new Date() const dateStr = getTodayStr()
const dateStr = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
const todayFile = path.join(LOG_DIR, `${dateStr}.log`) const todayFile = path.join(LOG_DIR, `${dateStr}.log`)
let todayStats = { total: 0, errors: 0, slow: 0, warns: 0 } let todayStats = { total: 0, errors: 0, slow: 0, warns: 0 }