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.

73 lines
2.6 KiB

package com.fr.plugin.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.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.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Lanlan
* @date 2019/1/29
*/
@FunctionRecorder
public class ThirdSmsService extends AbstractSMSServiceProvider {
public ThirdSmsService() {
startListener();
}
@Override
public Map<String, String> mapping() {
Map<String, String> map = new HashMap<>();
map.put("16", "【短信模板】您的手机验证码为#Verifiecode#,请于10分钟内正确输入。");
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("发送短信后监听事件");
}
});
}
}