基于P2P万信金融--day05 万信金融项搭建用户中心微服务项目

    科技2026-01-10  12

    5 用户登录

    5.1 传统实现方式

    前面两个用户中心微服务和账户微服务基本上搭建完了,其中主要的功能就是登录和注册,网关微服务和注册中心微服务则是直接进行的导入操作进行的没有进行过多的操作,这个而账户和用户中心微服务则主要进行api接口调用,在定义mapper,在从service进行调用的,期间基本的过程基本一致;

    注册功能搞定后,紧接着就该实现登录功能了。这个功能对于大家来说应该是很熟悉的,业务熟悉,代 码也熟悉,传统实现方式的思路:查询数据库,确定账号和密码是否存在(正确)。

    1. 接口定义 在AccountAPI接口中定义登录方法login:

    /** * 用户登录 * @param accountLoginDTO 封装登录请求数据 * @return */ RestResponse<AccountDTO> login(AccountLoginDTO accountLoginDTO);

    在AccountController中实现login方法:

    @ApiOperation("用户登录") @ApiImplicitParam(name = "accountLoginDTO", value = "登录信息", required = true, dataType = "AccountLoginDTO", paramType = "body") @PostMapping(value = "/l/accounts/session") @Override public RestResponse<AccountDTO> login(@RequestBody AccountLoginDTO accountLoginDTO) { return null; }

    2. 功能实现 在AccountService中新增登录接口login:

    /** 登录功能 @param accountLoginDTO 封装登录请求数据 @return 用户及权限信息 */ AccountDTO login(AccountLoginDTO accountLoginDTO);

    在AccountServiceImpl类中实现login方法:

    @Override public AccountDTO login(AccountLoginDTO accountLoginDTO) { Account account = null; if (accountLoginDTO.getDomain().equalsIgnoreCase("c")) { account = getAccountByMobile(accountLoginDTO.getMobile());//获取c端用户 } else { account = getAccountByUsername(accountLoginDTO.getUsername());//获取b端 用户 } if (account == null) { } AccountDTO accountDTO = convertAccountEntityToDTO(account); if (smsEnable) {// 如果smsEnable=true,说明是短信验证码登录,不做密码校验 return accountDTO; } //验证密码 if (PasswordUtil.verify(accountLoginDTO.getPassword(), account.getPassword())) { return accountDTO; } throw new BusinessException(AccountErrorCode.E_130105); } /** 根据手机获取账户信息 @param mobile 手机号 @return 账户实体 */ private Account getAccountByMobile(String mobile) { return getOne(new QueryWrapper<Account>().lambda() .eq(Account::getMobile, mobile)); } /** 根据用户名获取账户信息 @param username 用户名 @return 账户实体 */ private Account getAccountByUsername(String username) { return getOne(new QueryWrapper<Account>().lambda() .eq(Account::getUsername, username)); } /** entity转为dto @param entity对象 @return dto对象 */ private AccountDTO convertAccountEntityToDTO(Account entity) { if (entity == null) { return null; } AccountDTO dto = new AccountDTO(); BeanUtils.copyProperties(entity, dto); return dto; }

    3. 完善AccountController代码,调用AccountService完成登录功能:

    @ApiOperation("用户登录") @ApiImplicitParam(name = "accountLoginDTO", value = "登录信息", required = true, dataType = "AccountLoginDTO", paramType = "body") @PostMapping(value = "/l/accounts/session") @Override public RestResponse<AccountDTO> login(@RequestBody AccountLoginDTO accountLoginDTO) { return RestResponse.success(accountService.login(accountLoginDTO)); }

     

    Processed: 0.018, SQL: 9