diff --git a/mallplus-admin/src/main/java/com/zscat/mallplus/MallAdminApplication.java b/mallplus-admin/src/main/java/com/zscat/mallplus/MallAdminApplication.java index ecf9746..c20d050 100644 --- a/mallplus-admin/src/main/java/com/zscat/mallplus/MallAdminApplication.java +++ b/mallplus-admin/src/main/java/com/zscat/mallplus/MallAdminApplication.java @@ -14,7 +14,7 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @SpringBootApplication(exclude = {DataSourceAutoConfiguration.class}) @MapperScan({"com.zscat.mallplus.mapper", "com.zscat.mallplus.ums.mapper", "com.zscat.mallplus.sms.mapper", "com.zscat.mallplus.cms.mapper", "com.zscat.mallplus.sys.mapper", "com.zscat.mallplus.oms.mapper", "com.zscat.mallplus.pms.mapper"}) @EnableTransactionManagement -@EnableScheduling +// @EnableScheduling public class MallAdminApplication { public static void main(String[] args) { SpringApplication.run(MallAdminApplication.class, args); diff --git a/mallplus-admin/src/main/java/com/zscat/mallplus/task/SchedulingTaskComponent.java b/mallplus-admin/src/main/java/com/zscat/mallplus/task/SchedulingTaskComponent.java index 6398223..789f9a2 100644 --- a/mallplus-admin/src/main/java/com/zscat/mallplus/task/SchedulingTaskComponent.java +++ b/mallplus-admin/src/main/java/com/zscat/mallplus/task/SchedulingTaskComponent.java @@ -47,13 +47,13 @@ import java.util.Date; * @version 1.0 * @since 1.0 */ -@Component +// @Component public class SchedulingTaskComponent { @Autowired private SysMessageTaskService sysMessageTaskService; - @Scheduled(cron = "0 */5 * * * ?") + // @Scheduled(cron = "0 */5 * * * ?") public void remindVehicle() { sysMessageTaskService.checkTask(); } diff --git a/mallplus-portal/pom.xml b/mallplus-portal/pom.xml index c90a4f6..1b5a753 100644 --- a/mallplus-portal/pom.xml +++ b/mallplus-portal/pom.xml @@ -204,12 +204,12 @@ wsdl4j 1.6.3 - - - - - - + + com.getui.push + restful-sdk + 1.0.0.11 + compile + diff --git a/mallplus-portal/src/main/java/com/zscat/mallplus/sms/service/ScheduledService.java b/mallplus-portal/src/main/java/com/zscat/mallplus/sms/service/ScheduledService.java index b6443c0..2b61f04 100644 --- a/mallplus-portal/src/main/java/com/zscat/mallplus/sms/service/ScheduledService.java +++ b/mallplus-portal/src/main/java/com/zscat/mallplus/sms/service/ScheduledService.java @@ -1,5 +1,6 @@ package com.zscat.mallplus.sms.service; +import com.zscat.mallplus.unipush.SysMessageTaskService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @@ -19,7 +20,7 @@ public class ScheduledService { @Autowired private ISmsCouponHistoryService smsCouponHistoryService; -// @Scheduled(cron = "0 0 0 * * ?") + // @Scheduled(cron = "0 0 0 * * ?") // @Scheduled(cron = "0 0/5 11 * * ?") @Scheduled(cron = "*/5 * * * * ?") public void remindVehicle() { @@ -31,6 +32,15 @@ public class ScheduledService { calendar.setTime(now); String tomorrow = sdf.format(calendar.getTime()); //查询优惠券历史记录中已超过endTime时间的 - smsCouponHistoryService.updateUseStatus(2,tomorrow); + smsCouponHistoryService.updateUseStatus(2, tomorrow); + } + + + @Autowired + private SysMessageTaskService sysMessageTaskService; + + @Scheduled(cron = "0 */5 * * * ?") + public void checkSysMessageTask() { + sysMessageTaskService.checkTask(); } } diff --git a/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/ISysMessageService.java b/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/ISysMessageService.java index 3a36f1e..ac68afa 100644 --- a/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/ISysMessageService.java +++ b/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/ISysMessageService.java @@ -23,4 +23,6 @@ public interface ISysMessageService extends IService { IPage listPageByUserId(Long id, IPage page); IPage listPageOfSys(IPage page); + + void sendToAll(SysMessage sm); } diff --git a/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/impl/SysMessageServiceImpl.java b/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/impl/SysMessageServiceImpl.java index 8a669e3..a117e7c 100644 --- a/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/impl/SysMessageServiceImpl.java +++ b/mallplus-portal/src/main/java/com/zscat/mallplus/ums/service/impl/SysMessageServiceImpl.java @@ -6,8 +6,13 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.zscat.mallplus.ums.entity.SysMessage; import com.zscat.mallplus.ums.mapper.SysMessageMapper; import com.zscat.mallplus.ums.service.ISysMessageService; +import com.zscat.mallplus.unipush.UniPushService; +import com.zscat.mallplus.unipush.entity.PushCids; +import com.zscat.mallplus.unipush.mapper.PushCidsMapper; import org.springframework.stereotype.Service; +import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; /** @@ -21,6 +26,11 @@ import java.util.List; @Service public class SysMessageServiceImpl extends ServiceImpl implements ISysMessageService { + @Resource + private PushCidsMapper pushCidsMapper; + @Resource + private UniPushService uniPushService; + @Override public List listByUserId(Long userId) { QueryWrapper qw = new QueryWrapper<>(); @@ -53,4 +63,22 @@ public class SysMessageServiceImpl extends ServiceImpl cids = listPushCidAll(); + uniPushService.sendPushAsync(sysMessage, cids); + } + private List listPushCidAll() { + List list = pushCidsMapper.selectList(new QueryWrapper<>()); + List cids = new ArrayList<>(); + list.forEach(ent -> cids.add(ent.getGetuiCid())); + return cids; + } } diff --git a/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/SysMessageTaskService.java b/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/SysMessageTaskService.java new file mode 100644 index 0000000..d34d2dc --- /dev/null +++ b/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/SysMessageTaskService.java @@ -0,0 +1,40 @@ +package com.zscat.mallplus.unipush; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.zscat.mallplus.ums.entity.SysMessage; +import com.zscat.mallplus.ums.entity.SysMessageTask; +import com.zscat.mallplus.ums.mapper.SysMessageTaskMapper; +import com.zscat.mallplus.ums.service.ISysMessageService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.Date; +import java.util.List; + +@Service +public class SysMessageTaskService extends ServiceImpl { + + @Autowired + private ISysMessageService messageService; + + public void checkTask() { + QueryWrapper qw = new QueryWrapper<>(); + qw.eq("status", 0); + qw.lt("sendtime", new Date()); + List list = baseMapper.selectList(qw); + for (SysMessageTask task : list) { + SysMessage sm = new SysMessage(); + sm.setCode("系统消息"); + sm.setUserId(1); + sm.setId(null); + sm.setContent(task.getContent()); + sm.setParams(task.getParams()); + sm.setCtime(System.currentTimeMillis()); + messageService.sendToAll(sm); + task.setStatus(1); + baseMapper.updateById(task); + } + + } +} diff --git a/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/UniPushService.java b/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/UniPushService.java new file mode 100644 index 0000000..b72a60b --- /dev/null +++ b/mallplus-portal/src/main/java/com/zscat/mallplus/unipush/UniPushService.java @@ -0,0 +1,329 @@ +package com.zscat.mallplus.unipush; + +import cn.hutool.core.thread.ThreadUtil; +import cn.hutool.json.JSONUtil; +import com.getui.push.v2.sdk.ApiHelper; +import com.getui.push.v2.sdk.GtApiConfiguration; +import com.getui.push.v2.sdk.api.PushApi; +import com.getui.push.v2.sdk.common.ApiResult; +import com.getui.push.v2.sdk.dto.req.Audience; +import com.getui.push.v2.sdk.dto.req.AudienceDTO; +import com.getui.push.v2.sdk.dto.req.message.PushDTO; +import com.getui.push.v2.sdk.dto.req.message.PushMessage; +import com.getui.push.v2.sdk.dto.req.message.android.GTNotification; +import com.getui.push.v2.sdk.dto.res.TaskIdDTO; +import com.zscat.mallplus.ums.entity.SysMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@Component +public class UniPushService { + + private static final Logger L = LoggerFactory.getLogger(UniPushService.class); + + // @Value("${unipush.appid:UBUIDJ8NQm50rGJsB6LYx1}") + // private String appid; + + private static String apiurl = "https://restapi.getui.com/v2/"; + private static String appid = "UBUIDJ8NQm50rGJsB6LYx1"; + private static String appkey = "RS3UZfeS509hcNEkmfS6R"; + private static String appsecret = "6QuFcPWFga8DQzSa03ruR7"; + private static String mastersecret = "6fjUinwRfDA3BcEnDQvTl5"; + private static String apppackage = "org.jbase.yxt.yyd.pyw"; + + private static PushApi pushApi = null; + + private PushApi singlePushApi() { + if (pushApi == null) { + System.setProperty("http.maxConnections", "200"); + GtApiConfiguration apiConfiguration = new GtApiConfiguration(); + //填写应用配置 + apiConfiguration.setAppId(appid); + apiConfiguration.setAppKey(appkey); + apiConfiguration.setMasterSecret(mastersecret); + // 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId + apiConfiguration.setDomain(apiurl); + // 实例化ApiHelper对象,用于创建接口对象 + ApiHelper apiHelper = ApiHelper.build(apiConfiguration); + // 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi + pushApi = apiHelper.creatApi(PushApi.class); + } + return pushApi; + } + + + public void test() { + // 设置httpClient最大连接数,当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200 + System.setProperty("http.maxConnections", "200"); + GtApiConfiguration apiConfiguration = new GtApiConfiguration(); + //填写应用配置 + apiConfiguration.setAppId("UBUIDJ8NQm50rGJsB6LYx1"); + apiConfiguration.setAppKey("RS3UZfeS509hcNEkmfS6R"); + apiConfiguration.setMasterSecret("6fjUinwRfDA3BcEnDQvTl5"); + // 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId + apiConfiguration.setDomain("https://restapi.getui.com/v2/"); + // 实例化ApiHelper对象,用于创建接口对象 + ApiHelper apiHelper = ApiHelper.build(apiConfiguration); + // 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi + PushApi pushApi = apiHelper.creatApi(PushApi.class); + + //根据cid进行单推 + PushDTO pushDTO = new PushDTO(); + // 设置推送参数 + pushDTO.setRequestId(System.currentTimeMillis() + ""); + /**** 设置个推通道参数 *****/ + PushMessage pushMessage = new PushMessage(); + pushDTO.setPushMessage(pushMessage); + GTNotification notification = new GTNotification(); + pushMessage.setNotification(notification); + notification.setTitle("测试TitleXXX"); + notification.setBody("测试BodyXXXXXX"); + String payload = "{\"title\":\"liutitle\",\"body\":\"liubody\",\"name\":\"hao\",\"userid\":101,\"msgtitle\":\"haha\"}"; + notification.setPayload(payload); + notification.setClickType("payload"); +// notification.setClickType("url"); +// notification.setUrl("https://www.getui.com"); + /**** 设置个推通道参数,更多参数请查看文档或对象源码 *****/ + +// /**** 设置厂商相关参数 ****/ +// PushChannel pushChannel = new PushChannel(); +// pushDTO.setPushChannel(pushChannel); +// /*配置安卓厂商参数*/ +// AndroidDTO androidDTO = new AndroidDTO(); +// pushChannel.setAndroid(androidDTO); +// Ups ups = new Ups(); +// androidDTO.setUps(ups); +// ThirdNotification thirdNotification = new ThirdNotification(); +// ups.setNotification(thirdNotification); +// thirdNotification.setTitle("厂商title"); +// thirdNotification.setBody("厂商body"); +// thirdNotification.setClickType("url"); +// thirdNotification.setUrl("https://www.getui.com"); +// // 两条消息的notify_id相同,新的消息会覆盖老的消息,取值范围:0-2147483647 +// // thirdNotification.setNotifyId("11177"); +// /*配置安卓厂商参数结束,更多参数请查看文档或对象源码*/ + +// /*设置ios厂商参数*/ +// IosDTO iosDTO = new IosDTO(); +// pushChannel.setIos(iosDTO); +// // 相同的collapseId会覆盖之前的消息 +// iosDTO.setApnsCollapseId("xxx"); +// Aps aps = new Aps(); +// iosDTO.setAps(aps); +// Alert alert = new Alert(); +// aps.setAlert(alert); +// alert.setTitle("ios title"); +// alert.setBody("ios body"); +// /*设置ios厂商参数结束,更多参数请查看文档或对象源码*/ + + /*设置接收人信息*/ + Audience audience = new Audience(); + pushDTO.setAudience(audience); + audience.addCid("89e286aee78d38faf748a690e603b1a3"); + /*设置接收人信息结束*/ + /**** 设置厂商相关参数,更多参数请查看文档或对象源码 ****/ + + // 进行cid单推 + ApiResult>> apiResult = pushApi.pushToSingleByCid(pushDTO); + if (apiResult.isSuccess()) { + // success + System.out.println(apiResult.getData()); + } else { + // failed + System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); + } + + } + + public void testlist() { + String taskid = createMessageTaskaa(); + AudienceDTO pdto = new AudienceDTO(); + + Audience audience = new Audience(); + audience.addCid("89e286aee78d38faf748a690e603b1a3"); + audience.addCid("e4a96929cc5f40c886cb517853356bc2"); + + pdto.setAudience(audience); + pdto.setTaskid(taskid); + + ApiResult>> mapApiResult = singlePushApi().pushListByCid(pdto); + + System.out.println(mapApiResult); + } + + public String createMessageTaskaa() { + PushDTO pdto = new PushDTO<>(); + pdto.setGroupName("群发消息"); + + + PushMessage pushMessage = new PushMessage(); + + GTNotification notification = new GTNotification(); + notification.setTitle("测试Titleaa"); + notification.setBody("测试BodyXaa"); + + SysMessage msg = new SysMessage(); + msg.setId(11); + msg.setUserId(1); + msg.setCode("云链助手"); + msg.setParams("欢迎"); + msg.setContent("欢迎注册汇融云链~"); + msg.setCtime(System.currentTimeMillis()); + +// String payload = "{\"title\":\"lzh\",\"body\":\"liubody\",\"name\":\"hao\",\"userid\":101,\"msgtitle\":\"haha\"}"; + String payload = JSONUtil.toJsonStr(msg); + notification.setPayload(payload); + notification.setClickType("payload"); + + pushMessage.setNotification(notification); + + pdto.setPushMessage(pushMessage); + + ApiResult msgg = this.singlePushApi().createMsg(pdto); + System.out.println(msgg); + return msgg.getData().getTaskId(); + } + + public static void main(String[] args) { + List clist = new ArrayList<>(); + clist.add("079fb42d1f4f824a3defd471a432d0fe"); + clist.add("89e286aee78d38faf748a690e603b1a3"); + + SysMessage msg = new SysMessage(); + msg.setId(11); + msg.setUserId(1); + msg.setCode("交易物流"); + msg.setParams("ABC"); + msg.setContent("欢迎注册汇融云链~"); + msg.setCtime(System.currentTimeMillis()); + new UniPushService().sendPush(msg, clist); +// new UniPushService().testlist(); + } + + private static String imgUrltx = "http://mall.yyundong.com/tx.png"; + private static String imgUrltz = "http://mall.yyundong.com/tz.png"; + private static String imgUrlwl = "http://mall.yyundong.com/wl.png"; + + public void sendPush(SysMessage mess, List cids) { + // String code = mess.getCode(); + // if ("系统消息".equals(code)) { + // sendTzMsg(mess); + // } else { + // sendUserMsg(mess, cids); + // } + + if (cids == null || cids.isEmpty()) + return; + + PushMessage pushMessage = createPushMessage(mess); + int size = cids.size(); + for (int i = 0; i < size; i++) { + sendSingleMsg(pushMessage, cids.get(i), i); + } + + } + + public void sendPushAsync(SysMessage mess, List cids) { + ThreadUtil.execute(() -> sendPush(mess, cids)); + } + + private void sendSingleMsg(PushMessage pushMessage, String cid, int index) { + PushDTO pushDTO = new PushDTO(); + pushDTO.setRequestId("" + System.currentTimeMillis() + index); + pushDTO.setPushMessage(pushMessage); + Audience audience = new Audience(); + audience.addCid(cid); + pushDTO.setAudience(audience); + ApiResult>> apiResult = singlePushApi().pushToSingleByCid(pushDTO); + if (apiResult.isSuccess()) { + System.out.println(apiResult.getData()); + } else { + L.warn("推送信息失败:code:{}, msg: {}", apiResult.getCode(), apiResult.getMsg()); + } + } + + private void sendUserMsg(SysMessage mess, List cids) { + + if (cids == null || cids.isEmpty()) + return; + + String taskid = createMessageTask(mess); + AudienceDTO pdto = new AudienceDTO(); + + Audience audience = new Audience(); + cids.forEach(cid -> audience.addCid(cid)); + + pdto.setAudience(audience); + pdto.setTaskid(taskid); + + ApiResult>> mapApiResult = singlePushApi().pushListByCid(pdto); + + } + + private void sendTzMsg(SysMessage mess) { + + PushDTO pd = new PushDTO<>(); + pd.setRequestId("all" + System.currentTimeMillis()); + pd.setGroupName("system_notice"); + pd.setAudience("all"); + + pd.setPushMessage(createPushMessage(mess)); + + ApiResult taskIdDTOApiResult = singlePushApi().pushAll(pd); +// System.out.println(taskIdDTOApiResult); + } + + private PushMessage createPushMessage(SysMessage mess) { + + String code = mess.getCode(); + PushMessage pushMessage = new PushMessage(); + + GTNotification notification = new GTNotification(); +// notification.setTitle(code); +// notification.setBody(mess.getParams()); + notification.setTitle(mess.getParams()); + notification.setBody(mess.getContent()); + if ("交易物流".equals(code)) { + notification.setLogoUrl(imgUrlwl); + } else if ("云链助手".equals(code)) { + notification.setLogoUrl(imgUrltx); + } else { + notification.setLogoUrl(imgUrltz); + } + Map map = new HashMap<>(); + map.put("code", mess.getCode()); + map.put("params", mess.getParams()); + map.put("content", mess.getContent()); + map.put("cdate", mess.getCdate()); + map.put("id", mess.getId()); + map.put("ctime", mess.getCtime()); + map.put("status", mess.getStatus()); + map.put("userId", mess.getUserId()); + String payload = JSONUtil.toJsonStr(map); + notification.setPayload(payload); + notification.setClickType("payload"); +// notification.setClickType("payload_custom"); + + pushMessage.setNotification(notification); + + return pushMessage; + } + + private String createMessageTask(SysMessage mess) { + PushDTO pdto = new PushDTO<>(); + pdto.setGroupName("tolist"); + + pdto.setPushMessage(createPushMessage(mess)); + + ApiResult result = this.singlePushApi().createMsg(pdto); +// System.out.println(msgg); + return result.getData().getTaskId(); + } + +}