| | |
| | | import com.project.system.mapper.SysUserMapper; |
| | | import com.project.system.service.ISysConfigService; |
| | | import com.project.system.service.ISysUserService; |
| | | import com.project.system.sms.YPSmsApi; |
| | | import lombok.RequiredArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.security.authentication.AuthenticationManager; |
| | |
| | | private final ISysUserService userService; |
| | | private final SysUserMapper userMapper; |
| | | private final ISysConfigService configService; |
| | | private final YPSmsApi smsApi; |
| | | |
| | | |
| | | /** |
| | |
| | | return tokenService.createToken(loginUser); |
| | | } |
| | | |
| | | /** |
| | | * 新注册获取验证码 |
| | | * @param phone 手机 |
| | | * @return 验证码 |
| | | */ |
| | | public Boolean getVerifyCodeNew(String phone) { |
| | | // 生成4位随机数 |
| | | String code = ""; |
| | | Random ran = new Random(); |
| | | int randomNum = ran.nextInt(10000); |
| | | code = String.format("%04d", randomNum); |
| | | log.info("手机号:"+phone+"->验证码:"+code); |
| | | boolean send = sendYp(phone, code); |
| | | if (send){ |
| | | redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); |
| | | return true; |
| | | } |
| | | redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 获取验证码 |
| | |
| | | if (user==null){ |
| | | throw new BaseException("您手机号尚未注册!"); |
| | | } |
| | | return getVerifyCodeNew(phone); |
| | | } |
| | | |
| | | // 生成4位随机数 |
| | | String code = ""; |
| | | Random ran = new Random(); |
| | | int randomNum = ran.nextInt(10000); |
| | | code = String.format("%04d", randomNum); |
| | | log.info("手机号:"+phone+"->验证码:"+code); |
| | | Boolean flag = true; |
| | | /** |
| | | * 云片验证码 |
| | | * @param phone 手机 |
| | | * @param code 验证码 |
| | | * @return 结果 |
| | | */ |
| | | private boolean sendYp(String phone, String code) |
| | | { |
| | | String result = smsApi.sendSms(phone, StringUtils.format(YPSmsApi.CODE_TMP, code, Constants.PHONE_EXPIRATION)); |
| | | if (result.contains("\"code\":0,\"msg\":\"OK\"")){ |
| | | log.info("发送成功 ->验证码:"+code); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | | * 阿里验证码 |
| | | * @param phone 手机 |
| | | * @param code 验证码 |
| | | * @return 结果 |
| | | */ |
| | | private boolean sendAl(String phone, String code ) |
| | | { |
| | | DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", AliyunSmsConstants.SMS_APPID, AliyunSmsConstants.SMS_SECRET); |
| | | IAcsClient client = new DefaultAcsClient(profile); |
| | | CommonRequest request = new CommonRequest(); |
| | |
| | | JSONObject jsonObject = JSON.parseObject(response.getData()); |
| | | if ("OK".equals(jsonObject.get("Code"))) { |
| | | log.info("发送成功 ->验证码:"+code); |
| | | redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); |
| | | return true; |
| | | } |
| | | |
| | | } catch (ClientException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | redisCache.setCacheObject(getCacheKey(phone), code, Constants.PHONE_EXPIRATION, TimeUnit.MINUTES); |
| | | return flag; |
| | | return false; |
| | | } |
| | | |
| | | /** |
| | |
| | | public String phoneLogin(UserPhoneLoginBo bo) |
| | | { |
| | | String phone = bo.getPhone(); |
| | | Boolean verified = verifyPhone(phone, bo.getCode()); |
| | | if (!verified){ |
| | | throw new BaseException("手机号验证码校验失败!"); |
| | | // Boolean verified = verifyPhone(phone, bo.getCode()); |
| | | // if (!verified){ |
| | | // throw new BaseException("手机号验证码校验失败!"); |
| | | // } |
| | | SysUser user = null; |
| | | if ("01".equals(bo.getUserType())){ |
| | | user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getPhonenumber,bo.getPhone()) |
| | | .and(wrapper->wrapper.eq(SysUser::getUserType,"00").or().eq(SysUser::getUserType, "01"))); |
| | | } else { |
| | | user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>() |
| | | .eq(SysUser::getPhonenumber,bo.getPhone()) |
| | | .eq(SysUser::getUserType,bo.getUserType()) |
| | | ); |
| | | } |
| | | SysUser user = userMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getPhonenumber,bo.getPhone())); |
| | | if (user==null){ |
| | | throw new BaseException("您手机号尚未注册!"); |
| | | throw new BaseException("您手机号尚未注册或您选择登录类型有误!"); |
| | | } |
| | | if (!"0".equals(user.getStatus())){ |
| | | throw new BaseException("您账号已停用或待审批,请联系营商办管理人员!"); |
| | | } |
| | | return this.login(user.getUserName(), user.getRecommendUser(), null, null); |
| | | } |
| | |
| | | |
| | | return verify; |
| | | } |
| | | |
| | | |
| | | } |