fix: 修复前后端时区问题和日期范围选择器性能
- 前端时区修复:统一使用本地时区格式化日期 - 日期范围选择器优化:使用 untrack 避免循环更新 - 后端时区修复:使用 time.ParseInLocation 指定本地时区 - 其他优化:修复分页逻辑
This commit is contained in:
@@ -24,9 +24,9 @@ type ListBillsRequest struct {
|
||||
|
||||
// ListBillsResponse 账单列表响应
|
||||
type ListBillsResponse struct {
|
||||
Result bool `json:"result"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data *ListBillsData `json:"data,omitempty"`
|
||||
Result bool `json:"result"`
|
||||
Message string `json:"message,omitempty"`
|
||||
Data *ListBillsData `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// ListBillsData 账单列表数据
|
||||
@@ -69,13 +69,15 @@ func ListBills(c *gin.Context) {
|
||||
if req.StartDate != "" || req.EndDate != "" {
|
||||
timeFilter := make(map[string]interface{})
|
||||
if req.StartDate != "" {
|
||||
startTime, err := time.Parse("2006-01-02", req.StartDate)
|
||||
// 使用本地时区解析日期,避免 UTC 时区问题
|
||||
startTime, err := time.ParseInLocation("2006-01-02", req.StartDate, time.Local)
|
||||
if err == nil {
|
||||
timeFilter["$gte"] = startTime
|
||||
}
|
||||
}
|
||||
if req.EndDate != "" {
|
||||
endTime, err := time.Parse("2006-01-02", req.EndDate)
|
||||
// 使用本地时区解析日期,避免 UTC 时区问题
|
||||
endTime, err := time.ParseInLocation("2006-01-02", req.EndDate, time.Local)
|
||||
if err == nil {
|
||||
// 结束日期包含当天,所以加一天
|
||||
endTime = endTime.Add(24 * time.Hour)
|
||||
|
||||
@@ -489,13 +489,14 @@ func saveCleanedBillsFromJSON(filePath, billType, sourceFile, uploadBatch string
|
||||
}
|
||||
|
||||
// parseTime 解析时间字符串
|
||||
// 使用本地时区解析,避免 UTC 时区问题
|
||||
func parseTime(s string) time.Time {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
// 尝试多种时间格式
|
||||
// 尝试多种时间格式(使用本地时区)
|
||||
formats := []string{
|
||||
"2006-01-02 15:04:05",
|
||||
"2006/01/02 15:04:05",
|
||||
@@ -506,7 +507,7 @@ func parseTime(s string) time.Time {
|
||||
}
|
||||
|
||||
for _, format := range formats {
|
||||
if t, err := time.Parse(format, s); err == nil {
|
||||
if t, err := time.ParseInLocation(format, s, time.Local); err == nil {
|
||||
return t
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user