diff --git a/src/db/modify.sql b/src/db/modify.sql index 79f642563..dda8f0110 100644 --- a/src/db/modify.sql +++ b/src/db/modify.sql @@ -491,3 +491,15 @@ CREATE TABLE `pmt_directed_bill_code` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 + +CREATE TABLE `sys_mail_send` ( + `id` int(11) NOT NULL, + `mail_address` varchar(100) NOT NULL COMMENT '邮箱地址', + `message_id` varchar(60) NOT NULL COMMENT 'Mailgun Message id', + `status` int(9) NOT NULL DEFAULT 0, + `create_time` datetime not null DEFAULT now(), + `title` varchar(50) NOT NULL comment '邮件标题', + PRIMARY KEY (`id`,`mail_address`), + KEY `mail_address` (`mail_address`), + KEY `id` (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 \ No newline at end of file diff --git a/src/main/java/au/com/royalpay/payment/manage/mappers/system/MailSendMapper.java b/src/main/java/au/com/royalpay/payment/manage/mappers/system/MailSendMapper.java new file mode 100644 index 000000000..1f2151837 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/mappers/system/MailSendMapper.java @@ -0,0 +1,19 @@ +package au.com.royalpay.payment.manage.mappers.system; + +import com.alibaba.fastjson.JSONObject; + +import cn.yixblog.support.mybatis.autosql.annotations.AutoMapper; +import cn.yixblog.support.mybatis.autosql.annotations.AutoSql; +import cn.yixblog.support.mybatis.autosql.annotations.SqlType; + +/** + * Create by yixian at 2017-12-13 19:10 + */ +@AutoMapper(tablename = "sys_mail_send", pkName = "id") +public interface MailSendMapper { + @AutoSql(type = SqlType.INSERT) + void save(JSONObject record); + + @AutoSql(type = SqlType.UPDATE) + void update(JSONObject record); +} diff --git a/src/main/java/au/com/royalpay/payment/manage/system/core/MailService.java b/src/main/java/au/com/royalpay/payment/manage/system/core/MailService.java new file mode 100644 index 000000000..542ec8854 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/system/core/MailService.java @@ -0,0 +1,13 @@ +package au.com.royalpay.payment.manage.system.core; + + +import au.com.royalpay.payment.tools.mail.SendMail; + +import com.alibaba.fastjson.JSONObject; + +public interface MailService { + + void dealNotify(String nofityString) throws Exception; + + JSONObject sendMail(SendMail sendMail); +} diff --git a/src/main/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImpl.java new file mode 100644 index 000000000..be96a9027 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImpl.java @@ -0,0 +1,67 @@ +package au.com.royalpay.payment.manage.system.core.impl; + +import au.com.royalpay.payment.manage.mappers.system.MailSendMapper; +import au.com.royalpay.payment.manage.system.core.MailService; +import au.com.royalpay.payment.tools.mail.MailGunClient; +import au.com.royalpay.payment.tools.mail.SendMail; + +import com.alibaba.fastjson.JSONObject; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.net.URLDecoder; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.Resource; + +@Service +public class MailServiceImpl implements MailService { + Logger logger = LoggerFactory.getLogger(getClass()); + + @Resource + private MailSendMapper mailSendMapper; + + @Resource + private MailGunClient mailGunClient; + + @Override + public void dealNotify(String nofityString) throws Exception { + String dd = URLDecoder.decode(nofityString, "UTF-8"); + Map mailgunNotify = getQueryMap(nofityString); + String myData = mailgunNotify.get("my-custom-data"); + String recipient = mailgunNotify.get("recipient"); + if (StringUtils.isNotEmpty(myData) && StringUtils.isNotEmpty(recipient)) { + JSONObject tmpJSONObject = JSONObject.parseObject(myData); + String[] mailAddresses = recipient.split(","); + for (String mailAddress : mailAddresses) { + JSONObject record = new JSONObject(); + record.put("id", tmpJSONObject.getString("id")); + record.put("mail_address", mailAddress); + record.put("status", 1); + mailSendMapper.update(record); + } + } + } + + @Override + public JSONObject sendMail(SendMail sendMail) { + return mailGunClient.sendMail(sendMail); + } + + public Map getQueryMap(String query) { + String[] params = query.split("&"); + Map map = new HashMap<>(); + for (String param : params) { + String [] tmpArr= param.split("="); + if(tmpArr.length<2){ + continue; + } + map.put(tmpArr[0], tmpArr[1]); + } + return map; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/system/web/MailCallBackController.java b/src/main/java/au/com/royalpay/payment/manage/system/web/MailCallBackController.java index 7b172ce52..250245963 100644 --- a/src/main/java/au/com/royalpay/payment/manage/system/web/MailCallBackController.java +++ b/src/main/java/au/com/royalpay/payment/manage/system/web/MailCallBackController.java @@ -1,22 +1,24 @@ package au.com.royalpay.payment.manage.system.web; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import au.com.royalpay.payment.manage.system.core.MailService; + import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; -import java.util.Map; - -import javax.servlet.http.HttpServletRequest; +import javax.annotation.Resource; @RestController @RequestMapping(value = "/mailgun") public class MailCallBackController { - Logger logger = LoggerFactory.getLogger(getClass()); + + @Resource + private MailService mailService; + @RequestMapping(value = "/callback", method = RequestMethod.POST) - public void contractList(@RequestBody String content, HttpServletRequest req) { - logger.info(content); + public void contractList(@RequestBody String content) throws Exception { + mailService.dealNotify(content); } -} + +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 7a0ba348a..ace9ceb57 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -99,3 +99,4 @@ app.foreign-currency=AUD app.ofei.md5-key=Khjx6wejblaJzmG0JBWFlPFKAUxhFIXQ app.ofei.pwd=aomi@8888 app.ofei.sp-code=A1407200 +mail.mailgun.api_key=api:key-96fa3b5866ace125b8ec5a9d27e19353 diff --git a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java index a12a957e1..adf357c4e 100644 --- a/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java +++ b/src/test/java/au/com/royalpay/payment/manage/apps/core/impls/CustomerImpressionImplTest.java @@ -93,12 +93,41 @@ public class CustomerImpressionImplTest { // } // } + @Test public void sendSimpleMessage() throws Exception { + String url = "https://api.mailgun.net/v3/dev.showcodes.com/messages"; + HttpRequestGenerator generator = new HttpRequestGenerator(url, RequestMethod.POST).addHeader("Authorization", getHeader()); + generator.initFileEntity().attachExternalParam("from", "postmaster@dev.showcodes.com").attachExternalParam("to", "info@dev.showcodes.com") + .attachExternalParam("v:my-custom-data","{\"key\":\"value\"}") + .attachExternalParam("subject", "阿克江啊啊艾克安居客逻辑").attachExternalParam("subscribed", "false") + .attachExternalParam("text", "扯大额卡加二胺二胺傲娇了阿姐拿进来") + .attachExternalParam("html", "

啊看见的那会计师你打卡机难爱的那是的拉屎加上就开始啦看见了

").attachExternalParam("o:tag", "test1"); + + HttpRequestResult res = null; + try { + res = generator.execute(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + System.out.println(res.getException()); + System.out.println(); + System.out.println(); + + try { + System.out.println(res.getResponseContentJSONObj().toJSONString()); + } catch (IOException e) { + e.printStackTrace(); + } + } + + @Test + public void sendSimeMessage() throws Exception { String url = "https://api.mailgun.net/v3/dev.showcodes.com/events"; HttpRequestGenerator generator = new HttpRequestGenerator(url, RequestMethod.POST).addHeader("Authorization", getHeader()); generator.addQueryString("ascending", "yes"); + generator.addQueryString("v:my-custom-data", "123456789"); generator.addQueryString("limit", "12"); System.out.println(String.valueOf(DateUtils.addDays(new Date(),1))); HttpRequestResult res = null; diff --git a/src/test/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImplTest.java b/src/test/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImplTest.java new file mode 100644 index 000000000..2f423b10a --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/system/core/impl/MailServiceImplTest.java @@ -0,0 +1,30 @@ +package au.com.royalpay.payment.manage.system.core.impl; + +import au.com.royalpay.payment.manage.system.core.MailService; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +import javax.annotation.Resource; + +@SpringBootTest +@ActiveProfiles({ "local", "alipay", "wechat", "jd", "bestpay" }) +@RunWith(SpringRunner.class) +public class MailServiceImplTest { + + @Resource + MailService mailService; + @Test + public void dealNotify() { + String dd= "timestamp=1526298391&token=b7ad8dc0905c46b1c4853200941d30417b4d3f3695815dc729&signature=a3507be4d7ad2b48c4b57202eabd6f63b75abfacd974a9bc7773dc968a12c34e&X-Mailgun-Sid=WyJlMjYwYSIsICI0NjQ1NjMxMThAcXEuY29tIiwgIjQzODY0NiJd&domain=dev.showcodes.com&X-Mailgun-Tag=test1&event=delivered&event-timestamp=1526298391.16&message-headers=[[\"X-Mailgun-Sending-Ip\", \"184.173.153.207\"], [\"X-Mailgun-Sid\", \"WyJlMjYwYSIsICI0NjQ1NjMxMThAcXEuY29tIiwgIjQzODY0NiJd\"], [\"List-Unsubscribe\", \"\"], [\"Received\", \"by luna.mailgun.net with SMTP X-Mailgun-List-Id=5544171, 8794346058393; Mon, 14 May 2018 11:46:29 +0000\"], [\"X-Mailgun-List-Id\", \"5544171\"], [\"X-Mailgun-List-Address\", \"info@dev.showcodes.com\"], [\"List-Id\", \"\"], [\"Received\", \"by luna.mailgun.net with HTTP; Mon, 14 May 2018 11:46:27 +0000\"], [\"Date\", \"Mon, 14 May 2018 11:46:27 +0000\"], [\"Sender\", \"postmaster@dev.showcodes.com\"], [\"X-Mailgun-Variables\", \"{\\\"my-custom-data\\\": \\\"{\\\\\\\"key\\\\\\\":\\\\\\\"value\\\\\\\"}\\\"}\"], [\"X-Mailgun-Tag\", \"test1\"], [\"From\", \"postmaster@dev.showcodes.com\"], [\"Subject\", \"Testqweasdzxc1\"], [\"Mime-Version\", \"1.0\"], [\"Content-Type\", [\"multipart/alternative\", {\"boundary\": \"12109fc0295a479385fcbed01f501455\"}]], [\"Message-Id\", \"<20180514114629.1.2CEFA1D27B4659C4@dev.showcodes.com>\"], [\"To\", \"yz <464563118@qq.com>\"]]&Message-Id=<20180514114629.1.2CEFA1D27B4659C4@dev.showcodes.com>&recipient=464563118@qq.com&my-custom-data={\"id\":\"123\"}&body-plain="; + try { + mailService.dealNotify(dd); + + } catch (Exception e) { + e.printStackTrace(); + } + } +} \ No newline at end of file