|
@@ -1,86 +1,171 @@
|
|
|
package controllers
|
|
|
|
|
|
import (
|
|
|
+ "encoding/xml"
|
|
|
"eta/eta_mini_api/models"
|
|
|
"eta/eta_mini_api/services/wechat"
|
|
|
- "eta/eta_mini_api/services/wx_app"
|
|
|
"eta/eta_mini_api/utils"
|
|
|
"fmt"
|
|
|
+ "strconv"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type WechatController struct {
|
|
|
BaseCommonController
|
|
|
}
|
|
|
|
|
|
-// Login
|
|
|
-// @Title 微信用户登录
|
|
|
-// @Description 用户登录
|
|
|
-// @Param request body UserLoginReq true "type json string"
|
|
|
-// @Success 200 {object} models.LoginResp
|
|
|
-// @router /login [get]
|
|
|
-func (this *WechatController) Login() {
|
|
|
- br := new(models.BaseResponse).Init()
|
|
|
- defer func() {
|
|
|
- this.Data["json"] = br
|
|
|
- this.ServeJSON()
|
|
|
- }()
|
|
|
+// @Title 微信获取签名接口
|
|
|
+// @Description 微信获取签名接口
|
|
|
+// @Param Url query string true "url地址"
|
|
|
+// @Success 200 {object} models.WechatSign
|
|
|
+// @router /notify [get,post]
|
|
|
+func (this *WechatController) Notify() {
|
|
|
+ echostr := this.GetString("echostr")
|
|
|
+ method := this.Ctx.Input.Method()
|
|
|
|
|
|
- code := this.GetString("code")
|
|
|
- wxUserInfo, err := wx_app.GetSession(code)
|
|
|
- if err != nil {
|
|
|
- br.Msg = "获取失败"
|
|
|
- br.ErrMsg = "获取失败,Err:" + err.Error()
|
|
|
- return
|
|
|
+ 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"`
|
|
|
+ Content string `xml:"Content"`
|
|
|
}
|
|
|
+ if method == "POST" {
|
|
|
+ body := this.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 := "感谢关注东吴期货研究所\r\n公司地址:上海市黄浦区西藏南路1208号东吴证券大厦19楼\r\n\r\n业务合作:\r\n电话:021-6312 3065\r\n邮箱:lvan@dwqh88.com\r\n邮编:200001"
|
|
|
|
|
|
- fmt.Println("openid", wxUserInfo.OpenID)
|
|
|
- user, err := wechat.GetUserInfo(wxUserInfo.OpenID)
|
|
|
- if err != nil {
|
|
|
- br.Msg = "获取失败"
|
|
|
- br.ErrMsg = "获取失败,Err:" + err.Error()
|
|
|
- return
|
|
|
+ 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.DW_WX_Id, createTime, contactMsg)
|
|
|
+
|
|
|
+ if item.MsgType == "event" {
|
|
|
+ switch item.Event {
|
|
|
+ case "subscribe":
|
|
|
+ fmt.Println("关注")
|
|
|
+ go subscribe(openId)
|
|
|
+ case "unsubscribe":
|
|
|
+ fmt.Println("取消关注")
|
|
|
+ go models.UserSubscribe(0, openId)
|
|
|
+ case "CLICK":
|
|
|
+ returnResult = xmlTpl
|
|
|
+ default:
|
|
|
+ utils.FileLog.Info("wechat notify event:" + item.Event)
|
|
|
+ }
|
|
|
+ this.Ctx.WriteString(xmlTpl)
|
|
|
+ } else {
|
|
|
+ returnResult = xmlTpl
|
|
|
+ }
|
|
|
+ this.Ctx.WriteString(returnResult)
|
|
|
+ } else {
|
|
|
+ this.Ctx.WriteString(echostr)
|
|
|
}
|
|
|
- fmt.Println("user----", user)
|
|
|
- fmt.Println(wxUserInfo)
|
|
|
- fmt.Println("openid", wxUserInfo.OpenID)
|
|
|
- fmt.Println("unionid", wxUserInfo.UnionID)
|
|
|
- // token, userId, isBind, err := services.WxLogin(wxUserInfo)
|
|
|
}
|
|
|
|
|
|
-// GetUserInfo
|
|
|
-// @Title 获取微信用户信息
|
|
|
-// @Description 获取微信用户信息
|
|
|
-// @Param request body UserLoginReq true "type json string"
|
|
|
-// @Success 200 {object} models.LoginResp
|
|
|
-// @router /userInfo [get]
|
|
|
-func (this *WechatController) UserInfo() {
|
|
|
- br := new(models.BaseResponse).Init()
|
|
|
- defer func() {
|
|
|
- this.Data["json"] = br
|
|
|
- this.ServeJSON()
|
|
|
- }()
|
|
|
-
|
|
|
- openid := `oxgGc63ul7XT_kkxijd5mQI_6agg`
|
|
|
- userInfo, err := wechat.GetUserInfo(openid)
|
|
|
- if err != nil {
|
|
|
- br.Msg = "获取失败"
|
|
|
- br.ErrMsg = "获取失败,系统错误,Err:" + err.Error()
|
|
|
+// subscribe 关注后的处理逻辑
|
|
|
+func subscribe(openId string) {
|
|
|
+ userRecord, err := models.GetUserRecordByOpenId(openId)
|
|
|
+ if err != nil && err.Error() != utils.ErrNoRow() {
|
|
|
+ fmt.Println("通过openid获取user_record记录失败,err:" + err.Error())
|
|
|
return
|
|
|
}
|
|
|
+ err = nil
|
|
|
|
|
|
- br.Data = userInfo
|
|
|
- br.Msg = "获取成功"
|
|
|
- br.Success = true
|
|
|
- br.Ret = 200
|
|
|
-}
|
|
|
+ // openId已存在
|
|
|
+ if userRecord != nil {
|
|
|
+ if userRecord.UserId > 0 { //已经绑定了的话,那么就去修改用户状态
|
|
|
+ models.UserSubscribe(1, openId)
|
|
|
+ } else {
|
|
|
+ // 没有绑定的话,那么校验下unionid,然后再去修改
|
|
|
+ unionId := userRecord.UnionId
|
|
|
+ if unionId == `` {
|
|
|
+ wxUserItem, err := wechat.GetUserInfo(openId)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if wxUserItem.UnionID != `` {
|
|
|
+ unionId = wxUserItem.UnionID
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
-// SendEmail
|
|
|
-// @Title 获取微信用户信息
|
|
|
-// @Description 获取微信用户信息
|
|
|
-// @Param request body UserLoginReq true "type json string"
|
|
|
-// @Success 200 {object} models.LoginResp
|
|
|
-// @router /SendEmail [get]
|
|
|
-func (this *WechatController) SendEmail() {
|
|
|
- utils.SendEmailByHz("测试邮箱", "测试内容", "564693862@qq.com")
|
|
|
+ updateCol := make([]string, 0)
|
|
|
+ userRecord.Subscribe = 1
|
|
|
+ userRecord.SubscribeTime = time.Now()
|
|
|
+ updateCol = append(updateCol, "Subscribe")
|
|
|
+ if unionId != `` {
|
|
|
+ userRecord.UnionId = unionId
|
|
|
+ // 通过unionid获取已绑定用户的user_record信息
|
|
|
+ bindUserRecord, _ := models.GetBindUserRecordByUnionId(unionId)
|
|
|
+ if bindUserRecord != nil {
|
|
|
+ userRecord.UserId = bindUserRecord.UserId
|
|
|
+ userRecord.RealName = bindUserRecord.RealName
|
|
|
+ userRecord.Sex = bindUserRecord.Sex
|
|
|
+ updateCol = append(updateCol, "UserId", "RealName")
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err = userRecord.Update(updateCol)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("关注后,通过openid更新user_record异常,ERR:", err)
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // 没有记录,那么需要获取下unionid
|
|
|
+ wxUserItem, err := wechat.GetUserInfo(openId)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("获取用户信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
+ newUserRecord := &models.UserRecord{
|
|
|
+ UserRecordId: 0,
|
|
|
+ OpenId: openId,
|
|
|
+ UnionId: wxUserItem.UnionID,
|
|
|
+ Subscribe: 1,
|
|
|
+ SubscribeTime: time.Now(),
|
|
|
+ NickName: wxUserItem.Nickname,
|
|
|
+ Sex: int(wxUserItem.Sex),
|
|
|
+ Province: wxUserItem.Province,
|
|
|
+ City: wxUserItem.City,
|
|
|
+ Country: wxUserItem.Country,
|
|
|
+ Headimgurl: wxUserItem.Headimgurl,
|
|
|
+ CreateTime: time.Now(),
|
|
|
+ }
|
|
|
+ if wxUserItem.UnionID != `` {
|
|
|
+ // 通过unionid获取已绑定用户的user_record信息
|
|
|
+ bindUserRecord, _ := models.GetBindUserRecordByUnionId(wxUserItem.UnionID)
|
|
|
+ if bindUserRecord != nil {
|
|
|
+ newUserRecord.UserId = bindUserRecord.UserId
|
|
|
+ newUserRecord.RealName = bindUserRecord.RealName
|
|
|
+ newUserRecord.Sex = bindUserRecord.Sex
|
|
|
+ newUserRecord.Province = bindUserRecord.Province
|
|
|
+ newUserRecord.City = bindUserRecord.City
|
|
|
+ newUserRecord.Country = bindUserRecord.Country
|
|
|
+ newUserRecord.Headimgurl = bindUserRecord.Headimgurl
|
|
|
+ }
|
|
|
+ }
|
|
|
+ err = newUserRecord.Insert()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println("关注后,添加user_record信息失败,err:" + err.Error())
|
|
|
+ return
|
|
|
+ }
|
|
|
}
|