初始
This commit is contained in:
53
api/saft_hat/hat_tip.go
Normal file
53
api/saft_hat/hat_tip.go
Normal file
@ -0,0 +1,53 @@
|
||||
package saft_hat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/glog"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
)
|
||||
|
||||
// 脱帽提示数据上传结构体
|
||||
type HatTip struct {
|
||||
UTCDateTime int64 `json:"utcDateTime"` // 设备内部上传时间戳
|
||||
IMEI string `json:"IMEI"` // 设备IMEI编号
|
||||
}
|
||||
|
||||
type HatTipReq struct {
|
||||
g.Meta `path:"/device/tip" method:"post" tags:"安全帽相关" summary:"脱帽提醒(不需要前端调用)"`
|
||||
}
|
||||
|
||||
type HatTipRes struct {
|
||||
}
|
||||
|
||||
func (h Hat) HatTip(ctx context.Context, req *HatTipReq) (res *HatTipRes, err error) {
|
||||
res = new(HatTipRes)
|
||||
r := g.RequestFromCtx(ctx)
|
||||
body, err := ioutil.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Printf("Failed to read request body: %v", err)
|
||||
return nil, err
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
timestamp := r.GetHeader("timestamp")
|
||||
signature := r.GetHeader("signature")
|
||||
|
||||
if !VerifySignature(string(body)+timestamp, signature, secret) {
|
||||
glog.Errorf(ctx, "Signature verification failed")
|
||||
return nil, errors.New("signature verification failed")
|
||||
}
|
||||
|
||||
var hatTip HatTip
|
||||
if err := gjson.DecodeTo(body, &hatTip); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("脱帽提示数据:", hatTip)
|
||||
|
||||
return res, nil
|
||||
}
|
Reference in New Issue
Block a user