BusinessPushService.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.ruoyi.app.push;
  2. import com.ruoyi.app.utils.PayPush;
  3. import com.ruoyi.system.domain.InfoUser;
  4. import com.ruoyi.system.service.IInfoUserService;
  5. import org.slf4j.Logger;
  6. import org.slf4j.LoggerFactory;
  7. import org.springframework.stereotype.Service;
  8. import java.util.Locale;
  9. @Service
  10. public class BusinessPushService {
  11. private static final Logger log = LoggerFactory.getLogger(BusinessPushService.class);
  12. private final BusinessPushProperties properties;
  13. private final IInfoUserService users;
  14. private final FcmPushSender fcm;
  15. private final ApnsPushSender apns;
  16. private final PayPush uni;
  17. public BusinessPushService(BusinessPushProperties properties, IInfoUserService users,
  18. FcmPushSender fcm, ApnsPushSender apns, PayPush uni) {
  19. this.properties = properties;
  20. this.users = users;
  21. this.fcm = fcm;
  22. this.apns = apns;
  23. this.uni = uni;
  24. }
  25. public void send(RecipientApp app, Long userId, String cid,
  26. String title, String content, String payload) {
  27. String platform = "unknown";
  28. try {
  29. if (properties.getMode() == BusinessPushProperties.Mode.UNI) {
  30. if (cid == null || cid.isBlank()) {
  31. log.warn("Uni push address missing, app={}, userId={}", app, userId);
  32. return;
  33. }
  34. sendUni(app, cid, title, content, payload);
  35. return;
  36. }
  37. InfoUser current = userId == null ? null : users.getById(userId);
  38. String resolvedPlatform = platform(current);
  39. if (resolvedPlatform == null) {
  40. log.warn("Native push skipped, app={}, userId={}, platform={}, category=PLATFORM_INVALID_OR_USER_MISSING",
  41. app, userId, platform);
  42. return;
  43. }
  44. platform = resolvedPlatform;
  45. if (current.getDeviceToken() == null || current.getDeviceToken().isBlank()) {
  46. log.warn("Native push skipped, app={}, userId={}, platform={}, category=TOKEN_MISSING",
  47. app, userId, platform);
  48. return;
  49. }
  50. if ("android".equals(platform)) {
  51. fcm.send(app, current.getDeviceToken(), title, content, payload);
  52. } else {
  53. apns.send(app, current.getDeviceToken(), title, content, payload);
  54. }
  55. } catch (RuntimeException exception) {
  56. log.warn("Business push failed, app={}, userId={}, mode={}, platform={}, category={}",
  57. app, userId, properties.getMode(), platform, failureCategory(exception));
  58. }
  59. }
  60. private static String failureCategory(RuntimeException exception) {
  61. String category = exception.getMessage();
  62. if (exception instanceof IllegalStateException && category != null
  63. && category.matches("(?:FCM|APNS)_[A-Za-z0-9_]{1,64}")) {
  64. return category;
  65. }
  66. return exception.getClass().getSimpleName();
  67. }
  68. public String addressKey(InfoUser recipient) {
  69. if (recipient == null) {
  70. return null;
  71. }
  72. if (properties.getMode() == BusinessPushProperties.Mode.UNI) {
  73. String cid = recipient.getCid();
  74. return cid == null || cid.isBlank() ? null : cid;
  75. }
  76. try {
  77. InfoUser current = recipient.getUserId() == null ? null : users.getById(recipient.getUserId());
  78. String platform = platform(current);
  79. if (platform == null || current.getDeviceToken() == null || current.getDeviceToken().isBlank()) {
  80. return null;
  81. }
  82. return platform + ":" + current.getDeviceToken();
  83. } catch (RuntimeException exception) {
  84. log.warn("Cannot resolve push address, userId={}, category={}",
  85. recipient.getUserId(), exception.getClass().getSimpleName());
  86. return null;
  87. }
  88. }
  89. private void sendUni(RecipientApp app, String cid, String title, String content, String payload) {
  90. switch (app) {
  91. case USER -> uni.apppush(cid, title, content, payload);
  92. case MERCHANT -> uni.shpush(cid, title, content, payload);
  93. case RIDER -> uni.qspush(cid, title, content, payload);
  94. }
  95. }
  96. private static String platform(InfoUser user) {
  97. if (user == null || user.getCidType() == null) {
  98. return null;
  99. }
  100. String normalized = user.getCidType().trim().toLowerCase(Locale.ROOT);
  101. return "android".equals(normalized) || "ios".equals(normalized) ? normalized : null;
  102. }
  103. }