| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package com.uas.sso.util;
- import com.uas.message.mail.service.MailService;
- import com.uas.message.sms.service.SmsService;
- import com.uas.sso.entity.Setting;
- import com.uas.sso.service.SettingService;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import javax.annotation.PostConstruct;
- import java.util.Map;
- /**
- * @author wangmh
- * @create 2018-06-05 17:17
- * @desc 消息工具类
- **/
- @Component
- public class MessageUtils {
- @Autowired
- private SmsService smsService;
- @Autowired
- private MailService mailService;
- @Autowired
- private SettingService settingService;
- private static MessageUtils messageUtils;
- protected final Logger logger = LoggerFactory.getLogger(MessageUtils.class);
- @PostConstruct
- public void init() {
- messageUtils = this;
- }
- /**
- * 发送短信
- * @param templateId 模板id
- * @param mobile 手机号
- * @param data 发送短信适配数据,按顺序添加
- */
- public static void sendSms(String templateId, String mobile, Object... data) {
- try {
- if (StringUtils.isEmpty(mobile)) {
- throw new RuntimeException("用户手机号不存在");
- }
- Setting smsTplId = messageUtils.settingService.findOne(templateId);
- if (StringUtils.isEmpty(smsTplId)) {
- throw new RuntimeException("模板不存在");
- }
- messageUtils.logger.warn("短信发送准备, 模板:{},手机号:{}", templateId, mobile);
- messageUtils.smsService.send(smsTplId.getValue(), mobile, data);
- messageUtils.logger.warn("短信发送完成, 模板:{},手机号:{}", templateId, mobile);
- } catch (Exception e) {
- messageUtils.logger.warn("短信发送失败,{}, 模板:{},手机号:{}", e.getMessage(), templateId, mobile);
- }
- }
- /**
- * 发送邮件
- * @param templateId 模板id
- * @param email 邮箱
- * @param data 发送邮件适配数据,以键值对的形式添加
- */
- public static void sendEmail(String templateId, String email, Map<String, Object> data) {
- try {
- if (StringUtils.isEmpty(email)) {
- throw new RuntimeException("邮箱不存在");
- }
- Setting mailTplId = messageUtils.settingService.findOne(templateId);
- if (StringUtils.isEmpty(mailTplId)) {
- throw new RuntimeException("模板不存在");
- }
- messageUtils.mailService.send(mailTplId.getValue(), email, data);
- } catch (Exception e) {
- messageUtils.logger.warn("邮箱发送失败, {},模板:{},邮箱:{}", e.getMessage(), templateId, email);
- }
- }
- }
|