NativePushSoundResolver.java 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.ruoyi.app.push;
  2. import com.alibaba.fastjson2.JSON;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import com.ruoyi.system.utils.JedisFactory;
  5. import org.springframework.stereotype.Component;
  6. import redis.clients.jedis.Jedis;
  7. import java.util.Locale;
  8. import java.util.Set;
  9. /** 仅处理原生用户通知;从明确场景选择随包音频,不根据状态或通知文案猜测声音。 */
  10. @Component
  11. public class NativePushSoundResolver {
  12. private static final Set<String> SCENES = Set.of("RIDER_ACCEPTED", "ORDER_DELIVERING", "ORDER_DELIVERED",
  13. "MERCHANT_ACCEPTED", "MERCHANT_READY", "PAYMENT_SUCCEEDED", "ORDER_CANCELLED");
  14. private static final Set<String> STOP_EVENTS = Set.of("FLASH_PICKUP_STOP_COMPLETED", "FLASH_DELIVERY_STOP_COMPLETED");
  15. private final JedisFactory jedisFactory;
  16. public NativePushSoundResolver(JedisFactory jedisFactory) {
  17. this.jedisFactory = jedisFactory;
  18. }
  19. public NativePushSound forRecipient(RecipientApp app, Long userId, String payload) {
  20. String scene = scene(app, payload);
  21. if (scene == null) {
  22. return null;
  23. }
  24. String language = null;
  25. if (userId != null) {
  26. // 与 /changeLanguages 保持同一 DB 和原始字符串格式,不经过 JSON Redis 反序列化。
  27. try (Jedis jedis = jedisFactory.create()) {
  28. language = jedis.get(userId.toString());
  29. } catch (RuntimeException ignored) {
  30. // 缺失或暂时读不到语言不阻断通知,统一回退繁体中文。
  31. }
  32. }
  33. return sound(scene, language);
  34. }
  35. public NativePushSound forTest(RecipientApp app, String language, String payload) {
  36. String scene = scene(app, payload);
  37. return scene == null ? null : sound(scene, language);
  38. }
  39. private static String scene(RecipientApp app, String payload) {
  40. if (app != RecipientApp.USER || payload == null) {
  41. return null;
  42. }
  43. try {
  44. JSONObject fields = JSON.parseObject(payload);
  45. if (fields == null) {
  46. return null;
  47. }
  48. String scene = fields.getString("eventScene");
  49. if (scene != null && SCENES.contains(scene)) {
  50. return scene;
  51. }
  52. String event = fields.getString("eventType");
  53. return event != null && STOP_EVENTS.contains(event) ? event : null;
  54. } catch (RuntimeException ignored) {
  55. return null;
  56. }
  57. }
  58. private static NativePushSound sound(String scene, String language) {
  59. return new NativePushSound(scene.toLowerCase(Locale.ROOT) + "_" + languageSuffix(language));
  60. }
  61. private static String languageSuffix(String language) {
  62. if (language == null || language.isBlank()) {
  63. return "zh_hant";
  64. }
  65. String tag = language.trim().replace('_', '-').toLowerCase(Locale.ROOT);
  66. if (tag.equals("zhhant") || tag.equals("tw") || tag.equals("zhtw")
  67. || tag.equals("zh-tw") || tag.equals("zh-hk") || tag.equals("zh-mo")
  68. || tag.equals("zh-hant") || tag.startsWith("zh-hant-")) {
  69. return "zh_hant";
  70. }
  71. if (tag.equals("zhhans") || tag.equals("cn") || tag.equals("zhcn")
  72. || tag.equals("zh-cn") || tag.equals("zh-sg")
  73. || tag.equals("zh-hans") || tag.startsWith("zh-hans-")) {
  74. return "zh_hans";
  75. }
  76. return switch (tag.split("-", 2)[0]) {
  77. case "en" -> "en";
  78. case "th" -> "th";
  79. case "id", "in" -> "id";
  80. case "ms" -> "ms";
  81. case "tl", "fil" -> "tl";
  82. default -> "zh_hant";
  83. };
  84. }
  85. }