fix: 修复账单时间显示为UTC时区问题,改为本地时间

- 新增 LocalTime 自定义类型,JSON序列化输出本地时间格式
- 修改 CleanedBill.Time 字段类型为 LocalTime
- 更新 parseTime 函数返回 LocalTime 类型
- 前端添加 formatDateTime 工具函数(兼容处理)
- 版本号更新至 1.0.2
This commit is contained in:
2026-01-11 21:40:27 +08:00
parent d49d9afb3a
commit 8a9de1b328
9 changed files with 107 additions and 14 deletions

View File

@@ -3,9 +3,70 @@ package model
import (
"time"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsontype"
"go.mongodb.org/mongo-driver/bson/primitive"
)
// LocalTime 自定义时间类型JSON 序列化时输出本地时间格式
type LocalTime time.Time
// MarshalJSON 实现 json.Marshaler 接口,输出本地时间格式
func (t LocalTime) MarshalJSON() ([]byte, error) {
tt := time.Time(t)
if tt.IsZero() {
return []byte(`""`), nil
}
// 输出格式: "2006-01-02 15:04:05"
return []byte(`"` + tt.Local().Format("2006-01-02 15:04:05") + `"`), nil
}
// UnmarshalJSON 实现 json.Unmarshaler 接口
func (t *LocalTime) UnmarshalJSON(data []byte) error {
s := string(data)
if s == `""` || s == "null" {
*t = LocalTime(time.Time{})
return nil
}
// 去掉引号
s = s[1 : len(s)-1]
// 尝试多种格式解析
formats := []string{
"2006-01-02 15:04:05",
"2006-01-02T15:04:05Z07:00",
"2006-01-02T15:04:05Z",
"2006-01-02",
}
for _, format := range formats {
if parsed, err := time.ParseInLocation(format, s, time.Local); err == nil {
*t = LocalTime(parsed)
return nil
}
}
return nil
}
// MarshalBSONValue 实现 bson.ValueMarshaler 接口
func (t LocalTime) MarshalBSONValue() (bsontype.Type, []byte, error) {
return bson.MarshalValue(time.Time(t))
}
// UnmarshalBSONValue 实现 bson.ValueUnmarshaler 接口
func (t *LocalTime) UnmarshalBSONValue(btype bsontype.Type, data []byte) error {
var tt time.Time
if err := bson.UnmarshalValue(btype, data, &tt); err != nil {
return err
}
*t = LocalTime(tt)
return nil
}
// Time 返回标准 time.Time
func (t LocalTime) Time() time.Time {
return time.Time(t)
}
// RawBill 原始账单记录(存储上传的原始数据)
type RawBill struct {
ID primitive.ObjectID `bson:"_id,omitempty" json:"id,omitempty"`
@@ -23,7 +84,7 @@ type CleanedBill struct {
BillType string `bson:"bill_type" json:"bill_type"` // 账单类型: alipay/wechat
TransactionID string `bson:"transaction_id" json:"transaction_id"` // 交易订单号(用于去重)
MerchantOrderNo string `bson:"merchant_order_no" json:"merchant_order_no"` // 商家订单号(用于去重)
Time time.Time `bson:"time" json:"time"` // 交易时间
Time LocalTime `bson:"time" json:"time"` // 交易时间(本地时间格式)
Category string `bson:"category" json:"category"` // 交易分类
Merchant string `bson:"merchant" json:"merchant"` // 交易对方
Description string `bson:"description" json:"description"` // 商品说明