package member import ( "app-cloudep-portal-api-gateway/internal/domain" "context" "fmt" ers "code.30cm.net/digimon/library-go/errors" accountRpc "code.30cm.net/digimon/proto-all/pkg/member" permissionRpc "code.30cm.net/digimon/proto-all/pkg/permission" "app-cloudep-portal-api-gateway/internal/svc" "app-cloudep-portal-api-gateway/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type UpadtePasswordLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewUpadtePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpadtePasswordLogic { return &UpadtePasswordLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *UpadtePasswordLogic) UpadtePassword(req *types.UpdatePasswordReq) (resp *types.BaseResponse, err error) { // 驗證密碼,兩次密碼要一致 if req.Token != req.TokenCheck { return nil, ers.InvalidFormat("password confirmation does not match") } // 驗證碼 _, err = l.svcCtx.AccountRpc.VerifyRefreshCode(l.ctx, &accountRpc.VerifyRefreshCodeReq{ Account: req.Account, CodeType: domain.SendVerifyCodeTypeForgetPassword, VerifyCode: req.VerifyCode, }) if err != nil { // 表使沒有這驗證碼 return nil, ers.Forbidden("failed to get verify code") } info, err := l.svcCtx.AccountRpc.GetUserAccountInfo(l.ctx, &accountRpc.GetUIDByAccountReq{Account: req.Account}) if err != nil { return nil, err } if info.Data.Platform != domain.PlatformDigimon.ToInt64() { return nil, ers.Forbidden("invalid platform") } // 更新 _, err = l.svcCtx.AccountRpc.UpdateUserToken(l.ctx, &accountRpc.UpdateTokenReq{ Account: req.Account, Token: req.Token, Platform: int64(domain.PlatformDigimon), }) if err != nil { return nil, err } // 刪除 key rk := domain.GenerateVerifyCodeRedisKey.With(fmt.Sprintf("%s-%d", req.Account, domain.SendVerifyCodeTypeForgetPassword)).ToString() _, err = l.svcCtx.Redis.Del(rk) if err != nil { return nil, ers.ArkInternal(err.Error()) } // 取消所有Token ac, err := l.svcCtx.AccountRpc.GetUidByAccount(l.ctx, &accountRpc.GetUIDByAccountReq{Account: req.Account}) if err != nil { return nil, err } _, err = l.svcCtx.TokenRpc.CancelTokens(l.ctx, &permissionRpc.DoTokenByUIDReq{Uid: ac.Uid}) if err != nil { return nil, err } // 返回成功響應 return &types.BaseResponse{ Status: types.Status{ Code: domain.SuccessCode, Message: domain.SuccessMsg, }, }, nil }