wechat.go 6.0 KB

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