Files
zmkgC/api/saft_hat/hat_voice.go
2025-07-07 20:11:59 +08:00

82 lines
1.8 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package saft_hat
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/frame/g"
"io/ioutil"
"net/http"
"strconv"
"time"
)
// 定义发送内置语音的请求结构
type SendVoiceReq struct {
g.Meta `path:"/device/voice" method:"post" tags:"安全帽相关" summary:"发送内置语音"`
Data string `json:"data" dc:"设备号"`
VoiceType int `json:"voice_type" dc:"内置语音种类具体参照温度1、2、3、4、5....."`
}
type SendVoiceRes struct {
Code int `json:"code"` // 响应代码
Data string `json:"data"` // 设备数据
Msg string `json:"msg"` // 响应消息
}
// 处理设备开关机时间的更新
func (h Hat) SendVoice(ctx context.Context, req *SendVoiceReq) (res *SendVoiceRes, err error) {
res = new(SendVoiceRes)
data := req.Data
timestamp := time.Now().Unix()
// 准备生成签名的参数
params := map[string]string{
"appid": appid,
"data": data,
"secret": secret,
"timestamp": strconv.FormatInt(timestamp, 10),
"voice_type": strconv.Itoa(req.VoiceType),
}
// 生成签名
sign := GenerateSignature(params)
// 构造请求体
payload := map[string]interface{}{
"appid": appid,
"data": data,
"secret": secret,
"sign": sign,
"timestamp": timestamp,
"voice_type": strconv.Itoa(req.VoiceType),
}
payloadBytes, err := json.Marshal(payload)
if err != nil {
return res, err
}
// 发送POST请求
resp, err := http.Post("https://www.loctp.com/api/crm/v1/sendVoice", "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
}