Pārlūkot izejas kodu

智能柜项目提交

skyline 2 mēneši atpakaļ
vecāks
revīzija
2758e0799a

+ 48 - 0
haha-common/src/main/java/com/haha/common/enums/DeviceDoorStatus.java

@@ -0,0 +1,48 @@
+package com.haha.common.enums;
+
+public enum DeviceDoorStatus {
+
+    ERROR("1", "error", "开门失败"),
+    OPENED("2", "open", "门已开"),
+    CLOSED("3", "close", "门已关"),
+    BUSY("4", "busy", "设备繁忙");
+
+    private final String code;
+    private final String status;
+    private final String description;
+
+    DeviceDoorStatus(String code, String status, String description) {
+        this.code = code;
+        this.status = status;
+        this.description = description;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getStatus() {
+        return status;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static DeviceDoorStatus fromCode(String code) {
+        if (code == null) {
+            return null;
+        }
+        for (DeviceDoorStatus doorStatus : values()) {
+            if (doorStatus.code.equals(code)) {
+                return doorStatus;
+            }
+        }
+        return null;
+    }
+
+    public static String convertToStatus(String code) {
+        DeviceDoorStatus doorStatus = fromCode(code);
+        return doorStatus != null ? doorStatus.getStatus() : "unknown";
+    }
+}

+ 39 - 0
haha-common/src/main/java/com/haha/common/enums/DeviceOnlineStatus.java

@@ -0,0 +1,39 @@
+package com.haha.common.enums;
+
+public enum DeviceOnlineStatus {
+
+    OFFLINE(0, "离线"),
+    ONLINE(1, "在线");
+
+    private final int code;
+    private final String description;
+
+    DeviceOnlineStatus(int code, String description) {
+        this.code = code;
+        this.description = description;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static DeviceOnlineStatus fromCode(Integer code) {
+        if (code == null) {
+            return null;
+        }
+        for (DeviceOnlineStatus status : values()) {
+            if (status.code == code) {
+                return status;
+            }
+        }
+        return null;
+    }
+
+    public static boolean isOnline(Integer code) {
+        return code != null && code == ONLINE.code;
+    }
+}

+ 43 - 0
haha-common/src/main/java/com/haha/common/enums/NotifyType.java

@@ -0,0 +1,43 @@
+package com.haha.common.enums;
+
+public enum NotifyType {
+
+    DEVICE_STATUS("DEVICE_STATUS", "开关门状态通知"),
+    ONLINE_STATUS("ONLINE_STATUS", "设备在线状态通知"),
+    VOICE_RESULT("VOICE_RESULT", "音量调节结果通知"),
+    CLIENT_NEW_PRODUCT("CLIENT_NEW_PRODUCT", "新品审核结果回调"),
+    MERGE_PRODUCT("MERGE_PRODUCT", "商品合并结果通知"),
+    ORC_RESULT("ORC_RESULT", "AI识别结果通知");
+
+    private final String code;
+    private final String description;
+
+    NotifyType(String code, String description) {
+        this.code = code;
+        this.description = description;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static NotifyType fromCode(String code) {
+        if (code == null) {
+            return null;
+        }
+        for (NotifyType type : values()) {
+            if (type.code.equals(code)) {
+                return type;
+            }
+        }
+        return null;
+    }
+
+    public static boolean isValid(String code) {
+        return fromCode(code) != null;
+    }
+}

+ 40 - 0
haha-common/src/main/java/com/haha/common/enums/ProductAuditStatus.java

@@ -0,0 +1,40 @@
+package com.haha.common.enums;
+
+public enum ProductAuditStatus {
+
+    APPROVED(1, "已通过"),
+    REJECTED(2, "已拒绝"),
+    LISTED(3, "已上架");
+
+    private final int code;
+    private final String description;
+
+    ProductAuditStatus(int code, String description) {
+        this.code = code;
+        this.description = description;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static ProductAuditStatus fromCode(Integer code) {
+        if (code == null) {
+            return null;
+        }
+        for (ProductAuditStatus status : values()) {
+            if (status.code == code) {
+                return status;
+            }
+        }
+        return null;
+    }
+
+    public static boolean isRejected(Integer code) {
+        return code != null && code == REJECTED.code;
+    }
+}

+ 35 - 0
haha-common/src/main/java/com/haha/common/enums/RecognizeActionType.java

@@ -0,0 +1,35 @@
+package com.haha.common.enums;
+
+public enum RecognizeActionType {
+
+    IN("IN", "放入"),
+    OUT("OUT", "取出");
+
+    private final String code;
+    private final String description;
+
+    RecognizeActionType(String code, String description) {
+        this.code = code;
+        this.description = description;
+    }
+
+    public String getCode() {
+        return code;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static RecognizeActionType fromCode(String code) {
+        if (code == null) {
+            return null;
+        }
+        for (RecognizeActionType type : values()) {
+            if (type.code.equals(code)) {
+                return type;
+            }
+        }
+        return null;
+    }
+}

+ 39 - 0
haha-common/src/main/java/com/haha/common/enums/RecognizeConsumeType.java

@@ -0,0 +1,39 @@
+package com.haha.common.enums;
+
+public enum RecognizeConsumeType {
+
+    NO_CONSUME(1, "无消费"),
+    CONSUMED(0, "有消费");
+
+    private final int code;
+    private final String description;
+
+    RecognizeConsumeType(int code, String description) {
+        this.code = code;
+        this.description = description;
+    }
+
+    public int getCode() {
+        return code;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public static RecognizeConsumeType fromCode(Integer code) {
+        if (code == null) {
+            return null;
+        }
+        for (RecognizeConsumeType type : values()) {
+            if (type.code == code) {
+                return type;
+            }
+        }
+        return null;
+    }
+
+    public static boolean isNoConsume(Integer code) {
+        return code != null && code == NO_CONSUME.code;
+    }
+}

+ 59 - 66
haha-miniapp/src/main/java/com/haha/miniapp/controller/CallbackController.java

@@ -5,6 +5,12 @@ import com.alibaba.fastjson2.JSON;
 import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.haha.common.constant.OrderConstants;
+import com.haha.common.enums.DeviceDoorStatus;
+import com.haha.common.enums.DeviceOnlineStatus;
+import com.haha.common.enums.NotifyType;
+import com.haha.common.enums.ProductAuditStatus;
+import com.haha.common.enums.RecognizeActionType;
+import com.haha.common.enums.RecognizeConsumeType;
 import com.haha.entity.Order;
 import com.haha.service.OrderService;
 import lombok.extern.slf4j.Slf4j;
@@ -76,9 +82,7 @@ public class CallbackController {
      * @return 响应结果,必须返回 "success" 字符串
      */
     @PostMapping("/message")
-    public String handleMessageCallback(
-            HttpServletRequest request,
-            @RequestParam(required = false) Map<String, String> requestParams) {
+    public String handleMessageCallback(HttpServletRequest request, @RequestParam(required = false) Map<String, String> requestParams) {
         try {
             // 解析请求参数(兼容JSON和表单两种格式)
             Map<String, Object> params = parseRequestParams(request, requestParams);
@@ -97,28 +101,33 @@ public class CallbackController {
                 return "success";
             }
 
-            // 3. 根据通知类型分发处理
-            switch (notifyType) {
-                case "DEVICE_STATUS":
+            NotifyType type = NotifyType.fromCode(notifyType);
+            if (type == null) {
+                log.warn("未知的消息通知类型: {}", notifyType);
+                return "success";
+            }
+
+            switch (type) {
+                case DEVICE_STATUS:
                     handleDeviceStatus(params);
                     break;
-                case "ONLINE_STATUS":
+                case ONLINE_STATUS:
                     handleOnlineStatus(params);
                     break;
-                case "VOICE_RESULT":
+                case VOICE_RESULT:
                     handleVoiceResult(params);
                     break;
-                case "CLIENT_NEW_PRODUCT":
+                case CLIENT_NEW_PRODUCT:
                     handleNewProductAudit(params);
                     break;
-                case "MERGE_PRODUCT":
+                case MERGE_PRODUCT:
                     handleMergeProduct(params);
                     break;
-                case "ORC_RESULT":
+                case ORC_RESULT:
                     handleOrcResult(params);
                     break;
                 default:
-                    log.warn("未知的消息通知类型: {}", notifyType);
+                    log.warn("未处理的消息通知类型: {}", type);
             }
 
             // 4. 返回成功响应(必须返回 "success" 字符串)
@@ -161,26 +170,28 @@ public class CallbackController {
             statusData.put("status", status);
             statusData.put("openType", openType != null ? openType : "");
             statusData.put("userId", outUserId != null ? outUserId : "");
-            statusData.put("doorStatus", convertDoorStatus(status));
+            statusData.put("doorStatus", DeviceDoorStatus.convertToStatus(status));
             statusData.put("timestamp", String.valueOf(System.currentTimeMillis()));
-            
+
             redisTemplate.opsForHash().putAll(statusKey, statusData);
             redisTemplate.expire(statusKey, 30, TimeUnit.MINUTES);
 
-            // 可以根据业务需要处理不同状态
-            switch (status) {
-                case "2": // OPENED
-                    log.info("设备 {} 开门成功", deviceId);
-                    break;
-                case "3": // CLOSED
-                    log.info("设备 {} 关门成功,等待AI识别", deviceId);
-                    break;
-                case "1": // ERROR
-                    log.error("设备 {} 开门失败", deviceId);
-                    break;
-                case "4": // ANOTHER
-                    log.warn("设备 {} 繁忙", deviceId);
-                    break;
+            DeviceDoorStatus doorStatus = DeviceDoorStatus.fromCode(status);
+            if (doorStatus != null) {
+                switch (doorStatus) {
+                    case OPENED:
+                        log.info("设备 {} 开门成功", deviceId);
+                        break;
+                    case CLOSED:
+                        log.info("设备 {} 关门成功,等待AI识别", deviceId);
+                        break;
+                    case ERROR:
+                        log.error("设备 {} 开门失败", deviceId);
+                        break;
+                    case BUSY:
+                        log.warn("设备 {} 繁忙", deviceId);
+                        break;
+                }
             }
 
         } catch (Exception e) {
@@ -188,31 +199,13 @@ public class CallbackController {
         }
     }
 
-    private String convertDoorStatus(String status) {
-        if (status == null) return "unknown";
-        switch (status) {
-            case "2": return "open";
-            case "3": return "close";
-            case "1": return "error";
-            case "4": return "busy";
-            default: return "unknown";
-        }
-    }
-
-    /**
-     * 处理设备在线状态通知 (ONLINE_STATUS)
-     *
-     * @param params 通知参数
-     */
     private void handleOnlineStatus(Map<String, Object> params) {
         try {
             String deviceId = (String) params.get("device_id");
             Integer isOnline = parseInteger(params.get("is_online"));
 
             log.info("设备在线状态通知 - 设备: {}, 在线状态: {}",
-                deviceId, isOnline != null && isOnline == 1 ? "在线" : "离线");
-
-            // 可以在这里更新设备在线状态到数据库
+                deviceId, DeviceOnlineStatus.isOnline(isOnline) ? "在线" : "离线");
 
         } catch (Exception e) {
             log.error("处理设备在线状态通知失败", e);
@@ -256,7 +249,7 @@ public class CallbackController {
 
             log.info("新品审核结果 - ID: {}, 商品名: {}, 状态: {}, code: {}", id, name, status, code);
 
-            if (status != null && status == 2) {
+            if (ProductAuditStatus.isRejected(status)) {
                 log.warn("新品 {} 被拒绝,原因: {}", name, rejectReason);
             }
 
@@ -274,13 +267,11 @@ public class CallbackController {
         try {
             Object listObj = params.get("list");
 
-            if (listObj instanceof JSONArray) {
-                JSONArray list = (JSONArray) listObj;
+            if (listObj instanceof JSONArray list) {
                 log.info("商品合并结果通知 - 合并数量: {}", list.size());
 
                 for (Object item : list) {
-                    if (item instanceof JSONObject) {
-                        JSONObject merge = (JSONObject) item;
+                    if (item instanceof JSONObject merge) {
                         String mainCode = merge.getString("main_code");
                         String productCode = merge.getString("product_code");
                         log.info("商品合并 - 主商品: {}, 合并商品: {}", mainCode, productCode);
@@ -315,7 +306,7 @@ public class CallbackController {
             String resourceInfoStr = (String) params.get("resource_info");
 
             log.info("AI识别结果通知 - 设备: {}, 活动: {}, 是否消费: {}",
-                deviceId, activityId, nobuy != null && nobuy == 1 ? "无消费" : "有消费");
+                deviceId, activityId, RecognizeConsumeType.isNoConsume(nobuy) ? "无消费" : "有消费");
 
             // 保存识别结果到Redis,供小程序轮询查询
             String resultKey = RECOGNIZE_RESULT_KEY + activityId;
@@ -328,25 +319,26 @@ public class CallbackController {
             recognizeData.put("skuList", skuListStr != null ? skuListStr : "");
             recognizeData.put("resourceInfo", resourceInfoStr != null ? resourceInfoStr : "");
             recognizeData.put("timestamp", String.valueOf(System.currentTimeMillis()));
-            
+
             redisTemplate.opsForHash().putAll(resultKey, recognizeData);
             redisTemplate.expire(resultKey, 30, TimeUnit.MINUTES);
 
-            if (nobuy != null && nobuy == 1) {
+            if (RecognizeConsumeType.isNoConsume(nobuy)) {
                 log.info("用户打开柜门但未消费,无需处理");
                 return;
             }
 
-            // 解析result字段
             if (resultStr != null && !resultStr.isEmpty()) {
                 JSONObject result = JSON.parseObject(resultStr);
-                String type = result.getString("type"); // IN或OUT
+                String type = result.getString("type");
                 JSONArray data = result.getJSONArray("data");
                 JSONArray excepts = result.getJSONArray("excepts");
 
-                log.info("识别类型: {}, 数据: {}", type, data != null ? data.size() : 0);
+                RecognizeActionType actionType = RecognizeActionType.fromCode(type);
+                log.info("识别类型: {}, 数据: {}",
+                    actionType != null ? actionType.getDescription() : type,
+                    data != null ? data.size() : 0);
 
-                // 检查是否有异常
                 if (excepts != null && !excepts.isEmpty()) {
                     log.warn("识别结果包含异常: {}", excepts.toJSONString());
                 }
@@ -359,16 +351,17 @@ public class CallbackController {
             }
 
             // 解析resource_info获取视频URL
-//            if (resourceInfoStr != null && !resourceInfoStr.isEmpty()) {
-//                JSONObject resourceInfo = JSON.parseObject(resourceInfoStr);
-//                String videoUrl = resourceInfo.getString("video_url");
-//                log.info("视频URL: {}", videoUrl);
-//            }
+            if (resourceInfoStr != null && !resourceInfoStr.isEmpty()) {
+                JSONObject resourceInfo = JSON.parseObject(resourceInfoStr);
+                String videoUrl = resourceInfo.getString("video_url");
+                log.info("视频URL: {}", videoUrl);
+            }
 
             // TODO: 根据识别结果进行后续处理
             // 1. 计算订单金额
             // 2. 生成订单记录
-            // 3. 触发支付流程
+            // 3. 优惠信息处理
+            // 4. 触发支付流程
 
         } catch (Exception e) {
             log.error("处理AI识别结果通知失败", e);
@@ -460,7 +453,7 @@ public class CallbackController {
             orderData.put("totalAmount", orderMoney != null ? orderMoney.toString() : "0");
             orderData.put("products", orderDetail != null ? orderDetail : "");
             orderData.put("timestamp", String.valueOf(System.currentTimeMillis()));
-            
+
             redisTemplate.opsForHash().putAll(orderKey, orderData);
             redisTemplate.expire(orderKey, 30, TimeUnit.MINUTES);
 

+ 4 - 4
haha-mp/src/pages/my/my.vue

@@ -10,7 +10,7 @@
         <view class="user-phone">130****1579</view>
       </view>
     </view>
-    
+
     <!-- 菜单列表 -->
     <view class="menu-list">
       <view class="menu-item" @click="goToOrders">
@@ -60,7 +60,7 @@
           <image class="menu-svg-icon" src="/static/icons/service.svg"></image>
         </view>
         <view class="menu-text">联系客服</view>
-        <view class="service-phone">18371997424</view>
+        <view class="service-phone">400-0755-315</view>
       </view>
       <view class="menu-item" @click="goToOfficialAccount">
         <view class="menu-icon">
@@ -70,7 +70,7 @@
         <view class="menu-arrow"></view>
       </view>
     </view>
-    
+
     <!-- 版本信息 -->
     <view class="version-info">
       v1.62.11
@@ -270,4 +270,4 @@ const goToSettings = () => {
   color: #999999;
   background-color: #ffffff;
 }
-</style>
+</style>