验证码

ww
chengqf 2 years ago
parent c8695dfb2d
commit f061c8d737

@ -0,0 +1,27 @@
package com.glxp.api.controller.auth;
import java.util.Map;
import javax.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.glxp.api.common.res.BaseResponse;
import com.glxp.api.util.CaptchaUtils;
import lombok.extern.slf4j.Slf4j;
@RestController
@Slf4j
public class RegisterController {
@PostMapping("/admin/auth/register/getCheckcode")
public BaseResponse getCheckcode(@RequestBody Map<String,Object> params, HttpSession httpSession) {
return CaptchaUtils.checkCode(params);
}
}

@ -0,0 +1,70 @@
package com.glxp.api.util;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
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;
/**
*
*/
public class CaptchaUtils {
private static String Url = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
@Resource
static RedisUtil redisUtil;
private CaptchaUtils() {
}
public static BaseResponse checkCode(Map<String,Object> params) {
String codeStr = String.valueOf(redisUtil.get(Constant.CAPTCHAS + params.get("mobile")));
if (StrUtil.isBlank(codeStr) || "null".equals(codeStr)) {
return ResultVOUtils.error(500, "验证码已过期,请重新获取");
}
if(!codeStr.equals(params.get("code"))) {
return ResultVOUtils.error(500, "验证码错误,请重新获取");
}
redisUtil.del(Constant.CAPTCHAS + params.get("mobile"));
return ResultVOUtils.success();
}
public BaseResponse getCheckcode(Map<String,Object> 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.get("mobile"));
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.get("mobile"));
json.put("code", mobile_code);
json.put("createTime", System.currentTimeMillis());
//验证码存入redis中
redisUtil.set(Constant.CAPTCHAS + params.get("mobile"), json, 300L);
return ResultVOUtils.success("发送成功" + " " + mobile_code);
} else {
return ResultVOUtils.error(500, "验证码发送失败");
}
}
}
Loading…
Cancel
Save