2024-12-05 09:54:55 +08:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"gitee.ltd/lxh/logger/log"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-12-05 10:43:40 +08:00
|
|
|
"strings"
|
2024-12-05 09:54:55 +08:00
|
|
|
"wireguard-ui/global/client"
|
|
|
|
"wireguard-ui/model"
|
|
|
|
)
|
|
|
|
|
|
|
|
// wxid_472vas3av5ug22 /api/sendTextMsg
|
|
|
|
type wechatNotify struct {
|
|
|
|
Addr string
|
|
|
|
Path string
|
|
|
|
Method string
|
|
|
|
toUserId string
|
|
|
|
}
|
|
|
|
|
|
|
|
func WechatNotify(setting *model.Setting) wechatNotify {
|
|
|
|
var sm = make(map[string]string)
|
|
|
|
_ = json.Unmarshal([]byte(setting.Data), &sm)
|
|
|
|
|
|
|
|
return wechatNotify{
|
|
|
|
Addr: sm["addr"],
|
|
|
|
Path: sm["path"],
|
|
|
|
Method: sm["method"],
|
|
|
|
toUserId: sm["toUserId"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendTextMessage
|
|
|
|
// @description: 发送文字通知
|
|
|
|
// @receiver website
|
|
|
|
// @param msg
|
|
|
|
// @return error
|
|
|
|
func (w wechatNotify) SendTextMessage(msg string) error {
|
2024-12-05 10:43:40 +08:00
|
|
|
log.Debugf("发送通知到微信: %v", msg)
|
2024-12-05 09:54:55 +08:00
|
|
|
req := client.HttpClient.R()
|
|
|
|
|
|
|
|
req.SetHeader("Content-Type", "application/json")
|
|
|
|
req.SetBody(map[string]string{
|
|
|
|
"wxid": w.toUserId,
|
|
|
|
"msg": msg,
|
|
|
|
})
|
2024-12-05 10:43:40 +08:00
|
|
|
|
|
|
|
req.URL = fmt.Sprintf("%s:%s", w.Addr, w.Path)
|
|
|
|
switch strings.ToUpper(w.Method) {
|
|
|
|
case "POST":
|
|
|
|
req.Method = "POST"
|
|
|
|
}
|
|
|
|
result, err := req.SetDebug(true).Send()
|
2024-12-05 09:54:55 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if jsoniter.Get(result.Body(), "code").ToInt() != 1 {
|
|
|
|
log.Errorf("发送通知到微信失败: %v", jsoniter.Get(result.Body(), "msg").ToString())
|
|
|
|
return errors.New("发送通知到微信失败")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|