瀏覽代碼

购物车修改

sunshibo 2 月之前
父節點
當前提交
2ec15a6b9b
共有 1 個文件被更改,包括 15 次插入1 次删除
  1. 15 1
      pages/orderFood/index.vue

+ 15 - 1
pages/orderFood/index.vue

@@ -498,7 +498,21 @@ onLoad(async (options) => {
       sseRequestTask = createSSEConnection(sseConfig.url, sseConfig);
       sseRequestTask.onMessage((msg) => {
         console.log('SSE 收到:', msg);
-        // 可在此根据 msg.data / msg.event 更新页面
+        if (msg.event === 'cart_update' && msg.data) {
+          try {
+            const payload = typeof msg.data === 'string' ? JSON.parse(msg.data) : msg.data;
+            const items = payload?.items ?? [];
+            cartData.value = {
+              items: Array.isArray(items) ? items : [],
+              totalAmount: Number(payload?.totalAmount) || 0,
+              totalQuantity: Number(payload?.totalQuantity) || 0
+            };
+            pendingCartData = null;
+            mergeCartIntoFoodList();
+          } catch (e) {
+            console.error('SSE 购物车数据解析失败:', e);
+          }
+        }
       });
       sseRequestTask.onOpen(async () => {
         console.log('SSE 连接已建立');