|
@@ -2,6 +2,7 @@ package controllers
|
|
|
|
|
|
import (
|
|
import (
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
+ "encoding/xml"
|
|
"fmt"
|
|
"fmt"
|
|
"github.com/medivhzhan/weapp/v2"
|
|
"github.com/medivhzhan/weapp/v2"
|
|
adminResp "hongze/hongze_mobile_admin/models/response/admin"
|
|
adminResp "hongze/hongze_mobile_admin/models/response/admin"
|
|
@@ -9,6 +10,7 @@ import (
|
|
"hongze/hongze_mobile_admin/services"
|
|
"hongze/hongze_mobile_admin/services"
|
|
"hongze/hongze_mobile_admin/utils"
|
|
"hongze/hongze_mobile_admin/utils"
|
|
"strconv"
|
|
"strconv"
|
|
|
|
+ "time"
|
|
)
|
|
)
|
|
|
|
|
|
type WeChatCommon struct {
|
|
type WeChatCommon struct {
|
|
@@ -301,7 +303,6 @@ func (c *WeChatCommon) AdminWeChatLogin() {
|
|
c.FailWithMessage(fmt.Sprintf("%v", err), fmt.Sprintf("%v", err))
|
|
c.FailWithMessage(fmt.Sprintf("%v", err), fmt.Sprintf("%v", err))
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
if item.Errcode != 0 {
|
|
if item.Errcode != 0 {
|
|
if item.Errcode == 40163 {
|
|
if item.Errcode == 40163 {
|
|
c.FailWithCodeUsed("获取用户信息失败", "获取access_token 失败 errCode:"+strconv.Itoa(item.Errcode)+" ;errMsg:"+item.Errmsg)
|
|
c.FailWithCodeUsed("获取用户信息失败", "获取access_token 失败 errCode:"+strconv.Itoa(item.Errcode)+" ;errMsg:"+item.Errmsg)
|
|
@@ -376,4 +377,71 @@ func (c *WeChatCommon) AdminWeChatLogin() {
|
|
}
|
|
}
|
|
|
|
|
|
c.OkDetailed(resp, "登录成功")
|
|
c.OkDetailed(resp, "登录成功")
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// @Title 微信获取签名接口
|
|
|
|
+// @Description 微信获取签名接口
|
|
|
|
+// @Param Url query string true "url地址"
|
|
|
|
+// @Success 200 {object} models.WechatSign
|
|
|
|
+// @router /notify [get,post]
|
|
|
|
+func (c *WeChatCommon) Notify() {
|
|
|
|
+ echostr := c.GetString("echostr")
|
|
|
|
+ method := c.Ctx.Input.Method()
|
|
|
|
+ if method == "POST" {
|
|
|
|
+ body := c.Ctx.Input.RequestBody
|
|
|
|
+ utils.FileLog.Info("wechat notify:" + string(body))
|
|
|
|
+ item := new(Notify)
|
|
|
|
+ err := xml.Unmarshal(body, &item)
|
|
|
|
+ if err != nil {
|
|
|
|
+ utils.FileLog.Info("xml.Unmarshal:" + err.Error())
|
|
|
|
+ }
|
|
|
|
+ contactMsg := "感谢关注弘则内部。\n\n此公众号仅供内部员工使用,用于内部员工接收消息通知"
|
|
|
|
+
|
|
|
|
+ var openId, returnResult string
|
|
|
|
+ if item.MsgType != "" {
|
|
|
|
+ openId = item.FromUserName
|
|
|
|
+ }
|
|
|
|
+ xmlTpl := `<xml>
|
|
|
|
+ <ToUserName><![CDATA[%s]]></ToUserName>
|
|
|
|
+ <FromUserName><![CDATA[%s]]></FromUserName>
|
|
|
|
+ <CreateTime>%s</CreateTime>
|
|
|
|
+ <MsgType><![CDATA[text]]></MsgType>
|
|
|
|
+ <Content><![CDATA[%s]]></Content>
|
|
|
|
+ </xml>`
|
|
|
|
+ createTime := strconv.FormatInt(time.Now().Unix(), 10)
|
|
|
|
+ xmlTpl = fmt.Sprintf(xmlTpl, openId, utils.WxId, createTime, contactMsg)
|
|
|
|
+
|
|
|
|
+ if item.MsgType == "event" {
|
|
|
|
+ switch item.Event {
|
|
|
|
+ case "subscribe":
|
|
|
|
+ fmt.Println("关注")
|
|
|
|
+ // go models.UserSubscribe(1, openId)
|
|
|
|
+ break
|
|
|
|
+ case "unsubscribe":
|
|
|
|
+ fmt.Println("取消关注")
|
|
|
|
+ // go models.UserSubscribe(0, openId)
|
|
|
|
+ break
|
|
|
|
+ case "CLICK":
|
|
|
|
+ returnResult = xmlTpl
|
|
|
|
+ break
|
|
|
|
+ default:
|
|
|
|
+ utils.FileLog.Info("wechat notify event:" + item.Event)
|
|
|
|
+ }
|
|
|
|
+ c.Ctx.WriteString(xmlTpl)
|
|
|
|
+ } else {
|
|
|
|
+ returnResult = xmlTpl
|
|
|
|
+ }
|
|
|
|
+ c.Ctx.WriteString(returnResult)
|
|
|
|
+ } else {
|
|
|
|
+ c.Ctx.WriteString(echostr)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Notify struct {
|
|
|
|
+ ToUserName string `xml:"ToUserName"`
|
|
|
|
+ FromUserName string `xml:"FromUserName"`
|
|
|
|
+ CreateTime int `xml:"CreateTime"`
|
|
|
|
+ MsgType string `xml:"MsgType"`
|
|
|
|
+ Event string `xml:"Event"`
|
|
|
|
+ EventKey string `xml:"EventKey"`
|
|
}
|
|
}
|