手机版语雀测试
一、思路分析
登录逻辑是一个项目必不可少的功能,这个功能可以写的很简单,也可以很复杂,接下来以一个单体应用的项目分析
基本的登录逻辑流程图如下
- 前端填写表单,表单校验,发送登录网络请求
- 后端接受前端的数据
- 后端校验数据合法性
- 后端查询数据库,看数据是否存在
- 后端校验密码合法性
- 业务逻辑处理,生成 token,timestamp 等
- 登录成功

二、代码实现
public Result<Object> login(LoginDto dto) {if (dto == null || StringUtils.isNullOrEmpty(dto.getUsername()) || StringUtils.isNullOrEmpty(dto.getPassword())) {return new Result<>(ResultEnums.PARAMS_NULL.getCode(),ResultEnums.PARAMS_NULL.getMsg());}User userDto = userMapper.selectOne(new QueryWrapper<User>().eq("username", dto.getUsername()).eq("denied", BeatManEnums.ACCOUNT_ALLOW.getCode()));logger.info("userinfo is => {}",userDto);if (StringUtils.isNullOrEmpty(userDto)) {return new Result<>(ResultEnums.LOGIN_FAILED.getCode(),ResultEnums.LOGIN_FAILED.getMsg());}if (dto.getPassword().equals(userDto.getPassword())) {Integer accountType = userDto.getAccountType();String userId = userDto.getId();// auth 认证授权,生成 token 返回给前端 【账号还是有问题】if (accountType.equals(BeatManEnums.ACCOUNT_TYPE_ADMIN.getCode())) {// 重载 loginTypeStpUtil.setStpLogic(new StpLogic(BeatManEnums.ACCOUNT_TYPE_ADMIN.getMsg()));}if (accountType.equals(BeatManEnums.ACCOUNT_TYPE_USER.getCode())) {StpUtil.setStpLogic(new StpLogic(BeatManEnums.ACCOUNT_TYPE_USER.getMsg()));}StpUtil.login(userId, false);return new Result<>(ResultEnums.LOGIN_SUCCESS.getCode(),ResultEnums.LOGIN_SUCCESS.getMsg(),StpUtil.getTokenInfo());}return new Result<>(ResultEnums.USERNAME_OR_PASSWORD_NOT_CORRECT.getCode(),ResultEnums.USERNAME_OR_PASSWORD_NOT_CORRECT.getMsg());}
