wechat.go 4.6 KB

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