parent
745eb2e35b
commit
a1f0999368
@ -0,0 +1,47 @@ |
|||||||
|
package com.hfkj.config; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsRequest; |
||||||
|
import com.aliyun.dysmsapi20170525.models.SendSmsResponse; |
||||||
|
import com.aliyun.teaopenapi.models.Config; |
||||||
|
import com.aliyun.dysmsapi20170525.Client; |
||||||
|
|
||||||
|
public class AliMessageConfig { |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static Client createClient() throws Exception { |
||||||
|
Config config = new Config() |
||||||
|
// 配置 AccessKey ID,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||||
|
.setAccessKeyId("LTAI5tP9XgTDu49zAAQLGAtJ") |
||||||
|
// 配置 AccessKey Secret,请确保代码运行环境配置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||||
|
.setAccessKeySecret("bHESLcgX17KZ9amioQi7LNFDL51hoM"); |
||||||
|
// System.getenv()方法表示获取系统环境变量,不要直接在getenv()中填入AccessKey信息。
|
||||||
|
|
||||||
|
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
|
||||||
|
config.endpoint = "dysmsapi.aliyuncs.com"; |
||||||
|
|
||||||
|
return new Client(config); |
||||||
|
} |
||||||
|
|
||||||
|
public static SendSmsResponse sendSmsCode(String phone, String code , String templateCode) throws Exception{ |
||||||
|
// 初始化请求客户端
|
||||||
|
Client client = AliMessageConfig.createClient(); |
||||||
|
|
||||||
|
JSONObject jsonObject = new JSONObject(); |
||||||
|
jsonObject.put("code",code); |
||||||
|
jsonObject.put("number",phone); |
||||||
|
|
||||||
|
// 构造API请求对象,请替换请求参数值
|
||||||
|
SendSmsRequest sendSmsRequest = new SendSmsRequest() |
||||||
|
.setPhoneNumbers(phone) |
||||||
|
.setSignName("重庆惠付科技有限公司") |
||||||
|
.setTemplateCode(templateCode) |
||||||
|
// TemplateParam为序列化后的JSON字符串。其中\"表示转义后的双引号。
|
||||||
|
.setTemplateParam(jsonObject.toJSONString()); |
||||||
|
|
||||||
|
// 获取响应对象
|
||||||
|
|
||||||
|
return client.sendSms(sendSmsRequest); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,100 @@ |
|||||||
|
package com.hfkj.config; |
||||||
|
|
||||||
|
import java.io.IOException; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
/** |
||||||
|
* |
||||||
|
* 解析内部http请求包 http1.0、不使用URLENCODE、编码格式默认UTF-8<br> |
||||||
|
* |
||||||
|
* @author yuhanyang |
||||||
|
* @version [版本号, 2014年12月2日] |
||||||
|
* @see [相关类/方法] |
||||||
|
* @since [产品/模块版本] |
||||||
|
*/ |
||||||
|
public class PkgAnalyzeConfig |
||||||
|
{ |
||||||
|
private static final Logger LOGGER = Logger.getLogger(PkgAnalyzeConfig.class.getName()); |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* 从输入流中读出所有字节 |
||||||
|
* |
||||||
|
* @param is 输入流 |
||||||
|
* @return 字节数组 |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static byte[] getBytesFromStream(InputStream is) |
||||||
|
throws Exception |
||||||
|
{ |
||||||
|
byte[] buffer = new byte[1024]; |
||||||
|
byte[] all = null; |
||||||
|
int rlegth = 0; |
||||||
|
|
||||||
|
while ((rlegth = is.read(buffer)) != -1) |
||||||
|
{ |
||||||
|
if (all == null) |
||||||
|
{ |
||||||
|
all = new byte[rlegth]; |
||||||
|
System.arraycopy(buffer, 0, all, 0, rlegth); |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
byte[] bftmp = new byte[all.length]; |
||||||
|
System.arraycopy(all, 0, bftmp, 0, all.length); |
||||||
|
all = new byte[all.length + rlegth]; |
||||||
|
System.arraycopy(bftmp, 0, all, 0, bftmp.length); |
||||||
|
System.arraycopy(buffer, 0, all, bftmp.length, rlegth); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return all; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 从输入流中读取字符串 |
||||||
|
* |
||||||
|
* @param is 输入流 |
||||||
|
* @param charset 字符串编码格式 |
||||||
|
* @return 字符串 |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String getStringFromStream(InputStream is, String charset) |
||||||
|
throws Exception |
||||||
|
{ |
||||||
|
byte all[] = getBytesFromStream(is); |
||||||
|
return new String(all, charset); |
||||||
|
} |
||||||
|
|
||||||
|
// 从流中读取指定长度的字节
|
||||||
|
public static byte[] getFixedBytesFromStream(InputStream in, int length) |
||||||
|
throws IOException |
||||||
|
{ |
||||||
|
LOGGER.info("Begin Read of the InputStream..."); |
||||||
|
byte[] packBytes = new byte[length]; |
||||||
|
int readLen = in.read(packBytes, 0, length); |
||||||
|
LOGGER.info("The [1] Read Length: " + readLen); |
||||||
|
if (readLen < 0) |
||||||
|
{ |
||||||
|
throw new IOException("The InputStream is Closed!"); |
||||||
|
} |
||||||
|
int soReadLength = readLen; |
||||||
|
int noReadLength = length - soReadLength; |
||||||
|
int readTimes = 1; |
||||||
|
while (noReadLength > 0) |
||||||
|
{ |
||||||
|
readTimes++; |
||||||
|
readLen = in.read(packBytes, soReadLength, noReadLength); |
||||||
|
LOGGER.info("The [" + readTimes + "] Read Length: " + readLen); |
||||||
|
if (readLen < 0) |
||||||
|
{ |
||||||
|
break; |
||||||
|
} |
||||||
|
soReadLength = soReadLength + readLen; |
||||||
|
noReadLength = length - soReadLength; |
||||||
|
} |
||||||
|
LOGGER.info("End Read of the InputStream..."); |
||||||
|
return packBytes; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,185 @@ |
|||||||
|
package com.hfkj.config; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON; |
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.alibaba.fastjson.parser.Feature; |
||||||
|
import com.alipay.api.internal.util.AlipaySignature; |
||||||
|
import com.hfkj.common.pay.entity.AliPayReqInfo; |
||||||
|
import com.hfkj.common.utils.HttpsUtils; |
||||||
|
import io.netty.handler.codec.http.HttpUtil; |
||||||
|
|
||||||
|
import javax.net.ssl.SSLSocketFactory; |
||||||
|
import java.io.InputStream; |
||||||
|
import java.io.OutputStream; |
||||||
|
import java.net.Socket; |
||||||
|
import java.net.URL; |
||||||
|
import java.net.URLEncoder; |
||||||
|
import java.time.LocalDateTime; |
||||||
|
import java.time.format.DateTimeFormatter; |
||||||
|
import java.util.Iterator; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.TreeMap; |
||||||
|
import java.util.UUID; |
||||||
|
import java.util.logging.Logger; |
||||||
|
|
||||||
|
public class ShanGaoConfig { |
||||||
|
|
||||||
|
// FinanceGateway测试的调用地址 https://tsfin-fin-test.xlkeji.net/gateway/openapi/v2
|
||||||
|
|
||||||
|
// FinanceGateway生产的调用地址:https://fin.etcsd.com/gateway/openapi/v2
|
||||||
|
private static final String prodUrl = "https://fin.etcsd.com/gateway/openapi/v2"; |
||||||
|
|
||||||
|
// 第三方的私钥,生成地址:https://www.bejson.com/enc/rsa/,秘钥长度最好选择512bit
|
||||||
|
// 生成完成密钥对,提供公钥给山高信联,私钥填写homePrivateKey值,注意去掉回行符号
|
||||||
|
public static final String homeHsgPrivateKey = "MIIBVwIBADANBgkqhkiG9w0BAQEFAASCAUEwggE9AgEAAkEA31zt6gmGLlOwKX29yLrHPamNJq1KIkmHZOdKYMGmz1U1g3OAQU7Uttq8VH3nVC6ITQ5CrmNYb1VVfnCFBqfU5wIDAQABAkEA2dy3hkZh5X/lXKLz4P0lZySgs6/klkrnz+i1KGjaPnDIODHlRI2uFkYswhj29sJKL8mHHK1ARhy8YW7w3uOB2QIhAPpXX4leMAAFLR6WQ/RnIwks8V59aCtVG4E7XEfMjU8LAiEA5Glxf8QdhtYpOYqa+THRkjQ/CGj7q0G7v1sv0KxMqxUCIQD4Hj8fTE3wxzCX/0Ds7IgKIRQ9nbfwMtMm++mTdAaeTwIhAL653XBWjmE9CwdlWaqjkt+GiOu43g1OwjszJWYVuZ9RAiEAj1eh8EKwGSQlHP6gxz0FFGPoHApk+MKglhcXddOYVuQ="; |
||||||
|
public static final String homeQwlPrivateKey = "MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAluzRK15LPIbOxsEr42Nzwk7YPgWfVOIg+QfLkouzD2bpUt9FZ3crSk8bUVGlAeAG0JteTGhwceHoEp0kF4UxywIDAQABAkB7NRbI3bBz1+Vj8fyfI+gzXuQ/NZ3N0NB0G/3/Q1ZJIkwzatq0yEmyq51jSr2MAe1wdDdPiKgYMZN0bo/Ss6LRAiEAxm8iOdS/8iu028wTIXf8wWfUTnPuRSpriL+lPsJWEPMCIQDCtWLlYZ4HLKKkAoOF85Ij2b4U544GDFEuOcGebFdRyQIhAKPt2YuHC49HtiDCZaeHlWI9oXGyakvvcGjHUf8X0rwrAiABFyDxpeVLpv1WK8luw5lq5/o/hbhu4jzGJH6oE3U9uQIhAKZFMiv9k/FBVtKMBWM+HWoheFIQB/J6UXg3qTvJCi4T"; |
||||||
|
// 山高信联提供的的公钥
|
||||||
|
public static final String originHsgPublicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAJpcnJO7WXtriUcoogINLne6l3OsOS6lcmRHClbLguCjN6sCEVmslucSZXB+LimvJO3+efJu7WXg5obY1mKwexUCAwEAAQ=="; |
||||||
|
public static final String originQwlPublicKey = "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANHjdKupahgGKslSi0DvEKS+owL5LL6jOrFumW9eL/81ieeaEDXJOHa1XmPTJO6+zQjOvqS0FFtNAMBNvIg8kkMCAwEAAQ=="; |
||||||
|
// 请求响应参数编码
|
||||||
|
private static final String charset = "UTF-8"; |
||||||
|
|
||||||
|
// 由山高信联分配给第三方的appid
|
||||||
|
public static final String appidHsg = "XL_PARTNER_HAISENGUANG"; |
||||||
|
public static final String appidQwl = "XL_PARTNER_QWLJCJPZHPT"; |
||||||
|
public static final String marketcodHsg = "7711147302"; |
||||||
|
public static final String marketcodQwl = "7711147375"; |
||||||
|
public static final String sourceHsg = "319"; |
||||||
|
public static final String sourceQwl = "320"; |
||||||
|
|
||||||
|
private static final Logger log = Logger.getLogger("ShanGaoConfig"); |
||||||
|
|
||||||
|
|
||||||
|
public static String sendMessage(String uri, String bizContent , String appid , String homePrivateKey , String originPublicKey) throws Exception { |
||||||
|
String url = prodUrl + uri; |
||||||
|
|
||||||
|
Map<String, String> sortedParams = new TreeMap<>(); |
||||||
|
|
||||||
|
sortedParams.put("appid", appid); |
||||||
|
sortedParams.put("nonce_str", UUID.randomUUID().toString().replace("-", "")); |
||||||
|
sortedParams.put("sign_type", "RSA2"); |
||||||
|
sortedParams.put("timestamp", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))); |
||||||
|
sortedParams.put("version", "1.0"); |
||||||
|
sortedParams.put("biz_content", bizContent); |
||||||
|
|
||||||
|
String signContent = AlipaySignature.getSignContent(sortedParams); |
||||||
|
|
||||||
|
log.info("Signature data " + signContent); |
||||||
|
|
||||||
|
String signature = AlipaySignature.rsa256Sign(signContent, homePrivateKey, charset); |
||||||
|
|
||||||
|
log.info("Signature result " + signature); |
||||||
|
|
||||||
|
sortedParams.put("sign", signature); |
||||||
|
|
||||||
|
Iterator<Map.Entry<String, String>> iterator = sortedParams.entrySet().iterator(); |
||||||
|
|
||||||
|
int i = 0; |
||||||
|
|
||||||
|
StringBuffer buffer = new StringBuffer(); |
||||||
|
|
||||||
|
while(iterator.hasNext()) |
||||||
|
{ |
||||||
|
|
||||||
|
Map.Entry<String, String> next = iterator.next(); |
||||||
|
if(i==0){ |
||||||
|
|
||||||
|
i++; |
||||||
|
}else{ |
||||||
|
buffer.append("&"); |
||||||
|
} |
||||||
|
|
||||||
|
String param = next.getKey() + "=" + URLEncoder.encode(next.getValue(), charset); |
||||||
|
|
||||||
|
buffer.append(param); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
String requestBody = buffer.toString(); |
||||||
|
log.info("Request url " + url); |
||||||
|
|
||||||
|
log.info("Request body " + requestBody); |
||||||
|
|
||||||
|
String responseBody = post(url, requestBody); |
||||||
|
|
||||||
|
|
||||||
|
JSONObject jsonObject = JSON.parseObject(responseBody, Feature.OrderedField); |
||||||
|
|
||||||
|
String sign = jsonObject.getString("sign"); |
||||||
|
String bizResponse = jsonObject.getString("biz_response"); |
||||||
|
|
||||||
|
log.info("Sign str: "+ bizResponse); |
||||||
|
|
||||||
|
|
||||||
|
boolean rsa256CheckContent = AlipaySignature.rsa256CheckContent(bizResponse, sign, originPublicKey, charset); |
||||||
|
|
||||||
|
log.info("Check sign " + rsa256CheckContent); |
||||||
|
|
||||||
|
return bizResponse; |
||||||
|
} |
||||||
|
|
||||||
|
private static String post(String strUrl, String strMsg) throws Exception |
||||||
|
{ |
||||||
|
Socket s = null; |
||||||
|
|
||||||
|
try |
||||||
|
{ |
||||||
|
URL url = new URL(strUrl); |
||||||
|
String uri = url.getFile(); |
||||||
|
String ip = url.getHost(); |
||||||
|
int port = url.getPort(); |
||||||
|
String protocol = url.getProtocol(); |
||||||
|
if (-1 == port) |
||||||
|
{ |
||||||
|
if ("https".equals(protocol)) |
||||||
|
{ |
||||||
|
port = 443; |
||||||
|
} |
||||||
|
else |
||||||
|
{ |
||||||
|
port = 80; |
||||||
|
} |
||||||
|
} |
||||||
|
if ("https".equals(protocol)) { |
||||||
|
s = SSLSocketFactory.getDefault().createSocket(ip, port); |
||||||
|
}else { |
||||||
|
s = new Socket(ip, port); |
||||||
|
} |
||||||
|
s.setSoTimeout(60000); |
||||||
|
int len = strMsg.getBytes(charset).length; |
||||||
|
String httpHead = ""; |
||||||
|
httpHead += "POST " + uri + " HTTP/1.0\r\n"; |
||||||
|
httpHead += "Host: " + ip + ":" + port + "\r\n"; |
||||||
|
httpHead += "Accept: text/html,application/xhtml+xml,application/json,application/xml;q=0.9,*/*;q=0.8\r\n"; |
||||||
|
httpHead += "Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3\r\n"; |
||||||
|
httpHead += "Accept-Encoding: \r\n"; |
||||||
|
httpHead += "Content-Type: application/x-www-form-urlencoded;charset=" + charset + "\r\n"; |
||||||
|
httpHead += "Content-length: " + len + "\r\n"; |
||||||
|
httpHead += "\r\n"; |
||||||
|
|
||||||
|
String sendStr = httpHead + strMsg; |
||||||
|
log.info("The HTTP Request : " + sendStr); |
||||||
|
|
||||||
|
OutputStream os = s.getOutputStream(); |
||||||
|
os.write(sendStr.getBytes(charset)); |
||||||
|
os.flush(); |
||||||
|
|
||||||
|
InputStream is = s.getInputStream(); |
||||||
|
String strRet = PkgAnalyzeConfig.getStringFromStream(is, charset); |
||||||
|
log.info("The HTTP Response : " + strRet); |
||||||
|
|
||||||
|
int sie = strRet.indexOf("\r\n\r\n", 0) < 0 ? 0 : strRet.indexOf("\r\n\r\n", 0); |
||||||
|
|
||||||
|
return strRet.substring(sie).trim(); |
||||||
|
} |
||||||
|
finally |
||||||
|
{ |
||||||
|
if (null != s && !s.isClosed()) |
||||||
|
{ |
||||||
|
s.close(); |
||||||
|
s = null; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,32 @@ |
|||||||
|
package com.hfkj.model; |
||||||
|
|
||||||
|
public class OpenAccountRequestDto { |
||||||
|
|
||||||
|
String method; |
||||||
|
String mobile; |
||||||
|
String source; |
||||||
|
|
||||||
|
public String getMethod() { |
||||||
|
return method; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMethod(String method) { |
||||||
|
this.method = method; |
||||||
|
} |
||||||
|
|
||||||
|
public String getMobile() { |
||||||
|
return mobile; |
||||||
|
} |
||||||
|
|
||||||
|
public void setMobile(String mobile) { |
||||||
|
this.mobile = mobile; |
||||||
|
} |
||||||
|
|
||||||
|
public String getSource() { |
||||||
|
return source; |
||||||
|
} |
||||||
|
|
||||||
|
public void setSource(String source) { |
||||||
|
this.source = source; |
||||||
|
} |
||||||
|
} |
@ -1 +1 @@ |
|||||||
package com.user.controller;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.MessageConfig;
import com.hfkj.model.ResponseData;
import com.hfkj.service.user.BsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.Random;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/26 23:08
*/
@Controller
@RequestMapping(value = "/sms")
@Api(value = "短信服务")
public class SmsController {
private static Logger log = LoggerFactory.getLogger(SmsController.class);
@Resource
private RedisUtil redisUtil;
@RequestMapping(value = "/sendLoginSMSCode", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "获取登录验证码")
public ResponseData sendLoginSMSCode(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("phone"))) {
log.error("LoginController --> phone() error!", "请求参数校验失败");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
String phone = body.getString("phone");
// 校验手机号格式
if (MemberValidateUtil.validatePhone(phone) == false) {
log.error("LoginController --> phone() error!", "请输入正确的手机号");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
}
// 生成随机6位验证码
String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID7);
// 验证码缓存5分钟
redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);
return ResponseMsgUtil.success("短信发送成功");
} catch (Exception e) {
log.error("SMSController --> getLoginSMSCode() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/sendBindCardSMSCode", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "获取绑定卡片验证码")
public ResponseData sendBindCardSMSCode(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("phone"))) {
log.error("LoginController --> phone() error!", "请求参数校验失败");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
String phone = body.getString("phone");
// 校验手机号格式
if (MemberValidateUtil.validatePhone(phone) == false) {
log.error("LoginController --> phone() error!", "请输入正确的手机号");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
}
// 生成随机6位验证码
String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
MessageConfig.req(phone,smsCode, MessageConfig.HWMSG_ID7);
// 验证码缓存5分钟
redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);
return ResponseMsgUtil.success("短信发送成功");
} catch (Exception e) {
log.error("SMSController --> getLoginSMSCode() error!", e);
return ResponseMsgUtil.exception(e);
}
}
}
|
package com.user.controller;
import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.utils.MemberValidateUtil;
import com.hfkj.common.utils.RedisUtil;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.AliMessageConfig;
import com.hfkj.model.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.Random;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/26 23:08
*/
@Controller
@RequestMapping(value = "/sms")
@Api(value = "短信服务")
public class SmsController {
private static Logger log = LoggerFactory.getLogger(SmsController.class);
@Resource
private RedisUtil redisUtil;
@RequestMapping(value = "/sendLoginSMSCode", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "获取登录验证码")
public ResponseData sendLoginSMSCode(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("phone"))) {
log.error("LoginController --> phone() error!", "请求参数校验失败");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
String phone = body.getString("phone");
// 校验手机号格式
if (MemberValidateUtil.validatePhone(phone) == false) {
log.error("LoginController --> phone() error!", "请输入正确的手机号");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
}
// 生成随机6位验证码
String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
AliMessageConfig.sendSmsCode(phone, smsCode , "SMS_480970320");
// 验证码缓存5分钟
redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);
return ResponseMsgUtil.success("短信发送成功");
} catch (Exception e) {
log.error("SMSController --> getLoginSMSCode() error!", e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value = "/sendBindCardSMSCode", method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "获取绑定卡片验证码")
public ResponseData sendBindCardSMSCode(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("phone"))) {
log.error("LoginController --> phone() error!", "请求参数校验失败");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
String phone = body.getString("phone");
// 校验手机号格式
if (MemberValidateUtil.validatePhone(phone) == false) {
log.error("LoginController --> phone() error!", "请输入正确的手机号");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确的手机号");
}
// 生成随机6位验证码
String smsCode = String.valueOf(new Random().nextInt(899999) + 100000);
AliMessageConfig.sendSmsCode(phone, smsCode , "SMS_480970320");
// 验证码缓存5分钟
redisUtil.set("SMS_CODE:"+phone, smsCode, 60*5);
return ResponseMsgUtil.success("短信发送成功");
} catch (Exception e) {
log.error("SMSController --> getLoginSMSCode() error!", e);
return ResponseMsgUtil.exception(e);
}
}
}
|
Loading…
Reference in new issue