|
@@ -0,0 +1,110 @@
|
|
|
+package cn.com.qmth.examcloud.support.sms;
|
|
|
+
|
|
|
+import cn.com.qmth.examcloud.commons.exception.StatusException;
|
|
|
+import cn.com.qmth.examcloud.commons.util.JsonMapper;
|
|
|
+import cn.com.qmth.examcloud.web.bootstrap.PropertyHolder;
|
|
|
+import com.aliyuncs.CommonRequest;
|
|
|
+import com.aliyuncs.CommonResponse;
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.http.MethodType;
|
|
|
+import com.aliyuncs.http.ProtocolType;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.fasterxml.jackson.databind.JsonNode;
|
|
|
+import org.apache.tomcat.util.buf.StringUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class SmsHelper {
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(SmsHelper.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 短信配置代码 - 验证码
|
|
|
+ */
|
|
|
+ public static final String SMS_YZM = "YZM";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 短信配置代码 - 人脸比对失败报警短信
|
|
|
+ */
|
|
|
+ public static final String SMS_FACECOMPARE = "FACECOMPARE";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 短信配置代码 - 百度活体检测处理失败报警短信
|
|
|
+ */
|
|
|
+ public static final String SMS_FACELIVENESS = "FACELIVENESS";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发短信
|
|
|
+ *
|
|
|
+ * @param signName 短信签名
|
|
|
+ * @param templateCode 短信模板
|
|
|
+ * @param phoneList 手机号码
|
|
|
+ * @param templateParams 短信模板参数
|
|
|
+ * @author WANGWEI
|
|
|
+ */
|
|
|
+ public static void send(String signName, String templateCode, List<String> phoneList,
|
|
|
+ Map<String, String> templateParams) {
|
|
|
+ String accessKeyId = PropertyHolder.getString("aliyun.sms.accessKeyId");
|
|
|
+ String accessSecret = PropertyHolder.getString("aliyun.sms.accessKeySecret");
|
|
|
+
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+
|
|
|
+ CommonRequest request = new CommonRequest();
|
|
|
+ request.setProtocol(ProtocolType.HTTPS);
|
|
|
+ request.setMethod(MethodType.POST);
|
|
|
+ request.setDomain("dysmsapi.aliyuncs.com");
|
|
|
+ request.setVersion("2017-05-25");
|
|
|
+ request.setAction("SendSms");
|
|
|
+
|
|
|
+ String phoneNumbers = StringUtils.join(phoneList, ',');
|
|
|
+ String params = new JsonMapper().toJson(templateParams);
|
|
|
+ request.putQueryParameter("SignName", signName);
|
|
|
+ request.putQueryParameter("TemplateCode", templateCode);
|
|
|
+ request.putQueryParameter("TemplateParam", params);
|
|
|
+ request.putQueryParameter("PhoneNumbers", phoneNumbers);
|
|
|
+
|
|
|
+ try {
|
|
|
+ CommonResponse response = client.getCommonResponse(request);
|
|
|
+ String responseData = response.getData();
|
|
|
+
|
|
|
+ // 成功示例:{"Message":"OK","Code":"OK","RequestId":"xxx","BizId":"xxx"}
|
|
|
+ String code = null;
|
|
|
+ JsonNode jsonNode = new JsonMapper().getNode(responseData);
|
|
|
+ if (jsonNode != null) {
|
|
|
+ JsonNode codeNode = jsonNode.get("Code");
|
|
|
+ code = codeNode != null ? codeNode.asText() : null;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!"OK".equals(code)) {
|
|
|
+ log.error("sms send fail:{}", responseData);
|
|
|
+ throw new StatusException("101001", "短信发送失败");
|
|
|
+ }
|
|
|
+ log.warn("短信发送成功!phoneNumbers:{} params:{}", phoneNumbers, params);
|
|
|
+ } catch (StatusException e) {
|
|
|
+ throw e;
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("sms send error:{}", e.getMessage());
|
|
|
+ throw new StatusException("101002", "短信发送异常", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+ List<String> phones = Arrays.asList("18600000001");
|
|
|
+
|
|
|
+ Map<String, String> templateParams = new HashMap<>();
|
|
|
+ // templateParams.put("code", "123");
|
|
|
+ // SmsHelper.send("考试云平台", "SMS_153725618", phones, templateParams);
|
|
|
+
|
|
|
+ templateParams.put("name", "api");
|
|
|
+ templateParams.put("time", new SimpleDateFormat("HH:mm:ss").format(new Date()));
|
|
|
+ templateParams.put("status", "500");
|
|
|
+ templateParams.put("msg", "接口异常");
|
|
|
+ SmsHelper.send("启明泰和", "SMS_246635756", phones, templateParams);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|