kefu/wechathook/routers/wechat39581.go

52 lines
1.2 KiB
Go

package routers
import (
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"log"
"time"
"wechathook/models"
"wechathook/service"
)
func Wechat39581(c *gin.Context) {
// 声明一个map变量来存储JSON数据
var jsonData map[string]interface{}
// 解析JSON数据
if err := c.BindJSON(&jsonData); err != nil {
// 如果解析失败,返回错误信息
c.JSON(400, gin.H{"error": err.Error()})
return
}
// 将JSON数据转换为字符串
jsonBytes, _ := json.Marshal(jsonData)
jsonString := string(jsonBytes)
log.Println("收到数据:", jsonString)
c.JSON(200, gin.H{
"message": "pong",
})
}
// 发送微信消息
func SendWechat39581Msg(wxid, msg string) (string, error) {
if msg == "" {
return "", errors.New("回复内容为空!")
}
sleepTime, _ := service.GetRandomValue(models.FindSettingDefault("delayTime", "0"))
time.Sleep(time.Duration(sleepTime) * time.Second)
url := "http://127.0.0.1:19088/api/sendTextMsg"
data := map[string]interface{}{
"wxid": wxid,
"msg": msg,
}
respBytes, err := service.PostJSON(url, data)
respString := string(respBytes)
log.Println("发送信息:", respString)
if err != nil {
return "", err
}
return respString, nil
}