forked from fanruan/demo-third-sms-v2
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
4.7 KiB
132 lines
4.7 KiB
3 years ago
|
package com.fr.plugin.decision.third.sms;
|
||
|
|
||
|
import com.fr.base.sms.SMSContext;
|
||
|
import com.fr.json.JSONArray;
|
||
|
import com.fr.json.JSONObject;
|
||
|
import com.fr.log.FineLoggerFactory;
|
||
|
import com.fr.plugin.decision.third.sms.fun.SMSXmlReader;
|
||
|
import com.fr.plugin.decision.third.sms.fun.SmsClient;
|
||
|
import com.fr.plugin.transform.ExecuteFunctionRecord;
|
||
|
import com.fr.plugin.transform.FunctionRecorder;
|
||
|
import com.fr.stable.fun.impl.AbstractSMSServiceProvider;
|
||
|
|
||
|
import java.util.ArrayList;
|
||
|
import java.util.HashMap;
|
||
|
import java.util.List;
|
||
|
import java.util.Map;
|
||
|
|
||
|
@FunctionRecorder
|
||
|
public class ThirdSmsServiceProvider extends AbstractSMSServiceProvider {
|
||
|
|
||
|
private static final String SMS_PUBLIC_MODEL = "public-model";
|
||
|
private static final String SMS_PRIVATE_MODEL = "private-model";
|
||
|
|
||
|
public ThirdSmsServiceProvider() {
|
||
|
startListener();
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public String sign() {
|
||
|
return SmsClient.getInstance().getSign();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 必须大于1,且跟js文件中的优先级保持一致
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public int priority() {
|
||
|
return 20;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public boolean isSMSAvailable() {
|
||
|
//根据具体情况看短信服务是否可用
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 这里解析sms_template.xml文件中的模板返回,实际获取短信服务的模板
|
||
|
*
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public Map<String, List<SMSTemplateBean>> getSMSTemplate() {
|
||
|
Map<String, List<SMSTemplateBean>> result = new HashMap<>();
|
||
|
Map<String, Map<String, Object>> smsTemplates = SmsClient.getInstance().getSMSTemplate();
|
||
|
result.put("publicModel", convertSMSTemplateBeanList(smsTemplates.get(SMS_PUBLIC_MODEL)));
|
||
|
result.put("privateModel", convertSMSTemplateBeanList(smsTemplates.get(SMS_PRIVATE_MODEL)));
|
||
|
return result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 读取sms_template_mapping.xml配置文件中的映射规则
|
||
|
* @return
|
||
|
*/
|
||
|
@Override
|
||
|
public Map<String, String> mapping() {
|
||
|
Map<String, String> map = new HashMap<>();
|
||
|
Map<String, Object> templateMappings = SMSXmlReader.getSMSTemplateMappings();
|
||
|
for (Map.Entry<String, Object> entry : templateMappings.entrySet()) {
|
||
|
JSONObject jsonObject = JSONObject.mapFrom(entry.getValue());
|
||
|
if (jsonObject == null) {
|
||
|
continue;
|
||
|
}
|
||
|
map.put(entry.getKey(), jsonObject.getString("content"));
|
||
|
}
|
||
|
return map;
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Response sendTest(String mobile) {
|
||
|
SmsClient.getInstance().send(mobile);
|
||
|
return Response.create(Response.RES_STATUS_SUCCESS, "新插件发送测试短信成功", JSONObject.create());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
@ExecuteFunctionRecord
|
||
|
public Response send(String template, String mobile, JSONObject para, String receiver) throws Exception {
|
||
|
SmsClient.getInstance().send(template, mobile, para, receiver);
|
||
|
FineLoggerFactory.getLogger().info("短信模板:{}, 手机号:{},参数:{}", template, mobile, para);
|
||
|
return Response.create(Response.RES_STATUS_SUCCESS, "新插件发送普通短信成功", JSONObject.create());
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public Response batchSendSMS(String template, List<String> mobiles, JSONArray params, List<String> receivers) throws Exception {
|
||
|
SmsClient.getInstance().batchSend(template, mobiles, params, receivers);
|
||
|
return Response.create(Response.RES_STATUS_SUCCESS, "新插件发送批量短信成功", JSONObject.create());
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 启动发送短信的监听器
|
||
|
*/
|
||
|
private void startListener() {
|
||
|
SMSContext.addSmsListener(new Listener() {
|
||
|
|
||
|
@Override
|
||
|
public void beforeSend(String text, List<String> mobiles, JSONArray params, List<String> receivers) {
|
||
|
System.out.println("发送短信前监听事件");
|
||
|
FineLoggerFactory.getLogger().info("发送短信前监听事件");
|
||
|
}
|
||
|
|
||
|
@Override
|
||
|
public void afterSend(String text, List<String> mobiles, JSONArray params, List<String> receivers, Response response) {
|
||
|
System.out.println("发送短信后监听事件");
|
||
|
FineLoggerFactory.getLogger().info("发送短信后监听事件");
|
||
|
}
|
||
|
});
|
||
|
}
|
||
|
|
||
|
private List<SMSTemplateBean> convertSMSTemplateBeanList(Map<String, Object> map) {
|
||
|
List<SMSTemplateBean> beanList = new ArrayList<>();
|
||
|
for (Object data : map.values()) {
|
||
|
JSONObject json = JSONObject.mapFrom(data);
|
||
|
if (json == null) {
|
||
|
continue;
|
||
|
}
|
||
|
beanList.add(SMSTemplateBean.create(json.getString("id"), json.getString("content"), json.getString("language")));
|
||
|
}
|
||
|
return beanList;
|
||
|
}
|
||
|
}
|