Browse Source

短信签名修改为入参

杨杰 4 years ago
parent
commit
92b9981f13

+ 1 - 2
src/main/java/com/llisoft/sms/controller/SmsController.java

@@ -29,8 +29,7 @@ public class SmsController {
 	@ApiOperation(value="发送验证码")
 	@PostMapping("/sendCode")
 	public ResponseVo<Boolean> sendCode(@Valid @RequestBody SendCodeRequestVo requestVo, HttpServletRequest request) throws Exception{
-		return ResponseVo.success(smsService.sendCode(requestVo.getPhone(), requestVo.getAppkey(), requestVo.getScene(), requestVo.getToken(), 
-			requestVo.getSessionid(), requestVo.getSig(), ServletUtil.getIp(request), requestVo.getType()));
+		return ResponseVo.success(smsService.sendCode(requestVo, ServletUtil.getIp(request)));
 	}
 	
 	@ApiOperation(value="验证验证码")

+ 5 - 9
src/main/java/com/llisoft/sms/service/AliyunService.java

@@ -41,6 +41,7 @@ public class AliyunService {
 	private IAcsClient smsClient;
 	private IAcsClient afsClient;
 	
+	
 	@PostConstruct
 	public void init() { // 初始化
 		smsClient = new DefaultAcsClient(DefaultProfile.getProfile("default", accessKeyId, accessKeySecret));
@@ -49,7 +50,7 @@ public class AliyunService {
 		afsClient = new DefaultAcsClient(DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret));
 	}
 	
-	
+
 	/**
 	 * 发送短信
 	 * @param phone
@@ -57,7 +58,7 @@ public class AliyunService {
 	 * @param type 类型(0麦塔、1青谷)
 	 */
 	@Async
-	public void sms(String phone, String code, int type) {
+	public void sms(String sign, String phone, String code) {
 		logger.info("阿里云发送验证码: {} : {}", phone, code);
 		CommonRequest request = new CommonRequest();
 //        request.setSysProtocol(ProtocolType.HTTPS);
@@ -66,13 +67,8 @@ public class AliyunService {
         request.setSysDomain("dysmsapi.aliyuncs.com");
         request.setSysVersion("2017-05-25"); // 固定字符串
         request.putQueryParameter("PhoneNumbers", phone);
-        if (type == 0) { // 麦塔
-        	request.putQueryParameter("SignName", "麦塔"); // 签名
-        	request.putQueryParameter("TemplateCode", "SMS_216836530"); // 模板ID
-		}else if (type == 1) { // 青谷
-			request.putQueryParameter("SignName", "麦塔带货云"); // 签名
-			request.putQueryParameter("TemplateCode", "SMS_216836530"); // 模板ID
-		}
+    	request.putQueryParameter("SignName", sign); // 签名
+    	request.putQueryParameter("TemplateCode", "SMS_216836530"); // 模板ID
         request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
         try {
             CommonResponse response = smsClient.getCommonResponse(request);

+ 8 - 6
src/main/java/com/llisoft/sms/service/SmsService.java

@@ -11,7 +11,9 @@ import org.springframework.stereotype.Service;
 
 import com.llisoft.sms.config.MtaException;
 import com.llisoft.sms.util.CodeUtil;
+import com.llisoft.sms.util.JsonUtil;
 import com.llisoft.sms.util.RedisUtil;
+import com.llisoft.sms.vo.SendCodeRequestVo;
 
 /**
  * 短信服务
@@ -35,18 +37,18 @@ public class SmsService {
 	 * @param phone
 	 * @throws Exception 
 	 */
-	public boolean sendCode(String phone, String appkey, String scene, String token, 
-			String sessionId, String sig, String ip, int type) throws Exception {
+	public boolean sendCode(SendCodeRequestVo requestVo, String ip) throws Exception {
 		// 检查人机验证码
-		logger.info("人机验证: appkey={}, scene={}, token={}, sessionId={}, sig={}, ip={}", appkey, scene, token, sessionId, sig, ip);
+		logger.info("人机验证: ", JsonUtil.toJson(requestVo));
 		if (debug) {
 			logger.warn("开发模式:人机验证已跳过");
 		}
-		if (!debug && !aliyunService.afs(appkey, scene, token, sessionId, sig, ip)) {
-			logger.warn("人机验证失败:{}", token);
+		if (!debug && !aliyunService.afs(requestVo.getAppkey(), requestVo.getScene(), requestVo.getToken(), requestVo.getSessionid(), requestVo.getSig(), ip)) {
+			logger.warn("人机验证失败:{}", requestVo.getToken());
 			throw new MtaException("人机验证失败,请刷新页面后重试");
 		}
 		// 检查号码
+		String phone = requestVo.getPhone();
 		logger.info("检查手机号: {}", phone);
 		if(!this.checkSend(phone)) {
 			logger.warn("手机号被限制发送:{}", phone);
@@ -56,7 +58,7 @@ public class SmsService {
 		String code = String.valueOf(CodeUtil.ints(4));
 		RedisUtil.set(phone + "_" + code, "", 10, TimeUnit.MINUTES); // 有效期10分钟
 		logger.info("发送验证码: {}", code);
-		aliyunService.sms(phone, code, type); // 第三方发送短信(异步)
+		aliyunService.sms(requestVo.getSign(), phone, code); // 第三方发送短信(异步)
 		return true;
 	}
 	

+ 8 - 10
src/main/java/com/llisoft/sms/vo/SendCodeRequestVo.java

@@ -13,29 +13,27 @@ public class SendCodeRequestVo {
 	@NotBlank(message="手机号码不能为空")
 	@Pattern(regexp="1[0-9]{10}", message="手机号码格式不正确")
 	private String phone;
+
+	@ApiModelProperty(value="短信签名(需要阿里云审核通过)", example="麦塔")
+	@NotBlank(message="短信签名不能为空")
+	private String sign;
 	
 	@ApiModelProperty(value="验证码参数:appkey", example=" ")
 	@NotBlank(message="验证码参数:appkey不能为空")
 	private String appkey;
-	
 	@ApiModelProperty(value="验证码参数:scene", example=" ")
 	@NotBlank(message="验证码参数:scene不能为空")
 	private String scene;
-	
 	@ApiModelProperty(value="验证码参数:token", example=" ")
 	@NotBlank(message="验证码参数:token不能为空")
 	private String token;
-	
 	@ApiModelProperty(value="验证码参数:sessionid", example=" ")
 	@NotBlank(message="验证码参数:sessionid不能为空")
 	private String sessionid;
-	
 	@ApiModelProperty(value="验证码参数:sig", example=" ")
 	@NotBlank(message="验证码参数:sig不能为空")
 	private String sig;
 	
-	@ApiModelProperty(value="类型(0麦塔、1青谷)")
-	private int type;
 
 	public String getPhone() {
 		return phone;
@@ -73,11 +71,11 @@ public class SendCodeRequestVo {
 	public void setSessionid(String sessionid) {
 		this.sessionid = sessionid;
 	}
-	public int getType() {
-		return type;
+	public String getSign() {
+		return sign;
 	}
-	public void setType(int type) {
-		this.type = type;
+	public void setSign(String sign) {
+		this.sign = sign;
 	}
 
 }

+ 1 - 12
src/test/java/com/llisoft/sms/ServiceTest.java

@@ -2,28 +2,17 @@ package com.llisoft.sms;
 
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.boot.test.context.SpringBootTest;
 import org.springframework.test.context.junit4.SpringRunner;
 
-import com.llisoft.sms.service.SmsService;
-
 @SpringBootTest
 @RunWith(SpringRunner.class)
 public class ServiceTest {
-	
-	@Autowired
-	private SmsService smsService;
 
 	@Test
 	public void test() throws Exception{
 		long begin = System.currentTimeMillis();
-		System.out.println(smsService.sendCode("13552635233", "FFFF0N00000000007EC0", "nc_message", 
-				"564565", 
-				"1AqHb9a5BYfxoNlsnrGZyDBtd7Iaiuywjkwb7JGTL5zSXBuaiyoW56n95ldyMHj5lZkimcGbA-XllwI2o9t83k9WeNehFYXOArpda-XDNiEEKeVlrJDt01j1bkUlhx_BXhvRqTX57PTdHoUBvzdGVfltzUZPC7fSbpq3ioud8KxipjUK34DBIuco5KB0GPVzM", 
-				"5XqrtZ0EaFgmmqIQes-s-CF5NpYdMgFzO50Qw4NdPuD91JWqYsOLZUWJRzsyCiSiP3DBEMIenUZ2Yn5icRXVRutJ7F5YLXWOHzeBd5iN0Jor15E1gZkJicTJ-YXNgycJpsvJz3qSJXLj7DodnTTSHvfTVz7kHbAkAzvgM_G1ZmK-3SuyCw_2vnt6f-LhzjmKHip0AsEJpIdCCgUHhRtzAPgisplTknaZGYuj8LHsUAEgXXJf0zPWeMKQBbR_nwjyPufH9PZvYLpPxsgi-EI3_2043n5gcQ5M9HWkSw4IZpXKg2BD9GedAs-ZjYQnTl91vw91i_WJZJUKppW38DPxagcXlpNL3Dg-83bjBgQSk3ELnFOsSvlW43mEJAWXGzx_ZwIULIvg6ilA_rLGyCmG5uc3inTnLQO1t6vuj4CGCFM0", 
-				"119.109.18.203", 0));
-//		System.out.println(smsService.checkCode("13552635233", "5507"));
+
 		System.out.println("耗时:"+ (System.currentTimeMillis() - begin));
 	}