93 lines
2.5 KiB
Go
93 lines
2.5 KiB
Go
package member
|
|
|
|
import (
|
|
"biz-member-gateway/internal/domain"
|
|
"context"
|
|
"fmt"
|
|
|
|
"code.30cm.net/digimon/app-cloudep-member-server/pkg/domain/member"
|
|
ers "code.30cm.net/digimon/library-go/errors"
|
|
"code.30cm.net/digimon/library-go/errs"
|
|
memberProto "code.30cm.net/digimon/proto-all/pkg/member"
|
|
permissionProto "code.30cm.net/digimon/proto-all/pkg/permission"
|
|
|
|
"biz-member-gateway/internal/svc"
|
|
"biz-member-gateway/internal/types"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UpdatePasswordLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// NewUpdatePasswordLogic 更新密碼(要發送驗證碼才可以的流程)
|
|
func NewUpdatePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatePasswordLogic {
|
|
return &UpdatePasswordLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UpdatePasswordLogic) UpdatePassword(req *types.UpdatePasswordReq) (*types.RespOK, error) {
|
|
// 驗證密碼,兩次密碼要一致
|
|
if req.Token != req.TokenCheck {
|
|
return nil, errs.InvalidFormat("password confirmation does not match")
|
|
}
|
|
|
|
// 驗證碼
|
|
_, err := l.svcCtx.MemberRPC.VerifyRefreshCode(l.ctx, &memberProto.VerifyRefreshCodeReq{
|
|
Account: req.Account,
|
|
CodeType: int32(member.GenerateCodeTypeForgetPassword),
|
|
VerifyCode: req.VerifyCode,
|
|
})
|
|
|
|
if err != nil {
|
|
// 表使沒有這驗證碼 -> 未授權
|
|
return nil, errs.Unauthorized("failed to get verify code")
|
|
}
|
|
|
|
info, err := l.svcCtx.MemberRPC.GetUserAccountInfo(l.ctx, &memberProto.GetUIDByAccountReq{Account: req.Account})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if info.Data.Platform != member.Digimon.ToInt64() {
|
|
return nil, ers.InvalidRange("invalid platform")
|
|
}
|
|
|
|
// 更新
|
|
_, err = l.svcCtx.MemberRPC.UpdateUserToken(l.ctx, &memberProto.UpdateTokenReq{
|
|
Account: req.Account,
|
|
Token: req.Token,
|
|
Platform: member.Digimon.ToInt64(),
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 刪除驗證碼
|
|
rk := domain.GetSendCodeRedisKey(fmt.Sprintf("%s:%s", req.Account, member.GenerateCodeTypeForgetPassword))
|
|
_, err = l.svcCtx.Redis.Del(rk)
|
|
if err != nil {
|
|
return nil, ers.DBError("failed to get account data")
|
|
}
|
|
|
|
// 登出舊 Token 如果有的話
|
|
ac, err := l.svcCtx.MemberRPC.GetUIDByAccount(l.ctx, &memberProto.GetUIDByAccountReq{Account: req.Account})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
_, err = l.svcCtx.TokenRPC.CancelTokens(l.ctx, &permissionProto.DoTokenByUIDReq{Uid: ac.GetUid()})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// 返回成功響應
|
|
return &types.RespOK{}, nil
|
|
}
|