From de7a193402e8923008e950c68512712630533347 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Wed, 3 Jan 2018 18:24:51 +0800 Subject: [PATCH 1/8] Jpush --- pom.xml | 15 ++ .../appclient/core/RetailAppService.java | 2 - .../core/impls/RetailAppServiceImp.java | 188 ++++++------------ .../core/impls/ClientManagerImpl.java | 12 +- .../manage/pushMessage/APNSMessageHelper.java | 46 +++++ .../pushMessage/JpushMessageHelper.java | 88 ++++++++ .../manage/pushMessage/bean/JpushMessage.java | 44 ++++ 7 files changed, 263 insertions(+), 132 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/pushMessage/APNSMessageHelper.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java create mode 100644 src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/JpushMessage.java diff --git a/pom.xml b/pom.xml index 0973a0f03..689bc1b0e 100644 --- a/pom.xml +++ b/pom.xml @@ -132,6 +132,21 @@ com.alibaba druid + + + + cn.jpush.api + jpush-client + 3.3.3 + + + com.google.code.gson + gson + 2.2.4 + + + + diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/RetailAppService.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/RetailAppService.java index 108d09593..2029c8870 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/RetailAppService.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/RetailAppService.java @@ -32,8 +32,6 @@ public interface RetailAppService { void sign_out(JSONObject device); - void sendAppMessage(); - void sendOrderAppMessage(JSONObject order, int client_id); void sendRefundAppMessage(String refundAmount, JSONObject order); diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index f022a5aa3..219893af7 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -19,6 +19,7 @@ import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.notice.beans.NoticeInfo; import au.com.royalpay.payment.manage.notice.core.NoticeManage; import au.com.royalpay.payment.manage.notice.core.NoticePartner; +import au.com.royalpay.payment.manage.pushMessage.APNSMessageHelper; import au.com.royalpay.payment.manage.signin.beans.ChangePwdBean; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.signin.core.impls.SignInAccountServiceImpl; @@ -77,77 +78,55 @@ public class RetailAppServiceImp implements RetailAppService { private TransactionAnalysisMapper transactionAnalysisMapper; @Resource private TransactionMapper transactionMapper; - @Resource private ClientManager clientManager; - @Resource private NoticeManage noticeManage; - @Resource private NoticePartner noticePartner; - @Resource private CustomerAndOrdersStatisticsMapper customerAndOrdersStatisticsMapper; - @Resource private ClearingDetailMapper clearingDetailMapper; - @Resource private OrderMapper orderMapper; - @Resource private ClientAccountMapper clientAccountMapper; - @Resource private AppMessageLogMapper appMessageLogMapper; @Resource private SignInAccountService signInAccountService; - @Resource private CashbackService cashbackService; - @Resource private ClearingDetailAnalysisMapper clearingDetailAnalysisMapper; - @Resource private ClientSettleDelayConfMapper clientSettleDelayConfMapper; - @Resource private XPlanFundConfigServiceImpl xPlanFundConfigService; - @Resource private MerchantInfoProvider merchantInfoProvider; @Resource private RoyalPayCMSSupport royalPayCMSSupport; - @Resource private PaymentApi paymentApi; - @Resource private DeviceSupport deviceSupport; - @Resource private CustomerRelationAlipayMapper customerRelationAlipayMapper; @Resource private CustomerMapper customerMapper; - - @Value("classpath:apple_message_cert_dev.p12") - private org.springframework.core.io.Resource appleMessageFileDev; - @Value("classpath:apple_message_cert_product.p12") - private org.springframework.core.io.Resource appleMessageFileProduct; - @Value("${apple.message.apns.password}") - private String appleMessagePassword; + @Resource + private APNSMessageHelper apnsMessageHelper; @Resource private ClientDeviceTokenMapper clientDeviceTokenMapper; private ThreadPoolExecutor sendingAppleMsgPool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); - @Resource private CouponValidateService couponVerificationService; - //若app未传client_ids,检测是否包含子商户,若包含查询全部 + // 若app未传client_ids,检测是否包含子商户,若包含查询全部 private void setAllClientIds(JSONObject params, int clientId) { if (params.containsKey("client_ids")) { return; @@ -175,10 +154,10 @@ public class RetailAppServiceImp implements RetailAppService { params.put("client_id", client_id); res.put("trade_amount", transactionAnalysisMapper.analysisTotalAmount(params)); res.put("trade_count", transactionAnalysisMapper.analysisTotalCount(params)); -// res.put("refund_amount", transactionAnalysisMapper.analysisRefundAmount(params)); + // res.put("refund_amount", transactionAnalysisMapper.analysisRefundAmount(params)); res.put("not_settled", transactionAnalysisMapper.analysisNotSettled(params)); res.put("old_customers", transactionAnalysisMapper.countOldCustomers(params)); - //微信小程序不查询汇率 + // 微信小程序不查询汇率 if (!StringUtils.equals(device.getString("client_type"), "wxapp")) { res.put("rate", paymentApi.channelApi("Wechat").queryExchangeRateDecimal(client_id)); } @@ -266,7 +245,6 @@ public class RetailAppServiceImp implements RetailAppService { } } - @Override public JSONObject getClientSettlementLog(JSONObject device, AppQueryBean appQueryBean) { String clientType = device.getString("client_type"); @@ -280,7 +258,8 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject params = appQueryBean.toParams(); setAllClientIds(params, client_id); params.put("client_id", client_id); - PageList logs = clearingDetailMapper.listClientSettlementLog(params, new PageBounds(appQueryBean.getPage(), appQueryBean.getLimit(), Order.formString("report_date.desc"))); + PageList logs = clearingDetailMapper.listClientSettlementLog(params, + new PageBounds(appQueryBean.getPage(), appQueryBean.getLimit(), Order.formString("report_date.desc"))); return PageListUtils.buildPageListResult(logs); } @@ -354,11 +333,13 @@ public class RetailAppServiceImp implements RetailAppService { params.put("dev_id", device.getString("dev_id")); } params.put("client_id", client_id); - PageList orders = orderMapper.listOrdersByClients(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); + PageList orders = orderMapper.listOrdersByClients(params, + new PageBounds(query.getPage(), query.getLimit(), Order.formString("create_time.desc"))); TimeZoneUtils.switchTimeZone(orders, query.getTimezone(), "create_time", "transaction_time", "confirm_time"); ArrayList date_contains = new ArrayList<>(); for (JSONObject order : orders) { -// Date tradeDate = order.getDate("confirm_time") == null ? (order.getDate("transaction_time") == null ? order.getDate("create_time") : order.getDate("transaction_time")) : order.getDate("confirm_time"); + // Date tradeDate = order.getDate("confirm_time") == null ? (order.getDate("transaction_time") == null ? + // order.getDate("create_time") : order.getDate("transaction_time")) : order.getDate("confirm_time"); Calendar calendar = (Calendar) order.get("create_time"); String trade_date = DateFormatUtils.format(calendar, "yyyy-MM-dd"); String trade_time = DateFormatUtils.format(calendar, "HH:mm:ss"); @@ -369,7 +350,7 @@ public class RetailAppServiceImp implements RetailAppService { TradeLogQuery date_query = new TradeLogQuery(); date_query.setDatefrom(re_date); date_query.setDateto(re_date); -// date_query.setStatus(OrderStatus.ALL); + // date_query.setStatus(OrderStatus.ALL); if (params.containsKey("client_ids")) { date_query.setClient_ids((String[]) params.get("client_ids")); } @@ -388,7 +369,7 @@ public class RetailAppServiceImp implements RetailAppService { query.setGateway_app("11"); String clientType = device.getString("client_type"); DeviceRegister register = deviceSupport.findRegister(clientType); - Assert.notNull(register,"Device is not registered"); + Assert.notNull(register, "Device is not registered"); int client_id = device.getIntValue("client_id"); JSONObject client = clientManager.getClientInfo(client_id); String timezone = client.getString("timezone"); @@ -455,31 +436,6 @@ public class RetailAppServiceImp implements RetailAppService { } - @Override - public void sendAppMessage() { - -// String p12Path = "D:/MbaikeDevCertificates.p12"; -// InputStream keyIns = appleMessageFile.getInputStream(); -// String password = "F1b5*ChJPp73"; - String pushToken = "719e4b7a7ed8b9000188440228437714d9dc67785f1791fae01234dae6a1570c"; - JSONObject order = new JSONObject(); - order.put("clearing_amount", 0.01); - order.put("client_id", 9); - order.put("client_moniker", "PINE"); - order.put("order_id", "9201704110348156694986"); - - try { - /**设置参数,发送数据**/ - InputStream keyIns = appleMessageFileProduct.getInputStream(); - ApnsService service = APNS.newService().withCert(keyIns, appleMessagePassword).withProductionDestination().build(); - String payload = APNS.newPayload().alertTitle("到账通知").alertBody("Hello,0.01AUD").badge(1).sound("default").customField("data", order).build(); - service.push(pushToken, payload); - System.out.println("推送信息已发送!"); - } catch (Exception e) { - logger.error("出错了:" + e.getMessage()); - } - } - @Override public void sendOrderAppMessage(final JSONObject order, int client_id) { logger.debug("sendOrderAppMessage-" + client_id + "-" + order.getString("order_id")); @@ -491,21 +447,23 @@ public class RetailAppServiceImp implements RetailAppService { return; } System.out.println("Device Token:" + token); - //todo 抽象发送信息接口,根据设备类型选择不同实现类 - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "payment", token, order.getString("order_id")); + // todo 抽象发送信息接口,根据设备类型选择不同实现类 + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "payment", token, + order.getString("order_id")); try { order.put("send_type", "payment"); JSONObject type = new JSONObject(); type.put("send_type", "payment"); type.put("id", order.getString("order_id")); - sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), - LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), token, order, type); + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), + LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), token, + order, type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } }; sendingAppleMsgPool.execute(task); @@ -536,14 +494,15 @@ public class RetailAppServiceImp implements RetailAppService { return; } System.out.println("Device Token:" + token); - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "refund", token, order.getString("order_id")); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "refund", token, + order.getString("order_id")); try { order.put("send_type", "refund"); JSONObject type = new JSONObject(); type.put("send_type", "refund"); type.put("id", order.getString("order_id")); - sendAppleMessage("Refund Message", - LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, token, order, type); + apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, token, + order, type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -567,7 +526,8 @@ public class RetailAppServiceImp implements RetailAppService { if (token == null) { continue; } - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "notice", token, "notice_id:" + notice.getNotice_id()); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "notice", token, + "notice_id:" + notice.getNotice_id()); try { notice.setContent(null); JSONObject noticeObj = notice.toJson(); @@ -575,7 +535,8 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "notice"); type.put("id", notice.getNotice_id()); - sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, type); + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, + type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -600,19 +561,22 @@ public class RetailAppServiceImp implements RetailAppService { if (token == null) { return; } - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "cashback", token, order.getString("order_id") + "返现" + cashbackAmount); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "cashback", token, + order.getString("order_id") + "返现" + cashbackAmount); try { JSONObject type = new JSONObject(); type.put("send_type", "cashback"); type.put("id", order.getString("order_id")); - sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), - LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + cashbackAmount, token, order, type); + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), + LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + + cashbackAmount, + token, order, type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } } }); @@ -625,7 +589,8 @@ public class RetailAppServiceImp implements RetailAppService { String clearing_amount = log_clearing_detail.getBigDecimal("clearing_amount").setScale(2, BigDecimal.ROUND_HALF_DOWN).toString(); String settle_date_from = DateFormatUtils.format(log_clearing_detail.getDate("settle_date_from"), "MM/dd/yyyy"); String settle_date_to = DateFormatUtils.format(log_clearing_detail.getDate("settle_date_to"), "MM/dd/yyyy"); - logger.debug("sendCleanMessage-" + log_clearing_detail.getString("client_moniker") + "- " + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + "(" + settle_date_from + "-" + settle_date_to + ")"); + logger.debug("sendCleanMessage-" + log_clearing_detail.getString("client_moniker") + "- " + PlatformEnvironment.getEnv().getForeignCurrency() + " " + + clearing_amount + "(" + settle_date_from + "-" + settle_date_to + ")"); List tokens = clientDeviceTokenMapper.listTokensByClient_id(client_id); for (JSONObject devToken : tokens) { String token = devToken.getString("token"); @@ -633,24 +598,25 @@ public class RetailAppServiceImp implements RetailAppService { continue; } String log_clearing_detail_id = log_clearing_detail.getString("clear_detail_id"); - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "clean", token, "本次清算" + log_clearing_detail_id + ",$" + clearing_amount + "(" + settle_date_from + "-" + settle_date_to + ")"); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "clean", token, + "本次清算" + log_clearing_detail_id + ",$" + clearing_amount + "(" + settle_date_from + "-" + settle_date_to + ")"); try { JSONObject type = new JSONObject(); type.put("send_type", "clean"); type.put("id", log_clearing_detail_id); - sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), - LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + " (" + settle_date_from + "-" + settle_date_to + ")", token, log_clearing_detail, type); + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), LocaleSupport.localeMessage("app.message.body.clean") + + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + " (" + settle_date_from + "-" + settle_date_to + ")", + token, log_clearing_detail, type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } } } - @Override public void sendTransactionDailyMessage(JSONObject tradeInfo, int client_id) { String trade_date = tradeInfo.getString("trade_date"); @@ -663,50 +629,26 @@ public class RetailAppServiceImp implements RetailAppService { if (token == null) { continue; } - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "daily_notice", token, "Trade Date:" + trade_date); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "daily_notice", token, + "Trade Date:" + trade_date); try { JSONObject type = new JSONObject(); type.put("send_type", "daily_notice"); type.put("id", trade_date); - sendAppleMessageDetail(LocaleSupport.localeMessage("app.message.title.daily_notice"), - trade_date + ": 总交易额 " + PlatformEnvironment.getEnv().getForeignCurrency() + total_amount + ", 订单数:" + total_orders + ", 付款人数:" + customers, token, tradeInfo, type); + apnsMessageHelper.sendAppleMessageDetail( + LocaleSupport.localeMessage("app.message.title.daily_notice"), trade_date + ": 总交易额 " + + PlatformEnvironment.getEnv().getForeignCurrency() + total_amount + ", 订单数:" + total_orders + ", 付款人数:" + customers, + token, tradeInfo, type); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } } } - private void sendAppleMessage(String title, String body, String token, JSONObject data, JSONObject type) throws IOException { - /**设置参数,发送数据**/ - InputStream keyIns = appleMessageFileProduct.getInputStream(); - ApnsService service = APNS.newService().withCert(keyIns, appleMessagePassword).withProductionDestination().build(); - String payload = APNS.newPayload().alertTitle(title).alertBody(body).badge(1).sound("default").customField("data", data).customField("type", type).build(); - service.push(token, payload); - System.out.println("推送信息已发送!"); - } - - /** - * 可显示详情 - * - * @param title - * @param body - * @param token - * @param data - * @param type - * @throws IOException - */ - private void sendAppleMessageDetail(String title, String body, String token, JSONObject data, JSONObject type) throws IOException { - /**设置参数,发送数据**/ - InputStream keyIns = appleMessageFileProduct.getInputStream(); - ApnsService service = APNS.newService().withCert(keyIns, appleMessagePassword).withProductionDestination().build(); - String payload = APNS.newPayload().alertTitle(title).alertBody(body).category("myNotificationCategory").badge(1).sound("default").customField("data", data).customField("type", type).build(); - service.push(token, payload); - System.out.println("推送信息已发送!"); - } @Override public void updateReadStatus(JSONObject device, String noticeId) { @@ -747,7 +689,7 @@ public class RetailAppServiceImp implements RetailAppService { Document doc = Jsoup.parse(res.getString("content")); String father = "
"; String html = doc.body().children().wrap(father).html(); -// logger.debug("wrapped html---->"+html); + // logger.debug("wrapped html---->"+html); res.put("content", doc.html()); return res; } @@ -756,7 +698,7 @@ public class RetailAppServiceImp implements RetailAppService { public void changeAccountPassword(JSONObject device, ChangePwdBean change, String account_id) { String clientType = device.getString("client_type"); deviceSupport.findRegister(clientType); - //JSONObject client = signInAccountService.getClient(account_id); + // JSONObject client = signInAccountService.getClient(account_id); if (!account_id.equals(device.getString("account_id"))) { throw new ForbiddenException("You have no permission"); } @@ -779,7 +721,7 @@ public class RetailAppServiceImp implements RetailAppService { Date now = new Date(); JSONObject res = merchantInfoProvider.clientCurrentRate(clientId, now, "Wechat"); JSONObject client = clientManager.getClientInfo(clientId); - Assert.notNull(client,"Client is null"); + Assert.notNull(client, "Client is null"); JSONObject[] channels = new JSONObject[3]; @@ -804,7 +746,7 @@ public class RetailAppServiceImp implements RetailAppService { Date now = new Date(); JSONObject res = merchantInfoProvider.clientCurrentRate(clientId, now, "Wechat"); JSONObject client = clientManager.getClientInfo(clientId); - Assert.notNull(client,"Client is null"); + Assert.notNull(client, "Client is null"); ArrayList channels = new ArrayList<>(); if (client.getBigDecimal("customer_surcharge_rate") != null) { @@ -889,7 +831,7 @@ public class RetailAppServiceImp implements RetailAppService { } res.put("data", new_acts); } - //手续费返还活动 + // 手续费返还活动 List acts = (List) res.get("data"); for (JSONObject act : acts) { if (StringUtils.isNotBlank(act.getString("page_keywords")) && act.getString("page_keywords").equalsIgnoreCase("native")) { @@ -923,7 +865,7 @@ public class RetailAppServiceImp implements RetailAppService { String newAppVersion = PlatformEnvironment.getEnv().getNewVersion(); Boolean isUpdate = PlatformEnvironment.getEnv().isAppUpdate(); String updateContent = PlatformEnvironment.getEnv().getAppUpdateContent(); - int update_type = 0;//0:不更新 1:更新 2:强制更新 + int update_type = 0;// 0:不更新 1:更新 2:强制更新 if (newAppVersion.compareToIgnoreCase(version) > 0) { update_type = 1; if (isUpdate) { @@ -943,7 +885,6 @@ public class RetailAppServiceImp implements RetailAppService { return clientManager.listSubClients(device.getIntValue("client_id")); } - private JSONObject saveAppMessageLog(String dev_id, int client_id, String messageType, String dev_token, String remark) { JSONObject log = new JSONObject(); log.put("dev_id", dev_id); @@ -1078,7 +1019,7 @@ public class RetailAppServiceImp implements RetailAppService { if (clientRate.getIntValue("clean_days") == 1) { JSONObject config = clientSettleDelayConfMapper.findClientConf(device.getIntValue("client_id")); if (config == null) { -// res.put("show", true); + // res.put("show", true); res.put("interest_rate", xPlanFundConfigService.configuration().getInterestRate().setScale(2, BigDecimal.ROUND_DOWN)); } @@ -1086,7 +1027,6 @@ public class RetailAppServiceImp implements RetailAppService { return res; } - @Override public void sendMassageByCode(final JSONObject params) { try { @@ -1095,7 +1035,7 @@ public class RetailAppServiceImp implements RetailAppService { if (params.getString("type").equals("Merchant")) { for (String partner_code : codes) { JSONObject client = clientManager.getClientInfoByMoniker(partner_code); - Assert.notNull(client,"Client is null"); + Assert.notNull(client, "Client is null"); List tokens = clientDeviceTokenMapper.listTokensByClient_id(client.getInteger("client_id")); if (null != tokens && tokens.size() > 0) { for (final JSONObject devToken : tokens) { @@ -1103,7 +1043,8 @@ public class RetailAppServiceImp implements RetailAppService { sendingAppleMsgPool.execute(new Runnable() { @Override public void run() { - JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "dev", token, "Param:partner_code,自定义推送消息"); + JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "dev", token, + "Param:partner_code,自定义推送消息"); sending(token, params, log); } }); @@ -1129,7 +1070,7 @@ public class RetailAppServiceImp implements RetailAppService { throw new ServerErrorException(s.getMessage()); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } } @@ -1137,17 +1078,16 @@ public class RetailAppServiceImp implements RetailAppService { public void sending(String devToken, JSONObject params, JSONObject log) { try { - sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); + apnsMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App Message Failed", e); + throw new ServerErrorException("Send App JpushMessage Failed", e); } } - @Override public JSONObject getCoupons(JSONObject device, int page, int limit) { String clientType = device.getString("client_type"); diff --git a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java index 62364635e..2b3dc8825 100644 --- a/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/merchants/core/impls/ClientManagerImpl.java @@ -2774,7 +2774,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid MpWechatApi paymentApi = mpWechatApiProvider.getNewPaymentApi(); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,绿色通道开通完成" + e.getMessage()); + logger.error("Wechat JpushMessage Error,绿色通道开通完成" + e.getMessage()); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,绿色通道开通完成,openid=" + wxopenid)); } } @@ -2793,7 +2793,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid bd_user_name, "制作合同申请", "BD申请制作" + short_name + "的合同"); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,open_status=1" + e.getMessage()); + logger.error("Wechat JpushMessage Error,open_status=1" + e.getMessage()); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,open_status=1,openid=" + wxopenid)); } } @@ -2811,7 +2811,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid "Compliance", "合规材料", "上传完整合规材料,商户:" + short_name); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,open_status=3" + e.getMessage()); + logger.error("Wechat JpushMessage Error,open_status=3" + e.getMessage()); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,open_status=3,openid=" + wxopenid)); } @@ -2831,7 +2831,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid bd_user_name, "审核材料", "BD已提交合规材料,等待审核"); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,open_status=1" + e.getMessage()); + logger.error("Wechat JpushMessage Error,open_status=1" + e.getMessage()); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,open_status=1,openid=" + wxopenid)); } } @@ -2848,7 +2848,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid MpWechatApi paymentApi = mpWechatApiProvider.getNewPaymentApi(); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,open_status=5" + e.getMessage()); + logger.error("Wechat JpushMessage Error,open_status=5" + e.getMessage()); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,open_status=5,openid=" + wxopenid)); } } @@ -2867,7 +2867,7 @@ public class ClientManagerImpl implements ClientManager, ManagerTodoNoticeProvid bd_user_name, "绿色通道申请", "BD已提交绿色通道申请,商户:" + short_name); paymentApi.sendTemplateMessage(msg); } catch (WechatException e) { - logger.error("Wechat Message Error,open_status=10"); + logger.error("Wechat JpushMessage Error,open_status=10"); publisher.publishEvent(new WechatExceptionEvent(this, e, "Audit,open_status=10,openid=" + wxopenid)); } } diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/APNSMessageHelper.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/APNSMessageHelper.java new file mode 100644 index 000000000..3dffffcd3 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/APNSMessageHelper.java @@ -0,0 +1,46 @@ +package au.com.royalpay.payment.manage.pushMessage; + +import com.alibaba.fastjson.JSONObject; +import com.notnoop.apns.APNS; +import com.notnoop.apns.ApnsService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.io.IOException; +import java.io.InputStream; + +/** + * Created by wangning on 2018/1/3. + */ +@Service +public class APNSMessageHelper { + private Logger logger = LoggerFactory.getLogger(getClass()); + + @Value("classpath:apple_message_cert_dev.p12") + private org.springframework.core.io.Resource appleMessageFileDev; + @Value("classpath:apple_message_cert_product.p12") + private org.springframework.core.io.Resource appleMessageFileProduct; + @Value("${apple.message.apns.password}") + private String appleMessagePassword; + + public void sendAppleMessage(String title, String body, String token, JSONObject data, JSONObject type) throws IOException { + /**设置参数,发送数据**/ + InputStream keyIns = appleMessageFileProduct.getInputStream(); + ApnsService service = APNS.newService().withCert(keyIns, appleMessagePassword).withProductionDestination().build(); + String payload = APNS.newPayload().alertTitle(title).alertBody(body).badge(1).sound("default").customField("data", data).customField("type", type).build(); + service.push(token, payload); + logger.debug("推送信息已发送!"); + } + + public void sendAppleMessageDetail(String title, String body, String token, JSONObject data, JSONObject type) throws IOException { + /**设置参数,发送数据**/ + InputStream keyIns = appleMessageFileProduct.getInputStream(); + ApnsService service = APNS.newService().withCert(keyIns, appleMessagePassword).withProductionDestination().build(); + String payload = APNS.newPayload().alertTitle(title).alertBody(body).category("myNotificationCategory").badge(1).sound("default").customField("data", data).customField("type", type).build(); + service.push(token, payload); + logger.debug("推送信息已发送!"); + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java new file mode 100644 index 000000000..d27d4e998 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java @@ -0,0 +1,88 @@ +package au.com.royalpay.payment.manage.pushMessage; + +import au.com.royalpay.payment.manage.pushMessage.bean.JpushMessage; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import java.util.Map; +import java.util.Set; + +import cn.jpush.api.JPushClient; +import cn.jpush.api.push.model.Options; +import cn.jpush.api.push.model.Platform; +import cn.jpush.api.push.model.PushPayload; +import cn.jpush.api.push.model.audience.Audience; +import cn.jpush.api.push.model.notification.AndroidNotification; +import cn.jpush.api.push.model.notification.IosNotification; +import cn.jpush.api.push.model.notification.Notification; + +/** + * Created by wangning on 2018/1/3. + */ +@Service +public class JpushMessageHelper { + Logger logger = LoggerFactory.getLogger(getClass()); + + // 推送文本最大长度 + private final Integer NOTIFICATION_MAX_LENGTH = 54; + private static JPushClient client=null; + + public static JPushClient getPush() { + if (client == null) { + client = new JPushClient("c235e93f6b91abd72ca5d74e", "c235e93f6b91abd72ca5d74e"); + } + return client; + } + + public PushPayload makeSinglePayload(Set clientIds, JpushMessage pushMessage) { + Notification notify = getNotify(pushMessage); + return PushPayload.newBuilder() + .setPlatform(Platform.android_ios()) + .setAudience(Audience.registrationId(clientIds)) + .setNotification(notify) + // 设置离线时长 + .setOptions( + Options.newBuilder().setTimeToLive(600).build()).build(); + } + + private Notification getNotify(JpushMessage pushMessage) { + String alert = getText(pushMessage); + return Notification + .newBuilder() + .addPlatformNotification(IosNotification.newBuilder().setAlert(alert).setSound("default").addExtras(getExtra(pushMessage)).build()) + .addPlatformNotification(AndroidNotification.newBuilder().setTitle(pushMessage.getTitle()).setAlert(alert).addExtras(getExtra(pushMessage)).build()).build(); + } + + private String getText(JpushMessage pushMessage) { + String text = pushMessage.getText(); + int length = text.length(); + int trueLength = 0; + String cutString = ""; + for (int i = 0; i < length; i++) { + char c = text.charAt(i); + + if (c < 0 || c > 128) { + // 汉字 3个字符 + trueLength += 3; + } else { + // 英文等 1个字符 + trueLength++; + } + + cutString += c; + if (trueLength >= NOTIFICATION_MAX_LENGTH) { + cutString += "..."; + break; + } + } + return cutString; + } + + + private Map getExtra(JpushMessage message){ + Map map = message.getParams(); + return map; + } +} diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/JpushMessage.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/JpushMessage.java new file mode 100644 index 000000000..b74e8a993 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/JpushMessage.java @@ -0,0 +1,44 @@ +package au.com.royalpay.payment.manage.pushMessage.bean; + +import com.alibaba.fastjson.JSONObject; + +public class JpushMessage implements java.io.Serializable { + + private static final long serialVersionUID = 1L; + + /** + * 消息标题 + */ + private String title; + + /** + * 消息内容 + */ + private String text; + + private JSONObject params; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public JSONObject getParams() { + return params; + } + + public void setParams(JSONObject params) { + this.params = params; + } +} From e4c8f7de62a4d9bc6b5105840e679565b0f5a3a2 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Fri, 5 Jan 2018 13:37:42 +0800 Subject: [PATCH 2/8] update --- .../manage/pushMessage/JpushMessageHelper.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java index d27d4e998..d31cb2869 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java @@ -15,7 +15,6 @@ import cn.jpush.api.push.model.Platform; import cn.jpush.api.push.model.PushPayload; import cn.jpush.api.push.model.audience.Audience; import cn.jpush.api.push.model.notification.AndroidNotification; -import cn.jpush.api.push.model.notification.IosNotification; import cn.jpush.api.push.model.notification.Notification; /** @@ -36,22 +35,20 @@ public class JpushMessageHelper { return client; } - public PushPayload makeSinglePayload(Set clientIds, JpushMessage pushMessage) { + public PushPayload generateSinglePayload(Set alias, JpushMessage pushMessage) { Notification notify = getNotify(pushMessage); return PushPayload.newBuilder() - .setPlatform(Platform.android_ios()) - .setAudience(Audience.registrationId(clientIds)) + .setPlatform(Platform.android()) + .setAudience(Audience.alias(alias)) .setNotification(notify) // 设置离线时长 - .setOptions( - Options.newBuilder().setTimeToLive(600).build()).build(); + .setOptions(Options.newBuilder().setTimeToLive(60*60*4).build()).build(); } private Notification getNotify(JpushMessage pushMessage) { String alert = getText(pushMessage); return Notification .newBuilder() - .addPlatformNotification(IosNotification.newBuilder().setAlert(alert).setSound("default").addExtras(getExtra(pushMessage)).build()) .addPlatformNotification(AndroidNotification.newBuilder().setTitle(pushMessage.getTitle()).setAlert(alert).addExtras(getExtra(pushMessage)).build()).build(); } From 0327de0101ec1fa743a62e62950c3d337a744746 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Fri, 5 Jan 2018 15:51:49 +0800 Subject: [PATCH 3/8] tmp --- .../core/impls/RetailAppServiceImp.java | 133 ++++++++++++------ .../pushMessage/JpushMessageHelper.java | 44 +++--- .../system/ClientDeviceTokenMapper.xml | 25 +++- 3 files changed, 144 insertions(+), 58 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index 219893af7..785c3f000 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -1,5 +1,39 @@ package au.com.royalpay.payment.manage.appclient.core.impls; +import java.math.BigDecimal; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.TimeZone; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +import javax.annotation.Resource; + +import org.apache.commons.lang3.RandomUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.jsoup.Jsoup; +import org.jsoup.nodes.Document; +import org.slf4j.LoggerFactory; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONArray; +import com.alibaba.fastjson.JSONObject; +import com.github.miemiedev.mybatis.paginator.domain.Order; +import com.github.miemiedev.mybatis.paginator.domain.PageBounds; +import com.github.miemiedev.mybatis.paginator.domain.PageList; + import au.com.royalpay.payment.core.PaymentApi; import au.com.royalpay.payment.manage.analysis.mappers.CustomerAndOrdersStatisticsMapper; import au.com.royalpay.payment.manage.analysis.mappers.TransactionAnalysisMapper; @@ -14,12 +48,18 @@ import au.com.royalpay.payment.manage.mappers.log.ClearingDetailAnalysisMapper; import au.com.royalpay.payment.manage.mappers.log.ClearingDetailMapper; import au.com.royalpay.payment.manage.mappers.payment.OrderMapper; import au.com.royalpay.payment.manage.mappers.payment.TransactionMapper; -import au.com.royalpay.payment.manage.mappers.system.*; +import au.com.royalpay.payment.manage.mappers.system.ClientAccountMapper; +import au.com.royalpay.payment.manage.mappers.system.ClientDeviceTokenMapper; +import au.com.royalpay.payment.manage.mappers.system.ClientSettleDelayConfMapper; +import au.com.royalpay.payment.manage.mappers.system.CustomerMapper; +import au.com.royalpay.payment.manage.mappers.system.CustomerRelationAlipayMapper; import au.com.royalpay.payment.manage.merchants.core.ClientManager; import au.com.royalpay.payment.manage.notice.beans.NoticeInfo; import au.com.royalpay.payment.manage.notice.core.NoticeManage; import au.com.royalpay.payment.manage.notice.core.NoticePartner; import au.com.royalpay.payment.manage.pushMessage.APNSMessageHelper; +import au.com.royalpay.payment.manage.pushMessage.JpushMessageHelper; +import au.com.royalpay.payment.manage.pushMessage.bean.JpushMessage; import au.com.royalpay.payment.manage.signin.beans.ChangePwdBean; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; import au.com.royalpay.payment.manage.signin.core.impls.SignInAccountServiceImpl; @@ -36,37 +76,9 @@ import au.com.royalpay.payment.tools.merchants.core.MerchantInfoProvider; import au.com.royalpay.payment.tools.permission.enums.PartnerRole; import au.com.royalpay.payment.tools.utils.PageListUtils; import au.com.royalpay.payment.tools.utils.TimeZoneUtils; -import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONArray; -import com.alibaba.fastjson.JSONObject; -import com.github.miemiedev.mybatis.paginator.domain.Order; -import com.github.miemiedev.mybatis.paginator.domain.PageBounds; -import com.github.miemiedev.mybatis.paginator.domain.PageList; -import com.notnoop.apns.APNS; -import com.notnoop.apns.ApnsService; -import org.apache.commons.lang3.RandomUtils; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.lang3.time.DateFormatUtils; -import org.apache.commons.lang3.time.DateUtils; -import org.jsoup.Jsoup; -import org.jsoup.nodes.Document; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.stereotype.Service; -import org.springframework.util.Assert; -import javax.annotation.Resource; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigDecimal; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.*; -import java.util.concurrent.LinkedBlockingQueue; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; +import cn.jpush.api.JPushClient; +import cn.jpush.api.push.model.PushPayload; /** * Created by yishuqian on 28/03/2017. @@ -120,6 +132,8 @@ public class RetailAppServiceImp implements RetailAppService { private APNSMessageHelper apnsMessageHelper; @Resource private ClientDeviceTokenMapper clientDeviceTokenMapper; + @Resource + private JpushMessageHelper jpushMessageHelper; private ThreadPoolExecutor sendingAppleMsgPool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); @@ -455,10 +469,24 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "payment"); type.put("id", order.getString("order_id")); - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), - LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), token, - order, type); - log.put("status", 2); + if("iphone".equals(devToken.getString("client_type"))) { + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), + LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), token, + order, type); + } + if("android".equals(devToken.getString("client_type"))) { + JPushClient jPushClient= JpushMessageHelper.getPush(); + JpushMessage jpushMessage = new JpushMessage(); + jpushMessage.setTitle(LocaleSupport.localeMessage("app.message.title.payment")); + jpushMessage.setText(LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount")); + JSONObject param = new JSONObject(); + param.put("data",order); + param.put("type",type); + jpushMessage.setParams(param); + PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); + jPushClient.sendPush(pushPayload); + } + log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); @@ -501,9 +529,14 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "refund"); type.put("id", order.getString("order_id")); - apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, token, - order, type); - log.put("status", 2); + if("iphone".equals(devToken.getString("client_type"))) { + apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, token, + order, type); + } + if("android".equals(devToken.getString("client_type"))) { + sendAppleMessage + } + log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); @@ -535,8 +568,23 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "notice"); type.put("id", notice.getNotice_id()); - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, - type); + if("iphone".equals(devToken.getString("client_type"))){ + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, + type); + } + if("android".equals(devToken.getString("client_type"))){ + JPushClient jPushClient= JpushMessageHelper.getPush(); + JpushMessage jpushMessage = new JpushMessage(); + jpushMessage.setTitle(notice.getTitle()); + jpushMessage.setText(LocaleSupport.localeMessage("app.message.title.notice")); + JSONObject param = new JSONObject(); + param.put("data",noticeObj); + param.put("type",type); + jpushMessage.setParams(param); + PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); + jPushClient.sendPush(pushPayload); + } + log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -1147,4 +1195,9 @@ public class RetailAppServiceImp implements RetailAppService { String regex = "[A-Za-z0-9]{8}"; return str.matches(regex); } + + private void sendJpush(int client_id,JpushMessage message){ + + + } } diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java index d31cb2869..d30322181 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java @@ -2,13 +2,17 @@ package au.com.royalpay.payment.manage.pushMessage; import au.com.royalpay.payment.manage.pushMessage.bean.JpushMessage; +import com.alibaba.fastjson.JSONObject; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import java.io.IOException; import java.util.Map; -import java.util.Set; +import cn.jiguang.common.resp.APIConnectionException; +import cn.jiguang.common.resp.APIRequestException; import cn.jpush.api.JPushClient; import cn.jpush.api.push.model.Options; import cn.jpush.api.push.model.Platform; @@ -25,8 +29,8 @@ public class JpushMessageHelper { Logger logger = LoggerFactory.getLogger(getClass()); // 推送文本最大长度 - private final Integer NOTIFICATION_MAX_LENGTH = 54; - private static JPushClient client=null; + private static final Integer NOTIFICATION_MAX_LENGTH = 54; + private static JPushClient client = null; public static JPushClient getPush() { if (client == null) { @@ -35,24 +39,33 @@ public class JpushMessageHelper { return client; } - public PushPayload generateSinglePayload(Set alias, JpushMessage pushMessage) { + public void sendAppleMessage(String title, String body, String token, JSONObject data, JSONObject type) throws IOException, APIConnectionException, APIRequestException { + JPushClient jPushClient= JpushMessageHelper.getPush(); + JpushMessage jpushMessage = new JpushMessage(); + jpushMessage.setTitle(title); + jpushMessage.setText(body); + JSONObject param = new JSONObject(); + param.put("data",data); + param.put("type",type); + jpushMessage.setParams(param); + PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); + jPushClient.sendPush(pushPayload); + } + + public static PushPayload generateSinglePayload(String alias, JpushMessage pushMessage) { Notification notify = getNotify(pushMessage); - return PushPayload.newBuilder() - .setPlatform(Platform.android()) - .setAudience(Audience.alias(alias)) - .setNotification(notify) + return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alias)).setNotification(notify) // 设置离线时长 - .setOptions(Options.newBuilder().setTimeToLive(60*60*4).build()).build(); + .setOptions(Options.newBuilder().setTimeToLive(60 * 60 * 4).build()).build(); } - private Notification getNotify(JpushMessage pushMessage) { + private static Notification getNotify(JpushMessage pushMessage) { String alert = getText(pushMessage); - return Notification - .newBuilder() - .addPlatformNotification(AndroidNotification.newBuilder().setTitle(pushMessage.getTitle()).setAlert(alert).addExtras(getExtra(pushMessage)).build()).build(); + return Notification.newBuilder().addPlatformNotification( + AndroidNotification.newBuilder().setTitle(pushMessage.getTitle()).setAlert(alert).addExtras(getExtra(pushMessage)).build()).build(); } - private String getText(JpushMessage pushMessage) { + private static String getText(JpushMessage pushMessage) { String text = pushMessage.getText(); int length = text.length(); int trueLength = 0; @@ -77,8 +90,7 @@ public class JpushMessageHelper { return cutString; } - - private Map getExtra(JpushMessage message){ + private static Map getExtra(JpushMessage message) { Map map = message.getParams(); return map; } diff --git a/src/main/resources/au/com/royalpay/payment/manage/mappers/system/ClientDeviceTokenMapper.xml b/src/main/resources/au/com/royalpay/payment/manage/mappers/system/ClientDeviceTokenMapper.xml index 3551b3c64..66996e519 100644 --- a/src/main/resources/au/com/royalpay/payment/manage/mappers/system/ClientDeviceTokenMapper.xml +++ b/src/main/resources/au/com/royalpay/payment/manage/mappers/system/ClientDeviceTokenMapper.xml @@ -1,10 +1,31 @@ - + + From 835246c24ae586ddcbeba00eb187a31be1e49c5b Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Fri, 5 Jan 2018 16:20:32 +0800 Subject: [PATCH 4/8] tmp --- .../core/impls/RetailAppServiceImp.java | 3 ++- .../manage/pushMessage/JpushMessageHelper.java | 16 ++++++++-------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index 785c3f000..93d7933f1 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -534,7 +534,8 @@ public class RetailAppServiceImp implements RetailAppService { order, type); } if("android".equals(devToken.getString("client_type"))) { - sendAppleMessage + jpushMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount,token, + order, type); } log.put("status", 2); appMessageLogMapper.update(log); diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java index d30322181..337233c8e 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/JpushMessageHelper.java @@ -29,10 +29,10 @@ public class JpushMessageHelper { Logger logger = LoggerFactory.getLogger(getClass()); // 推送文本最大长度 - private static final Integer NOTIFICATION_MAX_LENGTH = 54; + private final Integer NOTIFICATION_MAX_LENGTH = 54; private static JPushClient client = null; - public static JPushClient getPush() { + private JPushClient getPush() { if (client == null) { client = new JPushClient("c235e93f6b91abd72ca5d74e", "c235e93f6b91abd72ca5d74e"); } @@ -40,7 +40,7 @@ public class JpushMessageHelper { } public void sendAppleMessage(String title, String body, String token, JSONObject data, JSONObject type) throws IOException, APIConnectionException, APIRequestException { - JPushClient jPushClient= JpushMessageHelper.getPush(); + JPushClient jPushClient= getPush(); JpushMessage jpushMessage = new JpushMessage(); jpushMessage.setTitle(title); jpushMessage.setText(body); @@ -48,24 +48,24 @@ public class JpushMessageHelper { param.put("data",data); param.put("type",type); jpushMessage.setParams(param); - PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); + PushPayload pushPayload = generateSinglePayload(token,jpushMessage); jPushClient.sendPush(pushPayload); } - public static PushPayload generateSinglePayload(String alias, JpushMessage pushMessage) { + private PushPayload generateSinglePayload(String alias, JpushMessage pushMessage) { Notification notify = getNotify(pushMessage); return PushPayload.newBuilder().setPlatform(Platform.android()).setAudience(Audience.alias(alias)).setNotification(notify) // 设置离线时长 .setOptions(Options.newBuilder().setTimeToLive(60 * 60 * 4).build()).build(); } - private static Notification getNotify(JpushMessage pushMessage) { + private Notification getNotify(JpushMessage pushMessage) { String alert = getText(pushMessage); return Notification.newBuilder().addPlatformNotification( AndroidNotification.newBuilder().setTitle(pushMessage.getTitle()).setAlert(alert).addExtras(getExtra(pushMessage)).build()).build(); } - private static String getText(JpushMessage pushMessage) { + private String getText(JpushMessage pushMessage) { String text = pushMessage.getText(); int length = text.length(); int trueLength = 0; @@ -90,7 +90,7 @@ public class JpushMessageHelper { return cutString; } - private static Map getExtra(JpushMessage message) { + private Map getExtra(JpushMessage message) { Map map = message.getParams(); return map; } From 2323459f31d1208b98f966e1e79cef0279b336ac Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Fri, 5 Jan 2018 16:55:41 +0800 Subject: [PATCH 5/8] finish refactor the message --- .../core/impls/RetailAppServiceImp.java | 99 ++++++++++--------- .../ui/static/config/devtools/devtools.js | 1 + .../devtools/templates/send_message.html | 7 ++ 3 files changed, 61 insertions(+), 46 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index 93d7933f1..0c8713957 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -469,24 +469,17 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "payment"); type.put("id", order.getString("order_id")); - if("iphone".equals(devToken.getString("client_type"))) { + if ("iphone".equals(devToken.getString("client_type"))) { apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), - LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), token, - order, type); + LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), + token, order, type); } - if("android".equals(devToken.getString("client_type"))) { - JPushClient jPushClient= JpushMessageHelper.getPush(); - JpushMessage jpushMessage = new JpushMessage(); - jpushMessage.setTitle(LocaleSupport.localeMessage("app.message.title.payment")); - jpushMessage.setText(LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount")); - JSONObject param = new JSONObject(); - param.put("data",order); - param.put("type",type); - jpushMessage.setParams(param); - PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); - jPushClient.sendPush(pushPayload); + if ("android".equals(devToken.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), + LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), + token, order, type); } - log.put("status", 2); + log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); @@ -529,15 +522,15 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "refund"); type.put("id", order.getString("order_id")); - if("iphone".equals(devToken.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, token, - order, type); + if ("iphone".equals(devToken.getString("client_type"))) { + apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, + token, order, type); } - if("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount,token, - order, type); + if ("android".equals(devToken.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, + token, order, type); } - log.put("status", 2); + log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); @@ -569,21 +562,13 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "notice"); type.put("id", notice.getNotice_id()); - if("iphone".equals(devToken.getString("client_type"))){ + if ("iphone".equals(devToken.getString("client_type"))) { apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, type); } - if("android".equals(devToken.getString("client_type"))){ - JPushClient jPushClient= JpushMessageHelper.getPush(); - JpushMessage jpushMessage = new JpushMessage(); - jpushMessage.setTitle(notice.getTitle()); - jpushMessage.setText(LocaleSupport.localeMessage("app.message.title.notice")); - JSONObject param = new JSONObject(); - param.put("data",noticeObj); - param.put("type",type); - jpushMessage.setParams(param); - PushPayload pushPayload =JpushMessageHelper.generateSinglePayload(token,jpushMessage); - jPushClient.sendPush(pushPayload); + if ("android".equals(devToken.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, + noticeObj, type); } log.put("status", 2); @@ -616,10 +601,19 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "cashback"); type.put("id", order.getString("order_id")); - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), - LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " - + cashbackAmount, - token, order, type); + if ("iphone".equals(devToken.getString("client_type"))) { + + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), + LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + + cashbackAmount, + token, order, type); + } + if ("android".equals(devToken.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), + LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + + cashbackAmount, + token, order, type); + } log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -653,9 +647,20 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "clean"); type.put("id", log_clearing_detail_id); - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), LocaleSupport.localeMessage("app.message.body.clean") - + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + " (" + settle_date_from + "-" + settle_date_to + ")", - token, log_clearing_detail, type); + if ("iphone".equals(devToken.getString("client_type"))) { + + apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), + LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + + " (" + settle_date_from + "-" + settle_date_to + ")", + token, log_clearing_detail, type); + } + + if ("android".equals(devToken.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), + LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount + + " (" + settle_date_from + "-" + settle_date_to + ")", + token, log_clearing_detail, type); + } log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -698,7 +703,6 @@ public class RetailAppServiceImp implements RetailAppService { } } - @Override public void updateReadStatus(JSONObject device, String noticeId) { String clientType = device.getString("client_type"); @@ -1125,9 +1129,13 @@ public class RetailAppServiceImp implements RetailAppService { } public void sending(String devToken, JSONObject params, JSONObject log) { - try { - apnsMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); + if ("iphone".equals(params.getString("client_type"))) { + apnsMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); + } + if ("android".equals(params.getString("client_type"))) { + jpushMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); + } log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -1197,8 +1205,7 @@ public class RetailAppServiceImp implements RetailAppService { return str.matches(regex); } - private void sendJpush(int client_id,JpushMessage message){ - + private void sendJpush(int client_id, JpushMessage message) { } } diff --git a/src/main/ui/static/config/devtools/devtools.js b/src/main/ui/static/config/devtools/devtools.js index cd653273d..709fc4f53 100644 --- a/src/main/ui/static/config/devtools/devtools.js +++ b/src/main/ui/static/config/devtools/devtools.js @@ -216,6 +216,7 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) { app.controller('devSendMessageCtrl', ['$scope', '$http', '$filter', function ($scope, $http, $filter) { $scope.params = {type:'Merchant'}; + alert($scope.params.client_type); $scope.sendMessage = function () { var params = angular.copy($scope.params); $http.put('/dev/send_message',params).then(function (resp) { diff --git a/src/main/ui/static/config/devtools/templates/send_message.html b/src/main/ui/static/config/devtools/templates/send_message.html index 378f1bbcb..dc37ff46a 100644 --- a/src/main/ui/static/config/devtools/templates/send_message.html +++ b/src/main/ui/static/config/devtools/templates/send_message.html @@ -31,6 +31,13 @@ +
+ + +
From 9b8b9639c0a31337afd7a1585605e8e77858268b Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Fri, 5 Jan 2018 18:03:01 +0800 Subject: [PATCH 6/8] update --- .../com/royalpay/payment/manage/dev/bean/Message.java | 10 +++++++++- src/main/ui/static/config/devtools/devtools.js | 1 - .../static/config/devtools/templates/send_message.html | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/dev/bean/Message.java b/src/main/java/au/com/royalpay/payment/manage/dev/bean/Message.java index 2ded3cb61..b4d3ce22c 100644 --- a/src/main/java/au/com/royalpay/payment/manage/dev/bean/Message.java +++ b/src/main/java/au/com/royalpay/payment/manage/dev/bean/Message.java @@ -16,7 +16,7 @@ public class Message { public String title; @NotEmpty(message = "content can not be null") public String content; - + public String client_type; public JSONObject insertObject() { JSONObject res = (JSONObject) JSON.toJSON(this); return res; @@ -53,4 +53,12 @@ public class Message { public void setContent(String content) { this.content = content; } + + public String getClient_type() { + return client_type; + } + + public void setClient_type(String client_type) { + this.client_type = client_type; + } } diff --git a/src/main/ui/static/config/devtools/devtools.js b/src/main/ui/static/config/devtools/devtools.js index 709fc4f53..cd653273d 100644 --- a/src/main/ui/static/config/devtools/devtools.js +++ b/src/main/ui/static/config/devtools/devtools.js @@ -216,7 +216,6 @@ define(['angular', 'uiRouter', 'uiBootstrap'], function (angular) { app.controller('devSendMessageCtrl', ['$scope', '$http', '$filter', function ($scope, $http, $filter) { $scope.params = {type:'Merchant'}; - alert($scope.params.client_type); $scope.sendMessage = function () { var params = angular.copy($scope.params); $http.put('/dev/send_message',params).then(function (resp) { diff --git a/src/main/ui/static/config/devtools/templates/send_message.html b/src/main/ui/static/config/devtools/templates/send_message.html index dc37ff46a..84aa5393b 100644 --- a/src/main/ui/static/config/devtools/templates/send_message.html +++ b/src/main/ui/static/config/devtools/templates/send_message.html @@ -34,8 +34,8 @@
From 0563d91d683ffe254093d8155ba84bc881131010 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Mon, 8 Jan 2018 17:54:11 +0800 Subject: [PATCH 7/8] update android msg --- .../core/impls/RetailAppServiceImp.java | 157 ++++++++++-------- .../bean/AppManagerMessageBuilder.java | 25 +++ 2 files changed, 117 insertions(+), 65 deletions(-) create mode 100644 src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java diff --git a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java index 0c8713957..782f3954a 100644 --- a/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java +++ b/src/main/java/au/com/royalpay/payment/manage/appclient/core/impls/RetailAppServiceImp.java @@ -5,10 +5,13 @@ import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.TimeZone; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; @@ -59,6 +62,7 @@ import au.com.royalpay.payment.manage.notice.core.NoticeManage; import au.com.royalpay.payment.manage.notice.core.NoticePartner; import au.com.royalpay.payment.manage.pushMessage.APNSMessageHelper; import au.com.royalpay.payment.manage.pushMessage.JpushMessageHelper; +import au.com.royalpay.payment.manage.pushMessage.bean.AppManagerMessageBuilder; import au.com.royalpay.payment.manage.pushMessage.bean.JpushMessage; import au.com.royalpay.payment.manage.signin.beans.ChangePwdBean; import au.com.royalpay.payment.manage.signin.core.SignInAccountService; @@ -66,6 +70,8 @@ import au.com.royalpay.payment.manage.signin.core.impls.SignInAccountServiceImpl import au.com.royalpay.payment.manage.tradelog.beans.TradeLogQuery; import au.com.royalpay.payment.tools.cms.RoyalPayCMSSupport; import au.com.royalpay.payment.tools.device.DeviceSupport; +import au.com.royalpay.payment.tools.device.message.AppMessage; +import au.com.royalpay.payment.tools.device.message.AppMsgSender; import au.com.royalpay.payment.tools.device.support.DeviceRegister; import au.com.royalpay.payment.tools.env.PlatformEnvironment; import au.com.royalpay.payment.tools.exceptions.BadRequestException; @@ -77,9 +83,6 @@ import au.com.royalpay.payment.tools.permission.enums.PartnerRole; import au.com.royalpay.payment.tools.utils.PageListUtils; import au.com.royalpay.payment.tools.utils.TimeZoneUtils; -import cn.jpush.api.JPushClient; -import cn.jpush.api.push.model.PushPayload; - /** * Created by yishuqian on 28/03/2017. */ @@ -135,6 +138,13 @@ public class RetailAppServiceImp implements RetailAppService { @Resource private JpushMessageHelper jpushMessageHelper; + private Map senderMap = new HashMap<>(); + + @Resource + public void setAppMsgSenders(AppMsgSender[] senders) { + Arrays.stream(senders).forEach(appMsgSender -> senderMap.put(appMsgSender.devType(), appMsgSender)); + } + private ThreadPoolExecutor sendingAppleMsgPool = new ThreadPoolExecutor(10, 30, 5, TimeUnit.SECONDS, new LinkedBlockingQueue()); @Resource @@ -461,7 +471,6 @@ public class RetailAppServiceImp implements RetailAppService { return; } System.out.println("Device Token:" + token); - // todo 抽象发送信息接口,根据设备类型选择不同实现类 JSONObject log = saveAppMessageLog(devToken.getString("dev_id"), devToken.getIntValue("client_id"), "payment", token, order.getString("order_id")); try { @@ -469,22 +478,25 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "payment"); type.put("id", order.getString("order_id")); - if ("iphone".equals(devToken.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), - LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), - token, order, type); - } - if ("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.payment"), - LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount"), - token, order, type); + AppMsgSender sender = senderMap.get(devToken.getString("client_type")); + if (token == null || sender == null) { + return; } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", LocaleSupport.localeMessage("app.message.title.payment")); + managerMsg.put("body", + LocaleSupport.localeMessage("app.message.body.payment") + order.getString("currency") + order.getDoubleValue("total_amount")); + managerMsg.put("type", type); + managerMsg.put("data", order); + managerMsg.put("msgType", "payment"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devToken); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App "+devToken.getString("client_type")+" Failed", e); } }; sendingAppleMsgPool.execute(task); @@ -522,14 +534,19 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "refund"); type.put("id", order.getString("order_id")); - if ("iphone".equals(devToken.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, - token, order, type); - } - if ("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage("Refund JpushMessage", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount, - token, order, type); + + AppMsgSender sender = senderMap.get(devToken.getString("client_type")); + if (token == null || sender == null) { + return; } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", "Refund "+devToken.getString("client_type")+""); + managerMsg.put("body", LocaleSupport.localeMessage("app.message.body.refund") + refundAmount); + managerMsg.put("type", type); + managerMsg.put("data", order); + managerMsg.put("msgType", "refund"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devToken); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -562,15 +579,19 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "notice"); type.put("id", notice.getNotice_id()); - if ("iphone".equals(devToken.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, noticeObj, - type); - } - if ("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), notice.getTitle(), token, - noticeObj, type); - } + AppMsgSender sender = senderMap.get(devToken.getString("client_type")); + if (token == null || sender == null) { + return; + } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", LocaleSupport.localeMessage("app.message.title.notice")); + managerMsg.put("body", notice.getTitle()); + managerMsg.put("type", type); + managerMsg.put("data", noticeObj); + managerMsg.put("msgType", "notice"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devToken); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { @@ -601,25 +622,26 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "cashback"); type.put("id", order.getString("order_id")); - if ("iphone".equals(devToken.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), - LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " - + cashbackAmount, - token, order, type); - } - if ("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.notice"), - LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + " " - + cashbackAmount, - token, order, type); + AppMsgSender sender = senderMap.get(devToken.getString("client_type")); + if (token == null || sender == null) { + return; } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", LocaleSupport.localeMessage("app.message.title.notice")); + managerMsg.put("body", LocaleSupport.localeMessage("app.message.body.cashback") + PlatformEnvironment.getEnv().getForeignCurrency() + + " " + cashbackAmount); + managerMsg.put("type", type); + managerMsg.put("data", order); + managerMsg.put("msgType", "cashback"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devToken); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App "+devToken.getString("client_type")+" Failed", e); } } }); @@ -647,26 +669,26 @@ public class RetailAppServiceImp implements RetailAppService { JSONObject type = new JSONObject(); type.put("send_type", "clean"); type.put("id", log_clearing_detail_id); - if ("iphone".equals(devToken.getString("client_type"))) { - - apnsMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), - LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount - + " (" + settle_date_from + "-" + settle_date_to + ")", - token, log_clearing_detail, type); - } - if ("android".equals(devToken.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage(LocaleSupport.localeMessage("app.message.title.clean"), - LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + clearing_amount - + " (" + settle_date_from + "-" + settle_date_to + ")", - token, log_clearing_detail, type); + AppMsgSender sender = senderMap.get(devToken.getString("client_type")); + if (token == null || sender == null) { + return; } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", LocaleSupport.localeMessage("app.message.title.clean")); + managerMsg.put("body", LocaleSupport.localeMessage("app.message.body.clean") + PlatformEnvironment.getEnv().getForeignCurrency() + " " + + clearing_amount + " (" + settle_date_from + "-" + settle_date_to + ")"); + managerMsg.put("type", type); + managerMsg.put("data", log_clearing_detail); + managerMsg.put("msgType", "clean"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devToken); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App "+devToken.getString("client_type")+" Failed", e); } } } @@ -698,7 +720,7 @@ public class RetailAppServiceImp implements RetailAppService { } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App "+devToken.getString("client_type")+" Failed", e); } } } @@ -1123,25 +1145,33 @@ public class RetailAppServiceImp implements RetailAppService { throw new ServerErrorException(s.getMessage()); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App Message Failed", e); } } public void sending(String devToken, JSONObject params, JSONObject log) { try { - if ("iphone".equals(params.getString("client_type"))) { - apnsMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); - } - if ("android".equals(params.getString("client_type"))) { - jpushMessageHelper.sendAppleMessage(params.getString("title"), params.getString("content"), devToken, new JSONObject(), new JSONObject()); + JSONObject devTokenJson = new JSONObject(); + devTokenJson.put("token", devToken); + AppMsgSender sender = senderMap.get(params.getString("client_type")); + if (devToken == null || sender == null) { + return; } + JSONObject managerMsg = new JSONObject(); + managerMsg.put("title", params.getString("title")); + managerMsg.put("body", params.getString("content")); + managerMsg.put("type", new JSONObject()); + managerMsg.put("data", new JSONObject()); + managerMsg.put("msgType", "payment"); + AppMessage appMessage = new AppManagerMessageBuilder(managerMsg).buildMessage(); + sender.sendMessage(appMessage, devTokenJson); log.put("status", 2); appMessageLogMapper.update(log); } catch (Exception e) { logger.error("出错了:" + e.getMessage()); appMessageLogMapper.updateStatus(log.getString("send_id"), 1, e.getMessage()); - throw new ServerErrorException("Send App JpushMessage Failed", e); + throw new ServerErrorException("Send App "+params.getString("client_type")+" Failed", e); } } @@ -1205,7 +1235,4 @@ public class RetailAppServiceImp implements RetailAppService { return str.matches(regex); } - private void sendJpush(int client_id, JpushMessage message) { - - } } diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java new file mode 100644 index 000000000..8e862ed67 --- /dev/null +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java @@ -0,0 +1,25 @@ +package au.com.royalpay.payment.manage.pushMessage.bean; + +import au.com.royalpay.payment.tools.device.message.AppMessage; +import au.com.royalpay.payment.tools.device.message.AppMessageBuilder; + +import com.alibaba.fastjson.JSONObject; + +import org.springframework.stereotype.Service; + +/** + * Created by wangning on 08/01/2018. + */ +@Service +public class AppManagerMessageBuilder implements AppMessageBuilder { + private JSONObject managerMsg; + + public AppManagerMessageBuilder(JSONObject managerMsg) { + this.managerMsg = managerMsg; + } + + @Override + public AppMessage buildMessage() { + return new AppMessage(managerMsg.getString("msgType"), managerMsg.getString("title"), managerMsg.getString("body")).setTypeData(managerMsg.getJSONObject("type")).setData(managerMsg.getJSONObject("data")); + } +} From ae5a21fc3c2d7b191819a93f8ed4a71d658d8148 Mon Sep 17 00:00:00 2001 From: wangning <164851225@qq.com> Date: Mon, 8 Jan 2018 20:05:38 +0800 Subject: [PATCH 8/8] update --- .../manage/pushMessage/bean/AppManagerMessageBuilder.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java index 8e862ed67..f008f7d6d 100644 --- a/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java +++ b/src/main/java/au/com/royalpay/payment/manage/pushMessage/bean/AppManagerMessageBuilder.java @@ -5,12 +5,9 @@ import au.com.royalpay.payment.tools.device.message.AppMessageBuilder; import com.alibaba.fastjson.JSONObject; -import org.springframework.stereotype.Service; - /** * Created by wangning on 08/01/2018. */ -@Service public class AppManagerMessageBuilder implements AppMessageBuilder { private JSONObject managerMsg;