Пример #1
0
        public async Task <OperateResult> LoginToSystem <T>(T loginIno, CancellationToken cancellationToken = default)
        {
            var loginModel = loginIno as XiaoMiLoginModel;

            if (loginModel == null)
            {
                return(OperateResult.Failed(null, string.Empty, "当前登录信息转换失败"));
            }

            var existUser = await _userRepo.FindByUserKeyInfoAsync(loginModel.UserPhone, StepFlyProviderType.XiaoMi);

            OperateResult operateResult = await Login(loginModel, existUser, cancellationToken);

            if (!operateResult.Succeeded)
            {
                return(operateResult);
            }

            var result = operateResult.Information.PlayLoad !as XiaoMiLoginAPISuccessModel
                         ?? throw new SoftlyMiCakeException($"转换{nameof(XiaoMiLoginAPISuccessModel)}返回结果出错");

            //保存当前用户的信息
            if (!result.AlreadyHasUser)
            {
                var user = StepFlyUser.Create(loginModel.UserPhone, loginModel.Password, result.UserId);
                user.SetProvider(StepFlyProviderType.XiaoMi);
                user.SetToken(result.Token, DateTime.Now.AddDays(invalidHours));
                user.SetClientInfo(null, result.DeviceId);
                user.SetAdditionalInfo(result.LoginToken);

                await _userRepo.AddAsync(user);

                result.User = user;
            }
            else
            {
                existUser.SetToken(result.Token, DateTime.Now.AddDays(invalidHours));
                existUser.SetAdditionalInfo(result.LoginToken);
                existUser.SetPassword(loginModel.Password);
                //保证设备ID不为空
                existUser.SetClientInfo(existUser.UserClientInfo, existUser.DeviceId);
                result.User = existUser;
            }

            return(OperateResult.Success(HttpStatusCode.OK.ToString(), "登录成功", result));
        }
Пример #2
0
        public async Task <OperateResult> LoginToSystem <T>(T loginIno, CancellationToken cancellationToken = default)
        {
            var loginModel = loginIno as LeXinLoginModel;

            if (loginModel == null)
            {
                return(OperateResult.Failed(null, string.Empty, "当前登录信息转换失败"));
            }

            var existUser = await _userRepo.FindByUserKeyInfoAsync(loginModel.UserKeyInfo);

            OperateResult operateResult = null;

            if (loginModel.Type == LeXinLoginType.AuthCode)
            {
                operateResult = await LoginWithAuthCode(loginModel.AuthCodeLoginInfo, existUser, cancellationToken);
            }
            else if (loginModel.Type == LeXinLoginType.Password)
            {
                operateResult = await LoginWithPassword(loginModel.PasswordLoginInfo, existUser, cancellationToken);
            }

            if (operateResult != null)
            {
                if (!operateResult.Succeeded)
                {
                    return(operateResult);
                }

                var result = operateResult.Information.PlayLoad !as LeXinLoginAPISuccessModel
                             ?? throw new SoftlyMiCakeException($"转换{nameof(LeXinLoginAPISuccessModel)}返回结果出错");

                var userLoginInfo = JsonSerializer.Deserialize <LeXinUserLoginSuccessModel>(result.APIResponseData, new JsonSerializerOptions()
                {
                    PropertyNameCaseInsensitive = true
                });
                //保存当前用户的信息
                if (!result.AlreadyHasUser)
                {
                    var user = StepFlyUser.Create(loginModel.UserKeyInfo, loginModel.PasswordLoginInfo?.Password ?? "", userLoginInfo.UserId);
                    user.SetToken(result.Cookie, DateTimeHelper.ConvertStampToDateTime(userLoginInfo.ExpireAt, TimeStampType.ThirteenLength));
                    user.SetClientInfo(result.ClientInfo, null);

                    await _userRepo.AddAsync(user);

                    result.User = user;
                }
                else
                {
                    existUser.SetToken(result.Cookie, DateTimeHelper.ConvertStampToDateTime(userLoginInfo.ExpireAt, TimeStampType.ThirteenLength));
                    existUser.SetPassword(loginModel.PasswordLoginInfo?.Password);
                    //保证设备ID不为空
                    existUser.SetClientInfo(existUser.UserClientInfo, existUser.DeviceId);
                    result.User = existUser;
                }


                return(OperateResult.Success(HttpStatusCode.OK.ToString(), "登录成功", result));
            }
            else
            {
                return(OperateResult.Failed(null, "E0", "未找到对应的登录方式"));
            }
        }