You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-cpt-java/src/main/java/com/glxp/api/util/CaptchaUtils.java

84 lines
3.4 KiB
Java

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.glxp.api.util;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import com.glxp.api.req.auth.loginmobileRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import com.alibaba.fastjson.JSONObject;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.common.util.ResultVOUtils;
import com.glxp.api.constant.Constant;
import cn.hutool.core.util.StrUtil;
/**
* 短信验证码
*/
@Component
public class CaptchaUtils {
private static final Logger logger = LoggerFactory.getLogger(CaptchaUtils.class);
private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
static RedisUtil redisUtil;
@Resource
public void getRedisUtil(RedisUtil redisUtil) {
this.redisUtil = redisUtil;
}
private CaptchaUtils() {
}
public static BaseResponse checkCode(loginmobileRequest params) {
logger.info(">>>>>"+redisUtil.get(Constant.CAPTCHAS + params.getMobile()));
String codeStr = String.valueOf(redisUtil.get(Constant.CAPTCHAS + params.getMobile()));
if (StrUtil.isBlank(codeStr) || "null".equals(codeStr)) {
return ResultVOUtils.error(500, "验证码已过期,请重新获取");
}
JSONObject json = JSONObject.parseObject(codeStr);
logger.info("code:::"+codeStr);
if(!json.getString("code").equals(params.getCheckCode())) {
return ResultVOUtils.error(500, "验证码错误,请重新获取");
}
redisUtil.del(Constant.CAPTCHAS + params.getMobile());
return ResultVOUtils.success(params.getMobile());
}
public static BaseResponse getCheckcode(loginmobileRequest params, HttpSession httpSession) {
RestTemplate restTemplate = new RestTemplate();
int mobile_code = (int) ((Math.random() * 9 + 1) * 100000);
MultiValueMap<String, Object> postParameters = new LinkedMultiValueMap<>();
int time = 5;
String content = new String("您正在进行手机验证,验证码是" + mobile_code + "" + time + "分钟内有效。");
postParameters.add("account", "C07086222");
postParameters.add("password", "2dddbbf73636c193c5903324bdb47c5c");
postParameters.add("mobile", params.getMobile());
postParameters.add("content", content);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded;charset=GBK");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(postParameters, headers);
String response = restTemplate.postForObject(Url, httpEntity, String.class);
if (response.contains("提交成功")) {
JSONObject json = new JSONObject();
json.put("memPhone", params.getMobile());
json.put("code", mobile_code);
json.put("createTime", System.currentTimeMillis());
//验证码存入redis中
redisUtil.set(Constant.CAPTCHAS + params.getMobile(), json, 300L);
return ResultVOUtils.success(mobile_code);
} else {
return ResultVOUtils.error(500, "验证码发送失败");
}
}
}