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

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