123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431 |
- package controllers
- import (
- "encoding/json"
- "encoding/xml"
- "fmt"
- "hongze/hongze_api/models"
- "hongze/hongze_api/services"
- "hongze/hongze_api/utils"
- "strconv"
- "time"
- )
- type WechatController struct {
- BaseAuthController
- }
- type WechatCommonController struct {
- BaseCommonController
- }
- func (this *WechatCommonController) WechatLogin() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- resp := new(models.WxLoginResp)
- code := this.GetString("Code")
- fmt.Println("code:", code)
- utils.FileLog.Info("WechatLogin code:%s", code)
- wxCodeInfo, err := models.GetWxUserCode(code)
- if err == nil && wxCodeInfo != nil && wxCodeInfo.Id > 0 {
- utils.FileLog.Info("WechatLogin code exist:%s", code)
- resp.UserId = wxCodeInfo.UserId
- resp.Code = 0
- resp.FirstLogin = wxCodeInfo.FirstLogin
- resp.Authorization = wxCodeInfo.Authorization
- resp.UserPermission = wxCodeInfo.UserPermission
- br.Ret = 200
- br.Success = true
- br.Msg = "登录成功"
- br.Data = resp
- return
- }
- item, err := services.WxGetUserOpenIdByCode(code)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
- return
- }
- if item.Errcode != 0 {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
- return
- }
- openId := item.Openid
- if openId == "" {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取openid失败,openid:" + item.Openid
- return
- }
- accessToken, err := services.WxGetAccessToken()
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取access_token失败,err:" + err.Error()
- return
- }
-
- wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
- return
- }
- if wxUserInfo.Errcode != 0 {
- userInfoJson, _ := json.Marshal(wxUserInfo)
- br.Msg = "登录失败"
- br.ErrMsg = "获取用户信息失败,err:" + string(userInfoJson)
- return
- }
- token, userId, firstLogin, permission, err := services.WxLogin(utils.WxPlatform, code, item, wxUserInfo)
- if err != nil {
- br.Msg = "微信登录失败"
- br.ErrMsg = "微信登录失败,err:" + err.Error()
- return
- }
- resp.UserId = userId
- resp.Code = 0
- resp.FirstLogin = firstLogin
- resp.Authorization = token
- resp.UserPermission = permission
- br.Ret = 200
- br.Success = true
- br.Msg = "登录成功"
- br.Data = resp
-
- {
- returnResult, err := json.Marshal(br)
- if err != nil {
- utils.FileLog.Info(this.Ctx.Input.URI() + " Err:%s" + err.Error())
- }
- utils.FileLog.Info(this.Ctx.Input.URI()+" code: %s , return data: %s", code, string(returnResult))
- }
- }
- func (this *WechatController) GetWxSign() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- getUrl := this.GetString("Url")
- fmt.Println("getUrl:", getUrl)
- accessToken, err := services.WxGetAccessToken()
- if err != nil {
- br.Msg = "获取用户信息失败"
- br.ErrMsg = "获取access_token失败,err:" + err.Error()
- return
- }
- ticket, err := services.GetWxTicket(accessToken)
- if err != nil {
- br.Msg = "获取Ticket失败,请联系客服"
- br.ErrMsg = "获取Ticket失败,Err" + err.Error()
- return
- }
- if ticket == "" {
- br.Msg = "获取Ticket失败,请联系客服"
- br.ErrMsg = "ticket为空" + ticket
- return
- }
- nonceStr := utils.GetRandStringNoSpecialChar(16)
- signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
- resp := new(models.WechatSign)
- resp.AppId = utils.WxAppId
- resp.NonceStr = nonceString
- resp.Timestamp = timestamp
- resp.Url = getUrl
- resp.Signature = signature
- br.Ret = 200
- br.Success = true
- br.Msg = "获取签名成功"
- br.Data = resp
- }
- 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(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)
- 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)
- }
- this.Ctx.WriteString(xmlTpl)
- } else {
- returnResult = xmlTpl
- }
- this.Ctx.WriteString(returnResult)
- } else {
- this.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"`
- }
|