yys 2 недель назад
Родитель
Сommit
bdef5fe586

+ 38 - 18
fs-user-app/src/main/java/com/fs/app/controller/WxH5MpController.java

@@ -22,6 +22,7 @@ import com.fs.his.domain.FsUserWx;
 import com.fs.his.enums.FsUserOperationEnum;
 import com.fs.his.service.IFsUserService;
 import com.fs.his.service.IFsUserWxService;
+import com.fs.qw.domain.QwExternalContact;
 import com.fs.qw.mapper.QwExternalContactMapper;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
@@ -180,8 +181,13 @@ public class WxH5MpController {
     private FsUser processUserInfo(WxOAuth2UserInfo wxMpUser, Company company,CompanyUser companyUser,FsUserLoginByMpParam param) {
         FsUser user = userService.selectFsUserByUnionId(wxMpUser.getUnionId());
 
+        // unionId 未命中时,通过企微外部联系人关联查询 fsUser
+        if (user == null) {
+            user = findFsUserByQwExternalId(param.getQwExternalId());
+        }
+
         if (user != null) {
-            // 更新现有用户信息
+            // 更新现有用户信息(含通过 qwExternal 关联到的老用户,回填 unionId)
             FsUser userUpdate = new FsUser();
             userUpdate.setUserId(user.getUserId());
             userUpdate.setMpOpenId(wxMpUser.getOpenid());
@@ -191,28 +197,42 @@ public class WxH5MpController {
             userUpdate.setAvatar(wxMpUser.getHeadImgUrl());
             // 老用户 - 检查并添加 appId(不重复添加)
             String updatedAppId = addAppIdIfNotExists(user.getAppId(), param.getAppId());
-            if (!updatedAppId.equals(user.getAppId())) {
+            if (!Objects.equals(updatedAppId, user.getAppId())) {
                 userUpdate.setAppId(updatedAppId);
             }
             userService.updateFsUser(userUpdate);
             return userUpdate;
-        } else {
-            // 创建新用户
-            FsUser newUser = new FsUser();
-            newUser.setNickName(wxMpUser.getNickname());
-            newUser.setAvatar(wxMpUser.getHeadImgUrl());
-            newUser.setStatus(1);
-            newUser.setMpOpenId(wxMpUser.getOpenid());
-//            newUser.setCompanyId(company.getCompanyId());
-//            newUser.setCompanyUserId(companyUser.getUserId());
-            newUser.setUnionId(wxMpUser.getUnionId());
-            newUser.setCreateTime(new Date());
-            newUser.setStatus(company != null && company.getFsUserIsDefaultBlack() == 1 ? 0 : 1);
-            // 新用户 - 添加 appId
-            newUser.setAppId(param.getAppId());
-            userService.insertFsUser(newUser);
+        }
 
-            return newUser;
+        // 创建新用户
+        FsUser newUser = new FsUser();
+        newUser.setNickName(wxMpUser.getNickname());
+        newUser.setAvatar(wxMpUser.getHeadImgUrl());
+        newUser.setMpOpenId(wxMpUser.getOpenid());
+        newUser.setUnionId(wxMpUser.getUnionId());
+        newUser.setCreateTime(new Date());
+        newUser.setStatus(company != null && Objects.equals(company.getFsUserIsDefaultBlack(), 1) ? 0 : 1);
+        newUser.setAppId(param.getAppId());
+        userService.insertFsUser(newUser);
+        return newUser;
+    }
+
+    /**
+     * 通过企微外部联系人 id 反查 fsUser;查询异常时不影响登录主流程
+     */
+    private FsUser findFsUserByQwExternalId(Long qwExternalId) {
+        if (qwExternalId == null || qwExternalId <= 0) {
+            return null;
+        }
+        try {
+            QwExternalContact qwExternalContact = qwExternalContactMapper.selectQwExternalContactById(qwExternalId);
+            if (qwExternalContact == null || qwExternalContact.getFsUserId() == null || qwExternalContact.getFsUserId() <= 0) {
+                return null;
+            }
+            return userService.selectFsUserById(qwExternalContact.getFsUserId());
+        } catch (Exception e) {
+            log.error("通过qwExternalId关联查询fsUser失败, qwExternalId={}", qwExternalId, e);
+            return null;
         }
     }
 

+ 5 - 0
fs-user-app/src/main/java/com/fs/app/param/FsUserLoginByMpParam.java

@@ -13,6 +13,11 @@ public class FsUserLoginByMpParam implements Serializable {
     private String code;
     private Long videoId;
 
+    /**
+     * 外部联系人id
+     */
+    private Long qwExternalId;
+
     @NotNull(message = "公司id不能为空")
     @ApiModelProperty(value = "公司id")
     private Long companyId;