初始
This commit is contained in:
54
api/saft_hat/hat_danger.go
Normal file
54
api/saft_hat/hat_danger.go
Normal file
@ -0,0 +1,54 @@
|
||||
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 Danger struct {
|
||||
BT_MAC string `json:"BT_MAC"` // 危险源MAC
|
||||
IMEI string `json:"IMEI"` // 设备IMEI编号
|
||||
}
|
||||
|
||||
// 危险源数据上传请求
|
||||
type DangerReq struct {
|
||||
g.Meta `path:"/device/danger" method:"post" tags:"安全帽相关" summary:"接收危险源数据(不需要前端调用)"`
|
||||
}
|
||||
|
||||
type DangerRes struct {
|
||||
}
|
||||
|
||||
func (h Hat) Danger(ctx context.Context, req *DangerReq) (res *DangerRes, err error) {
|
||||
res = new(DangerRes)
|
||||
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 danger Danger
|
||||
if err := gjson.DecodeTo(body, &danger); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fmt.Println("危险源数据", danger)
|
||||
|
||||
return res, nil
|
||||
}
|
Reference in New Issue
Block a user