fix: 修复时区问题 - 使用本地时区存储和比较时间

## 问题
- 之前使用UTC时间存储expires_at
- 前端解析时当成本地时间,导致显示时间差8小时
- 例如: 数据库存 08:37 (UTC),前端显示 08:37 (本地),实际应该显示 16:37

## 修复
1. database.js create方法: 使用本地时区格式化时间
   - 使用 getFullYear/getMonth/getDate 等获取本地时间
   - 格式化为 YYYY-MM-DD HH:MM:SS (本地时间)

2. database.js findByCode方法: 使用本地时区比较
   - 改为 datetime('now', 'localtime')
   - 确保与存储的本地时间格式匹配

3. 调试日志也使用本地时区

## 效果
- 创建分享时,expires_at存储的是服务器本地时间
- 前端显示时间与服务器时间一致
- 过期判断使用本地时区,结果正确

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
2025-11-14 16:46:26 +08:00
parent d56388dd29
commit 8cda4c1a0b

View File

@@ -303,9 +303,15 @@ const ShareDB = {
if (expiry_days) { if (expiry_days) {
const expireDate = new Date(); const expireDate = new Date();
expireDate.setDate(expireDate.getDate() + parseInt(expiry_days)); expireDate.setDate(expireDate.getDate() + parseInt(expiry_days));
// 转换为SQLite datetime格式 (YYYY-MM-DD HH:MM:SS),而不是ISO格式 // 使用本地时区时间,而不是UTC时间
// 这样才能正确与 datetime('now') 进行比较 // 这样前端解析时会正确显示为本地时间
expiresAt = expireDate.toISOString().replace('T', ' ').replace(/\.\d+Z$/, ''); const year = expireDate.getFullYear();
const month = String(expireDate.getMonth() + 1).padStart(2, '0');
const day = String(expireDate.getDate()).padStart(2, '0');
const hours = String(expireDate.getHours()).padStart(2, '0');
const minutes = String(expireDate.getMinutes()).padStart(2, '0');
const seconds = String(expireDate.getSeconds()).padStart(2, '0');
expiresAt = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
} }
const stmt = db.prepare(` const stmt = db.prepare(`
@@ -336,7 +342,7 @@ const ShareDB = {
// 根据分享码查找 // 根据分享码查找
findByCode(shareCode) { findByCode(shareCode) {
// 调试日志: findByCode 调用 // 调试日志: findByCode 调用
const currentTime = db.prepare("SELECT datetime('now') as now").get(); const currentTime = db.prepare("SELECT datetime('now', 'localtime') as now").get();
console.log('[ShareDB.findByCode]', { console.log('[ShareDB.findByCode]', {
shareCode, shareCode,
currentTime: currentTime.now, currentTime: currentTime.now,
@@ -348,7 +354,7 @@ const ShareDB = {
FROM shares s FROM shares s
JOIN users u ON s.user_id = u.id JOIN users u ON s.user_id = u.id
WHERE s.share_code = ? WHERE s.share_code = ?
AND (s.expires_at IS NULL OR s.expires_at > datetime('now')) AND (s.expires_at IS NULL OR s.expires_at > datetime('now', 'localtime'))
`).get(shareCode); `).get(shareCode);
// 调试日志: SQL查询结果 // 调试日志: SQL查询结果