Merge branch 'dev' of https://gitee.com/log4j/pig into dev

This commit is contained in:
冷冷 2018-05-14 20:37:04 +08:00
commit fde8a949d0
9 changed files with 109 additions and 78 deletions

View File

@ -0,0 +1,39 @@
package com.github.pig.common.constant.enums;
import lombok.Getter;
import lombok.Setter;
/**
* @author LiXunHuan
* @date 2018/1/16
* 短信通道模板
*/
public enum EnumSmsChannelTemplate {
/**
* 登录验证
*/
LOGIN_NAME_LOGIN("loginCodeChannel", "登录验证"),
/**
* 服务异常提醒
*/
SERVICE_STATUS_CHANGE("serviceStatusChange", "Pig4Cloud");
/**
* 模板名称
*/
@Getter
@Setter
private String template;
/**
* 模板签名
*/
@Getter
@Setter
private String signName;
EnumSmsChannelTemplate(String template, String signName) {
this.template = template;
this.signName = signName;
}
}

View File

@ -16,17 +16,32 @@ public class MobileMsgTemplate implements Serializable {
*/
private String mobile;
/**
* 文本
* 组装后的模板内容JSON字符串
*/
private String text;
private String context;
/**
* 类型通道
* 短信通道
*/
private String channel;
/**
* 短信类型(验证码或者通知短信)
* 暂时不用留着后面存数据库备用吧
*/
private String type;
/**
* 短信签名
*/
private String signName;
/**
* 短信模板
*/
private String template;
public MobileMsgTemplate(String mobile, String text, String type) {
public MobileMsgTemplate(String mobile, String context, String channel, String signName, String template){
this.mobile = mobile;
this.text = text;
this.type = type;
this.context = context;
this.channel = channel;
this.signName = signName;
this.template = template;
}
}

View File

@ -1,6 +1,5 @@
package com.github.pig.mc.handler;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
@ -12,8 +11,6 @@ import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.Assert;
import com.github.pig.common.util.template.MobileMsgTemplate;
import com.github.pig.mc.config.SmsAliyunPropertiesConfig;
import com.github.pig.mc.utils.constant.SmsChannelTemplateConstant;
import com.github.pig.mc.utils.sms.EnumSmsChannelTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -39,7 +36,7 @@ public class SmsAliyunMessageHandler extends AbstractMessageHandler {
@Override
public void check(MobileMsgTemplate mobileMsgTemplate) {
Assert.isBlank(mobileMsgTemplate.getMobile(), "手机号不能为空");
Assert.isBlank(mobileMsgTemplate.getText(), "验证码不能为空");
Assert.isBlank(mobileMsgTemplate.getContext(), "短信内容不能为空");
}
/**
@ -67,20 +64,21 @@ public class SmsAliyunMessageHandler extends AbstractMessageHandler {
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(mobileMsgTemplate.getMobile());
//必填:短信签名-可在短信控制台中找到
request.setSignName(EnumSmsChannelTemplate.LOGIN_NAME_LOGIN.getDescription());
request.setSignName(mobileMsgTemplate.getSignName());
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(smsAliyunPropertiesConfig.getChannels().get(SmsChannelTemplateConstant.LOGIN_NAME_LOGIN));
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}",此处的值为
JSONObject jsonObject = new JSONObject();
jsonObject.put("product", "pig_cloud");
jsonObject.put("code", mobileMsgTemplate.getText());
request.setTemplateParam(jsonObject.toJSONString());
request.setTemplateCode(smsAliyunPropertiesConfig.getChannels().get(mobileMsgTemplate.getTemplate()));
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"
request.setTemplateParam(mobileMsgTemplate.getContext());
request.setOutId(mobileMsgTemplate.getMobile());
//hint 此处可能会抛出异常注意catch
try {
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
log.info("短信发送完毕,手机号:{},返回状态:{}", mobileMsgTemplate.getMobile(), sendSmsResponse.getCode());
} catch (ClientException e) {
log.error("发送异常");
e.printStackTrace();

View File

@ -26,7 +26,7 @@ public class MobileCodeReceiveListener {
@RabbitHandler
public void receive(MobileMsgTemplate mobileMsgTemplate) {
long startTime = System.currentTimeMillis();
log.info("消息中心接收到短信发送请求-> 手机号:{} -> 验证码: {} ", mobileMsgTemplate.getMobile(), mobileMsgTemplate.getText());
log.info("消息中心接收到短信发送请求-> 手机号:{} -> 验证码: {} ", mobileMsgTemplate.getMobile(), mobileMsgTemplate.getContext());
String type = mobileMsgTemplate.getType();
SmsMessageHandler messageHandler = messageHandlerMap.get(type);
messageHandler.execute(mobileMsgTemplate);

View File

@ -2,11 +2,15 @@ package com.github.pig.mc.listener;
import com.github.pig.common.constant.MqQueueConstant;
import com.github.pig.common.util.template.MobileMsgTemplate;
import com.github.pig.mc.handler.SmsMessageHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author lengleng
* @date 2018年01月25日15:59:00
@ -16,10 +20,17 @@ import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = MqQueueConstant.MOBILE_SERVICE_STATUS_CHANGE)
public class MobileServiceChangeReceiveListener {
@Autowired
private Map<String, SmsMessageHandler> messageHandlerMap;
@RabbitHandler
public void receive(MobileMsgTemplate mobileMsgTemplate) {
long startTime = System.currentTimeMillis();
log.info("消息中心接收到短信发送请求-> 手机号:{} -> 验证码: {} ", mobileMsgTemplate.getMobile(), mobileMsgTemplate.getText());
log.info("消息中心接收到短信发送请求-> 手机号:{} -> 信息体:{} ", mobileMsgTemplate.getMobile(), mobileMsgTemplate.getContext());
String type = mobileMsgTemplate.getType();
SmsMessageHandler messageHandler = messageHandlerMap.get(type);
messageHandler.execute(mobileMsgTemplate);
long useTime = System.currentTimeMillis() - startTime;
log.info("调用 {} 短信网关处理完毕,耗时 {}毫秒", mobileMsgTemplate.getType(), useTime);
}

View File

@ -1,13 +0,0 @@
package com.github.pig.mc.utils.constant;
/**
* @author lengleng
* @date 2018/1/16
* 短信通道模板常量
*/
public interface SmsChannelTemplateConstant {
/**
* 登录验证码
*/
String LOGIN_NAME_LOGIN = "loginCodeChannel";
}

View File

@ -1,42 +0,0 @@
package com.github.pig.mc.utils.sms;
/**
* @author lengleng
* @date 2018/1/16
* 短信通道模板
*/
public enum EnumSmsChannelTemplate {
/**
* 登录验证
*/
LOGIN_NAME_LOGIN("loginCodeChannel", "登录验证");
/**
* 模板名称
*/
private String name;
/**
* 模板签名
*/
private String description;
EnumSmsChannelTemplate(String name, String description) {
this.name = name;
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}

View File

@ -1,5 +1,6 @@
package com.github.pig.admin.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
@ -17,6 +18,8 @@ import com.github.pig.common.bean.interceptor.DataScope;
import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.constant.MqQueueConstant;
import com.github.pig.common.constant.SecurityConstants;
import com.github.pig.common.constant.enums.EnumSmsChannel;
import com.github.pig.common.constant.enums.EnumSmsChannelTemplate;
import com.github.pig.common.util.Query;
import com.github.pig.common.util.R;
import com.github.pig.common.util.UserUtils;
@ -181,8 +184,18 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
String code = RandomUtil.randomNumbers(4);
JSONObject contextJson = new JSONObject();
contextJson.put("code", code);
contextJson.put("product", "Pig4Cloud");
log.info("短信发送请求消息中心 -> 手机号:{} -> 验证码:{}", mobile, code);
rabbitTemplate.convertAndSend(MqQueueConstant.MOBILE_CODE_QUEUE, new MobileMsgTemplate(mobile, code, CommonConstant.ALIYUN_SMS));
rabbitTemplate.convertAndSend(MqQueueConstant.MOBILE_CODE_QUEUE,
new MobileMsgTemplate(
mobile,
contextJson.toJSONString(),
EnumSmsChannel.ALIYUN.getName(),
EnumSmsChannelTemplate.LOGIN_NAME_LOGIN.getSignName(),
EnumSmsChannelTemplate.LOGIN_NAME_LOGIN.getTemplate()
));
redisTemplate.opsForValue().set(SecurityConstants.DEFAULT_CODE_KEY + mobile, code, SecurityConstants.DEFAULT_IMAGE_EXPIRE, TimeUnit.SECONDS);
return new R<>(true);
}

View File

@ -3,7 +3,7 @@ package com.github.pig.monitor.filter;
import com.alibaba.fastjson.JSONObject;
import com.github.pig.common.constant.MqQueueConstant;
import com.github.pig.common.constant.enums.EnumSmsChannel;
import com.github.pig.common.util.template.DingTalkMsgTemplate;
import com.github.pig.common.constant.enums.EnumSmsChannelTemplate;
import com.github.pig.common.util.template.MobileMsgTemplate;
import com.github.pig.monitor.config.MonitorPropertiesConfig;
import com.xiaoleilu.hutool.collection.CollectionUtil;
@ -60,12 +60,22 @@ public class StatusChangeNotifier extends AbstractStatusChangeNotifier {
event.getApplication().getId(), ((ClientApplicationStatusChangedEvent) event).getTo().getStatus());
String text = String.format("应用:%s 服务ID:%s 下线,时间:%s", event.getApplication().getName(), event.getApplication().getId(), DateUtil.date(event.getTimestamp()).toString());
JSONObject contextJson = new JSONObject();
contextJson.put("name", event.getApplication().getName());
contextJson.put("seid", event.getApplication().getId());
contextJson.put("time", DateUtil.date(event.getTimestamp()).toString());
//开启短信通知
if (monitorMobilePropertiesConfig.getMobile().getEnabled()) {
log.info("开始短信通知,内容:{}", text);
rabbitTemplate.convertAndSend(MqQueueConstant.MOBILE_SERVICE_STATUS_CHANGE,
new MobileMsgTemplate(CollectionUtil.join(monitorMobilePropertiesConfig.getMobile().getMobiles(), ","),
text, EnumSmsChannel.ALIYUN.getName()));
new MobileMsgTemplate(
CollectionUtil.join(monitorMobilePropertiesConfig.getMobile().getMobiles(), ","),
contextJson.toJSONString(),
EnumSmsChannel.ALIYUN.getName(),
EnumSmsChannelTemplate.SERVICE_STATUS_CHANGE.getSignName(),
EnumSmsChannelTemplate.SERVICE_STATUS_CHANGE.getTemplate()
));
}
if (monitorMobilePropertiesConfig.getDingTalk().getEnabled()) {