Browse Source

add callback api

wangyunpeng 3 days ago
parent
commit
b53d3f426f

+ 43 - 0
api-module/src/main/java/com/tzld/piaoquan/api/common/enums/wecom/WeComThirdPartyCallBackTypeEnum.java

@@ -0,0 +1,43 @@
+package com.tzld.piaoquan.api.common.enums.wecom;
+
+import lombok.Getter;
+
+import java.util.Objects;
+
+@Getter
+public enum WeComThirdPartyCallBackTypeEnum {
+
+    // 登录回调
+    SCAN_CODE(100001, "扫码返回数据"),
+    CONFIRM_SCAN_CODE(100002, "确认扫码返回数据"),
+    CANCEL_SCAN_CODE(100003, "取消扫码返回"),
+    NEED_VERIFY_CODE(100004, "需要输入验证码消息回调"),
+    LOGIN_SUCCESS(104001, "登录成功返回数据"),
+
+    // 断开回调
+    SECOND_VERIFY_NOTIFY(100012, "二次验证消息通知"),
+    LOGIN_END(100005, "手机端结束登录"),
+    OTHER_DEVICE_LOGIN(100008, "当前账号在其他设备登录"),
+    QR_CODE_SCAN_TIMEOUT(100009, "初始化接口二维码扫码超时断开"),
+    EXCEPTION_DISCONNECT(100007, "异常断开"),
+
+    other(999, "其他");
+
+    private final Integer val;
+    private final String description;
+
+    WeComThirdPartyCallBackTypeEnum(Integer val, String description) {
+        this.val = val;
+        this.description = description;
+    }
+
+    public static WeComThirdPartyCallBackTypeEnum from(Integer val) {
+        for (WeComThirdPartyCallBackTypeEnum typeEnum : WeComThirdPartyCallBackTypeEnum.values()) {
+            if (Objects.equals(typeEnum.getVal(), val)) {
+                return typeEnum;
+            }
+        }
+
+        return other;
+    }
+}

+ 52 - 8
api-module/src/main/java/com/tzld/piaoquan/api/service/wecom/thirdparty/impl/WeComThirdPartyCallBackServiceImpl.java

@@ -1,17 +1,22 @@
 package com.tzld.piaoquan.api.service.wecom.thirdparty.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.tzld.piaoquan.api.common.enums.wecom.WeComThirdPartyCallBackTypeEnum;
 import com.tzld.piaoquan.api.component.WeComThirdPartyApiClient;
-import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComCorpMapper;
-import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComRoomMapper;
-import com.tzld.piaoquan.api.dao.mapper.wecom.thirdpart.ThirdPartWeComStaffMapper;
+import com.tzld.piaoquan.api.model.param.wecom.thirdpart.UuidRequest;
 import com.tzld.piaoquan.api.model.param.wecom.thirdpart.WeComThirdPartyCallBackParam;
+import com.tzld.piaoquan.api.model.po.wecom.thirdpart.ThirdPartWeComStaff;
 import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyCallBackService;
+import com.tzld.piaoquan.api.service.wecom.thirdparty.WeComThirdPartyService;
+import com.tzld.piaoquan.growth.common.utils.LarkRobotUtil;
 import com.tzld.piaoquan.growth.common.utils.RedisUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
+import java.util.ArrayList;
+import java.util.List;
+
 @Slf4j
 @Service
 public class WeComThirdPartyCallBackServiceImpl implements WeComThirdPartyCallBackService {
@@ -19,11 +24,7 @@ public class WeComThirdPartyCallBackServiceImpl implements WeComThirdPartyCallBa
     @Autowired
     private WeComThirdPartyApiClient apiClient;
     @Autowired
-    private ThirdPartWeComStaffMapper thirdPartWeComStaffMapper;
-    @Autowired
-    private ThirdPartWeComRoomMapper thirdPartWeComRoomMapper;
-    @Autowired
-    private ThirdPartWeComCorpMapper thirdPartWeComCorpMapper;
+    private WeComThirdPartyService weComThirdPartyService;
 
     @Autowired
     private RedisUtils redisUtils;
@@ -31,5 +32,48 @@ public class WeComThirdPartyCallBackServiceImpl implements WeComThirdPartyCallBa
     @Override
     public void handleCallback(WeComThirdPartyCallBackParam param) {
         log.info("handleCallback param: {}", JSONObject.toJSONString(param));
+        WeComThirdPartyCallBackTypeEnum typeEnum = WeComThirdPartyCallBackTypeEnum.from(param.getType());
+        if (typeEnum == WeComThirdPartyCallBackTypeEnum.other) {
+            log.info("三方平台企微回调类型未处理:type={}, json={}", param.getType(), param.getJson());
+            return;
+        }
+        handle(param, typeEnum);
+    }
+
+    private void handle(WeComThirdPartyCallBackParam param, WeComThirdPartyCallBackTypeEnum typeEnum) {
+        if (typeEnum == WeComThirdPartyCallBackTypeEnum.LOGIN_SUCCESS) {
+            weComThirdPartyService.getRunClientByUuid(new UuidRequest(param.getUuid()));
+            return;
+        }
+        if (typeEnum == WeComThirdPartyCallBackTypeEnum.NEED_VERIFY_CODE) {
+            String needVerifyCode = "needVerifyCode:uuid:" + param.getUuid();
+            redisUtils.set(needVerifyCode, "1", 120);
+            return;
+        }
+        JSONObject json = JSONObject.parseObject(param.getJson());
+        List<Integer> disconnectCallBackTypes = getDisconnectCallBackTypes();
+        if (disconnectCallBackTypes.contains(typeEnum.getVal())) {
+            ThirdPartWeComStaff staff = weComThirdPartyService.getStaffByUuid(param.getUuid());
+            if (staff == null) {
+                return;
+            }
+            LarkRobotUtil.sendWeComThirdPartMessage(
+                    "【账号掉线回调通知】\n" +
+                            "账号名称:" + staff.getName() + "\n" +
+                            "账号VID:" + staff.getThirdStaffId() + "\n" +
+                            "账号UUID:" + staff.getThirdUuid() + "\n" +
+                            "回调信息:" + json.toJSONString());
+            return;
+        }
+    }
+
+    private List<Integer> getDisconnectCallBackTypes() {
+        List<Integer> result = new ArrayList<>();
+        result.add(WeComThirdPartyCallBackTypeEnum.SECOND_VERIFY_NOTIFY.getVal());
+        result.add(WeComThirdPartyCallBackTypeEnum.LOGIN_END.getVal());
+        result.add(WeComThirdPartyCallBackTypeEnum.OTHER_DEVICE_LOGIN.getVal());
+        result.add(WeComThirdPartyCallBackTypeEnum.QR_CODE_SCAN_TIMEOUT.getVal());
+        result.add(WeComThirdPartyCallBackTypeEnum.EXCEPTION_DISCONNECT.getVal());
+        return result;
     }
 }