Преглед изворни кода

统一cid消息回调接口回显格式以及异常处理

cgp пре 1 месец
родитељ
комит
93d05c9240

+ 5 - 3
fs-service/src/main/java/com/fs/wxcid/service/MessageCallbackService.java

@@ -1,8 +1,10 @@
 package com.fs.wxcid.service;
 
+import com.fs.wxcid.dto.callback.CallbackConfigResponse;
 import com.fs.wxcid.dto.callback.ReturnMessage;
 import com.fs.wxcid.dto.common.ApiResponse;
 import com.fs.wxcid.dto.callback.CallbackConfigRequest;
+import com.fs.wxcid.dto.common.ApiResponseCommon;
 
 import java.util.Map;
 
@@ -24,7 +26,7 @@ public interface MessageCallbackService {
      * @param config 回调配置(URL + 启用状态)
      * @return 统一响应结果
      */
-    ApiResponse setCallback(String authKey, CallbackConfigRequest config);
+    ApiResponseCommon<Void> setCallback(String authKey, CallbackConfigRequest config);
 
     /**
      * 获取当前账号的消息回调配置
@@ -33,7 +35,7 @@ public interface MessageCallbackService {
      * @param authKey 账号唯一标识
      * @return 包含 CallbackURL 和 Enabled 状态的响应
      */
-    ApiResponse getCallback(String authKey);
+    ApiResponseCommon<CallbackConfigResponse> getCallback(String authKey);
 
     /**
      * 删除(清空)消息回调配置
@@ -43,5 +45,5 @@ public interface MessageCallbackService {
      * @param authKey 账号唯一标识
      * @return 操作结果
      */
-    ApiResponse deleteCallback(String authKey);
+    ApiResponseCommon<Void> deleteCallback(String authKey);
 }

+ 48 - 14
fs-service/src/main/java/com/fs/wxcid/service/impl/MessageCallbackServiceImpl.java

@@ -1,9 +1,12 @@
 package com.fs.wxcid.service.impl;
 
 
+import com.fs.common.exception.CustomException;
+import com.fs.wxcid.dto.callback.CallbackConfigResponse;
 import com.fs.wxcid.dto.callback.ReturnMessage;
 import com.fs.wxcid.dto.common.ApiResponse;
 import com.fs.wxcid.dto.callback.CallbackConfigRequest;
+import com.fs.wxcid.dto.common.ApiResponseCommon;
 import com.fs.wxcid.service.MessageCallbackService;
 import com.fs.wxwork.utils.WxWorkHttpUtil;
 import com.alibaba.fastjson.TypeReference;
@@ -20,8 +23,8 @@ public class MessageCallbackServiceImpl implements MessageCallbackService {
 
     @Override
     public ReturnMessage returnMessage(Map<String, Object> callback) {
-        // 安全地获取 authKey
-        String authKey = (String) callback.get("authKey");
+        // 安全地获取 key
+        String key = (String) callback.get("key");
 
         // 获取 message 子对象(也是一个 Map)
         Map<String, Object> message = (Map<String, Object>) callback.get("message");
@@ -45,22 +48,53 @@ public class MessageCallbackServiceImpl implements MessageCallbackService {
     /**
      * 设置消息回调
      */
-    public ApiResponse setCallback(String authKey, CallbackConfigRequest config) {
-        return post("/message/SetCallback", authKey, config);
+    public ApiResponseCommon<Void> setCallback(String key, CallbackConfigRequest config) {
+        String url = BASE_URL + "/message/SetCallback?key=" + key;
+        ApiResponseCommon<Void> response = WxWorkHttpUtil.postWithType(
+                url,
+                config,
+                new TypeReference<ApiResponseCommon<Void>>() {}
+        );
+
+        if (response.getCode() != 200) {
+            throw new CustomException("设置回调失败: " + (response.getText() != null ? response.getText() : "未知错误"));
+        }
+
+        return response;
     }
 
     /**
      * 获取消息回调配置
      */
-    public ApiResponse getCallback(String authKey) {
-        return get("/message/GetCallback", authKey);
+    public ApiResponseCommon<CallbackConfigResponse> getCallback(String key) {
+        String url = BASE_URL + "/message/GetCallback?key=" + key;
+        ApiResponseCommon<CallbackConfigResponse> response = WxWorkHttpUtil.getWithType(
+                url,
+                new TypeReference<ApiResponseCommon<CallbackConfigResponse>>() {}
+        );
+
+        if (response.getCode() != 200 || response.getData() == null) {
+            throw new CustomException("获取回调配置失败: " + (response.getText() != null ? response.getText() : "未知错误"));
+        }
+
+        return response;
     }
 
     /**
      * 删除消息回调配置
      */
-    public ApiResponse deleteCallback(String authKey) {
-        return get("/message/DeleteCallback", authKey);
+    public ApiResponseCommon<Void> deleteCallback(String key) {
+        String url = BASE_URL + "/message/DeleteCallback?key=" + key;
+        ApiResponseCommon<Void> response = WxWorkHttpUtil.getWithType(
+                url,
+                new TypeReference<ApiResponseCommon<Void>>() {}
+        );
+
+        if (response.getCode() != 200) {
+            throw new CustomException("删除回调配置失败: " + (response.getText() != null ? response.getText() : "未知错误"));
+        }
+
+        return response;
     }
 
 
@@ -69,12 +103,12 @@ public class MessageCallbackServiceImpl implements MessageCallbackService {
      * 通用 POST 请求方法
      *
      * @param path   接口路径,如 "/friend/AgreeAdd"
-     * @param authKey    账号唯一标识(query 参数)
+     * @param key    账号唯一标识(query 参数)
      * @param request 请求体对象
      * @return 统一响应结果
      */
-    private ApiResponse post(String path, String authKey, Object request) {
-        String url = BASE_URL + path + "?key=" + authKey;
+    private ApiResponse post(String path, String key, Object request) {
+        String url = BASE_URL + path + "?key=" + key;
         return WxWorkHttpUtil.postWithType(url, request, new TypeReference<ApiResponse>() {});
     }
 
@@ -82,13 +116,13 @@ public class MessageCallbackServiceImpl implements MessageCallbackService {
      * 通用 GET 请求方法(无请求体)
      *
      * @param path 接口路径
-     * @param authKey  账号唯一标识
+     * @param key  账号唯一标识
      * @return 统一响应结果
      */
-    private ApiResponse get(String path, String authKey) {
+    private ApiResponse get(String path, String key) {
         String url = BASE_URL + path;
         Map<String, Object> params = new HashMap<>();
-        params.put("authKey", authKey);
+        params.put("key", key);
         String resp = WxWorkHttpUtil.get(url, params);
         return com.alibaba.fastjson.JSON.parseObject(resp, ApiResponse.class);
     }