|
@@ -2,6 +2,8 @@ package controllers
|
|
|
|
|
|
import (
|
|
|
"encoding/json"
|
|
|
+ "encoding/xml"
|
|
|
+ "fmt"
|
|
|
"hongze/hongze_cygxzs/models"
|
|
|
"hongze/hongze_cygxzs/services"
|
|
|
"hongze/hongze_cygxzs/utils"
|
|
@@ -267,3 +269,71 @@ func (this *WechatController) UserInfo() {
|
|
|
br.Msg = "获取成功"
|
|
|
br.Data = resp
|
|
|
}
|
|
|
+
|
|
|
+// @Title 微信获取签名接口
|
|
|
+// @Description 微信获取签名接口
|
|
|
+// @Param Url query string true "url地址"
|
|
|
+// @Success 200 {object} models.WechatSign
|
|
|
+// @router /notify [get,post]
|
|
|
+func (this *WechatCommonController) Notify() {
|
|
|
+ echostr := this.GetString("echostr")
|
|
|
+ method := this.Ctx.Input.Method()
|
|
|
+ if method == "POST" {
|
|
|
+ body := this.Ctx.Input.RequestBody
|
|
|
+ utils.FileLog.Info("wechat notify:" + string(body))
|
|
|
+ item := new(models.Notify)
|
|
|
+ err := xml.Unmarshal(body, &item)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("xml.Unmarshal:" + err.Error())
|
|
|
+ }
|
|
|
+ contactMsg := "感谢关注弘则研究。\r\n公司地址:上海市世纪大道210号21世纪中心大厦12层1206室\r\n\r\n业务合作:\r\n电话:86-21-61645300\r\n邮箱:service@hzinsights.com\r\n邮编:200120\r\n\r\n海外业务:\r\n邮箱:yyu@hzinsights.com "
|
|
|
+
|
|
|
+ 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)
|
|
|
+ wxUser, err := models.GetUserDetailBuOpenid(openId)
|
|
|
+ if err != nil {
|
|
|
+ utils.FileLog.Info("GetUserDetailBuOpenid:" + err.Error())
|
|
|
+ }
|
|
|
+ if wxUser == nil {
|
|
|
+ utils.FileLog.Info("用户不存在openId:" + openId)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if item.MsgType == "event" {
|
|
|
+ switch item.Event {
|
|
|
+ case "subscribe":
|
|
|
+ fmt.Println("关注")
|
|
|
+ go models.UserSubscribe(1, wxUser.UserId)
|
|
|
+ break
|
|
|
+ case "unsubscribe":
|
|
|
+ fmt.Println("取消关注")
|
|
|
+ go models.UserSubscribe(0, wxUser.UserId)
|
|
|
+ break
|
|
|
+ case "CLICK":
|
|
|
+ returnResult = xmlTpl
|
|
|
+ break
|
|
|
+ default:
|
|
|
+ utils.FileLog.Info("wechat notify event:" + item.Event)
|
|
|
+ }
|
|
|
+ this.Ctx.WriteString(xmlTpl)
|
|
|
+ } else if item.MsgType == "text" {
|
|
|
+ returnResult = xmlTpl
|
|
|
+ this.Ctx.WriteString(returnResult)
|
|
|
+ } else {
|
|
|
+ returnResult = xmlTpl
|
|
|
+ }
|
|
|
+ this.Ctx.WriteString(returnResult)
|
|
|
+ } else {
|
|
|
+ this.Ctx.WriteString(echostr)
|
|
|
+ }
|
|
|
+}
|