package wx_helper_sdk import ( "encoding/json" "fmt" "github.com/go-resty/resty/v2" "log" "strings" ) type WxHelper struct { Host string Port uint tmpResult CommonResponse client *resty.Client } func NewWxHelper(host string, port uint) WxHelper { hc := resty.New() return WxHelper{ host, port, CommonResponse{}, hc, } } // GetAddr // @description: 获取请求地址 // @receiver w // @param uri // @return string func (w *WxHelper) GetAddr(uri string) string { if !strings.HasPrefix(w.Host, "http") && !strings.HasPrefix(w.Host, "https://") { w.Host = fmt.Sprintf("http://%s", w.Host) } return fmt.Sprintf("%s:%d%s", w.Host, w.Port, uri) } // SetHook // @description: 设置回调 // @receiver w // @param req // @return err func (w *WxHelper) SetHook(param *SetHook) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/hookSyncMsg")) if err != nil { return } if w.tmpResult.Code != 0 { return fmt.Errorf("设置回调失败: %v", w.tmpResult.Msg) } return } // UnHook // @description: 取消回调设置 // @receiver w // @return err func (w *WxHelper) UnHook() (err error) { req := w.client.R() _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/unhookSyncMsg")) if err != nil { return } if w.tmpResult.Code != 0 { return fmt.Errorf("取消回调设置失败: %v", w.tmpResult.Msg) } return } // CheckUserIsLogin // @description: 校验用户是否已经登陆 // @receiver *WxHelper // @return bool func (w *WxHelper) CheckUserIsLogin() bool { req := w.client.R() _, err := req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/checkLogin")) if err != nil { log.Fatalf("获取用户登陆状态失败: %v", err.Error()) return false } if w.tmpResult.Code != 1 { return false } return true } // GetCurrentLoginUserInfo // @description: 获取当前登陆用户信息 // @receiver w // @return response // @return err func (w *WxHelper) GetCurrentLoginUserInfo() (response UserInfo, err error) { req := w.client.R() _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/userInfo")) if err != nil { log.Fatalf("获取用户登陆状态失败: %v", err.Error()) return } if w.tmpResult.Code != 1 { return } data, _ := json.Marshal(w.tmpResult.Data) _ = json.Unmarshal(data, &response) return } // GetContactList // @description: 获取好友列表 // @receiver w // @param isShowTool 是否展示一些工具聊天,什么文件助手呀,订阅号等等没用的东西 // @return friends // @return err func (w *WxHelper) GetContactList(isShowTool bool) (friends []Friend, err error) { req := w.client.R() _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/getContactList")) if err != nil { log.Fatalf("获取好友列表失败: %v", err.Error()) return } if w.tmpResult.Code != 1 { return } data, _ := json.Marshal(w.tmpResult.Data) _ = json.Unmarshal(data, &friends) // 如果不需要展示那些工具号,直接剔除 if !isShowTool { var onlyFriends []Friend for _, v := range friends { // 固定开头 if strings.HasPrefix(v.Wxid, "wxid") { onlyFriends = append(onlyFriends, v) } } friends = onlyFriends } return } // SendAtText // @description: 群内圈人文本消息发送 // @receiver w // @param param // @return err func (w *WxHelper) SendAtText(param *SendAtText) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendAtText")) if err != nil { return fmt.Errorf("发送群内圈人消息失败: %v", err.Error()) } if w.tmpResult.Code <= 0 { return fmt.Errorf("发送群内圈人消息失败: %v", w.tmpResult.Msg) } return } // SendText // @description: 发送文本信息(私聊) // @receiver w // @param param // @return err func (w *WxHelper) SendText(param *SendText) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendAtText")) if err != nil { return fmt.Errorf("发送私聊文本信息失败: %v", err.Error()) } if w.tmpResult.Code == 0 { return fmt.Errorf("发送私聊文本信息失败: %v", w.tmpResult.Msg) } return } // SendFileMsg // @description: 发送文件 // @receiver w // @param param // @return err func (w *WxHelper) SendFileMsg(param SendFileMsg) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendFileMsg")) if err != nil { return fmt.Errorf("发送文件失败: %v", err.Error()) } if w.tmpResult.Code == 0 { return fmt.Errorf("发送文件失败: %v", w.tmpResult.Msg) } return } // SendImageMsg // @description: 发送图片 // @receiver w // @param param // @return err func (w *WxHelper) SendImageMsg(param SendImageMsg) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/sendImagesMsg")) if err != nil { return fmt.Errorf("发送图片失败: %v", err.Error()) } if w.tmpResult.Code <= 0 { return fmt.Errorf("发送图片失败: %v", w.tmpResult.Msg) } return } // ForwardMsg // @description: 转发消息 // @receiver w // @param param // @return err func (w *WxHelper) ForwardMsg(param ForwardMsg) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/forwardMsg")) if err != nil { return fmt.Errorf("转发消息失败: %v", err.Error()) } if w.tmpResult.Code != 1 { return fmt.Errorf("转发消息失败: %v", w.tmpResult.Msg) } return } // DecodeImage // @description: 解码图片 // @receiver w // @param param // @return err func (w *WxHelper) DecodeImage(param DecodeImage) (err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/decodeImage")) if err != nil { return fmt.Errorf("解码图片失败: %v", err.Error()) } if w.tmpResult.Code <= 0 { return fmt.Errorf("解码图片失败: %v", w.tmpResult.Msg) } return } // GetChatRoomDetail // @description: 获取群聊详情 // @receiver w // @param param // @return chatRoot // @return err func (w *WxHelper) GetChatRoomDetail(param ChatRoomId) (chatRoot *ChatRoomDetail, err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/getChatRoomDetailInfo")) if err != nil { return nil, fmt.Errorf("获取群聊信息失败: %v", err.Error()) } if w.tmpResult.Code != 1 { return nil, fmt.Errorf("获取群聊信息失败: %v", w.tmpResult.Msg) } data, _ := json.Marshal(w.tmpResult.Data) _ = json.Unmarshal(data, &chatRoot) return } // GetChatRoomMember // @description: 获取群聊人员 // @receiver w // @param param // @return members // @return err func (w *WxHelper) GetChatRoomMember(param ChatRoomId) (members *ChatRoomMembers, err error) { req := w.client.R() req.Header.Set("Content-Type", "application/json") req.SetBody(param) _, err = req.SetResult(&w.tmpResult).Post(w.GetAddr("/api/getMemberFromChatRoom")) if err != nil { return nil, fmt.Errorf("获取群聊人员信息失败: %v", err.Error()) } if w.tmpResult.Code != 1 { return nil, fmt.Errorf("获取群聊人员信息失败: %v", w.tmpResult.Msg) } data, _ := json.Marshal(w.tmpResult.Data) _ = json.Unmarshal(data, &members) return }