account_service.go 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package myselfsrv
  2. import (
  3. "Cold_Logistic/internal/pkg/common/constant"
  4. "Cold_Logistic/internal/pkg/common/global"
  5. "Cold_Logistic/internal/server/domain/domainservice"
  6. "Cold_Logistic/internal/server/infra/dao"
  7. "Cold_Logistic/internal/server/infra/models"
  8. "context"
  9. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors"
  10. "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/authutil"
  11. "time"
  12. )
  13. type MyselfService struct {
  14. store *dao.DataStore
  15. }
  16. func NewMyselfService(store *dao.DataStore) *MyselfService {
  17. return &MyselfService{store: store}
  18. }
  19. // MyselfInfo 个人信息
  20. func (srv *MyselfService) MyselfInfo(ctx context.Context) (res global.AccountInfoVo, err error) {
  21. accountInfo := global.GetTokenInfoFromContext(ctx)
  22. res.AccountId = accountInfo.AccountId
  23. res.AccountType = accountInfo.AccountType
  24. res.AccountUuid = accountInfo.AccountUuid
  25. res.Openid = accountInfo.Openid
  26. res.Name = accountInfo.Name
  27. res.Gender = accountInfo.Gender
  28. res.Phone = accountInfo.Phone
  29. res.CompanyName = accountInfo.CompanyName
  30. res.IsFirst = accountInfo.IsFirst
  31. res.Role = accountInfo.Role
  32. return res, nil
  33. }
  34. // UpdateMyselfInfo 修改个人信息
  35. func (srv *MyselfService) UpdateMyselfInfo(ctx context.Context, req UpdateMyselfInfoReqVO) (res global.AccountInfoVo, err error) {
  36. tokenInfo := global.GetTokenInfoFromContext(ctx)
  37. account := models.Account{}
  38. err = srv.store.Account().FirstById(ctx, &account, tokenInfo.AccountId)
  39. if err != nil {
  40. return res, errors.WithStackOnce(err)
  41. }
  42. token, err := domainservice.GetToken(ctx, account.Id)
  43. if err != nil {
  44. return res, errors.WithStackOnce(err)
  45. }
  46. account.Name = req.Name
  47. account.Gender = req.Gender
  48. account.Phone = req.Phone
  49. account.CompanyName = req.CompanyName
  50. account.FirstLogin = constant.NO
  51. err = srv.store.InTx(ctx, func(ctx context.Context) error {
  52. if err = srv.store.Account().Save(ctx, &account); err != nil {
  53. return errors.WithStackOnce(err)
  54. }
  55. if _, err = domainservice.SetTokenAndInfo(ctx, account, token, constant.DefaultTokenLimit); err != nil {
  56. return errors.WithStackOnce(err)
  57. }
  58. return nil
  59. })
  60. if err != nil {
  61. return res, errors.WithStackOnce(err)
  62. }
  63. res.AccountId = account.Id
  64. res.AccountUuid = account.Uuid
  65. res.AccountType = account.AccountType
  66. res.Openid = account.Openid
  67. res.Name = account.Name
  68. res.Gender = account.Gender
  69. res.Phone = account.Phone
  70. res.CompanyName = account.CompanyName
  71. res.IsFirst = account.FirstLogin
  72. return res, nil
  73. }
  74. // RefreshToken 刷新token
  75. func (srv *MyselfService) RefreshToken(ctx context.Context) (res RefreshTokenRespVo, err error) {
  76. tokenInfo := global.GetTokenInfoFromContext(ctx)
  77. exp := time.Now().Add(constant.DefaultTokenLimit).Unix()
  78. // 签发token
  79. token := authutil.Sign(tokenInfo.AccountId, "", constant.CCLAppletLoginJWTISS, "", exp)
  80. err = domainservice.UpdateToken(ctx, tokenInfo.AccountId, token, constant.DefaultTokenLimit)
  81. if err != nil {
  82. return res, errors.WithStackOnce(err)
  83. }
  84. res.TokenType = constant.AuthTokenTypeBearer
  85. res.AccessToken = token
  86. res.ExpiresIn = exp
  87. return res, nil
  88. }