修正时区
This commit is contained in:
parent
e376b160ce
commit
fe56ce587f
@ -2,10 +2,12 @@ package location
|
||||
|
||||
import (
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"matrix-middle-service/pkg/database"
|
||||
"matrix-middle-service/pkg/database/model"
|
||||
"matrix-middle-service/pkg/logger"
|
||||
response "matrix-middle-service/pkg/resp"
|
||||
"matrix-middle-service/pkg/resp/errorx"
|
||||
utils "matrix-middle-service/pkg/utils/json"
|
||||
"matrix-middle-service/pkg/utils/sonyflake"
|
||||
"matrix-middle-service/pkg/utils/time"
|
||||
)
|
||||
|
||||
@ -47,15 +49,20 @@ type Data struct {
|
||||
// @Router /record/location/post [post]
|
||||
func postLocation(api fiber.Router) {
|
||||
api.Post("/post", func(ctx *fiber.Ctx) error {
|
||||
data := &Data{}
|
||||
data := &model.LocationRecord{}
|
||||
err := ctx.BodyParser(data)
|
||||
if err != nil {
|
||||
logger.Log().Error(err)
|
||||
return ctx.JSON(errorx.ParseError(err))
|
||||
}
|
||||
|
||||
logger.Log().Debug(utils.Json(data))
|
||||
|
||||
data.Id = sonyflake.NextStrId()
|
||||
return database.Exec(func(db database.DataBase) error {
|
||||
err := db.RecordLocation(data)
|
||||
if err = errorx.ParseError(err); err != nil {
|
||||
return ctx.JSON(err)
|
||||
}
|
||||
return ctx.JSON(response.NewResponse(data))
|
||||
})
|
||||
})
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"matrix-middle-service/cmd/service"
|
||||
_ "matrix-middle-service/pkg/database/driver/mysql"
|
||||
_ "matrix-middle-service/pkg/logger"
|
||||
"time"
|
||||
)
|
||||
|
||||
// @title matrix-middle-service API
|
||||
@ -16,5 +17,6 @@ import (
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
// @BasePath /
|
||||
func main() {
|
||||
_, _ = time.LoadLocation("Asia/Shanghai")
|
||||
service.Main()
|
||||
}
|
||||
|
@ -19,6 +19,8 @@ type DataBase interface {
|
||||
GetAllBotTypes() ([]*model.BotType, error)
|
||||
AddBotType(bt *model.BotType) error
|
||||
DeleteBotTypeById(id string) error
|
||||
|
||||
RecordLocation(record *model.LocationRecord) error
|
||||
}
|
||||
|
||||
func Init(conf []interface{}) {
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"matrix-middle-service/pkg/logger"
|
||||
fib "matrix-middle-service/pkg/utils/fiber"
|
||||
"strings"
|
||||
"time"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
@ -40,6 +41,8 @@ func init() {
|
||||
if err != nil {
|
||||
logger.Log().Fatal(err)
|
||||
}
|
||||
dbEngine.TZLocation, _ = time.LoadLocation("Asia/Shanghai") // 本程序时区设置为上海
|
||||
dbEngine.DatabaseTZ, _ = time.LoadLocation("Asia/Shanghai") // 数据库时区设置为上海
|
||||
|
||||
dbInst = &db{
|
||||
db: dbEngine,
|
||||
@ -55,6 +58,11 @@ type db struct {
|
||||
config *conf.Config
|
||||
}
|
||||
|
||||
func (d *db) RecordLocation(record *model.LocationRecord) error {
|
||||
_, err := d.db.Insert(record)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *db) DeleteBotTypeById(id string) error {
|
||||
if strutil.IsBlank(id) {
|
||||
return errors.New(fmt.Sprintf("id 不能为空"))
|
||||
@ -126,7 +134,8 @@ func (d *db) Open() (err error) {
|
||||
&model.QQBot{},
|
||||
&model.WebHook{},
|
||||
&model.BotType{},
|
||||
&model.BotAccess{})
|
||||
&model.BotAccess{},
|
||||
&model.LocationRecord{})
|
||||
|
||||
if err != nil {
|
||||
logger.Log().Fatal(err)
|
||||
|
@ -0,0 +1,26 @@
|
||||
package model
|
||||
|
||||
import "matrix-middle-service/pkg/utils/time"
|
||||
|
||||
type LocationRecord struct {
|
||||
Id string `xorm:"NOT NULL VARCHAR(255) pk"`
|
||||
CallbackTime time.Time `xorm:"DATETIME" json:"callbackTime"`
|
||||
LocationTime time.Time `xorm:"DATETIME" json:"locationTime"`
|
||||
LocationType int `json:"locationType,omitempty"`
|
||||
Latitude float64 `json:"latitude,omitempty"`
|
||||
Longitude float64 `json:"longitude,omitempty"`
|
||||
Accuracy float64 `json:"accuracy,omitempty"`
|
||||
Altitude float64 `json:"altitude,omitempty"`
|
||||
Bearing float64 `json:"bearing,omitempty"`
|
||||
Speed float64 `json:"speed,omitempty"`
|
||||
Country string `json:"country,omitempty"`
|
||||
Province string `json:"province,omitempty"`
|
||||
City string `json:"city,omitempty"`
|
||||
District string `json:"district,omitempty"`
|
||||
Street string `json:"street,omitempty"`
|
||||
StreetNumber string `json:"streetNumber,omitempty"`
|
||||
CityCode string `json:"cityCode,omitempty"`
|
||||
AdCode string `json:"adCode,omitempty"`
|
||||
Address string `json:"address,omitempty"`
|
||||
Description string `json:"description,omitempty"`
|
||||
}
|
@ -25,7 +25,8 @@ func (t *Time) UnmarshalJSON(data []byte) error {
|
||||
}
|
||||
// Fractional seconds are handled implicitly by Parse.
|
||||
var err error
|
||||
rawT, err := time.Parse(`"`+timeFormat+`"`, string(data))
|
||||
loc, _ := time.LoadLocation("Asia/Shanghai")
|
||||
rawT, err := time.ParseInLocation(`"`+timeFormat+`"`, string(data), loc)
|
||||
*t = Time(rawT)
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user