123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- package index
- import (
- "encoding/json"
- "fmt"
- "github.com/gin-gonic/gin"
- "github.com/gin-gonic/gin/binding"
- "github.com/sbabiv/xml2map"
- "eta/eta_menu_sync/controller/resp"
- "eta/eta_menu_sync/global"
- "eta/eta_menu_sync/models"
- "eta/eta_menu_sync/services"
- "eta/eta_menu_sync/services/alarm_msg"
- "eta/eta_menu_sync/utils"
- "strconv"
- "strings"
- "time"
- )
- type IndexController struct {
- }
- func (s *IndexController) ServerCheck(c *gin.Context) {
- fmt.Println(c.Request.Body)
- resp.OkData("检测成功", 1, c)
- return
- }
- func (s *IndexController) Listen(c *gin.Context) {
- //strByte, err := c.GetRawData()
- //if err != nil {
- // fmt.Println("err:", err)
- //}
- //fmt.Println(string(strByte))
- //return
- var req EventReq
- if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
- fmt.Println("bind err:", err)
- resp.Fail("参数有误", c)
- return
- }
- jsonMap := make(map[string]interface{})
- if err := c.ShouldBindBodyWith(&jsonMap, binding.JSON); err == nil {
- //jsonStr, _ := json.Marshal(jsonMap)
- //global.LOG.Info("请求入参:", string(jsonStr))
- }
- //global.CONFIG.Serve.ListenWxId
- var err error
- defer func() {
- if err != nil {
- go alarm_msg.SendAlarmMsg(utils.APPNAME+"监听微信消息失败:"+time.Now().Format("2006-01-02 15:04:05")+";Err:"+err.Error(), 3, ``)
- }
- }()
- if req.Data.Type == "D0003" {
- fromTypeInter, ok := req.Data.Data["fromType"]
- if !ok {
- resp.Fail("错误的来源", c)
- return
- }
- // 消息来源,1:私聊,2:群聊,3:公众号
- fromType := fromTypeInter.(float64)
- if fromType != 1 && fromType != 2 {
- resp.Ok("不做处理", c)
- return
- }
- msgTypeInter, ok := req.Data.Data["msgType"]
- if !ok {
- resp.Fail("错误的消息", c)
- return
- }
- var fromWxid, finalFromWechatId, jumpPath, msg string
- {
- fromWxidInter, ok := req.Data.Data["fromWxid"]
- if !ok {
- resp.OkData("找不到来源id", 1, c)
- return
- }
- fromWxid = fromWxidInter.(string) //私聊时为用户id,群聊是为群id,公众号则是公众号id
- }
- //群内发言人的微信id
- {
- if finalFromWxidInter, ok := req.Data.Data["finalFromWxid"]; ok {
- finalFromWechatId = finalFromWxidInter.(string) //群内发言人的微信id
- }
- }
- if fromWxid != global.CONFIG.Serve.ListenWxId {
- resp.OkData("不监听该来源信息", 1, c)
- return
- }
- msgType := msgTypeInter.(float64)
- switch msgType {
- case 49: //分享链接或者附件(通过xml发送)
- //return
- // 来源用户
- msg = req.Data.Data["msg"].(string)
- wxappInfo := getWxappInfo(msg)
- if wxappInfo.Appid != `` && wxappInfo.PagePath != `` {
- jumpPath = wxappInfo.Appid + `/` + wxappInfo.PagePath
- }
- tmpMsg, tmpErr := strconv.Unquote(strings.Replace(strconv.Quote(msg), `\\u`, `\u`, -1))
- if tmpErr != nil {
- err = tmpErr
- fmt.Println("err:", tmpErr)
- return
- }
- msg = tmpMsg
- //msg = strings.Replace(msg, fromWxid, "wxid_pslc6fxtftcb22", -1)
- //sendErr = sendXml(msg)
- //fmt.Println(msg)
- }
- jsonStr, _ := json.Marshal(jsonMap)
- item := &models.WechatListenMsgRecord{
- //WechatMsgId: 0,
- Content: string(jsonStr),
- FromWechatId: fromWxid,
- FinalFromWechatId: finalFromWechatId,
- WechatId: req.Wxid,
- MsgType: int(msgType),
- Msg: msg,
- JumpPath: jumpPath,
- CreateTime: time.Now(),
- }
- err = item.Create()
- if err != nil {
- global.LOG.Info("添加监听消息失败")
- }
- }
- resp.OkData("回调成功", 1, c)
- return
- }
- func (s *IndexController) ListenBak(c *gin.Context) {
- //strByte, err := c.GetRawData()
- //if err != nil {
- // fmt.Println("err:", err)
- //}
- //fmt.Println(string(strByte))
- //return
- var req EventReq
- if err := c.ShouldBindBodyWith(&req, binding.JSON); err != nil {
- fmt.Println("bind err:", err)
- resp.Fail("参数有误", c)
- return
- }
- jsonMap := make(map[string]interface{})
- if err := c.ShouldBindBodyWith(&jsonMap, binding.JSON); err == nil {
- jsonStr, _ := json.Marshal(jsonMap)
- global.LOG.Info("请求入参:", string(jsonStr))
- }
- if req.Data.Type == "D0003" {
- msgTypeInter, ok := req.Data.Data["msgType"]
- if !ok {
- resp.Fail("错误的消息", c)
- return
- }
- msgType := msgTypeInter.(float64)
- var sendErr error
- switch msgType {
- case 1: //文本
- sendErr = sendText(req.Data.Data["msg"].(string))
- case 3: //图片
- case 49: //分享链接或者附件(通过xml发送
- //return
- // 来源用户
- fromWxidInter, ok := req.Data.Data["fromWxid"]
- if !ok {
- resp.OkData("找不到来源id", 1, c)
- return
- }
- fromWxid := fromWxidInter.(string)
- msg := req.Data.Data["msg"].(string)
- msg, err := strconv.Unquote(strings.Replace(strconv.Quote(string(msg)), `\\u`, `\u`, -1))
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- msg = strings.Replace(msg, fromWxid, "wxid_pslc6fxtftcb22", -1)
- sendErr = sendXml(msg)
- }
- if sendErr != nil {
- fmt.Println("发送失败,err:", sendErr.Error())
- }
- }
- resp.OkData("回调成功", 1, c)
- return
- }
- type EventReq struct {
- Event int `json:"event"`
- Wxid string `json:"wxid"`
- Data EventDataReq `json:"data"`
- }
- type EventDataReq struct {
- Type string `json:"type"`
- Des string `json:"des"`
- Timestamp string `json:"timestamp"`
- Wxid string `json:"wxid"`
- Port int `json:"port"`
- Pid int `json:"pid"`
- Flag string `json:"flag"`
- Data map[string]interface{} `json:"data"`
- }
- type TextDataReq struct {
- TimeStamp string `json:"timeStamp"`
- FromType int `json:"fromType"`
- MsgType int `json:"msgType"`
- MsgSource int `json:"msgSource"`
- FromWxid string `json:"fromWxid"`
- FinalFromWxid string `json:"finalFromWxid"`
- AtWxidList []interface{} `json:"atWxidList"`
- Silence int `json:"silence"`
- Membercount int `json:"membercount"`
- Signature string `json:"signature"`
- Msg string `json:"msg"`
- MsgBase64 string `json:"msgBase64"`
- }
- func sendText(msg string) (err error) {
- //wxRoomId := `wxid_5pf21jz6tbea22`
- wxRoomId := `wxid_pslc6fxtftcb22`
- toWxId := `wxid_vgnigr1qaizm22`
- result, _, err := services.SendText("192.168.77.14", wxRoomId, toWxId, msg)
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- fmt.Println(result)
- return
- }
- func sendXml(msg string) (err error) {
- //fmt.Println(msg)
- //return
- //wxRoomId := `wxid_5pf21jz6tbea22`
- fmt.Println("进来了")
- wxRoomId := `wxid_pslc6fxtftcb22`
- toWxId := `wxid_vgnigr1qaizm22`
- _, _, err = services.SendXml("192.168.77.14", wxRoomId, toWxId, msg)
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- //fmt.Println(result)
- return
- }
- type WxappInfo struct {
- PagePath string `description:"小程序跳转路径"`
- UserName string `description:"小程序唯一标识"`
- Appid string `description:"appid"`
- }
- func getWxappInfo(msg string) (wxappInfo WxappInfo) {
- decoder := xml2map.NewDecoder(strings.NewReader(msg))
- result, err := decoder.Decode()
- if err != nil {
- fmt.Println("err:", err)
- return
- }
- msgMapInterface, ok := result["msg"]
- if !ok {
- return
- }
- msgMap := msgMapInterface.(map[string]interface{})
- appMsgMapInterface, ok := msgMap["appmsg"]
- if !ok {
- return
- }
- appMsgMap := appMsgMapInterface.(map[string]interface{})
- // 小程序的一些信息
- weappinfoMapInterface, ok := appMsgMap["weappinfo"]
- if !ok {
- return
- }
- weappinfoMap := weappinfoMapInterface.(map[string]interface{})
- // <pagepath>pages/roadShow/video/list.html?videoId=254</pagepath>
- // <username>gh_75abb562a946@app</username>
- // <appid>wxb059c872d79b9967</appid>
- // <version>88</version>
- // <type>2</type>
- // <weappiconurl>http://wx.qlogo.cn/mmhead/Q3auHgzwzM6Q4hiceHmmbCl9XFAicAevBiaywMXNZXNicFHsUqibAMybcUQ/96</weappiconurl>
- // <appservicetype>0</appservicetype>
- // <shareId>1_wxb059c872d79b9967_5ee3244b8e0678fd6d8bab0b4976ea15_1679996983_1</shareId>
- // 小程序跳转地址
- tmpInterface, ok := weappinfoMap["pagepath"]
- if !ok {
- return
- }
- wxappInfo.PagePath = tmpInterface.(string)
- // 移除跳转路径中的.html字符
- wxappInfo.PagePath = strings.Replace(wxappInfo.PagePath, ".html?", "?", 1)
- // 小程序appid
- tmpInterface, ok = weappinfoMap["appid"]
- if !ok {
- return
- }
- wxappInfo.Appid = tmpInterface.(string)
- return
- }
- // func init() {
- // dataStr := `{"type":"Q0001","data":{"msg":"<?xml version=\"1.0\"?>\r\n<msg>\r\n\t<appmsg appid=\"\" sdkver=\"0\">\r\n\t\t<title>欧美市场隔夜复盘:德银和更多银行业风波的暴风眼</title>\r\n\t\t<des />\r\n\t\t<username />\r\n\t\t<action>view</action>\r\n\t\t<type>33</type>\r\n\t\t<showtype>0</showtype>\r\n\t\t<content />\r\n\t\t<url>https://mp.weixin.qq.com/mp/waerrpage?appid=wxb059c872d79b9967&type=upgrade&upgradetype=3#wechat_redirect</url>\r\n\t\t<lowurl />\r\n\t\t<forwardflag>0</forwardflag>\r\n\t\t<dataurl />\r\n\t\t<lowdataurl />\r\n\t\t<contentattr>0</contentattr>\r\n\t\t<streamvideo>\r\n\t\t\t<streamvideourl />\r\n\t\t\t<streamvideototaltime>0</streamvideototaltime>\r\n\t\t\t<streamvideotitle />\r\n\t\t\t<streamvideowording />\r\n\t\t\t<streamvideoweburl />\r\n\t\t\t<streamvideothumburl />\r\n\t\t\t<streamvideoaduxinfo />\r\n\t\t\t<streamvideopublishid />\r\n\t\t</streamvideo>\r\n\t\t<canvasPageItem>\r\n\t\t\t<canvasPageXml><![CDATA[]]></canvasPageXml>\r\n\t\t</canvasPageItem>\r\n\t\t<appattach>\r\n\t\t\t<attachid />\r\n\t\t\t<cdnthumburl>3057020100044b30490201000204503cca9502032df73102043023dd3a020464225cee042463353633656535652d313936312d343832642d393833332d3633323862653135616338340204011408030201000405004c4f2900</cdnthumburl>\r\n\t\t\t<cdnthumbmd5>3b7a15b6611904ff756372c962c49fcd</cdnthumbmd5>\r\n\t\t\t<cdnthumblength>55698</cdnthumblength>\r\n\t\t\t<cdnthumbheight>576</cdnthumbheight>\r\n\t\t\t<cdnthumbwidth>720</cdnthumbwidth>\r\n\t\t\t<cdnthumbaeskey>0feaa7e810f7d0f09c6e58fd815faf49</cdnthumbaeskey>\r\n\t\t\t<aeskey>0feaa7e810f7d0f09c6e58fd815faf49</aeskey>\r\n\t\t\t<encryver>1</encryver>\r\n\t\t\t<fileext />\r\n\t\t\t<islargefilemsg>0</islargefilemsg>\r\n\t\t</appattach>\r\n\t\t<extinfo />\r\n\t\t<androidsource>3</androidsource>\r\n\t\t<sourceusername>gh_75abb562a946@app</sourceusername>\r\n\t\t<sourcedisplayname>弘则研究</sourcedisplayname>\r\n\t\t<commenturl />\r\n\t\t<thumburl />\r\n\t\t<mediatagname />\r\n\t\t<messageaction><![CDATA[]]></messageaction>\r\n\t\t<messageext><![CDATA[]]></messageext>\r\n\t\t<emoticongift>\r\n\t\t\t<packageflag>0</packageflag>\r\n\t\t\t<packageid />\r\n\t\t</emoticongift>\r\n\t\t<emoticonshared>\r\n\t\t\t<packageflag>0</packageflag>\r\n\t\t\t<packageid />\r\n\t\t</emoticonshared>\r\n\t\t<designershared>\r\n\t\t\t<designeruin>0</designeruin>\r\n\t\t\t<designername>null</designername>\r\n\t\t\t<designerrediretcturl>null</designerrediretcturl>\r\n\t\t</designershared>\r\n\t\t<emotionpageshared>\r\n\t\t\t<tid>0</tid>\r\n\t\t\t<title>null</title>\r\n\t\t\t<desc>null</desc>\r\n\t\t\t<iconUrl>null</iconUrl>\r\n\t\t\t<secondUrl />\r\n\t\t\t<pageType>0</pageType>\r\n\t\t\t<setKey>null</setKey>\r\n\t\t</emotionpageshared>\r\n\t\t<webviewshared>\r\n\t\t\t<shareUrlOriginal />\r\n\t\t\t<shareUrlOpen />\r\n\t\t\t<jsAppId />\r\n\t\t\t<publisherId />\r\n\t\t</webviewshared>\r\n\t\t<template_id />\r\n\t\t<md5>3b7a15b6611904ff756372c962c49fcd</md5>\r\n\t\t<websearch />\r\n\t\t<weappinfo>\r\n\t\t\t<pagepath><![CDATA[pages-voice/voiceDetail.html?voiceId=351]]></pagepath>\r\n\t\t\t<username>gh_75abb562a946@app</username>\r\n\t\t\t<appid>wxb059c872d79b9967</appid>\r\n\t\t\t<version>88</version>\r\n\t\t\t<type>2</type>\r\n\t\t\t<weappiconurl><![CDATA[http://mmbiz.qpic.cn/mmbiz_png/a4OiaGCTMP201pmKhL0sib1URsAVZyiazKs4Cw7g7pVAIXBvVtzS8aXA0aHoric91ret4iazkZIh1zKO0NicJksOKJGw/640?wx_fmt=png&wxfrom=200]]></weappiconurl>\r\n\t\t\t<weapppagethumbrawurl><![CDATA[https://hzstatic.hzinsights.com/static/images/yb/htm2img/D3EANBLgyuIVWZkGmVicu7KO3km4.jpg]]></weapppagethumbrawurl>\r\n\t\t\t<shareId><![CDATA[0_wxb059c872d79b9967_c29fb8d17937563630f66b3afba69df7_1679962340_0]]></shareId>\r\n\t\t\t<appservicetype>0</appservicetype>\r\n\t\t\t<secflagforsinglepagemode>0</secflagforsinglepagemode>\r\n\t\t\t<videopageinfo>\r\n\t\t\t\t<thumbwidth>720</thumbwidth>\r\n\t\t\t\t<thumbheight>576</thumbheight>\r\n\t\t\t\t<fromopensdk>0</fromopensdk>\r\n\t\t\t</videopageinfo>\r\n\t\t</weappinfo>\r\n\t\t<statextstr />\r\n\t\t<musicShareItem>\r\n\t\t\t<musicDuration>0</musicDuration>\r\n\t\t</musicShareItem>\r\n\t\t<finderLiveProductShare>\r\n\t\t\t<finderLiveID><![CDATA[]]></finderLiveID>\r\n\t\t\t<finderUsername><![CDATA[]]></finderUsername>\r\n\t\t\t<finderObjectID><![CDATA[]]></finderObjectID>\r\n\t\t\t<finderNonceID><![CDATA[]]></finderNonceID>\r\n\t\t\t<liveStatus><![CDATA[]]></liveStatus>\r\n\t\t\t<appId><![CDATA[]]></appId>\r\n\t\t\t<pagePath><![CDATA[]]></pagePath>\r\n\t\t\t<productId><![CDATA[]]></productId>\r\n\t\t\t<coverUrl><![CDATA[]]></coverUrl>\r\n\t\t\t<productTitle><![CDATA[]]></productTitle>\r\n\t\t\t<marketPrice><![CDATA[0]]></marketPrice>\r\n\t\t\t<sellingPrice><![CDATA[0]]></sellingPrice>\r\n\t\t\t<platformHeadImg><![CDATA[]]></platformHeadImg>\r\n\t\t\t<platformName><![CDATA[]]></platformName>\r\n\t\t\t<shopWindowId><![CDATA[]]></shopWindowId>\r\n\t\t\t<flashSalePrice><![CDATA[0]]></flashSalePrice>\r\n\t\t\t<flashSaleEndTime><![CDATA[0]]></flashSaleEndTime>\r\n\t\t\t<ecSource><![CDATA[]]></ecSource>\r\n\t\t\t<sellingPriceWording><![CDATA[]]></sellingPriceWording>\r\n\t\t\t<platformIconURL><![CDATA[]]></platformIconURL>\r\n\t\t\t<firstProductTagURL><![CDATA[]]></firstProductTagURL>\r\n\t\t\t<firstProductTagAspectRatioString><![CDATA[0.0]]></firstProductTagAspectRatioString>\r\n\t\t\t<secondProductTagURL><![CDATA[]]></secondProductTagURL>\r\n\t\t\t<secondProductTagAspectRatioString><![CDATA[0.0]]></secondProductTagAspectRatioString>\r\n\t\t\t<firstGuaranteeWording><![CDATA[]]></firstGuaranteeWording>\r\n\t\t\t<secondGuaranteeWording><![CDATA[]]></secondGuaranteeWording>\r\n\t\t\t<thirdGuaranteeWording><![CDATA[]]></thirdGuaranteeWording>\r\n\t\t\t<isPriceBeginShow>false</isPriceBeginShow>\r\n\t\t</finderLiveProductShare>\r\n\t\t<finderOrder>\r\n\t\t\t<appID><![CDATA[]]></appID>\r\n\t\t\t<orderID><![CDATA[]]></orderID>\r\n\t\t\t<path><![CDATA[]]></path>\r\n\t\t\t<priceWording><![CDATA[]]></priceWording>\r\n\t\t\t<stateWording><![CDATA[]]></stateWording>\r\n\t\t\t<productImageURL><![CDATA[]]></productImageURL>\r\n\t\t\t<products><![CDATA[]]></products>\r\n\t\t\t<productsCount><![CDATA[0]]></productsCount>\r\n\t\t</finderOrder>\r\n\t\t<finderShopWindowShare>\r\n\t\t\t<finderUsername><![CDATA[]]></finderUsername>\r\n\t\t\t<avatar><![CDATA[]]></avatar>\r\n\t\t\t<nickname><![CDATA[]]></nickname>\r\n\t\t\t<commodityInStockCount><![CDATA[]]></commodityInStockCount>\r\n\t\t\t<appId><![CDATA[]]></appId>\r\n\t\t\t<path><![CDATA[]]></path>\r\n\t\t\t<appUsername><![CDATA[]]></appUsername>\r\n\t\t\t<query><![CDATA[]]></query>\r\n\t\t\t<liteAppId><![CDATA[]]></liteAppId>\r\n\t\t\t<liteAppPath><![CDATA[]]></liteAppPath>\r\n\t\t\t<liteAppQuery><![CDATA[]]></liteAppQuery>\r\n\t\t</finderShopWindowShare>\r\n\t\t<findernamecard>\r\n\t\t\t<username />\r\n\t\t\t<avatar><![CDATA[]]></avatar>\r\n\t\t\t<nickname />\r\n\t\t\t<auth_job />\r\n\t\t\t<auth_icon>0</auth_icon>\r\n\t\t\t<auth_icon_url />\r\n\t\t\t<ecSource><![CDATA[]]></ecSource>\r\n\t\t</findernamecard>\r\n\t\t<finderGuarantee>\r\n\t\t\t<scene><![CDATA[0]]></scene>\r\n\t\t</finderGuarantee>\r\n\t\t<directshare>0</directshare>\r\n\t\t<gamecenter>\r\n\t\t\t<namecard>\r\n\t\t\t\t<iconUrl />\r\n\t\t\t\t<name />\r\n\t\t\t\t<desc />\r\n\t\t\t\t<tail />\r\n\t\t\t\t<jumpUrl />\r\n\t\t\t</namecard>\r\n\t\t</gamecenter>\r\n\t\t<patMsg>\r\n\t\t\t<chatUser />\r\n\t\t\t<records>\r\n\t\t\t\t<recordNum>0</recordNum>\r\n\t\t\t</records>\r\n\t\t</patMsg>\r\n\t\t<secretmsg>\r\n\t\t\t<issecretmsg>0</issecretmsg>\r\n\t\t</secretmsg>\r\n\t\t<referfromscene>0</referfromscene>\r\n\t\t<gameshare>\r\n\t\t\t<liteappext>\r\n\t\t\t\t<liteappbizdata />\r\n\t\t\t\t<liteapppriority>0</liteapppriority>\r\n\t\t\t</liteappext>\r\n\t\t\t<gameshareid />\r\n\t\t\t<sharedata />\r\n\t\t\t<isvideo>0</isvideo>\r\n\t\t\t<duration>0</duration>\r\n\t\t\t<isexposed>0</isexposed>\r\n\t\t\t<readtext />\r\n\t\t</gameshare>\r\n\t</appmsg>\r\n\t<fromusername>wxid_6895448954512</fromusername>\r\n\t<scene>0</scene>\r\n\t<appinfo>\r\n\t\t<version>1</version>\r\n\t\t<appname></appname>\r\n\t</appinfo>\r\n\t<commenturl></commenturl>\r\n</msg>\r\n","wxid":"wxid_vgnigr1qaizm22"}}`
- //
- // fmt.Println("进来了")
- // decoder := xml2map.NewDecoder(strings.NewReader(dataStr))
- // result, err := decoder.Decode()
- // if err != nil {
- // fmt.Println("err:", err)
- // return
- // }
- // fmt.Println(result)
- // }
|