123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- package services
- import (
- "bytes"
- "encoding/json"
- "fmt"
- "eta/eta_menu_sync/global"
- "eta/eta_menu_sync/utils"
- "io/ioutil"
- "net/http"
- "strings"
- )
- // const BaseUrl = `http://127.0.0.1:7777/DaenWxHook/httpapi/?`
- const BaseUrl = `http://192.168.77.14:7777/DaenWxHook/httpapi/?`
- const BasePath = `/DaenWxHook/httpapi/?`
- type WxPushResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Result interface{} `json:"result"`
- Wxid string `json:"wxid"`
- Port int `json:"port"`
- Pid int `json:"pid"`
- Flag string `json:"flag"`
- Timestamp string `json:"timestamp"`
- }
- type SendReq struct {
- Type string `json:"type"`
- Data interface{} `json:"data"`
- }
- // "wxid": "wxid_6895448954512", //鹏
- //"wxid": "wxid_vgnigr1qaizm22", //培鼎
- // SendH5 发送链接
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- // @param toWxId string 需要发送的微信用户/群的id
- // @param title string h5链接的标题
- // @param picPath string 图片路径,可以是网络图片,也可以是服务器上的本地路径
- // @param content string h5链接上面的分享文案(简介)
- // @param jumpUrl string h5跳转链接
- func SendH5(serverUrl, wxHelperId, toWxId, title, picPath, content, jumpUrl string) (resp WxPushResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- // "app": "wx64f9cf5b17af074d",
- requestDataMap["wxid"] = toWxId
- requestDataMap["path"] = picPath
- requestDataMap["title"] = title
- requestDataMap["content"] = content
- requestDataMap["jumpUrl"] = jumpUrl
- //requestDataMap["app"] = jumpUrl
- requestData := SendReq{
- Type: "Q0012",
- Data: requestDataMap,
- }
- postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- body, err := HttpPost(urlStr, string(postData), true, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- //fmt.Println(resultStr)
- err = json.Unmarshal(body, &resp)
- return
- }
- // SendPic 发送图片
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- // @param toWxId string 需要发送的微信用户/群的id
- // @param picPath string 图片路径,可以是网络图片,也可以是服务器上的本地路径
- func SendPic(serverUrl, wxHelperId, toWxId, picPath string) (resp WxPushResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- // "app": "wx64f9cf5b17af074d",
- requestDataMap["wxid"] = toWxId
- requestDataMap["path"] = picPath
- requestData := SendReq{
- Type: "Q0010",
- Data: requestDataMap,
- }
- postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- body, err := HttpPost(urlStr, string(postData), true, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- //fmt.Println(resultStr)
- err = json.Unmarshal(body, &resp)
- return
- }
- // SendText 发送文字
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- // @param toWxId string 需要发送的微信用户/群的id
- // @param content string 文案内容
- func SendText(serverUrl, wxHelperId, toWxId, content string) (resp WxPushResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- requestDataMap["wxid"] = toWxId
- requestDataMap["msg"] = content
- requestData := SendReq{
- Type: "Q0001",
- Data: requestDataMap,
- }
- postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- body, err := HttpPost(urlStr, string(postData), true, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- //fmt.Println(resultStr)
- err = json.Unmarshal(body, &resp)
- return
- }
- // SendXml 发送xml(目前用来发送小程序推送)
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- // @param toWxId string 需要发送的微信用户/群的id
- // @param xml string xml内容
- func SendXml(serverUrl, wxHelperId, toWxId, xml string) (resp WxPushResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- requestDataMap["wxid"] = toWxId
- requestDataMap["xml"] = xml
- requestData := SendReq{
- Type: "Q0015",
- Data: requestDataMap,
- }
- //postData, err := json.Marshal(requestData)
- //if err != nil {
- // return
- //}
- bf := bytes.NewBuffer([]byte{})
- jsonEncoder := json.NewEncoder(bf)
- jsonEncoder.SetEscapeHTML(false)
- err = jsonEncoder.Encode(requestData)
- //jsonStr := bf.String()
- //postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- //postStr := string(postData)
- postStr := bf.String()
- body, err := HttpPost(urlStr, postStr, true, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- //fmt.Println(resultStr)
- err = json.Unmarshal(body, &resp)
- return
- }
- // Listen 微信状态检测
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- func Listen(serverUrl, wxHelperId string) (resp WxPushResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- requestData := SendReq{
- Type: "Q0000",
- Data: requestDataMap,
- }
- postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- body, err := HttpPost(urlStr, string(postData), false, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- // 调用函数处理特殊隐藏字符
- jsonStr := TrimHiddenCharacter(resultStr)
- //fmt.Println(resultStr)
- err = json.Unmarshal([]byte(jsonStr), &resp)
- return
- }
- // WxGroupResp 微信群列表返回
- type WxGroupResp struct {
- Code int `json:"code"`
- Msg string `json:"msg"`
- Result []GroupResult `json:"result"`
- Wxid string `json:"wxid"`
- Port int `json:"port"`
- Pid int `json:"pid"`
- Flag string `json:"flag"`
- Timestamp string `json:"timestamp"`
- }
- // GroupResult 群信息
- type GroupResult struct {
- AvatarMaxUrl string `json:"avatarMaxUrl"`
- AvatarMinUrl string `json:"avatarMinUrl"`
- City string `json:"city"`
- Country string `json:"country"`
- EnBrief string `json:"enBrief"`
- EnWhole string `json:"enWhole"`
- MemberNum int `json:"memberNum"`
- MomentsBackgroudImgUrl string `json:"momentsBackgroudImgUrl"`
- Nick string `json:"nick"`
- NickBrief string `json:"nickBrief"`
- NickWhole string `json:"nickWhole"`
- Province string `json:"province"`
- Remark string `json:"remark"`
- RemarkBrief string `json:"remarkBrief"`
- RemarkWhole string `json:"remarkWhole"`
- Sex string `json:"sex"`
- Sign string `json:"sign"`
- V3 string `json:"v3"`
- WxNum string `json:"wxNum"`
- Wxid string `json:"wxid"`
- }
- // GetAllGroup 获取所有的微信群
- // @param serverUrl string 小助手所在的服务器地址
- // @param wxHelperId string 小助手的微信id
- func GetAllGroup(serverUrl, wxHelperId string) (resp WxGroupResp, resultStr string, err error) {
- urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
- requestDataMap := make(map[string]interface{})
- requestDataMap["type"] = 2 //1:缓存获取,2:实时获取
- requestData := SendReq{
- Type: "Q0006",
- Data: requestDataMap,
- }
- postData, err := json.Marshal(requestData)
- if err != nil {
- return
- }
- body, err := HttpPost(urlStr, string(postData), true, "application/json")
- if err != nil {
- return
- }
- resultStr = string(body)
- // 调用函数处理特殊隐藏字符
- jsonStr := TrimHiddenCharacter(resultStr)
- //fmt.Println(resultStr)
- err = json.Unmarshal([]byte(jsonStr), &resp)
- return
- }
- func HttpPost(url, postData string, isLog bool, params ...string) ([]byte, error) {
- body := ioutil.NopCloser(strings.NewReader(postData))
- client := &http.Client{}
- req, err := http.NewRequest("POST", url, body)
- if err != nil {
- return nil, err
- }
- contentType := "application/x-www-form-urlencoded;charset=utf-8"
- if len(params) > 0 && params[0] != "" {
- contentType = params[0]
- }
- req.Header.Set("Content-Type", contentType)
- req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
- resp, err := client.Do(req)
- if err != nil {
- global.LOG.Info(fmt.Sprint("发送消息失败:request params:", postData, ";err:", err.Error()))
- return []byte{}, err
- }
- defer resp.Body.Close()
- b, err := ioutil.ReadAll(resp.Body)
- if isLog {
- global.LOG.Info(fmt.Sprint("request params:", postData, ";response:", string(b)))
- }
- //fmt.Println("HttpPost:" + string(b))
- return b, err
- }
- // TrimHiddenCharacter 处理特殊隐藏字符
- func TrimHiddenCharacter(originStr string) string {
- srcRunes := []rune(originStr)
- dstRunes := make([]rune, 0, len(srcRunes))
- for _, c := range srcRunes {
- if c >= 0 && c <= 31 {
- continue
- }
- if c == 127 {
- continue
- }
- dstRunes = append(dstRunes, c)
- }
- return string(dstRunes)
- }
|