| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package com.ruoyi.app.push;
- import com.ruoyi.app.utils.PayPush;
- import com.ruoyi.system.domain.InfoUser;
- import com.ruoyi.system.service.IInfoUserService;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import java.util.Locale;
- @Service
- public class BusinessPushService {
- private static final Logger log = LoggerFactory.getLogger(BusinessPushService.class);
- private final BusinessPushProperties properties;
- private final IInfoUserService users;
- private final FcmPushSender fcm;
- private final ApnsPushSender apns;
- private final PayPush uni;
- public BusinessPushService(BusinessPushProperties properties, IInfoUserService users,
- FcmPushSender fcm, ApnsPushSender apns, PayPush uni) {
- this.properties = properties;
- this.users = users;
- this.fcm = fcm;
- this.apns = apns;
- this.uni = uni;
- }
- public void send(RecipientApp app, Long userId, String cid,
- String title, String content, String payload) {
- String platform = "unknown";
- try {
- if (properties.getMode() == BusinessPushProperties.Mode.UNI) {
- if (cid == null || cid.isBlank()) {
- log.warn("Uni push address missing, app={}, userId={}", app, userId);
- return;
- }
- sendUni(app, cid, title, content, payload);
- return;
- }
- InfoUser current = userId == null ? null : users.getById(userId);
- String resolvedPlatform = platform(current);
- if (resolvedPlatform == null) {
- log.warn("Native push skipped, app={}, userId={}, platform={}, category=PLATFORM_INVALID_OR_USER_MISSING",
- app, userId, platform);
- return;
- }
- platform = resolvedPlatform;
- if (current.getDeviceToken() == null || current.getDeviceToken().isBlank()) {
- log.warn("Native push skipped, app={}, userId={}, platform={}, category=TOKEN_MISSING",
- app, userId, platform);
- return;
- }
- if ("android".equals(platform)) {
- fcm.send(app, current.getDeviceToken(), title, content, payload);
- } else {
- apns.send(app, current.getDeviceToken(), title, content, payload);
- }
- } catch (RuntimeException exception) {
- log.warn("Business push failed, app={}, userId={}, mode={}, platform={}, category={}",
- app, userId, properties.getMode(), platform, failureCategory(exception));
- }
- }
- private static String failureCategory(RuntimeException exception) {
- String category = exception.getMessage();
- if (exception instanceof IllegalStateException && category != null
- && category.matches("(?:FCM|APNS)_[A-Za-z0-9_]{1,64}")) {
- return category;
- }
- return exception.getClass().getSimpleName();
- }
- public String addressKey(InfoUser recipient) {
- if (recipient == null) {
- return null;
- }
- if (properties.getMode() == BusinessPushProperties.Mode.UNI) {
- String cid = recipient.getCid();
- return cid == null || cid.isBlank() ? null : cid;
- }
- try {
- InfoUser current = recipient.getUserId() == null ? null : users.getById(recipient.getUserId());
- String platform = platform(current);
- if (platform == null || current.getDeviceToken() == null || current.getDeviceToken().isBlank()) {
- return null;
- }
- return platform + ":" + current.getDeviceToken();
- } catch (RuntimeException exception) {
- log.warn("Cannot resolve push address, userId={}, category={}",
- recipient.getUserId(), exception.getClass().getSimpleName());
- return null;
- }
- }
- private void sendUni(RecipientApp app, String cid, String title, String content, String payload) {
- switch (app) {
- case USER -> uni.apppush(cid, title, content, payload);
- case MERCHANT -> uni.shpush(cid, title, content, payload);
- case RIDER -> uni.qspush(cid, title, content, payload);
- }
- }
- private static String platform(InfoUser user) {
- if (user == null || user.getCidType() == null) {
- return null;
- }
- String normalized = user.getCidType().trim().toLowerCase(Locale.ROOT);
- return "android".equals(normalized) || "ios".equals(normalized) ? normalized : null;
- }
- }
|