package myselfsrv import ( "Cold_Logistic/internal/pkg/common/constant" "Cold_Logistic/internal/pkg/common/global" "Cold_Logistic/internal/server/domain/domainservice" "Cold_Logistic/internal/server/infra/dao" "Cold_Logistic/internal/server/infra/models" "context" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/errors" "gogs.baozhida.cn/Cold_Logistic_libs/pkg/contrib/util/authutil" "time" ) type MyselfService struct { store *dao.DataStore } func NewMyselfService(store *dao.DataStore) *MyselfService { return &MyselfService{store: store} } // MyselfInfo 个人信息 func (srv *MyselfService) MyselfInfo(ctx context.Context) (res global.AccountInfoVo, err error) { accountInfo := global.GetTokenInfoFromContext(ctx) res.AccountId = accountInfo.AccountId res.AccountType = accountInfo.AccountType res.AccountUuid = accountInfo.AccountUuid res.Openid = accountInfo.Openid res.Name = accountInfo.Name res.Gender = accountInfo.Gender res.Phone = accountInfo.Phone res.CompanyName = accountInfo.CompanyName res.IsFirst = accountInfo.IsFirst res.Role = accountInfo.Role return res, nil } // UpdateMyselfInfo 修改个人信息 func (srv *MyselfService) UpdateMyselfInfo(ctx context.Context, req UpdateMyselfInfoReqVO) (res global.AccountInfoVo, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) account := models.Account{} err = srv.store.Account().FirstById(ctx, &account, tokenInfo.AccountId) if err != nil { return res, errors.WithStackOnce(err) } token, err := domainservice.GetToken(ctx, account.Id) if err != nil { return res, errors.WithStackOnce(err) } account.Name = req.Name account.Gender = req.Gender account.Phone = req.Phone account.CompanyName = req.CompanyName account.FirstLogin = constant.NO err = srv.store.InTx(ctx, func(ctx context.Context) error { if err = srv.store.Account().Save(ctx, &account); err != nil { return errors.WithStackOnce(err) } if _, err = domainservice.SetTokenAndInfo(ctx, account, token, constant.DefaultTokenLimit); err != nil { return errors.WithStackOnce(err) } return nil }) if err != nil { return res, errors.WithStackOnce(err) } res.AccountId = account.Id res.AccountUuid = account.Uuid res.AccountType = account.AccountType res.Openid = account.Openid res.Name = account.Name res.Gender = account.Gender res.Phone = account.Phone res.CompanyName = account.CompanyName res.IsFirst = account.FirstLogin return res, nil } // RefreshToken 刷新token func (srv *MyselfService) RefreshToken(ctx context.Context) (res RefreshTokenRespVo, err error) { tokenInfo := global.GetTokenInfoFromContext(ctx) exp := time.Now().Add(constant.DefaultTokenLimit).Unix() // 签发token token := authutil.Sign(tokenInfo.AccountId, "", constant.CCLAppletLoginJWTISS, "", exp) err = domainservice.UpdateToken(ctx, tokenInfo.AccountId, token, constant.DefaultTokenLimit) if err != nil { return res, errors.WithStackOnce(err) } res.TokenType = constant.AuthTokenTypeBearer res.AccessToken = token res.ExpiresIn = exp return res, nil }