wechat.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package wechat
  2. import (
  3. "github.com/gin-gonic/gin"
  4. "hongze/hongze_yb/controller/response"
  5. response2 "hongze/hongze_yb/models/response"
  6. userResp "hongze/hongze_yb/models/response/user"
  7. "hongze/hongze_yb/services/user"
  8. userService "hongze/hongze_yb/services/user"
  9. "hongze/hongze_yb/services/wechat"
  10. "hongze/hongze_yb/services/wx_app"
  11. "strconv"
  12. )
  13. func GetQrCode(c *gin.Context) {
  14. //wxApp := wx_app.GetWxApp()
  15. //response.OkData("获取成功", qrCode, c)
  16. }
  17. func GetUserInfo(c *gin.Context) {
  18. openid := `oN0jD1S3P-FVosLhq-YiVOXldtRo`
  19. userInfo, err := wechat.GetUserInfo(openid)
  20. if err != nil {
  21. response.Fail("获取失败,Err:"+err.Error(), c)
  22. return
  23. }
  24. response.OkData("获取成功", userInfo, c)
  25. }
  26. func GetUserSession(c *gin.Context) {
  27. code, _ := c.GetQuery("code")
  28. //c.Sho
  29. //fmt.Println(c.Request.)
  30. userInfo, err := wx_app.GetSession(code)
  31. if err != nil {
  32. response.Fail("获取失败,Err:"+err.Error(), c)
  33. return
  34. }
  35. response.OkData("获取成功", userInfo, c)
  36. }
  37. // Login 微信登录
  38. // @Tags 微信相关接口
  39. // @Summary 微信登录
  40. // @Description 微信登录
  41. // @Accept json
  42. // @Product json
  43. // @Param code query string true "微信code"
  44. // @Success 200 {object} response.LoginResp
  45. // @Router /wechat/login [get]
  46. func Login(c *gin.Context) {
  47. //code, _ := c.GetQuery("code")
  48. code := c.DefaultQuery("code", "")
  49. wxUserInfo, err := wx_app.GetSession(code)
  50. if err != nil {
  51. response.Fail("获取失败,Err:"+err.Error(), c)
  52. return
  53. }
  54. token, userId, isBind, err := user.WxLogin(wx_app.WxPlatform, wxUserInfo)
  55. if err != nil {
  56. response.Fail("登录失败,Err:"+err.Error(), c)
  57. return
  58. }
  59. response.OkData("获取成功", response2.LoginResp{
  60. UserId: userId,
  61. IsBind: isBind,
  62. Authorization: token,
  63. }, c)
  64. }
  65. // 消息解密请求参数
  66. type EncryptReq struct {
  67. EncryptedData string
  68. Iv string
  69. IsBind bool `json:"isBind" description:"是否需要去授权绑定"`
  70. }
  71. // GetEncryptInfo 消息解密
  72. // @Tags 微信相关接口
  73. // @Summary 消息解密
  74. // @Description 消息解密
  75. // @Accept json
  76. // @Product json
  77. // @Param encryptedData query string true "加密数据"
  78. // @Param iv query string true "加密算法初始向量"
  79. // @Success 200 {string} string "获取成功"
  80. // @Router /wechat/getEncryptInfo [post]
  81. func GetEncryptInfo(c *gin.Context) {
  82. var req EncryptReq
  83. err := c.ShouldBind(&req)
  84. if err != nil {
  85. response.Fail("参数异常", c)
  86. return
  87. }
  88. if req.EncryptedData == "" {
  89. response.Fail("参数异常:encryptedData", c)
  90. return
  91. }
  92. if req.Iv == "" {
  93. response.Fail("参数异常:iv", c)
  94. return
  95. }
  96. // 获取登录用户信息
  97. userInfo := user.GetInfoByClaims(c)
  98. if userInfo.RecordInfo.SessionKey == "" {
  99. response.Fail("请重新登录", c)
  100. return
  101. }
  102. decryptData, err := wx_app.GetDecryptInfo(userInfo.RecordInfo.SessionKey, req.EncryptedData, req.Iv)
  103. if err != nil {
  104. response.Fail("获取失败,Err:"+err.Error(), c)
  105. return
  106. }
  107. if !req.IsBind {
  108. response.OkData("获取成功", decryptData, c)
  109. return
  110. }
  111. //如果需要静默授权绑定,那么就去走静默绑定手机号流程
  112. openId := userInfo.OpenID
  113. //国际区号
  114. countryCode, _ := strconv.Atoi(decryptData.CountryCode)
  115. token, newUserInfo, err, errMsg := userService.BindWxUser(openId, decryptData.PurePhoneNumber, "", "", 3, countryCode, 1)
  116. if err != nil {
  117. if errMsg == "" {
  118. errMsg = "登录失败"
  119. }
  120. response.Fail(errMsg, c)
  121. return
  122. }
  123. response.OkData("登录成功", userResp.LoginResp{
  124. UserID: int(newUserInfo.UserID),
  125. Token: token,
  126. Email: newUserInfo.Email,
  127. Mobile: newUserInfo.Mobile,
  128. }, c)
  129. }
  130. // GetWxJsConf
  131. // @Title 微信获取签名接口
  132. // @Description 微信获取签名接口
  133. // @Param Url query string true "url地址"
  134. // @Success 200 {string} string "获取成功"
  135. // @router /getWxJsConf [get]
  136. func GetWxJsConf(c *gin.Context) {
  137. getUrl := c.DefaultQuery("Url", "")
  138. jsConf, err := wechat.GetJsConfig(getUrl)
  139. if err != nil {
  140. response.Fail("获取失败,Err:"+err.Error(), c)
  141. return
  142. }
  143. //resp := new(models.WechatSign)
  144. //resp.AppId = utils.WxAppId
  145. //resp.NonceStr = nonceString
  146. //resp.Timestamp = timestamp
  147. //resp.Url = getUrl
  148. //resp.Signature = signature
  149. //jsConf.AppID
  150. response.OkData("获取成功", jsConf, c)
  151. }