service.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package authsrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/codex"
  4. "Cold_Logistic/internal/pkg/common/constant"
  5. "Cold_Logistic/internal/pkg/common/global"
  6. "Cold_Logistic/internal/server/domain/domainservice"
  7. "Cold_Logistic/internal/server/infra/dao"
  8. "Cold_Logistic/internal/server/infra/models"
  9. "Cold_Logistic/internal/server/infra/thirdparty/internalservice/clod"
  10. "context"
  11. "fmt"
  12. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  13. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/authutil"
  14. "time"
  15. )
  16. type AuthService struct {
  17. store *dao.DataStore
  18. }
  19. func NewAuthService(store *dao.DataStore) *AuthService {
  20. return &AuthService{store: store}
  21. }
  22. const limitTime = 7 * 24 * time.Hour
  23. // UserLogin 普通用户登录
  24. func (srv *AuthService) UserLogin(ctx context.Context, req LoginReqVo) (res LoginRespVo, err error) {
  25. token := ""
  26. exp := time.Now().Add(limitTime).Unix()
  27. account := models.Account{}
  28. err = srv.store.InTx(ctx, func(tx context.Context) error {
  29. account, err = srv.store.Account().FindByOpenid(tx, req.Openid, req.LoginType)
  30. if err != nil {
  31. return errors.WithStackOnce(err)
  32. }
  33. if account.Id == 0 {
  34. account.Openid = req.Openid
  35. account.AccountType = req.LoginType
  36. account.FirstLogin = constant.YES
  37. if err = srv.store.Account().Create(tx, &account); err != nil {
  38. return errors.WithStackOnce(err)
  39. }
  40. }
  41. // 签发token
  42. token = authutil.Sign(account.Id, "", constant.CCLAppletLoginJWTISS, "", exp)
  43. if _, err = domainservice.SetTokenAndInfo(ctx, account, token, limitTime); err != nil {
  44. return errors.WithStackOnce(err)
  45. }
  46. return nil
  47. })
  48. if err != nil {
  49. return res, errors.WithStackOnce(err)
  50. }
  51. res.AccessToken = token
  52. res.TokenType = constant.AuthTokenTypeBearer
  53. res.ExpiresIn = exp
  54. res.IsFirst = account.FirstLogin
  55. return res, nil
  56. }
  57. // PlatformLogin 平台账号登录
  58. func (srv *AuthService) PlatformLogin(ctx context.Context, req LoginReqVo) (res LoginRespVo, err error) {
  59. ok, pid, user, err := clod.NewBzdClodService().LoginVerification(ctx, req.TokenKey)
  60. if !ok || err != nil {
  61. return res, errors.WithCode(codex.ErrSignatureInvalid, "验证失败,请重新登陆")
  62. }
  63. token := ""
  64. exp := time.Now().Add(limitTime).Unix()
  65. account := models.Account{}
  66. err = srv.store.InTx(ctx, func(tx context.Context) error {
  67. account, err = srv.store.Account().FindByUuid(ctx, user.T_uuid)
  68. if err != nil {
  69. return errors.WithStackOnce(err)
  70. }
  71. account.Name = user.T_name
  72. account.Phone = user.T_phone
  73. account.PowerId = user.T_power
  74. account.UsePid = user.T_pid
  75. if pid > 0 {
  76. account.UsePid = pid
  77. }
  78. if account.Id == 0 {
  79. account.Uuid = user.T_uuid
  80. account.AccountType = constant.AccountPlatform
  81. account.FirstLogin = constant.NO
  82. account.Pid = user.T_pid
  83. if err = srv.store.Account().Create(ctx, &account); err != nil {
  84. return errors.WithStackOnce(err)
  85. }
  86. }
  87. if err = srv.store.Account().UpdateById(tx, &account); err != nil {
  88. return errors.WithStackOnce(err)
  89. }
  90. // 签发token
  91. token = authutil.Sign(account.Id, "", constant.CCLAppletLoginJWTISS, "", exp)
  92. if _, err = domainservice.SetTokenAndInfo(ctx, account, token, limitTime); err != nil {
  93. return errors.WithStackOnce(err)
  94. }
  95. return nil
  96. })
  97. if err != nil {
  98. return res, errors.WithStackOnce(err)
  99. }
  100. res.AccessToken = token
  101. res.TokenType = constant.AuthTokenTypeBearer
  102. res.ExpiresIn = exp
  103. return res, nil
  104. }
  105. func (srv *AuthService) LoginOut(ctx context.Context) error {
  106. tokenInfo := global.GetTokenInfoFromContext(ctx)
  107. _, err := global.CommonConnectRepoInst.Redis.
  108. HDel(ctx, fmt.Sprintf("acc_%d", tokenInfo.AccountId)).Result()
  109. return errors.WithStackOnce(err)
  110. }