package member import ( "app-cloudep-portal-api-gateway/internal/domain" "context" "time" 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" "github.com/gogo/protobuf/proto" "app-cloudep-portal-api-gateway/internal/svc" "app-cloudep-portal-api-gateway/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type LoginLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic { return &LoginLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *LoginLogic) Login(req *types.LoginReq) (resp *types.LoginResp, err error) { var result *accountRpc.VerifyAuthResultResp // Step 1 驗證進來的 Token platform := domain.GetPlatformByPlatformCode(req.Platform) switch platform { case domain.PlatformDigimon: // 原始平台驗證 result, err = l.svcCtx.AccountRpc.VerifyPlatformAuthResult(l.ctx, &accountRpc.VerifyAuthResultReq{ Account: proto.String(req.Account), Token: req.Token, }) if err != nil { return nil, err } case domain.PlatformGoogle: result, err = l.svcCtx.AccountRpc.VerifyGoogleAuthResult(l.ctx, &accountRpc.VerifyAuthResultReq{ Token: req.Token, }) if err != nil { return nil, err } case domain.PlatformTwitter: default: return nil, ers.InvalidFormat("invalid platform") } if !result.Status { return nil, ers.Forbidden("failed to validate password ") } account, err := l.svcCtx.AccountRpc.GetUidByAccount(l.ctx, &accountRpc.GetUIDByAccountReq{ Account: req.Account, }) if err != nil { return nil, err } // 發 token token, err := l.generateToken(account.Uid, req.DeviceID) if err != nil { return nil, err } // 建立回應 return &types.LoginResp{ Status: types.Status{ Code: domain.SuccessCode, Message: domain.SuccessMsg, }, Data: types.LoginItem{ UID: account.Uid, AccessToken: token.AccessToken, RefreshToken: token.RefreshToken, TokenType: token.TokenType, }, }, nil } // generateToken 生成 token func (l *LoginLogic) generateToken(uid, deviceID string) (*permissionRpc.TokenResp, error) { return l.svcCtx.TokenRpc.NewToken(l.ctx, &permissionRpc.AuthorizationReq{ GrantType: domain.GrantTypeClientCredentials.ToString(), DeviceId: deviceID, Scope: domain.DefaultScope, IsRefreshToken: true, Expires: int32(time.Now().UTC().Add(l.svcCtx.Config.Token.Expired).Unix()), Data: map[string]string{ "uid": uid, }, }) }