send_to_wx.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. package services
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "eta/eta_menu_sync/global"
  7. "eta/eta_menu_sync/utils"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. )
  12. // const BaseUrl = `http://127.0.0.1:7777/DaenWxHook/httpapi/?`
  13. const BaseUrl = `http://192.168.77.14:7777/DaenWxHook/httpapi/?`
  14. const BasePath = `/DaenWxHook/httpapi/?`
  15. type WxPushResp struct {
  16. Code int `json:"code"`
  17. Msg string `json:"msg"`
  18. Result interface{} `json:"result"`
  19. Wxid string `json:"wxid"`
  20. Port int `json:"port"`
  21. Pid int `json:"pid"`
  22. Flag string `json:"flag"`
  23. Timestamp string `json:"timestamp"`
  24. }
  25. type SendReq struct {
  26. Type string `json:"type"`
  27. Data interface{} `json:"data"`
  28. }
  29. // "wxid": "wxid_6895448954512", //鹏
  30. //"wxid": "wxid_vgnigr1qaizm22", //培鼎
  31. // SendH5 发送链接
  32. // @param serverUrl string 小助手所在的服务器地址
  33. // @param wxHelperId string 小助手的微信id
  34. // @param toWxId string 需要发送的微信用户/群的id
  35. // @param title string h5链接的标题
  36. // @param picPath string 图片路径,可以是网络图片,也可以是服务器上的本地路径
  37. // @param content string h5链接上面的分享文案(简介)
  38. // @param jumpUrl string h5跳转链接
  39. func SendH5(serverUrl, wxHelperId, toWxId, title, picPath, content, jumpUrl string) (resp WxPushResp, resultStr string, err error) {
  40. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  41. requestDataMap := make(map[string]interface{})
  42. // "app": "wx64f9cf5b17af074d",
  43. requestDataMap["wxid"] = toWxId
  44. requestDataMap["path"] = picPath
  45. requestDataMap["title"] = title
  46. requestDataMap["content"] = content
  47. requestDataMap["jumpUrl"] = jumpUrl
  48. //requestDataMap["app"] = jumpUrl
  49. requestData := SendReq{
  50. Type: "Q0012",
  51. Data: requestDataMap,
  52. }
  53. postData, err := json.Marshal(requestData)
  54. if err != nil {
  55. return
  56. }
  57. body, err := HttpPost(urlStr, string(postData), true, "application/json")
  58. if err != nil {
  59. return
  60. }
  61. resultStr = string(body)
  62. //fmt.Println(resultStr)
  63. err = json.Unmarshal(body, &resp)
  64. return
  65. }
  66. // SendPic 发送图片
  67. // @param serverUrl string 小助手所在的服务器地址
  68. // @param wxHelperId string 小助手的微信id
  69. // @param toWxId string 需要发送的微信用户/群的id
  70. // @param picPath string 图片路径,可以是网络图片,也可以是服务器上的本地路径
  71. func SendPic(serverUrl, wxHelperId, toWxId, picPath string) (resp WxPushResp, resultStr string, err error) {
  72. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  73. requestDataMap := make(map[string]interface{})
  74. // "app": "wx64f9cf5b17af074d",
  75. requestDataMap["wxid"] = toWxId
  76. requestDataMap["path"] = picPath
  77. requestData := SendReq{
  78. Type: "Q0010",
  79. Data: requestDataMap,
  80. }
  81. postData, err := json.Marshal(requestData)
  82. if err != nil {
  83. return
  84. }
  85. body, err := HttpPost(urlStr, string(postData), true, "application/json")
  86. if err != nil {
  87. return
  88. }
  89. resultStr = string(body)
  90. //fmt.Println(resultStr)
  91. err = json.Unmarshal(body, &resp)
  92. return
  93. }
  94. // SendText 发送文字
  95. // @param serverUrl string 小助手所在的服务器地址
  96. // @param wxHelperId string 小助手的微信id
  97. // @param toWxId string 需要发送的微信用户/群的id
  98. // @param content string 文案内容
  99. func SendText(serverUrl, wxHelperId, toWxId, content string) (resp WxPushResp, resultStr string, err error) {
  100. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  101. requestDataMap := make(map[string]interface{})
  102. requestDataMap["wxid"] = toWxId
  103. requestDataMap["msg"] = content
  104. requestData := SendReq{
  105. Type: "Q0001",
  106. Data: requestDataMap,
  107. }
  108. postData, err := json.Marshal(requestData)
  109. if err != nil {
  110. return
  111. }
  112. body, err := HttpPost(urlStr, string(postData), true, "application/json")
  113. if err != nil {
  114. return
  115. }
  116. resultStr = string(body)
  117. //fmt.Println(resultStr)
  118. err = json.Unmarshal(body, &resp)
  119. return
  120. }
  121. // SendXml 发送xml(目前用来发送小程序推送)
  122. // @param serverUrl string 小助手所在的服务器地址
  123. // @param wxHelperId string 小助手的微信id
  124. // @param toWxId string 需要发送的微信用户/群的id
  125. // @param xml string xml内容
  126. func SendXml(serverUrl, wxHelperId, toWxId, xml string) (resp WxPushResp, resultStr string, err error) {
  127. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  128. requestDataMap := make(map[string]interface{})
  129. requestDataMap["wxid"] = toWxId
  130. requestDataMap["xml"] = xml
  131. requestData := SendReq{
  132. Type: "Q0015",
  133. Data: requestDataMap,
  134. }
  135. //postData, err := json.Marshal(requestData)
  136. //if err != nil {
  137. // return
  138. //}
  139. bf := bytes.NewBuffer([]byte{})
  140. jsonEncoder := json.NewEncoder(bf)
  141. jsonEncoder.SetEscapeHTML(false)
  142. err = jsonEncoder.Encode(requestData)
  143. //jsonStr := bf.String()
  144. //postData, err := json.Marshal(requestData)
  145. if err != nil {
  146. return
  147. }
  148. //postStr := string(postData)
  149. postStr := bf.String()
  150. body, err := HttpPost(urlStr, postStr, true, "application/json")
  151. if err != nil {
  152. return
  153. }
  154. resultStr = string(body)
  155. //fmt.Println(resultStr)
  156. err = json.Unmarshal(body, &resp)
  157. return
  158. }
  159. // Listen 微信状态检测
  160. // @param serverUrl string 小助手所在的服务器地址
  161. // @param wxHelperId string 小助手的微信id
  162. func Listen(serverUrl, wxHelperId string) (resp WxPushResp, resultStr string, err error) {
  163. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  164. requestDataMap := make(map[string]interface{})
  165. requestData := SendReq{
  166. Type: "Q0000",
  167. Data: requestDataMap,
  168. }
  169. postData, err := json.Marshal(requestData)
  170. if err != nil {
  171. return
  172. }
  173. body, err := HttpPost(urlStr, string(postData), false, "application/json")
  174. if err != nil {
  175. return
  176. }
  177. resultStr = string(body)
  178. // 调用函数处理特殊隐藏字符
  179. jsonStr := TrimHiddenCharacter(resultStr)
  180. //fmt.Println(resultStr)
  181. err = json.Unmarshal([]byte(jsonStr), &resp)
  182. return
  183. }
  184. // WxGroupResp 微信群列表返回
  185. type WxGroupResp struct {
  186. Code int `json:"code"`
  187. Msg string `json:"msg"`
  188. Result []GroupResult `json:"result"`
  189. Wxid string `json:"wxid"`
  190. Port int `json:"port"`
  191. Pid int `json:"pid"`
  192. Flag string `json:"flag"`
  193. Timestamp string `json:"timestamp"`
  194. }
  195. // GroupResult 群信息
  196. type GroupResult struct {
  197. AvatarMaxUrl string `json:"avatarMaxUrl"`
  198. AvatarMinUrl string `json:"avatarMinUrl"`
  199. City string `json:"city"`
  200. Country string `json:"country"`
  201. EnBrief string `json:"enBrief"`
  202. EnWhole string `json:"enWhole"`
  203. MemberNum int `json:"memberNum"`
  204. MomentsBackgroudImgUrl string `json:"momentsBackgroudImgUrl"`
  205. Nick string `json:"nick"`
  206. NickBrief string `json:"nickBrief"`
  207. NickWhole string `json:"nickWhole"`
  208. Province string `json:"province"`
  209. Remark string `json:"remark"`
  210. RemarkBrief string `json:"remarkBrief"`
  211. RemarkWhole string `json:"remarkWhole"`
  212. Sex string `json:"sex"`
  213. Sign string `json:"sign"`
  214. V3 string `json:"v3"`
  215. WxNum string `json:"wxNum"`
  216. Wxid string `json:"wxid"`
  217. }
  218. // GetAllGroup 获取所有的微信群
  219. // @param serverUrl string 小助手所在的服务器地址
  220. // @param wxHelperId string 小助手的微信id
  221. func GetAllGroup(serverUrl, wxHelperId string) (resp WxGroupResp, resultStr string, err error) {
  222. urlStr := "http://" + serverUrl + BasePath + "wxid=" + wxHelperId
  223. requestDataMap := make(map[string]interface{})
  224. requestDataMap["type"] = 2 //1:缓存获取,2:实时获取
  225. requestData := SendReq{
  226. Type: "Q0006",
  227. Data: requestDataMap,
  228. }
  229. postData, err := json.Marshal(requestData)
  230. if err != nil {
  231. return
  232. }
  233. body, err := HttpPost(urlStr, string(postData), true, "application/json")
  234. if err != nil {
  235. return
  236. }
  237. resultStr = string(body)
  238. // 调用函数处理特殊隐藏字符
  239. jsonStr := TrimHiddenCharacter(resultStr)
  240. //fmt.Println(resultStr)
  241. err = json.Unmarshal([]byte(jsonStr), &resp)
  242. return
  243. }
  244. func HttpPost(url, postData string, isLog bool, params ...string) ([]byte, error) {
  245. body := ioutil.NopCloser(strings.NewReader(postData))
  246. client := &http.Client{}
  247. req, err := http.NewRequest("POST", url, body)
  248. if err != nil {
  249. return nil, err
  250. }
  251. contentType := "application/x-www-form-urlencoded;charset=utf-8"
  252. if len(params) > 0 && params[0] != "" {
  253. contentType = params[0]
  254. }
  255. req.Header.Set("Content-Type", contentType)
  256. req.Header.Set("authorization", utils.MD5(utils.APP_EDB_LIB_NAME_EN+utils.EDB_LIB_Md5_KEY))
  257. resp, err := client.Do(req)
  258. if err != nil {
  259. global.LOG.Info(fmt.Sprint("发送消息失败:request params:", postData, ";err:", err.Error()))
  260. return []byte{}, err
  261. }
  262. defer resp.Body.Close()
  263. b, err := ioutil.ReadAll(resp.Body)
  264. if isLog {
  265. global.LOG.Info(fmt.Sprint("request params:", postData, ";response:", string(b)))
  266. }
  267. //fmt.Println("HttpPost:" + string(b))
  268. return b, err
  269. }
  270. // TrimHiddenCharacter 处理特殊隐藏字符
  271. func TrimHiddenCharacter(originStr string) string {
  272. srcRunes := []rune(originStr)
  273. dstRunes := make([]rune, 0, len(srcRunes))
  274. for _, c := range srcRunes {
  275. if c >= 0 && c <= 31 {
  276. continue
  277. }
  278. if c == 127 {
  279. continue
  280. }
  281. dstRunes = append(dstRunes, c)
  282. }
  283. return string(dstRunes)
  284. }