| | |
| | | import com.project.common.exception.user.CaptchaException; |
| | | import com.project.common.exception.user.CaptchaExpireException; |
| | | import com.project.common.exception.user.UserPasswordNotMatchException; |
| | | import com.project.common.sms.YPSmsApi; |
| | | import com.project.common.utils.DateUtils; |
| | | import com.project.common.utils.MessageUtils; |
| | | import com.project.common.utils.ServletUtils; |
| | |
| | | if (user==null){ |
| | | throw new BaseException("您手机号尚未注册!"); |
| | | } |
| | | |
| | | // 生成4位随机数 |
| | | String code = ""; |
| | | Random ran = new Random(); |
| | | int randomNum = ran.nextInt(10000); |
| | | code = String.format("%04d", randomNum); |
| | | log.info("手机号:"+phone+"->验证码:"+code); |
| | | Boolean flag = true; |
| | | boolean send = sendAl(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; |
| | | } |
| | | |
| | | private boolean sendYp(String phone, String code ){ |
| | | String result = YPSmsApi.sendSms(phone, StringUtils.format(YPSmsApi.VERIFY_CODE_TEMPLATE, code, Constants.PHONE_EXPIRATION)); |
| | | if (result.contains("\"code\":0,\"msg\":\"OK\"")){ |
| | | log.info("发送成功 ->验证码:"+code); |
| | | return true; |
| | | } |
| | | return false; |
| | | } |
| | | |
| | | 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; |
| | | } |
| | | |
| | | /** |