javaweb实现手机短信验证码接口

@ResponseBody
@RequestMapping(value = "/getCheckCode", produces = "text/plain;charset=UTF-8")
public String getCheckCode(HttpServletRequest request,
HttpServletResponse response,
@RequestParam(value = "userPhone") String tel) {


JSONObject jsonObject = new JSONObject();


try {
if (StringUtils.isEmpty(tel)) {
jsonObject.put("code", Constants.COMMON_FAIL_ERROR_CODE);
jsonObject.put("msg", "手机号码不能为空");
return jsonObject.toString();
} else {
User user = userDao.selectUserByUserName(tel);
if (user != null) {
jsonObject.put("code", Constants.COMMON_FAIL_ERROR_CODE);
jsonObject.put("msg", "手机号码已经被注册");
return jsonObject.toString();
}
TelValiCode telCheck = ServerStaticData.valiCodeMap.get(tel);
if (telCheck != null) {
Date now = new Date();
Date ytime = telCheck.getCreateTime();
if (now.getTime() - ytime.getTime() < 60 * 1000) {
jsonObject
.put("code", Constants.COMMON_FAIL_ERROR_CODE);
jsonObject.put("msg", "两次验证码获取时间间隔不能少于60秒");
return jsonObject.toString();
}
} else {
telCheck = new TelValiCode();
}


String code = String
.valueOf((int) (Math.random() * (9999 - 1000 + 1)) + 1000);


telCheck.setCreateTime(new Date());
telCheck.setTel(tel);
telCheck.setValiCode(code);


jsonObject.put("code", Constants.COMMON_SUCCESS_CODE);
jsonObject.put("msg", Constants.COMMON_SUCCESS_CODE_MSG);
jsonObject.put("data", new JSONObject().put("vcode", code));// code
}
} catch (Exception e) {
jsonObject.put("code", Constants.COMMON_FAIL_ERROR_CODE);
jsonObject.put("msg", Constants.COMMON_FAIL_ERROR_CODE_MSG);
e.printStackTrace(LogUtil.getErrorStream(logger));
}


return jsonObject.toString();
}

你可能感兴趣的:(Javaweb)