All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mingsoft.msend.util.SendUtil Maven / Gradle / Ivy

There is a newer version: 2.2.5
Show newest version
/**
 * The MIT License (MIT)
 * Copyright (c) 2020 铭软科技(mingsoft.net)
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package net.mingsoft.msend.util;

import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.basic.util.SpringUtil;
import net.mingsoft.basic.util.StringUtil;
import net.mingsoft.msend.biz.ILogBiz;
import net.mingsoft.msend.biz.IMailBiz;
import net.mingsoft.msend.biz.ISmsBiz;
import net.mingsoft.msend.biz.ITemplateBiz;
import net.mingsoft.msend.constant.e.SendEnum;
import net.mingsoft.msend.constant.e.SmsEnum;
import net.mingsoft.msend.entity.LogEntity;
import net.mingsoft.msend.entity.MailEntity;
import net.mingsoft.msend.entity.SmsEntity;
import net.mingsoft.msend.entity.TemplateEntity;

public class SendUtil {

	private static final Logger LOG = LoggerFactory.getLogger(SendUtil.class);


	/**
	 * 发送
	 *
	 * @param code
	 *            模板编码
	 * @param receive
	 *            接收用户
	 * @param values
	 *            替换的内容参数
	 * @param type
	 *            发送类型sms|mail|
	 * @return
	 */
	public static boolean send(String code, String receive, Map values, String type,MailEntity.SendTypeEnum sendType) {

		ITemplateBiz templateBiz = SpringUtil.getBean(ITemplateBiz.class);
		TemplateEntity template = new TemplateEntity();
		template.setTemplateCode(code);
		template = (TemplateEntity) templateBiz.getEntity(template);

		String mailContent = template.getTemplateMail();
		if(type.equals(SendEnum.MAIL.toString())){
			if (template.getIntId() > 0) {
				if (values != null) {
					Iterator it = values.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next() + "";
						if (values.get(key) instanceof String) {
							mailContent = mailContent.replaceAll("\\{" + key + "/\\}", values.get(key));
						}
					}
				}
				LOG.debug(code + "send  to:" + receive + " content:" + mailContent);
				// 如果实体不为空就获取邮箱模板的标题和内容一起发送指定的邮箱地址
			} else {
				LOG.error("发送模板不存在");
				return false;
			}
		}
		if (type.equalsIgnoreCase(SendEnum.MAIL.toString())) {
			return SendUtil.sendMail(sendType, template.getTemplateTitle(), mailContent, receive.split(","),
					template);
		} else if (type.equalsIgnoreCase(SendEnum.SMS.toString())) {
			return SendUtil.sendSms(code, receive, values, template);
		}
		return true;
	}

	/**
	 * 发送邮件
	 * 
	 * @param sendType
	 *            邮件类型(MailEntity.TEXT MailEntity.HTML)
	 * @param title
	 *            标题
	 * @param content
	 *            内容
	 * @param toUser
	 *            接收用户
	 * @param template
	 * 			  模板内容
	 */
	private static boolean sendMail(MailEntity.SendTypeEnum sendType, String title, String content, String[] toUser,
			TemplateEntity template) {
		IMailBiz mailBiz = SpringUtil.getBean(IMailBiz.class);
		ILogBiz logBiz = SpringUtil.getBean(ILogBiz.class);
		MailEntity mail = mailBiz.getFirstEntity();
		LogEntity log = new LogEntity();
		if (mail == null) {
			LOG.error("没有配置邮件服务器");
			return false;
		}
		if (mail.getMailType()!= null && mail.getMailType().equals(MailEntity.MailType.SENDCLOUD)) {
			try {
				String _toUser = "";
				for (int i = 0; i < toUser.length; i++) {
					if (StringUtil.isEmail(toUser[i])) {
						_toUser += toUser[i];
						if (i < toUser.length) {
							_toUser += ";";
						}
					}
				}
				boolean flag = false;
				flag = SendcloudUtil.sendMail(mail.getMailName(), mail.getMailPassword(), mail.getMailForm(),
						mail.getMailFormName(), _toUser, title, content);
				if(flag){
					log.setLogType(SendEnum.MAIL.toInt());
					log.setLogDatetime(new Date());
					log.setLogContent("mail类型"+template.getTemplateCode());
					log.setLogReceive(_toUser.replace(";",""));
					logBiz.saveEntity(log);
				}
				return flag;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else {
			if (sendType == MailEntity.SendTypeEnum.TEXT) {
				MailUtil.sendText(mail.getMailServer(), mail.getMailPort(), mail.getMailName(), mail.getMailPassword(),
						title, content, toUser);
			} else if (sendType == MailEntity.SendTypeEnum.HTML) {
				MailUtil.sendHtml(mail.getMailServer(), mail.getMailPort(), mail.getMailName(), mail.getMailPassword(),mail.getMailFormName(),
						title, content, toUser);
			}
			for(int i = 0; i < toUser.length ; i++){
				log.setLogType(SendEnum.MAIL.toInt());
				log.setLogDatetime(new Date());
				log.setLogContent("mail类型");
				log.setLogReceive(toUser[i]);
				logBiz.saveEntity(log);
			}
			return true;
		}
		return false;

	}

	/**
	 * 
	 * @param code
	 *            模块编号,
	 * @param phone
	 *            接收手机号,多个手机号逗号隔开
	 * @param values
	 *            根据values.key值替换替换模版里面内容的{key/},
	 * @param template
	 * 			  模板内容
	 */
	private static boolean sendSms(String code, String phone, Map values, TemplateEntity template) {
		ISmsBiz smsBiz = SpringUtil.getBean(ISmsBiz.class);
		ILogBiz logBiz = SpringUtil.getBean(ILogBiz.class);
		SmsEntity sms = smsBiz.getFirstEntity();
		if(ObjectUtil.isNull(sms)){
			LOG.error("没有设置短信配置");
			return false;
		}
		if (sms.getSmsType().equals(MailEntity.MailType.SENDCLOUD)) {
			String templateSms = template.getTemplateSms();
			if (!StringUtil.isInteger(templateSms)) {
				LOG.error("sendcloud 的模板id不正确");
				return false;
			}
			LOG.debug(code + "send sms to:" + phone + " 模板ID:" + templateSms);
			try {
				boolean flag = false;
				String[] phones = phone.split(",");
				for (int i = 0; i < phones.length; i++) {
					flag = SendcloudUtil.sendSms(sms.getSmsUsername(), sms.getSmsPassword(),
							Integer.parseInt(templateSms), "0", phones[i], JSONArray.toJSONString(values));
					if (flag) {
						LogEntity log = new LogEntity();
						log.setLogType(SendEnum.SMS.toInt());
						log.setLogDatetime(new Date());
						log.setLogContent("模板编号:" + template.getTemplateCode());
						log.setLogReceive(phones[i]);
						logBiz.saveEntity(log);
					} else {
						LOG.error("发送失败:" +  phones[i]);
						break;
					}
				}
				return flag;
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else { // 普通通过post 地址的方式请求
			String mailContent = template.getTemplateSms();
			 if (values != null) {
				if (template.getIntId() > 0) {
					Iterator it = values.keySet().iterator();
					while (it.hasNext()) {
						String key = it.next() + "";
						if (values.get(key) instanceof String) {
							mailContent = mailContent.replaceAll("\\{" + key + "/\\}", values.get(key));
						}
					}
				} else {
					LOG.error("发送模板不存在");
					return false;
				}
			 }
			 String smsUrl = sms.getSmsSendUrl().replace(SmsEnum.CONTENT.toString(), mailContent).replace(SmsEnum.PHONE.toString(), phone);
			 String result = HttpUtil.post(smsUrl, "");
			 LOG.debug("消息发送结果"+result);
			 String[] phones = phone.split(",");
				for (int i = 0; i < phones.length; i++) {
					LogEntity log = new LogEntity();
					log.setLogType(SendEnum.SMS.toInt());
					log.setLogDatetime(new Date());
					log.setLogContent("模板编号:" + template.getTemplateCode());
					log.setLogReceive(phones[i]);
					logBiz.saveEntity(log);
				}
			 return true;
		}

		return false;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy