wechat.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. package controllers
  2. import (
  3. "fmt"
  4. "hongze/hongze_api/models"
  5. "hongze/hongze_api/services"
  6. "hongze/hongze_api/utils"
  7. "strconv"
  8. "time"
  9. )
  10. type WechatController struct {
  11. BaseAuthController
  12. }
  13. type WechatCommonController struct {
  14. BaseCommonController
  15. }
  16. // @Title 微信登录接口
  17. // @Description 微信登录接口
  18. // @Param Code query string true "微信唯一编码code"
  19. // @Success 200 {object} models.WxLoginResp
  20. // @router /login [get]
  21. func (this *WechatCommonController) WechatLogin() {
  22. br := new(models.BaseResponse).Init()
  23. defer func() {
  24. this.Data["json"] = br
  25. this.ServeJSON()
  26. }()
  27. code := this.GetString("Code")
  28. fmt.Println("code:", code)
  29. item, err := services.WxGetUserOpenIdByCode(code)
  30. if err != nil {
  31. br.Msg = "获取用户信息失败"
  32. br.ErrMsg = "获取用户信息失败,Err:" + err.Error()
  33. return
  34. }
  35. if item.Errcode != 0 {
  36. br.Msg = "获取用户信息失败"
  37. br.ErrMsg = "获取access_token 失败 errcode:" + strconv.Itoa(item.Errcode) + " ;errmsg:" + item.Errmsg
  38. return
  39. }
  40. openId := item.Openid
  41. firstLogin := 1
  42. userId := 0
  43. //获取成功
  44. if openId != "" {
  45. wxUser, err := models.GetWxUserItemByOpenId(openId)
  46. if err != nil && err.Error() != utils.ErrNoRow() {
  47. br.Msg = "获取用户信息失败"
  48. br.ErrMsg = "根据openid获取用户信息失败,Eerr:" + err.Error()
  49. return
  50. }
  51. if wxUser == nil || (err != nil && err.Error() == utils.ErrNoRow()) {
  52. accessToken, err := services.WxGetAccessToken()
  53. if err != nil {
  54. br.Msg = "获取用户信息失败"
  55. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  56. return
  57. }
  58. //获取用户信息
  59. wxUserInfo, err := services.WxGetUserInfo(openId, accessToken)
  60. if err != nil {
  61. br.Msg = "获取用户信息失败"
  62. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  63. return
  64. }
  65. /*if wxUserInfo.Errcode != 0 {
  66. br.Msg = "获取用户信息失败"
  67. br.ErrMsg = "获取用户信息失败 errcode:" + item.Errcode + " ;errmsg:" + item.Errmsg
  68. return
  69. }*/
  70. user := new(models.WxUser)
  71. user.OpenId = openId
  72. user.CompanyId = 1
  73. user.CreatedTime = time.Now()
  74. user.UnionId = wxUserInfo.Unionid
  75. user.Unionid = wxUserInfo.Unionid
  76. user.Subscribe = wxUserInfo.SubscribeScene
  77. user.SubscribeTime = wxUserInfo.SubscribeTime
  78. user.NickName = wxUserInfo.Nickname
  79. user.Sex = wxUserInfo.Sex
  80. user.City = wxUserInfo.City
  81. user.Province = wxUserInfo.Province
  82. user.Country = wxUserInfo.Country
  83. user.Headimgurl = wxUserInfo.Headimgurl
  84. user.Remark = wxUserInfo.Remark
  85. user.FirstLogin = 1
  86. user.Enabled = 1
  87. user.RegisterTime=time.Now()
  88. _,err = models.AddWxUser(user)
  89. if wxUserInfo.Unionid != "" {
  90. wxUser, err = models.GetWxUserItemByUnionid(wxUserInfo.Unionid)
  91. if err != nil {
  92. br.Msg = "获取用户信息失败"
  93. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  94. return
  95. }
  96. } else {
  97. wxUser, err = models.GetWxUserItemByOpenId(openId)
  98. if err != nil {
  99. br.Msg = "获取用户信息失败"
  100. br.ErrMsg = "获取微信用户信息失败,Err:" + err.Error()
  101. return
  102. }
  103. }
  104. userId = wxUser.UserId
  105. } else {
  106. firstLogin = wxUser.FirstLogin
  107. userId = wxUser.UserId
  108. }
  109. } else {
  110. br.Msg = "获取用户信息失败"
  111. br.ErrMsg = "获取openid失败,openid:" + item.Openid
  112. return
  113. }
  114. timeUnix := time.Now().Unix()
  115. timeUnixStr := strconv.FormatInt(timeUnix, 10)
  116. token := utils.MD5(strconv.Itoa(userId)) + utils.MD5(timeUnixStr)
  117. //新增session
  118. {
  119. session := new(models.Session)
  120. session.OpenId = openId
  121. session.UserId = userId
  122. session.CreatedTime = time.Now()
  123. session.LastUpdatedTime = time.Now()
  124. session.ExpireTime = time.Now().AddDate(1, 0, 0)
  125. session.AccessToken = token
  126. err = models.AddSession(session)
  127. if err != nil {
  128. br.Msg = "登录失败"
  129. br.ErrMsg = "登录失败,新增用户session信息失败:" + err.Error()
  130. return
  131. }
  132. }
  133. permission, err := services.CheckUserPermission(userId)
  134. if err != nil {
  135. br.Msg = "登录失败"
  136. br.ErrMsg = "登录失败,判断权限失败:" + err.Error()
  137. return
  138. }
  139. resp := new(models.WxLoginResp)
  140. resp.UserId = userId
  141. resp.OpenId = openId
  142. resp.Code = 0
  143. resp.FirstLogin = firstLogin
  144. resp.Authorization = token
  145. resp.UserPermission = permission
  146. br.Ret = 200
  147. br.Success = true
  148. br.Msg = "登录成功"
  149. br.Data = resp
  150. }
  151. // @Title 微信登录接口
  152. // @Description 微信登录接口
  153. // @Param Url query string true "url地址"
  154. // @Success 200 {object} models.WechatSign
  155. // @router /getWxSign [get]
  156. func (this *WechatController) GetWxSign() {
  157. br := new(models.BaseResponse).Init()
  158. defer func() {
  159. this.Data["json"] = br
  160. this.ServeJSON()
  161. }()
  162. getUrl := this.GetString("Url")
  163. fmt.Println("getUrl:", getUrl)
  164. accessToken, err := services.WxGetAccessToken()
  165. if err != nil {
  166. br.Msg = "获取用户信息失败"
  167. br.ErrMsg = "获取access_token失败,err:" + err.Error()
  168. return
  169. }
  170. ticket, err := services.GetWxTicket(accessToken)
  171. if err != nil {
  172. br.Msg = "获取Ticket失败,请联系客服"
  173. br.ErrMsg = "获取Ticket失败,Err" + err.Error()
  174. return
  175. }
  176. if ticket == "" {
  177. br.Msg = "获取Ticket失败,请联系客服"
  178. br.ErrMsg = "ticket为空" + ticket
  179. return
  180. }
  181. nonceStr := utils.GetRandStringNoSpecialChar(16)
  182. signature, nonceString, timestamp := services.GetWxSignature(ticket, getUrl, nonceStr)
  183. resp := new(models.WechatSign)
  184. resp.AppId = utils.WxAppId
  185. resp.NonceStr = nonceString
  186. resp.Timestamp = timestamp
  187. resp.Url = getUrl
  188. resp.Signature = signature
  189. br.Ret = 200
  190. br.Success = true
  191. br.Msg = "获取签名成功"
  192. br.Data = resp
  193. }
  194. /*
  195. $app->bag('api/wechat/check', "WechatController@check");
  196. */