|
@@ -0,0 +1,417 @@
|
|
|
+package com.minto.app.organization.workpro.service.impl;
|
|
|
+
|
|
|
+import java.text.MessageFormat;
|
|
|
+import java.util.*;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+import com.minto.app.organization.beans.OrgImBean;
|
|
|
+import com.minto.app.organization.beans.OrgPersonBean;
|
|
|
+import com.minto.app.organization.beans.OrgUnitBean;
|
|
|
+import com.minto.app.organization.manager.DepartmentManager;
|
|
|
+import com.minto.app.organization.manager.IOrgImMapper;
|
|
|
+import com.minto.app.organization.manager.IOrgManager;
|
|
|
+import com.minto.app.organization.workpro.api.IWorkProImApi;
|
|
|
+import com.minto.app.organization.workpro.api.orgs.*;
|
|
|
+import com.minto.app.organization.workpro.api.token.TokenCreate;
|
|
|
+import com.minto.app.organization.workpro.api.users.UsersCreate;
|
|
|
+import com.minto.app.organization.workpro.api.users.UsersDelete;
|
|
|
+import com.minto.app.organization.workpro.api.users.UsersGet;
|
|
|
+import com.minto.app.organization.workpro.api.users.UsersSet;
|
|
|
+import com.minto.app.organization.workpro.config.WorkProConfig;
|
|
|
+import com.minto.app.organization.workpro.service.IWorkProService;
|
|
|
+import com.minto.core.util.StringUtil;
|
|
|
+import com.minto.tip.common.exceptions.BusinessException;
|
|
|
+import com.minto.tip.organization.po.OrgPrincipal;
|
|
|
+import com.minto.tip.organization.service.PrincipalService;
|
|
|
+import org.apache.commons.collections.CollectionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 应用模块名称<p>
|
|
|
+ * 代码描述<p>
|
|
|
+ *
|
|
|
+ * @author wany
|
|
|
+ * @since 2024/7/22
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class WorkProServiceImpl implements IWorkProService{
|
|
|
+
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(WorkProServiceImpl.class);
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IOrgManager orgManager;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private IOrgImMapper orgImMapper;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private PrincipalService principalService;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private DepartmentManager orgDepartmentManager;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <P, R> void callApi(IWorkProImApi<P, R> api) throws BusinessException{
|
|
|
+ callApi(api, null);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public <P, R> void callApi(IWorkProImApi<P, R> api, String uid) throws BusinessException{
|
|
|
+ WorkProConfig workProConfig = WorkProConfig.getInstance();
|
|
|
+ if(!workProConfig.isEnable()){
|
|
|
+ throw new BusinessException("workPro IM 配置错误!");
|
|
|
+ }
|
|
|
+ if(api == null){
|
|
|
+ throw new BusinessException("workPro IM 请求不能为空!");
|
|
|
+ }
|
|
|
+ if(!api.isRequestOk()){
|
|
|
+ throw new BusinessException("workPro IM 请求构建错误!");
|
|
|
+ }
|
|
|
+ try{
|
|
|
+ api.call(workProConfig, uid);
|
|
|
+ } catch(Exception e){
|
|
|
+ log.error("workPro IM 请求失败!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void syncSystem(Long accountId){
|
|
|
+ WorkProConfig workProConfig = WorkProConfig.getInstance();
|
|
|
+ if(!workProConfig.isEnable()){
|
|
|
+ throw new BusinessException("workPro IM 配置错误");
|
|
|
+ }
|
|
|
+ OrgUnitBean org = orgManager.findAccountById(accountId);
|
|
|
+ //1、同步组织
|
|
|
+ OrgImBean orgIm = syncOrg(org, workProConfig);
|
|
|
+ //2、同步组织内人员
|
|
|
+ syncPerson(orgIm.getImId(), org);
|
|
|
+ //3、同步部门和部门成员
|
|
|
+ syncDept(org.getId(), orgIm.getImId(), new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步组织信息到IM系统。
|
|
|
+ * 根据给定的组织单元信息和工作平台配置,同步创建或更新IM系统的组织。
|
|
|
+ * 如果IM系统中不存在对应的组织,则创建新组织;如果已存在,则更新组织信息。
|
|
|
+ *
|
|
|
+ * @param org 组织单元信息,包含待同步的组织ID和名称。
|
|
|
+ * @param config 工作平台配置,包含用于IM系统认证的账号信息和密码。
|
|
|
+ * @return 返回同步后的组织信息bean。
|
|
|
+ * @throws BusinessException 如果IM系统操作失败,则抛出业务异常。
|
|
|
+ */
|
|
|
+ private OrgImBean syncOrg(OrgUnitBean org, WorkProConfig config){
|
|
|
+ //不考虑Im系统有组织,而OrgImBean 没有数据的场景
|
|
|
+ OrgImBean byMintoId = orgImMapper.findByMintoId(String.valueOf(org.getId()));
|
|
|
+
|
|
|
+ if(byMintoId == null){
|
|
|
+ // IM系统中不存在该组织,调用API进行组织创建
|
|
|
+ OrgsCreate orgsCreate = new OrgsCreate(org.getName(), config.getName(), config.getAccount(),
|
|
|
+ config.getPassword());
|
|
|
+ callApi(orgsCreate);
|
|
|
+ // 检查组织创建是否成功
|
|
|
+ if(!orgsCreate.isResponseOk()){
|
|
|
+ throw new BusinessException("workPro IM 创建组织失败!");
|
|
|
+ }
|
|
|
+ // 获取创建后的组织ID,并在本地数据库中创建记录
|
|
|
+ OrgsCreate.Result result = orgsCreate.getResult();
|
|
|
+ String gid = result.getGid();
|
|
|
+ byMintoId = orgImMapper.create(gid, String.valueOf(org.getId()), "org");
|
|
|
+ } else{
|
|
|
+ // IM系统中已存在该组织,调用API进行组织信息更新
|
|
|
+ OrgsSet orgsSet = new OrgsSet(byMintoId.getImId(), org.getName());
|
|
|
+ callApi(orgsSet);
|
|
|
+ // 检查组织信息更新是否成功
|
|
|
+ if(!orgsSet.isResponseOk()){
|
|
|
+ throw new BusinessException("workPro IM 更新组织失败!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 返回同步后的组织信息
|
|
|
+ return byMintoId;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步组织人员信息到IM系统。
|
|
|
+ *
|
|
|
+ * @param imOrgId IM系统的组织ID。
|
|
|
+ * @param org 企业组织信息。
|
|
|
+ */
|
|
|
+ private void syncPerson(String imOrgId, OrgUnitBean org){
|
|
|
+ // 初始化一个OrgsExportUser对象,用于后续调用API获取所有用户信息
|
|
|
+ //im服务器所有人员
|
|
|
+ OrgsExportUser orgsTree = new OrgsExportUser(null, null, null, "-1");
|
|
|
+ // 调用API获取组织的所有用户信息
|
|
|
+ callApi(orgsTree);
|
|
|
+ // 检查API调用是否成功,如果失败则抛出业务异常
|
|
|
+ if(!orgsTree.isResponseOk()){
|
|
|
+ throw new BusinessException("workPro IM 获取所有人员失败!");
|
|
|
+ }
|
|
|
+ // 获取API返回的用户列表结果
|
|
|
+ OrgsExportUser.Result result = orgsTree.getResult();
|
|
|
+ // 将用户ID列表转换为字符串列表
|
|
|
+ List<String> userIds = result.getList().stream().map(e -> e.getGid()).collect(Collectors.toList());
|
|
|
+ // 从组织管理器中获取当前组织的所有人员信息
|
|
|
+ List<OrgPersonBean> persons = orgManager.findAllPersons(org.getId());
|
|
|
+ // 将人员信息映射为ID到人员对象的映射
|
|
|
+ Map<String, OrgPersonBean> personBeanMap = persons.stream()
|
|
|
+ .collect(Collectors.toMap(e -> String.valueOf(e.getId()), e -> e));
|
|
|
+ // 比较IM系统用户ID列表和组织人员ID列表的差异,得到删除、更新和新增的ID集合
|
|
|
+ List<Set<String>> sets = compareLists(userIds, new ArrayList<>(personBeanMap.keySet()));
|
|
|
+ // 遍历需要删除的用户ID集合,调用API删除用户,并在数据库中更新对应记录
|
|
|
+ sets.get(0).forEach(e -> {
|
|
|
+ UsersDelete usersDelete = new UsersDelete(e);
|
|
|
+ callApi(usersDelete);
|
|
|
+ if(usersDelete.isResponseOk()){
|
|
|
+ orgImMapper.deleteByImId(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 遍历需要更新的用户ID集合,调用API更新用户信息,并在数据库中更新对应记录
|
|
|
+ sets.get(2).forEach(e -> {
|
|
|
+ OrgPersonBean person = personBeanMap.get(e);
|
|
|
+ OrgPrincipal principal = principalService.getPrincipalByPersonId(person.getId());
|
|
|
+ UsersSet usersSet = new UsersSet(e, principal.getUsername(), person.getPname(), null, null, null, null,
|
|
|
+ null, null, 1 == person.getSex() ? 1 : 2, null, null, 1, null, null);
|
|
|
+ callApi(usersSet);
|
|
|
+ if(usersSet.isResponseOk()){
|
|
|
+ orgImMapper.updateByMintoId(e, e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 遍历需要新增的用户ID集合,调用API创建用户,并在数据库中新增对应记录
|
|
|
+ sets.get(1).forEach(e -> {
|
|
|
+ OrgPersonBean person = personBeanMap.get(e);
|
|
|
+ OrgPrincipal principal = principalService.getPrincipalByPersonId(person.getId());
|
|
|
+ UsersCreate usersCreate = new UsersCreate(principal.getUsername(), principal.getUsername(), e,
|
|
|
+ person.getPname(), 3, null, null, null, null, 1 == person.getSex() ? 1 : 2, null, null, null, 1,
|
|
|
+ null);
|
|
|
+ callApi(usersCreate);
|
|
|
+ if(usersCreate.isResponseOk()){
|
|
|
+ orgImMapper.create(e, e, "person");
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步部门信息和人员信息。
|
|
|
+ * 根据给定的部门ID,IM组织ID和IM部门ID列表,同步企业系统中的部门和人员信息到IM系统中。
|
|
|
+ * 这包括创建新部门、更新现有部门、删除已删除的部门,以及同步部门内的人员变动。
|
|
|
+ *
|
|
|
+ * @param deptId 企业系统中的部门ID
|
|
|
+ * @param imOrgId IM系统中的组织ID
|
|
|
+ * @param imDeptIds IM系统中的部门ID列表
|
|
|
+ */
|
|
|
+ private void syncDept(Long deptId, String imOrgId, List<String> imDeptIds){
|
|
|
+ // 获取IM部门列表中的最后一个部门ID,用于后续处理
|
|
|
+ String imDeptId = CollectionUtils.isEmpty(imDeptIds) ? "" : imDeptIds.get(imDeptIds.size() - 1);
|
|
|
+ // 根据IM组织ID和部门ID列表查询IM系统中的部门和人员信息
|
|
|
+ OrgsTree.Result orgsTree = findImDept(imOrgId, imDeptIds);
|
|
|
+ // 初始化企业系统中的部门和人员列表
|
|
|
+ List<OrgsTree.Result> depts = new ArrayList<>();
|
|
|
+ List<OrgsTree.Result> users = new ArrayList<>();
|
|
|
+ // 如果查询到IM系统的部门信息,进行进一步处理
|
|
|
+ if(orgsTree != null){
|
|
|
+ // 根据类型(部门或人员)对IM系统的部门和人员进行分组
|
|
|
+ Map<String, List<OrgsTree.Result>> collect = orgsTree.getChildren().stream()
|
|
|
+ .filter(e -> !("d".equals(e.getType()) && "[未分配人员]".equals(e.getName())))
|
|
|
+ .collect(Collectors.groupingBy(OrgsTree.Result::getType));
|
|
|
+
|
|
|
+ // 将部门和人员信息分别存入对应的列表
|
|
|
+ if(CollectionUtils.isNotEmpty(collect.get("d"))){
|
|
|
+ depts.addAll(collect.get("d"));
|
|
|
+ }
|
|
|
+ if(CollectionUtils.isNotEmpty(collect.get("u"))){
|
|
|
+ users.addAll(collect.get("u"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 从企业系统中查询指定父部门ID的所有子部门
|
|
|
+ List<OrgUnitBean> departments = orgDepartmentManager.findOrgDepartmentBeansByParentId(deptId);
|
|
|
+ // 将企业系统的部门ID映射到部门对象
|
|
|
+ Map<String, OrgUnitBean> deptMaps = departments.stream()
|
|
|
+ .collect(Collectors.toMap(e -> String.valueOf(e.getId()), e -> e));
|
|
|
+ // 获取IM系统中的部门ID列表
|
|
|
+ List<String> imDepIds = depts.stream().map(OrgsTree.Result::getId).collect(Collectors.toList());
|
|
|
+ // 比较企业系统和IM系统中的部门差异,包括新增、更新和删除的部门
|
|
|
+ List<Set<String>> deptDiffSets = compareLists(imDepIds, new ArrayList<>(deptMaps.keySet()));
|
|
|
+ // 处理需要删除的部门
|
|
|
+ deptDiffSets.get(0).forEach(e -> {
|
|
|
+ DeptDelete deptDelete = new DeptDelete(imOrgId, e);
|
|
|
+ callApi(deptDelete);
|
|
|
+ if(deptDelete.isResponseOk()){
|
|
|
+ orgImMapper.deleteByImId(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 处理需要更新的部门
|
|
|
+ deptDiffSets.get(2).forEach(e -> {
|
|
|
+ OrgUnitBean dept = deptMaps.get(e);
|
|
|
+ DeptSet deptSet = new DeptSet(imOrgId, e, dept.getDname(), imDeptId, null);
|
|
|
+ callApi(deptSet);
|
|
|
+ if(deptSet.isResponseOk()){
|
|
|
+ orgImMapper.updateByMintoId(e, e);
|
|
|
+ List<String> objects = new ArrayList<>(imDeptIds);
|
|
|
+ objects.add(e);
|
|
|
+ syncDept(dept.getId(), imOrgId, objects);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ // 处理需要新增的部门
|
|
|
+ deptDiffSets.get(1).forEach(e -> {
|
|
|
+ OrgUnitBean dept = deptMaps.get(e);
|
|
|
+ DeptCreate deptCreate = new DeptCreate(imOrgId, dept.getDname(), e, imDeptId, null);
|
|
|
+ callApi(deptCreate);
|
|
|
+ if(deptCreate.isResponseOk()){
|
|
|
+ orgImMapper.create(e, e, "dept");
|
|
|
+ List<String> objects = new ArrayList<>(imDeptIds);
|
|
|
+ objects.add(e);
|
|
|
+ syncDept(dept.getId(), imOrgId, objects);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ // 如果当前处理的不是最顶层部门,则返回,不处理人员同步
|
|
|
+ if(StringUtil.isEmpty(imDeptId)){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 从企业系统中查询指定部门的所有人员
|
|
|
+ List<OrgPersonBean> deptPersons = orgManager.findPersonsByDepartmentId(deptId);
|
|
|
+ // 将企业系统的人员ID映射到人员对象
|
|
|
+ Map<String, OrgPersonBean> deptPersonMap = deptPersons.stream()
|
|
|
+ .collect(Collectors.toMap(e -> String.valueOf(e.getId()), e -> e));
|
|
|
+ // 获取IM系统中的人员ID列表
|
|
|
+ List<String> imDeptUserIds = users.stream().map(OrgsTree.Result::getId).collect(Collectors.toList());
|
|
|
+ // 比较企业系统和IM系统中的人员差异,包括新增、更新和删除的人员
|
|
|
+ List<Set<String>> userDiffSets = compareLists(imDeptUserIds, new ArrayList<>(deptPersonMap.keySet()));
|
|
|
+ // 处理需要删除的人员
|
|
|
+ if(CollectionUtils.isNotEmpty(userDiffSets.get(0))){
|
|
|
+ String collect1 = userDiffSets.get(0).stream().collect(Collectors.joining(","));
|
|
|
+ DeptUserDelete deptUserDelete = new DeptUserDelete(imOrgId, collect1, imDeptId);
|
|
|
+ callApi(deptUserDelete);
|
|
|
+ }
|
|
|
+ // 处理需要新增的人员
|
|
|
+ if(CollectionUtils.isNotEmpty(userDiffSets.get(1))){
|
|
|
+ String collect1 = userDiffSets.get(1).stream().collect(Collectors.joining(","));
|
|
|
+ DeptUserCreate deptUserCreate = new DeptUserCreate(imOrgId, imDeptId, collect1, null, null);
|
|
|
+ callApi(deptUserCreate);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据IM组织ID和IM部门ID列表,查找对应的IM部门在组织树中的位置。
|
|
|
+ *
|
|
|
+ * @param imOrgId IM的组织ID。
|
|
|
+ * @param imDeptIds IM的部门ID列表。
|
|
|
+ * @return 如果找到对应的部门,返回组织树中的部门节点;如果未找到或请求失败,返回null。
|
|
|
+ */
|
|
|
+ private OrgsTree.Result findImDept(String imOrgId, List<String> imDeptIds){
|
|
|
+ // 初始化组织树对象,指定IM组织ID,以及起始层级和页码
|
|
|
+ OrgsTree orgsTree = new OrgsTree(imOrgId, "", 1, 1);
|
|
|
+ // 调用API获取组织树数据
|
|
|
+ callApi(orgsTree);
|
|
|
+ // 检查API响应是否成功,以及返回的组织树数据是否为空
|
|
|
+ if((!orgsTree.isResponseOk()) || (CollectionUtils.isEmpty(orgsTree.getResult()))){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 从组织树结果中找出对应IM组织ID的根节点
|
|
|
+ OrgsTree.Result root = orgsTree.getResult().stream().filter(e -> imOrgId.equals(e.getId())).findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ // 如果IM部门ID列表为空,直接返回根节点
|
|
|
+ if(CollectionUtils.isEmpty(imDeptIds)){
|
|
|
+ return root;
|
|
|
+ }
|
|
|
+ // 如果IM部门ID列表不为空,尝试在根节点下查找对应的子部门,如果找不到则返回null
|
|
|
+ return Optional.ofNullable(root).map(rootResult -> findChildDepartment(rootResult, imDeptIds)).orElse(null);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据父部门和部门ID列表,查找子部门。
|
|
|
+ * 该方法通过递归方式在给定的部门树中查找指定ID的子部门。
|
|
|
+ *
|
|
|
+ * @param parent 父部门对象,表示当前查找的起点。
|
|
|
+ * @param imDeptIds 部门ID列表,按层级顺序给出,用于指定需要查找的子部门路径。
|
|
|
+ * @return 返回找到的子部门对象,如果未找到或输入列表为空,则返回父部门对象或null。
|
|
|
+ */
|
|
|
+ private OrgsTree.Result findChildDepartment(OrgsTree.Result parent, List<String> imDeptIds){
|
|
|
+ // 避免对空list进行循环,提高效率
|
|
|
+ if(imDeptIds.isEmpty()){
|
|
|
+ return parent;
|
|
|
+ }
|
|
|
+ // 获取部门ID列表中的第一个ID,作为当前查找的目标。
|
|
|
+ String firstImDeptId = imDeptIds.get(0);
|
|
|
+ //从父部门的子部门中查找匹配第一个ID的部门对象。
|
|
|
+ OrgsTree.Result result = parent.getChildren().stream().filter(e -> e.getId().equals(firstImDeptId)).findFirst()
|
|
|
+ .orElse(null);
|
|
|
+ // 如果未找到匹配的部门对象,返回null,表示无法继续向下查找。
|
|
|
+ if(result == null){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 准备剩余的部门ID列表,用于下一次递归查找。
|
|
|
+ List<String> remainingDeptIds = imDeptIds.subList(1, imDeptIds.size());
|
|
|
+ // 继续递归查找下一个子部门,直到部门ID列表为空或找到匹配的部门对象。
|
|
|
+ return findChildDepartment(result, remainingDeptIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 比较两个字符串列表的元素,并返回三个集合:
|
|
|
+ * 仅在列表A中出现的元素、仅在列表B中出现的元素,以及在两个列表中都出现的元素。
|
|
|
+ *
|
|
|
+ * @param listA 第一个字符串列表,不为null。
|
|
|
+ * @param listB 第二个字符串列表,不为null。
|
|
|
+ * @return 一个包含三个集合的列表,分别代表上述三个分类的元素。
|
|
|
+ */
|
|
|
+ private static List<Set<String>> compareLists(List<String> listA, List<String> listB){
|
|
|
+ // 将列表A转换为集合A,去除重复元素。
|
|
|
+ Set<String> setA = new HashSet<>(listA);
|
|
|
+ // 将列表B转换为集合B,去除重复元素。
|
|
|
+ Set<String> setB = new HashSet<>(listB);
|
|
|
+ // 使用流从集合A中筛选出不在集合B中的元素,得到A中独有的元素集合。
|
|
|
+ Set<String> uniqueToA = setA.stream().filter(e -> !setB.contains(e)).collect(Collectors.toSet());
|
|
|
+ // 使用流从集合B中筛选出不在集合A中的元素,得到B中独有的元素集合。
|
|
|
+ Set<String> uniqueToB = setB.stream().filter(e -> !setA.contains(e)).collect(Collectors.toSet());
|
|
|
+ // 初始化一个集合来存储A和B的交集。
|
|
|
+ Set<String> commonElements = new HashSet<>(setA);
|
|
|
+ // 通过保留A和B的交集来计算共同元素。
|
|
|
+ commonElements.retainAll(setB);
|
|
|
+ // 返回包含三个集合的列表。
|
|
|
+ return List.of(uniqueToA, uniqueToB, commonElements);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String findWebUrl(Long userId){
|
|
|
+ // 获取工作平台配置实例
|
|
|
+ WorkProConfig workProConfig = WorkProConfig.getInstance();
|
|
|
+ // 检查工作平台配置是否启用,如果未启用,则记录错误并返回null
|
|
|
+ if(!workProConfig.isEnable()){
|
|
|
+ log.error("workPro IM 配置错误!");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 根据用户ID查找IM信息
|
|
|
+ OrgImBean byMintoId = orgImMapper.findByMintoId(String.valueOf(userId));
|
|
|
+ // 检查是否找到IM信息或IM ID为空,如果为空,则记录错误并返回null
|
|
|
+ if(byMintoId == null || StringUtil.isEmpty(byMintoId.getImId())){
|
|
|
+ log.error("当前用户没有与IM服务器关联!");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 创建UsersGet对象,用于获取用户账户信息
|
|
|
+ UsersGet usersGet = new UsersGet(byMintoId.getImId(), "gid", null);
|
|
|
+ // 调用API获取用户账户信息
|
|
|
+ callApi(usersGet, byMintoId.getImId());
|
|
|
+ // 获取用户账户信息,如果响应不OK,则返回null
|
|
|
+ String account = usersGet.isResponseOk() ? usersGet.getResult().getAccount() : null;
|
|
|
+ if(StringUtil.isEmpty(account)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 创建TokenCreate对象,用于获取用户令牌
|
|
|
+ TokenCreate tokenCreate = new TokenCreate(byMintoId.getImId(), "1", "0", "120");
|
|
|
+ // 调用API获取用户令牌
|
|
|
+ callApi(tokenCreate, byMintoId.getImId());
|
|
|
+ // 获取用户令牌,如果响应不OK,则返回null
|
|
|
+ String token = tokenCreate.isResponseOk() ? tokenCreate.getResult().getToken() : null;
|
|
|
+ if(StringUtil.isEmpty(token)){
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 定义URL模式,用于构建最终的Web URL
|
|
|
+ String pwdPattern = "{0}/webim/pc/#/chat?account={1}&password={2}";
|
|
|
+ String tokenPattern = "{0}/webim/pc/#/chat/index?account={1}&token={2}";
|
|
|
+ // 根据工作平台配置、用户账户和令牌构建并返回Web URL
|
|
|
+ return MessageFormat.format(tokenPattern, workProConfig.getUrl(), account, token);
|
|
|
+ }
|
|
|
+}
|