auth.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package pub
  2. import (
  3. "app/apis/middleware"
  4. "app/commons/constant"
  5. "app/commons/core"
  6. "app/commons/core/exchange"
  7. "app/commons/model/entity"
  8. "fmt"
  9. "github.com/gin-gonic/gin"
  10. "time"
  11. )
  12. // 授权登录
  13. func (s *Server) AuthInfo(ctx *gin.Context) {
  14. c := s.FromContext(ctx)
  15. redirectUri := c.Query("redirectUri") // 当前端传入调整地址 授权成功后需跳转地址
  16. type AuthInfoResp struct {
  17. DefaultDomainAuthUrl string `json:"defaultDomainAuthUrl"` // 含默认主域名授权地址
  18. NoDomainAuthUrl string `json:"noDomainAuthUrl"` // 无主域名授权地址 -- 授权路径 用于正式环境下前端获取主域名后 拼接用
  19. DefaultDomain string `json:"defaultDomain"` // 默认主域名
  20. IsLogin bool `json:"isLogin"` // 用户当前是否登陆
  21. }
  22. resp := new(AuthInfoResp)
  23. resp.IsLogin = c.OpenId() != ""
  24. resp.DefaultDomainAuthUrl = exchange.AuthUrl(redirectUri)
  25. resp.DefaultDomain = exchange.AuthRoot()
  26. resp.NoDomainAuthUrl = exchange.NoDomainAuthUrl(redirectUri)
  27. c.Resp(resp)
  28. }
  29. func (s *Server) Auth(ctx *gin.Context) {
  30. c := s.FromContext(ctx)
  31. type request struct {
  32. Code string `json:"code"`
  33. }
  34. req := new(request)
  35. if err := c.BindJSON(&req); err != nil {
  36. c.Fail(err.Error())
  37. return
  38. }
  39. // code 获取用户信息 openId
  40. loginInfo, err := exchange.AccessToken(req.Code)
  41. if err != nil {
  42. core.JobLog.Infof("兑换交易所TOKEN失败:%s", err.Error())
  43. c.Fail(constant.ErrorAuthorizationFail)
  44. return
  45. }
  46. core.JobLog.Infof("兑换交易所TOKEN:%+v req:%+v", loginInfo, req)
  47. if loginInfo.Code != 200 {
  48. core.JobLog.Infof("兑换交易所TOKEN失败:%+v req:%+v", loginInfo, req)
  49. c.Fail(constant.ErrorAuthorizationFail)
  50. return
  51. }
  52. core.Log.Infof("用户授权登录:%+v", loginInfo.Data)
  53. strUserId := fmt.Sprintf("%d", loginInfo.Data.UserId)
  54. user, err := s.CheckUserWithOpenIdAndUid(loginInfo.Data.OpenId, strUserId, true)
  55. if err != nil {
  56. core.Log.Infof("用户授权登录:%+v", err.Error())
  57. c.Resp(err.Error())
  58. return
  59. }
  60. err = s.DB().Model(&entity.User{}).
  61. Where("id", user.Id).Updates(map[string]interface{}{
  62. "last_login_time": time.Now().Unix(),
  63. "last_login_ip": c.ClientIP(),
  64. "last_login_remote_ip": c.RemoteIP(),
  65. }).Error
  66. if err != nil {
  67. core.Log.Infof("ERROR:记录用户登陆信息失败:%+v", err.Error())
  68. }
  69. type LoginResp struct {
  70. Uid string `json:"uid"`
  71. OpenId string `json:"openId"`
  72. Code string `json:"code"`
  73. Authorization string `json:"authorization"`
  74. }
  75. resp := new(LoginResp)
  76. resp.Uid = strUserId
  77. resp.OpenId = user.OpenId
  78. resp.Code = user.Code
  79. resp.Authorization, err = s.getTokenWithUser(user)
  80. if err != nil {
  81. c.Resp(err.Error())
  82. return
  83. }
  84. c.Resp(resp)
  85. }
  86. // 通过用户信息获取TOKEN
  87. func (s *Server) getTokenWithUser(user *entity.User) (string, error) {
  88. member := middleware.Member{
  89. ID: user.Id,
  90. OpenId: user.OpenId,
  91. Uid: user.Uid,
  92. }
  93. token, err := middleware.GenerateJWT(member)
  94. if err != nil {
  95. return "", err
  96. }
  97. // 将新token设置为用户当前有效token -- redis 单点登录限制
  98. if err := middleware.SetUserCurrentToken(user.Id, token, 24*time.Hour); err != nil {
  99. core.Log.Errorf("设置用户当前token失败:%+v", err.Error())
  100. }
  101. return token, nil
  102. }