package utils import ( "encoding/json" "errors" "fmt" "gitee.ltd/lxh/logger/log" jsoniter "github.com/json-iterator/go" "strings" "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 { log.Debugf("发送通知到微信: %v", msg) req := client.HttpClient.R() req.SetHeader("Content-Type", "application/json") req.SetBody(map[string]string{ "wxid": w.toUserId, "msg": msg, }) 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() 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 }