79 lines
1.6 KiB
Go
79 lines
1.6 KiB
Go
package saft_hat
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
)
|
|
|
|
// 更新设备定位数据
|
|
type LocationReq struct {
|
|
g.Meta `path:"/device/location" method:"post" tags:"安全帽相关" summary:"刷新设备定位数据"`
|
|
Data string `json:"data" dc:"设备号"` // 设备数据
|
|
}
|
|
|
|
type LocationRes struct {
|
|
Code int `json:"code"` // 响应代码
|
|
Data bool `json:"data"` // 是否成功
|
|
Msg string `json:"msg"` // 响应消息
|
|
}
|
|
|
|
// 处理设备开关机时间的更新
|
|
func (h Hat) Location(ctx context.Context, req *LocationReq) (res *LocationRes, err error) {
|
|
res = new(LocationRes)
|
|
data := req.Data
|
|
timestamp := time.Now().Unix()
|
|
|
|
// 准备生成签名的参数
|
|
params := map[string]string{
|
|
"appid": appid,
|
|
"data": data,
|
|
"secret": secret,
|
|
"timestamp": strconv.FormatInt(timestamp, 10),
|
|
}
|
|
|
|
// 生成签名
|
|
sign := GenerateSignature(params)
|
|
|
|
// 构造请求体
|
|
payload := map[string]interface{}{
|
|
"appid": appid,
|
|
"data": data,
|
|
"secret": secret,
|
|
"timestamp": timestamp,
|
|
"sign": sign,
|
|
}
|
|
payloadBytes, err := json.Marshal(payload)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
// 发送POST请求
|
|
resp, err := http.Post("https://www.loctp.com/api/crm/v1/refresh/location", "application/json", bytes.NewBuffer(payloadBytes))
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// 检查响应
|
|
respBody, err := ioutil.ReadAll(resp.Body)
|
|
fmt.Println(string(respBody))
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
err = json.Unmarshal(respBody, res)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
|
|
return res, nil
|
|
}
|