Преглед на файлове

用户下单给商家推送

qmj преди 1 седмица
родител
ревизия
b04cd2ddc8

+ 27 - 0
ruoyi-admin/src/main/java/com/ruoyi/app/order/UserOrderController.java

@@ -6,14 +6,18 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.ruoyi.app.order.dto.OrderCreateInput;
 import com.ruoyi.app.order.dto.OrderCreatItem;
+import com.ruoyi.app.order.dto.OrderPushBodyDto;
 import com.ruoyi.app.utils.DateUtil;
 import com.ruoyi.app.utils.OperatingUtil;
+import com.ruoyi.app.utils.PayPush;
+import com.ruoyi.app.utils.event.PushEventService;
 import com.ruoyi.common.annotation.Anonymous;
 import com.ruoyi.common.core.controller.BaseController;
 import com.ruoyi.common.core.domain.AjaxResult;
 import com.ruoyi.common.exception.ServiceException;
 import com.ruoyi.common.utils.MessageUtils;
 import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.framework.manager.AsyncManager;
 import com.ruoyi.system.domain.*;
 import com.ruoyi.system.service.*;
 import com.ruoyi.system.utils.Auth;
@@ -25,6 +29,7 @@ import org.springframework.transaction.annotation.Transactional;
 import java.util.Date;
 import java.util.List;
 import java.util.Optional;
+import java.util.TimerTask;
 import java.util.stream.Collectors;
 
 /**
@@ -48,6 +53,10 @@ public class UserOrderController extends BaseController {
     private IOperatingHoursService operatingHoursService;
     @Autowired
     private IUserFootprintService userFootprintService;
+    @Autowired
+    private IInfoUserService infoUserService;
+    @Autowired
+    private PushEventService pushEventService;
 
     /**
      * 创建订单
@@ -168,6 +177,24 @@ public class UserOrderController extends BaseController {
             // 保存子订单
             createFootprint(userId, item.getMdId());
             posOrderService.save(posOrder);
+
+            // 创建订单成功,给商家推送"您有新订单了"
+            InfoUser shUser = infoUserService.getById(item.getShId());
+            if (shUser != null && !StringUtils.isEmpty(shUser.getCid())) {
+                PayPush push = new PayPush();
+                String title = "no.message.push.message";
+                String content = "no.message.push.new.order";
+                String body = OrderPushBodyDto.getJson(subddId, String.valueOf(posOrder.getState()), 0);
+                Long shUserId = shUser.getUserId();
+                String cid = shUser.getCid();
+
+                AsyncManager.me().execute(new TimerTask() {
+                    @Override
+                    public void run() {
+                        PayPush.shPushHandleLocal(push, pushEventService, shUserId, cid, title, content, body, "", subddId);
+                    }
+                });
+            }
         }
     }
 

+ 170 - 22
ruoyi-admin/src/main/java/com/ruoyi/app/utils/PayPush.java

@@ -4,23 +4,34 @@ import cn.hutool.http.HttpRequest;
 import cn.hutool.http.HttpUtil;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
+import com.ruoyi.app.utils.event.PushEventService;
+import com.ruoyi.common.utils.LocaleUtils;
+import com.ruoyi.common.utils.MessageUtils;
+import com.ruoyi.common.utils.StringUtils;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.context.i18n.LocaleContextHolder;
 
 import java.util.List;
+import java.util.Locale;
 
 public class PayPush {
+    private static final Logger log = LoggerFactory.getLogger(PayPush.class);
+
     //用户端推送
-    public void apppush(String cid,String title, String content, String body){
-        System.out.println("用户端推送cid:"+cid);
-        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msduser?cid="+cid+
-                "&title="+title+"&content="+content+"&payload="+body;
-        String res="";
+    public void apppush(String cid, String title, String content, String body) {
+        System.out.println("用户端推送cid:" + cid);
+        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msduser?cid=" + cid +
+                "&title=" + title + "&content=" + content + "&payload=" + body;
+        String res = "";
         res = HttpUtil.get(url);
-        JSONObject jsonObject =  JSON.parseObject(res);
-        System.out.println("=========================1111111111111111"+res);
+        JSONObject jsonObject = JSON.parseObject(res);
+        System.out.println("=========================1111111111111111" + res);
     }
 
     //用户端批量推送
-    public void userPushBatch(List<String> cid, String title, String content, String body){
+    public void userPushBatch(List<String> cid, String title, String content, String body) {
         String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msduser";
 
         JSONObject json = new JSONObject();
@@ -41,17 +52,17 @@ public class PayPush {
     }
 
     //骑手端推送
-    public void qspush(String cid,String title, String content, String body){
-        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdrider?cid="+cid+
-                "&title="+title+"&content="+content+"&payload="+body+"&sound=pushsound.caf";
-        String res="";
+    public void qspush(String cid, String title, String content, String body) {
+        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdrider?cid=" + cid +
+                "&title=" + title + "&content=" + content + "&payload=" + body + "&sound=pushsound.caf";
+        String res = "";
         res = HttpUtil.get(url);
-        JSONObject jsonObject =  JSON.parseObject(res);
-        System.out.println("=========================用户推送"+res);
+        JSONObject jsonObject = JSON.parseObject(res);
+        System.out.println("=========================用户推送" + res);
     }
 
     //用户端批量推送
-    public void qsPushBatch(List<String> cid, String title, String content, String body){
+    public void qsPushBatch(List<String> cid, String title, String content, String body) {
         String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdrider";
 
         JSONObject json = new JSONObject();
@@ -72,13 +83,150 @@ public class PayPush {
     }
 
     //商家端推送
-    public void shpush(String cid,String title, String content, String body){
-        System.out.println("商家cid:"+cid);
-        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdstore?cid="+cid+
-                "&title="+title+"&content="+content+"&payload="+body;
-        String res="";
+    public void shpush(String cid, String title, String content, String body) {
+        System.out.println("商家cid:" + cid);
+        String url = "https://fc-mp-20ccbe79-10f8-41d7-a06c-c42581f351c1.next.bspapp.com/msdstore?cid=" + cid +
+                "&title=" + title + "&content=" + content + "&payload=" + body;
+        String res = "";
         res = HttpUtil.get(url);
-        JSONObject jsonObject =  JSON.parseObject(res);
-        System.out.println("=========================商家推送"+res);
+        JSONObject jsonObject = JSON.parseObject(res);
+        System.out.println("=========================商家推送" + res);
     }
+
+    /**
+     * 商家推送
+     *
+     * @param push
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     */
+    public static void shPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
+        if (pushEventService != null) {
+            pushEventService.PublisherEvent(userId, title, content, body);
+        }
+        if (cid == null || cid.trim().isEmpty()) {
+            log.error("商家推送失败:Device token is empty, userId:{}", userId);
+            return;
+        }
+
+        push.shpush(cid, title, content, body);
+
+
+    }
+
+    /**
+     * 发送前设置local信息
+     *
+     * @param push
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     * @param cidType
+     * @param ddId
+     */
+    public static void shPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
+        Locale local = LocaleUtils.getUserLocale(userId);
+        LocaleContextHolder.setLocale(local);
+        title = MessageUtils.message(title);
+        content = MessageUtils.message(content) + ",NO:" + ddId;
+        shPush(push, pushEventService, userId, cid, title, content, body, cidType);
+    }
+
+    /**
+     * 骑手推送
+     *
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     */
+    public static void qsPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
+        if (pushEventService != null) {
+            pushEventService.PublisherEvent(userId, title, content, body);
+        }
+        if (cid == null || cid.trim().isEmpty()) {
+            log.error("骑手推送失败:Device token is empty, userId:{}", userId);
+            return;
+        }
+        log.info("useId{}:cid{}:cidType{}:content:{}", userId, cid, cidType, content);
+        log.info("给骑手发送进入uni推送:{},{},{},{}", userId, cid, cidType, content);
+        push.qspush(cid, title, content, body);
+    }
+
+    /**
+     * 发送前设置local信息
+     *
+     * @param push
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     * @param cidType
+     * @param ddId
+     */
+    public static void qsPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
+        Locale local = LocaleUtils.getUserLocale(userId);
+        LocaleContextHolder.setLocale(local);
+        title = MessageUtils.message(title);
+        content = MessageUtils.message(content) + ",NO:" + ddId;
+        qsPush(push, pushEventService, userId, cid, title, content, body, cidType);
+    }
+
+    /**
+     * 用户推送
+     *
+     * @param push
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     */
+    public static void userPush(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType) {
+        if (pushEventService != null) {
+            pushEventService.PublisherEvent(userId, title, content, body);
+        }
+        if (cid == null || cid.trim().isEmpty()) {
+            log.error("Device token is empty, cannot send push message");
+            return;
+        }
+        push.apppush(cid, title, content, body);
+    }
+
+    /**
+     * 发送前设置local信息
+     *
+     * @param push
+     * @param pushEventService
+     * @param userId
+     * @param cid
+     * @param title
+     * @param content
+     * @param body
+     * @param cidType
+     * @param ddId
+     */
+    public static void userPushHandleLocal(PayPush push, PushEventService pushEventService, Long userId, String cid, String title, String content, String body, String cidType, String ddId) {
+        Locale local = LocaleUtils.getUserLocale(userId);
+        LocaleContextHolder.setLocale(local);
+        title = MessageUtils.message(title);
+        content = MessageUtils.message(content);
+        if (!StringUtils.isEmpty(ddId)) {
+            content = content + ",NO:" + ddId;
+        }
+        userPush(push, pushEventService, userId, cid, title, content, body, cidType);
+    }
+
 }

+ 190 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/LocaleUtils.java

@@ -0,0 +1,190 @@
+package com.ruoyi.common.utils;
+
+import com.ruoyi.common.core.redis.RedisCache;
+import com.ruoyi.common.utils.spring.SpringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Locale;
+
+/**
+ * ���Ի���������
+ * ���ڴ�����������صĹ��߷���
+ *
+ * @author ruoyi
+ */
+public class LocaleUtils
+{
+    private static final Logger log = LoggerFactory.getLogger(LocaleUtils.class);
+
+    /** Ĭ�����Ի�����Խ��� */
+    private static final Locale DEFAULT_LOCALE = new Locale("zh_TW");
+
+    /** Redis���û��������õ�keyǰ׺ */
+    private static final String USER_LANGUAGE_KEY_PREFIX = "user:language:";
+
+    /**
+     * ���������ַ���ΪLocale����
+     * ֧�ֶ��ָ�ʽ��
+     * - �»��߸�ʽ��zh_CN, en_US
+     * - ���ַ���ʽ��zh-CN, en-US
+     * - �����Դ��룺zh, en, vi
+     *
+     * @param language �����ַ���
+     * @return Locale�������languageΪ���򷵻�Ĭ��Locale��Խ���
+     */
+    public static Locale parseLocale(String language)
+    {
+        if (StringUtils.isEmpty(language))
+        {
+            return DEFAULT_LOCALE;
+        }
+
+        // �����»��߸�ʽ���� zh_CN��
+        if (language.contains("_"))
+        {
+            String[] parts = language.split("_", 2);
+            if (parts.length == 2)
+            {
+                return new Locale(parts[0], parts[1]);
+            }
+            else
+            {
+                return new Locale(parts[0]);
+            }
+        }
+
+        // �������ַ���ʽ���� zh-CN��
+        if (language.contains("-"))
+        {
+            return Locale.forLanguageTag(language);
+        }
+
+        // �����Դ��루�� zh, en, vi��
+        return new Locale(language);
+    }
+
+    /**
+     * ��ȡ�û������Ի���
+     * ���ȴ�Redis�л�ȡ�û����������ã�����������򷵻�Ĭ�����Ի�����Խ���
+     *
+     * @param userId �û�ID
+     * @return Locale����
+     */
+    public static Locale getUserLocale(Long userId)
+    {
+        if (userId == null)
+        {
+            return DEFAULT_LOCALE;
+        }
+
+        try
+        {
+            // ��Redis��ȡ�û���������
+            RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
+            if (redisCache != null)
+            {
+                String key = userId.toString();
+                String language = redisCache.getCacheObject(key);
+                if (StringUtils.isNotEmpty(language))
+                {
+                    return parseLocale(language);
+                }
+            }
+        }
+        catch (Exception e)
+        {
+            log.debug("��ȡ�û���������ʧ�ܣ�ʹ��Ĭ�����ԣ��û�ID: {}", userId, e);
+        }
+
+        // Ĭ�Ϸ���Խ����
+        return DEFAULT_LOCALE;
+    }
+
+    /**
+     * �����û������Ի�����Redis
+     *
+     * @param userId �û�ID
+     * @param language �����ַ���
+     */
+    public static void setUserLocale(String userId, String language)
+    {
+        if (userId == null || StringUtils.isEmpty(language) || StringUtils.isEmpty(userId))
+        {
+            return;
+        }
+
+        try
+        {
+            RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
+            if (redisCache != null)
+            {
+                String key = userId;
+                redisCache.setCacheObject(key, language);
+            }
+        }
+        catch (Exception e)
+        {
+            log.error("�����û�����ʧ�ܣ��û�ID: {}, ����: {}", userId, language, e);
+        }
+    }
+
+    /**
+     * ��ȡĬ�����Ի���
+     *
+     * @return Ĭ��Locale����Խ���
+     */
+    public static Locale getDefaultLocale()
+    {
+        return DEFAULT_LOCALE;
+    }
+
+    /**
+     * 获取用户语言编码
+     * 从Redis中获取用户的语言设置,并返回对应的语言编码
+     * 语言编码映射:
+     * - zh-CN -> 2 (简体中文)
+     * - zh-TW -> 3 (繁体中文)
+     * - en-US -> 1 (英文)
+     * - 其他或默认 -> 0 (越南语)
+     *
+     * @param userId 用户ID字符串
+     * @return 语言编码:0-越南语(默认), 1-英文, 2-简体中文, 3-繁体中文
+     */
+    public static String getUserLanguageCode(String userId)
+    {
+        String result = "0"; // 默认语言(越南语)
+        if (StringUtils.isEmpty(userId))
+        {
+            return result;
+        }
+
+        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
+        try
+        {
+
+            String lang = redisCache.getCacheObject(userId);
+            log.debug("从redis获取的语言:{}, 用户ID: {}", lang, userId);
+            if ("zh-CN".equals(lang))
+            {
+                result = "2";
+            }
+            else if ("zh-TW".equals(lang))
+            {
+                result = "3";
+            }
+            else if ("en-US".equals(lang))
+            {
+                result = "1";
+            }
+        }
+        catch (Exception e)
+        {
+            // Redis连接失败或其他异常时,使用默认语言
+            log.debug("获取用户语言设置失败,使用默认语言,用户ID: {}", userId, e);
+        }
+
+        log.debug("获取的语言编码:{}, 用户ID: {}", result, userId);
+        return result;
+    }
+}