loginbyweixinlogic.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package logic
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. "git.i2edu.net/i2/go-zero/core/logx"
  7. "git.i2edu.net/i2/go-zero/core/stores/sqlc"
  8. "git.i2edu.net/i2/go-zero/core/stores/sqlx"
  9. "git.i2edu.net/i2/i2-bill-api/internal/svc"
  10. "git.i2edu.net/i2/i2-bill-api/internal/types"
  11. "git.i2edu.net/i2/i2-bill-api/internal/utils"
  12. "git.i2edu.net/i2/i2-bill-api/model"
  13. "github.com/dgrijalva/jwt-go"
  14. )
  15. type LoginByWeixinLogic struct {
  16. logx.Logger
  17. ctx context.Context
  18. svcCtx *svc.ServiceContext
  19. }
  20. func NewLoginByWeixinLogic(ctx context.Context, svcCtx *svc.ServiceContext) LoginByWeixinLogic {
  21. return LoginByWeixinLogic{
  22. Logger: logx.WithContext(ctx),
  23. ctx: ctx,
  24. svcCtx: svcCtx,
  25. }
  26. }
  27. func (l *LoginByWeixinLogic) LoginByWeixin(req types.AuthLoginBody) (*types.AuthResponse, error) {
  28. alb, rtnInfo := req, types.AuthResponse{}
  29. userInfo, err := l.Login(alb.Code)
  30. if err != nil {
  31. return nil, err
  32. }
  33. err = l.svcCtx.SqlConn.Transact(func(session sqlx.Session) error {
  34. var user model.User
  35. err := session.QueryRow(&user, fmt.Sprintf("select %s from user where `weixin_openid` = ? limit 1", model.UserRows), userInfo.OpenID)
  36. if err == sqlc.ErrNotFound {
  37. user.Username = utils.GetUUID()
  38. user.Password = ""
  39. user.RegisterTime = utils.GetTimestamp()
  40. user.RegisterIp = ""
  41. user.Mobile = ""
  42. user.WeixinOpenid = userInfo.OpenID
  43. user.Avatar = userInfo.AvatarUrl
  44. user.Gender = userInfo.Gender
  45. user.Nickname = userInfo.NickName
  46. _, err = session.Exec(fmt.Sprintf("insert into user (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", model.UserRowsExpectAutoSet), user.Mobile, user.Avatar, user.WeixinOpenid, user.Password, user.Birthday, user.RegisterTime, user.LastLoginTime, user.Nickname, user.Username, user.Gender, user.UserLevelId, user.RegisterIp, user.LastLoginIp)
  47. if err != nil {
  48. return err
  49. }
  50. err = session.QueryRow(&user, fmt.Sprintf("select %s from user where `weixin_openid` = ? limit 1", model.UserRows), userInfo.OpenID)
  51. if err != nil {
  52. return err
  53. }
  54. }
  55. rtnInfo.UserInfo.ID = user.Id
  56. rtnInfo.UserInfo.UserName = user.Username
  57. rtnInfo.UserInfo.NickName = user.Nickname
  58. rtnInfo.UserInfo.Mobile = user.Mobile
  59. rtnInfo.UserInfo.Gender = user.Gender
  60. rtnInfo.UserInfo.Avatar = user.Avatar
  61. rtnInfo.UserInfo.Birthday = user.Birthday
  62. user.LastLoginIp = ""
  63. user.LastLoginTime = utils.GetTimestamp()
  64. _, err = session.Exec(fmt.Sprintf("update user set %s where `id` = ?", model.UserRowsWithPlaceHolder), user.Mobile, user.Avatar, user.WeixinOpenid, user.Password, user.Birthday, user.RegisterTime, user.LastLoginTime, user.Nickname, user.Username, user.Gender, user.UserLevelId, user.RegisterIp, user.LastLoginIp, user.Id)
  65. if err != nil {
  66. return err
  67. }
  68. return nil
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. var accessExpire = l.svcCtx.Config.JwtAuth.AccessExpire
  74. now := time.Now().Unix()
  75. payloads := map[string]interface{}{
  76. "user_id": rtnInfo.UserInfo.ID,
  77. }
  78. accessToken, err := l.CreateJWT(now, l.svcCtx.Config.JwtAuth.AccessSecret, payloads, accessExpire)
  79. if err != nil {
  80. return nil, err
  81. }
  82. rtnInfo.Token = accessToken
  83. return &rtnInfo, nil
  84. }
  85. func (l *LoginByWeixinLogic) CreateJWT(iat int64, secretKey string, payloads map[string]interface{}, seconds int64) (string, error) {
  86. claims := make(jwt.MapClaims)
  87. claims["exp"] = iat + seconds
  88. claims["iat"] = iat
  89. for k, v := range payloads {
  90. claims[k] = v
  91. }
  92. token := jwt.New(jwt.SigningMethodHS256)
  93. token.Claims = claims
  94. return token.SignedString([]byte(secretKey))
  95. }
  96. func (l *LoginByWeixinLogic) Login(code string) (*types.WXUserInfo, error) {
  97. secret := l.svcCtx.Config.Weixin.Secret
  98. appid := l.svcCtx.Config.Weixin.Appid
  99. req := utils.Get("https://api.weixin.qq.com/sns/jscode2session")
  100. req.Param("grant_type", "authorization_code")
  101. req.Param("js_code", code)
  102. req.Param("secret", secret)
  103. req.Param("appid", appid)
  104. var res types.WXLoginResponse
  105. req.ToJSON(&res)
  106. userinfo, err := l.DecryptUserInfoData(res.SessionKey)
  107. userinfo.OpenID = res.OpenID
  108. return userinfo, err
  109. }
  110. func (l *LoginByWeixinLogic) DecryptUserInfoData(sessionKey string) (*types.WXUserInfo, error) {
  111. var wxuserinfo types.WXUserInfo
  112. return &wxuserinfo, nil
  113. }