| ¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.project.system.service.impl; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | 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.message.PushChannel; |
| | | 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.AndroidDTO; |
| | | import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification; |
| | | import com.getui.push.v2.sdk.dto.req.message.android.Ups; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.project.system.domain.GetuiConfig; |
| | | import com.ruoyi.project.system.domain.SysMenu; |
| | | import com.ruoyi.project.system.domain.SysNotice; |
| | | import com.ruoyi.project.system.domain.SysUserClient; |
| | | import com.ruoyi.project.system.mapper.SysMenuMapper; |
| | | import com.ruoyi.project.system.service.SysUserClientService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.jetbrains.annotations.NotNull; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.Async; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * APPæ¶æ¯æ¨éæå¡ |
| | | * |
| | | * @author deslrey |
| | | * @version 1.4 |
| | | * @since 2026/2/9 |
| | | */ |
| | | @Slf4j |
| | | @Component |
| | | public class UnipushService { |
| | | |
| | | @Autowired |
| | | private SysMenuMapper sysMenuMapper; |
| | | |
| | | @Autowired |
| | | private GetuiConfig getuiConfig; |
| | | |
| | | @Autowired |
| | | private SysUserClientService userClientService; |
| | | |
| | | private PushApi pushApi; |
| | | |
| | | private static final String DEFAULT_APP_PAGE = "pages/index"; |
| | | |
| | | @PostConstruct |
| | | public void init() { |
| | | GtApiConfiguration config = new GtApiConfiguration(); |
| | | config.setAppId(getuiConfig.getAppId()); |
| | | config.setAppKey(getuiConfig.getAppKey()); |
| | | config.setMasterSecret(getuiConfig.getMasterSecret()); |
| | | config.setDomain(getuiConfig.getDomain()); |
| | | ApiHelper apiHelper = ApiHelper.build(config); |
| | | this.pushApi = apiHelper.creatApi(PushApi.class); |
| | | } |
| | | |
| | | /** |
| | | * æ¹éåééç¥å
¬åå°ç§»å¨ç«¯ |
| | | */ |
| | | @Async |
| | | public void sendClientMessage(List<SysNotice> sysNoticeList) { |
| | | if (sysNoticeList == null || sysNoticeList.isEmpty()) { |
| | | return; |
| | | } |
| | | |
| | | for (SysNotice sysNotice : sysNoticeList) { |
| | | SysUserClient client = userClientService.getById(sysNotice.getConsigneeId()); |
| | | if (client == null || StringUtils.isEmpty(client.getCid())) { |
| | | log.warn("ç¨æ· {} æªç»å®ç§»å¨ç«¯ CID,è·³è¿æ¨é", sysNotice.getConsigneeId()); |
| | | continue; |
| | | } |
| | | |
| | | // 转æ¢è·¯å¾ |
| | | String appPath = convertWebPathToAppPath(sysNotice.getJumpPath()); |
| | | |
| | | // æ¨é |
| | | sendRoutingPush( |
| | | sysNotice.getNoticeId(), |
| | | client.getCid(), |
| | | sysNotice.getNoticeTitle(), |
| | | sysNotice.getRemark() != null ? sysNotice.getRemark() : sysNotice.getNoticeContent(), |
| | | appPath |
| | | ); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * å° Web 端åå±å
¨è·¯å¾è½¬æ¢ä¸º App 端ç»ä»¶è·¯ç± |
| | | */ |
| | | public String convertWebPathToAppPath(String webPath) { |
| | | if (StringUtils.isEmpty(webPath)) { |
| | | return DEFAULT_APP_PAGE; |
| | | } |
| | | |
| | | String pathOnly = webPath; |
| | | String queryString = ""; |
| | | if (webPath.contains("?")) { |
| | | int index = webPath.indexOf("?"); |
| | | pathOnly = webPath.substring(0, index); |
| | | queryString = webPath.substring(index); |
| | | } |
| | | |
| | | String lastSegment; |
| | | int lastSlashIndex = pathOnly.lastIndexOf("/"); |
| | | if (lastSlashIndex != -1) { |
| | | lastSegment = pathOnly.substring(lastSlashIndex + 1); |
| | | } else { |
| | | lastSegment = pathOnly; |
| | | } |
| | | |
| | | if (StringUtils.isEmpty(lastSegment)) { |
| | | return DEFAULT_APP_PAGE; |
| | | } |
| | | |
| | | SysMenu menu = sysMenuMapper.selectMenuByPath(lastSegment); |
| | | |
| | | if (menu != null && StringUtils.isNotEmpty(menu.getAppComponent())) { |
| | | String appPath = menu.getAppComponent(); |
| | | |
| | | if (appPath.startsWith("/")) { |
| | | appPath = appPath.substring(1); |
| | | } |
| | | |
| | | // æ¼æ¥ Web 端åå§åæ°å¹¶è¿å |
| | | return appPath + queryString; |
| | | } |
| | | |
| | | return DEFAULT_APP_PAGE; |
| | | } |
| | | |
| | | /** |
| | | * åéåäººè·¯ç±æ¨é |
| | | */ |
| | | private void sendRoutingPush(Long noticeId, String cid, String title, String content, String targetPath) { |
| | | log.info("å夿¨éæ¶æ¯:NoticeId={}, CID={}, Title={}, TargetPath={}", noticeId, cid, title, targetPath); |
| | | |
| | | PushDTO<Audience> pushDTO = new PushDTO<>(); |
| | | pushDTO.setRequestId("REQ_" + System.currentTimeMillis()); |
| | | |
| | | // å¨çº¿éä¼ å
容 |
| | | PushMessage pushMessage = new PushMessage(); |
| | | Map<String, Object> pushMessageMap = new HashMap<>(); |
| | | Map<String, Object> payloadMap = new HashMap<>(); |
| | | pushMessageMap.put("title", title); |
| | | pushMessageMap.put("content", content); |
| | | payloadMap.put("url", targetPath); |
| | | payloadMap.put("noticeId", noticeId); |
| | | pushMessageMap.put("payload", JSON.toJSONString(payloadMap)); |
| | | |
| | | String transmissionContent = JSON.toJSONString(pushMessageMap); |
| | | pushMessage.setTransmission(transmissionContent); |
| | | pushDTO.setPushMessage(pushMessage); |
| | | |
| | | // æ¥æ¶äºº |
| | | Audience audience = new Audience(); |
| | | audience.addCid(cid); |
| | | pushDTO.setAudience(audience); |
| | | |
| | | // 离线æ¨ééé |
| | | // pushDTO.setPushChannel(getPushChannel(noticeId, title, content, targetPath)); |
| | | |
| | | try { |
| | | ApiResult<Map<String, Map<String, String>>> result = pushApi.pushToSingleByCid(pushDTO); |
| | | if (result.isSuccess()) { |
| | | log.info("Unipush æ¨éæå: CID={}", cid); |
| | | } else { |
| | | log.error("Unipush æ¨é失败: CID={}, Code={}, Msg={}", cid, result.getCode(), result.getMsg()); |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("Unipush æ¨éå¼å¸¸: ", e); |
| | | } |
| | | } |
| | | |
| | | @NotNull |
| | | private PushChannel getPushChannel(Long noticeId, String title, String content, String targetPath) { |
| | | PushChannel pushChannel = new PushChannel(); |
| | | AndroidDTO androidDTO = new AndroidDTO(); |
| | | Ups ups = new Ups(); |
| | | ThirdNotification thirdNotification = new ThirdNotification(); |
| | | thirdNotification.setTitle(title); |
| | | thirdNotification.setBody(content); |
| | | thirdNotification.setClickType("intent"); |
| | | |
| | | String intent = "intent:#Intent;launchFlags=0x04000000;" |
| | | + "component=" + getuiConfig.getIntentComponent() + ";" |
| | | + "S.UP-OL-P9=true;" |
| | | + "S.path=" + targetPath + ";" |
| | | + "S.payload=" + targetPath + ";" |
| | | + "end"; |
| | | thirdNotification.setIntent(intent); |
| | | |
| | | ups.setNotification(thirdNotification); |
| | | androidDTO.setUps(ups); |
| | | pushChannel.setAndroid(androidDTO); |
| | | return pushChannel; |
| | | } |
| | | } |