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{})
// pages/roadShow/video/list.html?videoId=254
// gh_75abb562a946@app
// wxb059c872d79b9967
// 88
// 2
// http://wx.qlogo.cn/mmhead/Q3auHgzwzM6Q4hiceHmmbCl9XFAicAevBiaywMXNZXNicFHsUqibAMybcUQ/96
// 0
// 1_wxb059c872d79b9967_5ee3244b8e0678fd6d8bab0b4976ea15_1679996983_1
// 小程序跳转地址
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":"\r\n\r\n\t\r\n\t\t欧美市场隔夜复盘:德银和更多银行业风波的暴风眼\r\n\t\t\r\n\t\t\r\n\t\tview\r\n\t\t33\r\n\t\t0\r\n\t\t\r\n\t\thttps://mp.weixin.qq.com/mp/waerrpage?appid=wxb059c872d79b9967&type=upgrade&upgradetype=3#wechat_redirect\r\n\t\t\r\n\t\t0\r\n\t\t\r\n\t\t\r\n\t\t0\r\n\t\t\r\n\t\t\t\r\n\t\t\t0\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\t3057020100044b30490201000204503cca9502032df73102043023dd3a020464225cee042463353633656535652d313936312d343832642d393833332d3633323862653135616338340204011408030201000405004c4f2900\r\n\t\t\t3b7a15b6611904ff756372c962c49fcd\r\n\t\t\t55698\r\n\t\t\t576\r\n\t\t\t720\r\n\t\t\t0feaa7e810f7d0f09c6e58fd815faf49\r\n\t\t\t0feaa7e810f7d0f09c6e58fd815faf49\r\n\t\t\t1\r\n\t\t\t\r\n\t\t\t0\r\n\t\t\r\n\t\t\r\n\t\t3\r\n\t\tgh_75abb562a946@app\r\n\t\t弘则研究\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t0\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t0\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t\t0\r\n\t\t\tnull\r\n\t\t\tnull\r\n\t\t\r\n\t\t\r\n\t\t\t0\r\n\t\t\tnull\r\n\t\t\tnull\r\n\t\t\tnull\r\n\t\t\t\r\n\t\t\t0\r\n\t\t\tnull\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\r\n\t\t\r\n\t\t3b7a15b6611904ff756372c962c49fcd\r\n\t\t\r\n\t\t\r\n\t\t\t\r\n\t\t\tgh_75abb562a946@app\r\n\t\t\twxb059c872d79b9967\r\n\t\t\t88\r\n\t\t\t2\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t\r\n\t\t\t0\r\n\t\t\t0\r\n\t\t\t\r\n\t\t\t\t720\r\n\t\t\t\t576\r\n\t\t\t\t0\r\n\t\t\t\r\n\t\t\r\n\t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\t\t1\r\n\t\t\r\n\t\r\n\t\r\n\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)
// }