123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- package controllers
- import (
- "encoding/json"
- "fmt"
- "hongze/hongze_api/models"
- "hongze/hongze_api/services"
- "hongze/hongze_api/utils"
- "strconv"
- )
- 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() {
- signature := this.GetString("signature")
- timestamp := this.GetString("timestamp")
- nonce := this.GetString("nonce")
- echostr := this.GetString("echostr")
- fmt.Println("signature:", signature)
- fmt.Println("timestamp:", timestamp)
- fmt.Println("nonce:", nonce)
- body := this.Ctx.Input.RequestBody
- paramsMap := this.Ctx.Input.Params()
- fmt.Println("paramsMap")
- fmt.Println(paramsMap)
- paramsJson, err := json.Marshal(paramsMap)
- if err != nil {
- fmt.Println("json.Marshal Err:" + err.Error())
- }
- utils.FileLog.Info("notify result:" + string(body))
- utils.FileLog.Info("notify paramsJson result:" + string(paramsJson))
- this.Ctx.WriteString(echostr)
- }
|