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 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 params, HttpSession httpSession) { RestTemplate restTemplate = new RestTemplate(); int mobile_code = (int) ((Math.random() * 9 + 1) * 100000); MultiValueMap 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> 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, "验证码发送失败"); } } }