wechat.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. appConf, e := wx_app.GetWxAppConf()
  55. if e != nil {
  56. response.FailMsg("获取失败", "获取小程序配置失败, Err: " + e.Error(), c)
  57. return
  58. }
  59. token, userId, isBind, err := user.WxLogin(appConf.WxPlatform, wxUserInfo)
  60. if err != nil {
  61. response.Fail("登录失败,Err:"+err.Error(), c)
  62. return
  63. }
  64. response.OkData("获取成功", response2.LoginResp{
  65. UserId: userId,
  66. IsBind: isBind,
  67. Authorization: token,
  68. }, c)
  69. }
  70. // 消息解密请求参数
  71. type EncryptReq struct {
  72. EncryptedData string
  73. Iv string
  74. IsBind bool `json:"isBind" description:"是否需要去授权绑定"`
  75. }
  76. // GetEncryptInfo 消息解密
  77. // @Tags 微信相关接口
  78. // @Summary 消息解密
  79. // @Description 消息解密
  80. // @Accept json
  81. // @Product json
  82. // @Param encryptedData query string true "加密数据"
  83. // @Param iv query string true "加密算法初始向量"
  84. // @Success 200 {string} string "获取成功"
  85. // @Router /wechat/getEncryptInfo [post]
  86. func GetEncryptInfo(c *gin.Context) {
  87. var req EncryptReq
  88. err := c.ShouldBind(&req)
  89. if err != nil {
  90. response.Fail("参数异常", c)
  91. return
  92. }
  93. if req.EncryptedData == "" {
  94. response.Fail("参数异常:encryptedData", c)
  95. return
  96. }
  97. if req.Iv == "" {
  98. response.Fail("参数异常:iv", c)
  99. return
  100. }
  101. // 获取登录用户信息
  102. userInfo := user.GetInfoByClaims(c)
  103. if userInfo.RecordInfo.SessionKey == "" {
  104. response.Fail("请重新登录", c)
  105. return
  106. }
  107. decryptData, err := wx_app.GetDecryptInfo(userInfo.RecordInfo.SessionKey, req.EncryptedData, req.Iv)
  108. if err != nil {
  109. response.Fail("获取失败,Err:"+err.Error(), c)
  110. return
  111. }
  112. if !req.IsBind {
  113. response.OkData("获取成功", decryptData, c)
  114. return
  115. }
  116. //如果需要静默授权绑定,那么就去走静默绑定手机号流程
  117. openId := userInfo.OpenID
  118. //国际区号
  119. countryCode, _ := strconv.Atoi(decryptData.CountryCode)
  120. token, newUserInfo, err, errMsg := userService.BindWxUser(openId, decryptData.PurePhoneNumber, "", "", 3, countryCode, 1)
  121. if err != nil {
  122. if errMsg == "" {
  123. errMsg = "登录失败"
  124. }
  125. response.Fail(errMsg, c)
  126. return
  127. }
  128. response.OkData("登录成功", userResp.LoginResp{
  129. UserID: int(newUserInfo.UserID),
  130. Token: token,
  131. Email: newUserInfo.Email,
  132. Mobile: newUserInfo.Mobile,
  133. }, c)
  134. }
  135. // GetWxJsConf
  136. // @Title 微信获取签名接口
  137. // @Description 微信获取签名接口
  138. // @Param Url query string true "url地址"
  139. // @Success 200 {string} string "获取成功"
  140. // @router /getWxJsConf [get]
  141. func GetWxJsConf(c *gin.Context) {
  142. getUrl := c.DefaultQuery("Url", "")
  143. jsConf, err := wechat.GetJsConfig(getUrl)
  144. if err != nil {
  145. response.Fail("获取失败,Err:"+err.Error(), c)
  146. return
  147. }
  148. //resp := new(models.WechatSign)
  149. //resp.AppId = utils.WxAppId
  150. //resp.NonceStr = nonceString
  151. //resp.Timestamp = timestamp
  152. //resp.Url = getUrl
  153. //resp.Signature = signature
  154. //jsConf.AppID
  155. response.OkData("获取成功", jsConf, c)
  156. }