fix: 解决时间同步错误的问题

This commit is contained in:
ssongliu 2022-12-22 18:06:49 +08:00 committed by ssongliu
parent 26eacf81b7
commit 1e46d6359b
5 changed files with 20 additions and 35 deletions

View File

@ -92,13 +92,14 @@ func (b *BaseApi) SyncTime(c *gin.Context) {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
ts := ntime.Format(timeLayoutStr)
if err := ntp.UpdateSystemDate(ts); err != nil {
helper.ErrorWithDetail(c, constant.CodeErrInternalServer, constant.ErrTypeInternalServer, err)
return
}
helper.SuccessWithData(c, ts)
helper.SuccessWithData(c, ntime.String())
}
func (b *BaseApi) CleanMonitor(c *gin.Context) {

View File

@ -4,23 +4,23 @@ type SettingInfo struct {
UserName string `json:"userName"`
Email string `json:"email"`
SessionTimeout int `json:"sessionTimeout"`
SessionTimeout string `json:"sessionTimeout"`
LocalTime string `json:"localTime"`
PanelName string `json:"panelName"`
Theme string `json:"theme"`
Language string `json:"language"`
ServerPort int `json:"serverPort"`
ServerPort string `json:"serverPort"`
SecurityEntrance string `json:"securityEntrance"`
ExpirationDays int `json:"expirationDays"`
ExpirationDays string `json:"expirationDays"`
ExpirationTime string `json:"expirationTime"`
ComplexityVerification string `json:"complexityVerification"`
MFAStatus string `json:"mfaStatus"`
MFASecret string `json:"mfaSecret"`
MonitorStatus string `json:"monitorStatus"`
MonitorStoreDays int `json:"monitorStoreDays"`
MonitorStoreDays string `json:"monitorStoreDays"`
MessageType string `json:"messageType"`
EmailVars string `json:"emailVars"`

View File

@ -40,12 +40,10 @@ func (u *SettingService) GetSettingInfo() (*dto.SettingInfo, error) {
if err != nil {
return nil, err
}
_ = json.Unmarshal(arr, &info)
info.MonitorStoreDays, _ = strconv.Atoi(settingMap["MonitorStoreDays"])
info.ServerPort, _ = strconv.Atoi(settingMap["ServerPort"])
info.SessionTimeout, _ = strconv.Atoi(settingMap["SessionTimeout"])
info.ExpirationDays, _ = strconv.Atoi(settingMap["ExpirationDays"])
info.LocalTime = time.Now().Format("2006-01-02 15:04:05")
if err := json.Unmarshal(arr, &info); err != nil {
return nil, err
}
info.LocalTime = time.Now().String()
return &info, err
}

View File

@ -2,7 +2,6 @@ package ntp
import (
"encoding/binary"
"flag"
"fmt"
"net"
"runtime"
@ -31,29 +30,25 @@ type packet struct {
TxTimeFrac uint32
}
func Getremotetime() (*time.Time, error) {
var host string
flag.StringVar(&host, "e", "ntp1.aliyun.com:123", "NTP host")
flag.Parse()
conn, err := net.Dial("udp", host)
func Getremotetime() (time.Time, error) {
conn, err := net.Dial("udp", "ntp.aliyun.com:123")
if err != nil {
return nil, fmt.Errorf("failed to connect: %v", err)
return time.Time{}, fmt.Errorf("failed to connect: %v", err)
}
defer conn.Close()
if err := conn.SetDeadline(time.Now().Add(15 * time.Second)); err != nil {
return nil, fmt.Errorf("failed to set deadline: %v", err)
return time.Time{}, fmt.Errorf("failed to set deadline: %v", err)
}
req := &packet{Settings: 0x1B}
if err := binary.Write(conn, binary.BigEndian, req); err != nil {
return nil, fmt.Errorf("failed to set request: %v", err)
return time.Time{}, fmt.Errorf("failed to set request: %v", err)
}
rsp := &packet{}
if err := binary.Read(conn, binary.BigEndian, rsp); err != nil {
return nil, fmt.Errorf("failed to read server response: %v", err)
return time.Time{}, fmt.Errorf("failed to read server response: %v", err)
}
secs := float64(rsp.TxTimeSec) - ntpEpochOffset
@ -61,7 +56,7 @@ func Getremotetime() (*time.Time, error) {
showtime := time.Unix(int64(secs), nanos)
return &showtime, nil
return showtime, nil
}
func UpdateSystemDate(dateTime string) error {

View File

@ -12,10 +12,10 @@ const settingRouter = {
children: [
{
path: '/setting',
name: 'Setting',
component: () => import('@/views/setting/index.vue'),
name: 'Panel',
component: () => import('@/views/setting/panel/index.vue'),
hidden: true,
meta: {
requiresAuth: true,
key: 'Setting',
},
},
@ -46,15 +46,6 @@ const settingRouter = {
key: 'Setting',
},
},
{
path: '/setting/panel',
name: 'Panel',
component: () => import('@/views/setting/panel/index.vue'),
hidden: true,
meta: {
key: 'Setting',
},
},
{
path: '/setting/safe',
name: 'Safe',