|
@@ -1,8 +1,17 @@
|
|
|
package cn.wildfirechat.app;
|
|
|
|
|
|
+import com.aliyuncs.CommonRequest;
|
|
|
+import com.aliyuncs.CommonResponse;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.exceptions.ServerException;
|
|
|
+import com.aliyuncs.http.MethodType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.github.qcloudsms.SmsSingleSender;
|
|
|
import com.github.qcloudsms.SmsSingleSenderResult;
|
|
|
import com.github.qcloudsms.httpclient.HTTPException;
|
|
|
+import com.google.gson.Gson;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -16,22 +25,32 @@ public class SmsServiceImpl implements SmsService {
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(SmsServiceImpl.class);
|
|
|
|
|
|
|
|
|
+ private static class AliyunCommonResponse {
|
|
|
+ String Message;
|
|
|
+ String Code;
|
|
|
+ }
|
|
|
+
|
|
|
@Value("${sms.verdor}")
|
|
|
private int smsVerdor;
|
|
|
|
|
|
@Autowired
|
|
|
private TencentSMSConfig mTencentSMSConfig;
|
|
|
|
|
|
+ @Autowired
|
|
|
+ private AliyunSMSConfig aliyunSMSConfig;
|
|
|
+
|
|
|
@Override
|
|
|
public RestResult.RestCode sendCode(String mobile, String code) {
|
|
|
if (smsVerdor == 1) {
|
|
|
return sendTencentCode(mobile, code);
|
|
|
+ } else if(smsVerdor == 2) {
|
|
|
+ return sendAliyunCode(mobile, code);
|
|
|
} else {
|
|
|
return RestResult.RestCode.ERROR_SERVER_NOT_IMPLEMENT;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public RestResult.RestCode sendTencentCode(String mobile, String code) {
|
|
|
+ private RestResult.RestCode sendTencentCode(String mobile, String code) {
|
|
|
try {
|
|
|
String[] params = {code};
|
|
|
SmsSingleSender ssender = new SmsSingleSender(mTencentSMSConfig.appid, mTencentSMSConfig.appkey);
|
|
@@ -51,4 +70,41 @@ public class SmsServiceImpl implements SmsService {
|
|
|
return RestResult.RestCode.ERROR_SERVER_ERROR;
|
|
|
}
|
|
|
|
|
|
+ private RestResult.RestCode sendAliyunCode(String mobile, String code) {
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("default", aliyunSMSConfig.getAccessKeyId(), aliyunSMSConfig.getAccessSecret());
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ String templateparam = "{\"code\":\"" + code + "\"}";
|
|
|
+ CommonRequest request = new CommonRequest();
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
+ request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
+ request.setVersion("2017-05-25");
|
|
|
+ request.setAction("SendSms");
|
|
|
+ request.putQueryParameter("PhoneNumbers", mobile);
|
|
|
+ request.putQueryParameter("SignName", aliyunSMSConfig.getSignName());
|
|
|
+ request.putQueryParameter("TemplateCode", aliyunSMSConfig.getTemplateCode());
|
|
|
+ request.putQueryParameter("TemplateParam", templateparam);
|
|
|
+ try {
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
+ System.out.println(response.getData());
|
|
|
+ if (response.getData() != null) {
|
|
|
+ AliyunCommonResponse aliyunCommonResponse = new Gson().fromJson(response.getData(), AliyunCommonResponse.class);
|
|
|
+ if (aliyunCommonResponse != null) {
|
|
|
+ if (aliyunCommonResponse.Code.equalsIgnoreCase("OK")) {
|
|
|
+ return RestResult.RestCode.SUCCESS;
|
|
|
+ } else {
|
|
|
+ System.out.println("Send aliyun sms failure with message:" + aliyunCommonResponse.Message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (ServerException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (ClientException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ return RestResult.RestCode.ERROR_SERVER_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
}
|