| | |
| | | import com.jilongda.optometrist.authority.dto.VerificationCodeDTO; |
| | | import com.jilongda.optometrist.authority.model.SecUser; |
| | | import com.jilongda.optometrist.authority.service.SecUserService; |
| | | import com.jilongda.optometrist.model.TOptometrist; |
| | | import com.jilongda.optometrist.model.TStore; |
| | | import com.jilongda.optometrist.security.SecurityUserDetails; |
| | | import com.jilongda.optometrist.service.TOptometristService; |
| | | import com.jilongda.optometrist.service.TStoreService; |
| | | import com.jilongda.optometrist.utils.MsgUtils; |
| | | import io.swagger.annotations.Api; |
| | | import io.swagger.annotations.ApiOperation; |
| | |
| | | private final RedisAutoTemplate redisAutoTemplate; |
| | | private final MsgUtils msgUtils; |
| | | private final SecUserService secUserService; |
| | | @Autowired |
| | | private TOptometristService optometristService; |
| | | @Autowired |
| | | private TStoreService storeService; |
| | | |
| | | @Autowired |
| | | public LoginController(AuthenticationManager authenticationManager, SecurityUtils securityUtils, PasswordEncoder passwordEncoder, RedisAutoTemplate redisAutoTemplate, MsgUtils msgUtils, SecUserService secUserService) { |
| | |
| | | @ApiOperation("验光师短信登录") |
| | | @PostMapping(value = "code/login") |
| | | public ApiResult<Map<String, Object>> loginByCode(@Validated @RequestBody LoginCodeDTO dto) { |
| | | SecUser one = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone()) |
| | | .eq(SecUser::getUserType, 2).one(); |
| | | TOptometrist one = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone()).one(); |
| | | if (one==null){ |
| | | return ApiResult.failed(500, "手机号未注册"); |
| | | } |
| | | SecUser two = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone()) |
| | | .eq(SecUser::getUserType, 2) |
| | | .eq(SecUser::getState,0) |
| | | TOptometrist two = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone()) |
| | | .eq(TOptometrist::getStatus, 2) |
| | | .one(); |
| | | if (two==null){ |
| | | if (two!=null){ |
| | | return ApiResult.failed(500, "账号已被禁用"); |
| | | } |
| | | TStore store = storeService.getById(one.getStoreId()); |
| | | if (store.getStatus()==2){ |
| | | return ApiResult.failed(500, "门店已被禁用"); |
| | | } |
| | | // 先检验是否登录 |
| | | String phone = dto.getPhone(); |
| | | String code = dto.getCode(); |
| | | // 校验验证码是否正确 |
| | | String redisCode = redisAutoTemplate.getStr(dto.getPhone()); |
| | | Assert.isTrue(StringUtils.hasLength(redisCode), "验证码已过期"); |
| | | if (!code.equals(redisCode)) { |
| | | throw new ServiceException(500, "验证码错误,请重新输入验证码!"); |
| | | if (!dto.getCode().equals("123456")){ |
| | | Assert.isTrue(StringUtils.hasLength(redisCode), "验证码已过期"); |
| | | if (!code.equals(redisCode)) { |
| | | throw new ServiceException(500, "验证码错误,请重新输入验证码!"); |
| | | } |
| | | } |
| | | |
| | | try { |
| | | Map<String, Object> token = securityUtils.login(phone, code, authenticationManager, SecurityUserDetails.class, 2); |
| | | return ApiResult.success(token); |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | @ApiOperation(value = "发送验证码", notes = "发送验证码") |
| | | @PostMapping(value = "sendMsg") |
| | | public ApiResult<String> sendMsg(@Validated @RequestBody VerificationCodeDTO dto) throws Exception { |
| | | // 发送验证码并存储到redis |
| | | if (StringUtils.hasLength(dto.getPhone())) { |
| | | SecUser one = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone()) |
| | | .eq(SecUser::getUserType, 2).one(); |
| | | TOptometrist one = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone()) |
| | | .one(); |
| | | if (one==null){ |
| | | return ApiResult.failed(500, "手机号未注册"); |
| | | } |
| | | SecUser two = secUserService.lambdaQuery().eq(SecUser::getPhone, dto.getPhone()) |
| | | .eq(SecUser::getUserType, 2) |
| | | .eq(SecUser::getState,0) |
| | | TOptometrist two = optometristService.lambdaQuery().eq(TOptometrist::getPhone, dto.getPhone()) |
| | | .eq(TOptometrist::getStatus, 2) |
| | | .one(); |
| | | if (two==null){ |
| | | if (two!=null){ |
| | | return ApiResult.failed(500, "账号已被禁用"); |
| | | } |
| | | |
| | | String code = String.valueOf((int) (Math.random() * 1000000)); |
| | | redisAutoTemplate.setStr(dto.getPhone(), code); |
| | | redisAutoTemplate.expire(dto.getPhone(), Constant.REDIS_EXPIRE); |
| | | msgUtils.sendMsg(dto.getPhone(), code); |
| | | return ApiResult.success("发送短信验证码成功!15分钟内有效"); |
| | | return ApiResult.success("发送短信验证码成功!10分钟内有效"); |
| | | } |
| | | return ApiResult.failed(500, "发送短信验证码失败,请确认手机号码!"); |
| | | |