| | |
| | | import com.ycl.exception.ApiException; |
| | | import com.ycl.service.depart.UmsDepartService; |
| | | import com.ycl.service.user.UmsRoleService; |
| | | import com.ycl.util.DingUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.data.redis.core.StringRedisTemplate; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | @Autowired |
| | | private UmsDepartService umsDepartService; |
| | | |
| | | @Autowired |
| | | private DingUtil dingUtil; |
| | | |
| | | /** |
| | | * 获取用户信息 |
| | |
| | | public DingUserInfo getDingUserInfo(String code) { |
| | | // 判断ding 信息是否存在 |
| | | boolean hasDing = redisTemplate.hasKey("ding"); |
| | | String getToken = ""; |
| | | if (hasDing) { |
| | | log.info("redis正常有消息"); |
| | | String dingStr = redisTemplate.opsForValue().get("ding"); |
| | | JSONObject dingObj = JSONObject.parseObject(dingStr); |
| | | String getToken = dingObj.getString("token"); |
| | | if (ObjectUtil.isEmpty(code) || ObjectUtil.isEmpty(getToken)) { |
| | | throw new ApiException("登录异常"); |
| | | getToken = dingObj.getString("token"); |
| | | |
| | | } else { |
| | | getToken = dingUtil.getToken(); |
| | | } |
| | | if (ObjectUtil.isEmpty(code) || ObjectUtil.isEmpty(getToken)) { |
| | | throw new ApiException("登录异常"); |
| | | } |
| | | //获取用户信息 |
| | | PostClient postClient = executableClient.newPostClient(GET_USER_INFO); |
| | | postClient.addParameter("access_token", getToken); //token |
| | | postClient.addParameter("auth_code", code);//临时授权码 |
| | | String apiResult = postClient.post(); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | log.info("resObj--------------->{}", resObj); |
| | | if (null != resObj) { |
| | | resObj = resObj.getJSONObject("data"); |
| | | redisTemplate.opsForValue().set("realmId", resObj.getString("realmId")); |
| | | JSONObject empCodeObj = getEmpByCode(resObj.getString("employeeCode"), resObj.getString("realmId")); |
| | | if (ObjectUtil.isNotEmpty(empCodeObj)) { |
| | | resObj.putAll(empCodeObj); |
| | | } |
| | | //获取用户信息 |
| | | PostClient postClient = executableClient.newPostClient(GET_USER_INFO); |
| | | postClient.addParameter("access_token", getToken); //token |
| | | postClient.addParameter("auth_code", code);//临时授权码 |
| | | String apiResult = postClient.post(); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | if (null != resObj) { |
| | | resObj = resObj.getJSONObject("data"); |
| | | redisTemplate.opsForValue().set("realmId", resObj.getString("realmId")); |
| | | JSONObject empCodeObj = getEmpByCode(resObj.getString("employeeCode"), resObj.getString("realmId")); |
| | | if (ObjectUtil.isNotEmpty(empCodeObj)) { |
| | | resObj.putAll(empCodeObj); |
| | | } |
| | | DingUserInfo dingUserInfo = BeanUtil.toBean(resObj, DingUserInfo.class); |
| | | String accId = DingEncodeUtil.getMD5(resObj.getString("employeeCode").getBytes()); |
| | | dingUserInfo.setUserId(Long.parseLong(accId)); |
| | | dingUserInfo.setAccountId(resObj.getLong("accountId")); |
| | | return dingUserInfo; |
| | | } |
| | | DingUserInfo dingUserInfo = BeanUtil.toBean(resObj, DingUserInfo.class); |
| | | String accId = DingEncodeUtil.getMD5(resObj.getString("employeeCode").getBytes()); |
| | | dingUserInfo.setUserId(Long.parseLong(accId)); |
| | | |
| | | String employeeCode = resObj.getString("employeeCode"); |
| | | JSONArray users = getAccountId(Arrays.asList(employeeCode)); |
| | | JSONObject jsonObject = users.getJSONObject(0); |
| | | String accountId = jsonObject.getString("accountId"); |
| | | dingUserInfo.setAccountId(Long.parseLong(accountId)); |
| | | return dingUserInfo; |
| | | } |
| | | return null; |
| | | } |
| | |
| | | * 根据员工Code查询详情 |
| | | * |
| | | * @param employeeCode 员工code列表 |
| | | * @param TENANT_ID 租户id |
| | | * @param tenantId 租户id |
| | | * @return |
| | | */ |
| | | public JSONObject getEmpByCode(String employeeCode, String tenantId) { |
| | |
| | | postClient.addParameter("tenantId", dingConfig.getTenantId()); |
| | | postClient.addParameter("msg", getJsonStringText(content)); |
| | | String apiResult = postClient.post(); |
| | | log.info("apiResult------>{}", apiResult); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | if (null != resObj) { |
| | | resObj = resObj.getJSONObject("data"); |
| | | } |
| | | return resObj; |
| | | } |
| | | |
| | | /** |
| | | * 发送markdown消息 |
| | | * |
| | | * @param receiverIds 接收人用户ID(accountId), 多个人时使用半角逗号分隔, |
| | | * @param text 消息,最好500字以内 |
| | | * @return |
| | | */ |
| | | public JSONObject sendDingMsgMarkDown(String receiverIds, String title, String text) { |
| | | //获取用户信息 |
| | | PostClient postClient = executableClient.newPostClient(POST_SEND_NOTIFY); |
| | | postClient.addParameter("receiverIds", receiverIds); |
| | | postClient.addParameter("tenantId", dingConfig.getTenantId()); |
| | | postClient.addParameter("msg", getJsonMarkDown(title, text)); |
| | | String apiResult = postClient.post(); |
| | | log.info("apiResult------>{}", apiResult); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | if (null != resObj) { |
| | | resObj = resObj.getJSONObject("data"); |
| | |
| | | */ |
| | | public List<UmsDepart> getOrgInfo(JSONArray deptTopArr) { |
| | | List<UmsDepart> orgList = new ArrayList<>(); |
| | | if(CollectionUtils.isEmpty(deptTopArr)){ |
| | | if (CollectionUtils.isEmpty(deptTopArr)) { |
| | | return null; |
| | | } |
| | | //获取全部部门code |
| | |
| | | orgLambdaQueryWrapper.likeRight(UmsDepart::getCode, "GO_"); |
| | | List<UmsDepart> orgList = umsDepartService.list(orgLambdaQueryWrapper); |
| | | Set<DingUserInfo> dingUserInfoList = new HashSet<>(); |
| | | List<String> employeeCodes = new ArrayList(); |
| | | for (UmsDepart org : orgList) { |
| | | if (ObjectUtil.isNotEmpty(org.getCode())) { |
| | | IntelligentGetClient intelligentGetClient = executableClient.newIntelligentGetClient(POST_PERSON_INFO_BY_ORG_CODE); |
| | |
| | | JSONObject data = dataArr.getJSONObject(i); |
| | | // String empName = data.getString("employeeName"); |
| | | String employeeCode = data.getString("employeeCode"); |
| | | employeeCodes.add(employeeCode); |
| | | //根据员工code 获取员工详情 |
| | | DingUserInfo dingUserInfo = getEmpInfoByCode(employeeCode); |
| | | if (ObjectUtil.isNotEmpty(dingUserInfo)) { |
| | |
| | | } while (isHasPage); |
| | | } |
| | | } |
| | | //补充accountId |
| | | if (CollectionUtils.isNotEmpty(employeeCodes)) { |
| | | log.info("开始补充accountId"); |
| | | JSONArray users = getAccountId(employeeCodes); |
| | | if (CollectionUtils.isNotEmpty(users)) { |
| | | for (int i = 0; i < users.size(); i++) { |
| | | JSONObject jsonObject = users.getJSONObject(i); |
| | | String employeeCode = jsonObject.getString("employeeCode"); |
| | | String accountId = jsonObject.getString("accountId"); |
| | | Optional<DingUserInfo> first = dingUserInfoList.stream().filter(dingUserInfo -> dingUserInfo.getEmployeeCode().equals(employeeCode)) |
| | | .findFirst(); |
| | | if (first.isPresent()) { |
| | | DingUserInfo dingUserInfo = first.get(); |
| | | dingUserInfo.setAccountId(Long.parseLong(accountId)); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | JSONArray arr = new JSONArray(Collections.singletonList(dingUserInfoList)); |
| | | redisTemplate.opsForValue().set("userJsonList", arr.toJSONString()); |
| | | return dingUserInfoList; |
| | | } |
| | | |
| | | /** |
| | | * 根据员工code 获取员工accountId |
| | | * |
| | | * @param employeeCodes 员工code |
| | | * @return 钉钉用户体系实体对象 |
| | | */ |
| | | private JSONArray getAccountId(List<String> employeeCodes) { |
| | | log.info("Code参数集合------>{}", employeeCodes); |
| | | PostClient postClient = executableClient.newPostClient(POST_ACCOUNTID_BY_EMPLOYEECODE); |
| | | for (String code : employeeCodes) { |
| | | postClient.addParameter("employeeCodes", code); |
| | | } |
| | | postClient.addParameter("tenantId", dingConfig.getTenantId()); |
| | | String apiResult = postClient.post(); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | if (null != resObj) { |
| | | return resObj.getJSONArray("data"); |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | @Resource |
| | |
| | | //查询任职 |
| | | JSONArray jobArr = getStaffAppointment(employeeCode); |
| | | assert jobArr != null; |
| | | log.info("任职----------------->{}",jobArr); |
| | | //临时保存任职的ids(系统的用户体系) |
| | | List<Long> posIds = new ArrayList<>(); |
| | | //用户角色code |
| | |
| | | if (isMain) { |
| | | JSONObject holdapost = jobArr.getJSONObject(0); |
| | | String orgCode = holdapost.getString("organizationCode"); |
| | | log.info("orgCode---------------->{}",orgCode); |
| | | if (StrUtil.isNotBlank(orgCode)) { |
| | | // JSONObject orgDetail = getOrgDetailByCode(orgCode); |
| | | // if (null != orgDetail) { |
| | | // String orgId = orgDetail.getString("organizationCode"); |
| | | // String orgName = orgDetail.getString("organizationName"); |
| | | Long orgId = Long.parseLong(DingEncodeUtil.getMD5(orgCode.getBytes())); |
| | | log.info("解密后的orgId-------------->{}",orgId); |
| | | empCodeObj.put("orgId", orgId); |
| | | Long orgId = Long.parseLong(DingEncodeUtil.getMD5(orgCode.getBytes())); |
| | | empCodeObj.put("orgId", orgId); |
| | | // empCodeObj.put("orgName", orgName); |
| | | // } |
| | | } |
| | |
| | | String code = DingEncodeUtil.getMD5(posName.getBytes()); |
| | | //查询是否有重复的 |
| | | LambdaQueryWrapper<UmsRole> posLambdaQueryWrapper = new LambdaQueryWrapper<>(); |
| | | // posLambdaQueryWrapper.eq(UmsRole::getCode, code); |
| | | // posLambdaQueryWrapper.eq(UmsRole::getCode, code); |
| | | posLambdaQueryWrapper.eq(UmsRole::getName, posName); |
| | | posLambdaQueryWrapper.last("LIMIT 1"); |
| | | UmsRole posOne = userRoleService.getOne(posLambdaQueryWrapper); |
| | |
| | | posOne.setCode(code); |
| | | posOne.setStatus(1); |
| | | userRoleService.save(posOne); |
| | | }else { |
| | | } else { |
| | | posOne.setCode(code); |
| | | userRoleService.updateById(posOne); |
| | | } |
| | |
| | | //转换为实体类 |
| | | DingUserInfo dingUserInfo = BeanUtil.toBean(empCodeObj, DingUserInfo.class); |
| | | Long orgId = dingUserInfo.getOrgId(); |
| | | log.info("最后结果的orgId----------->{}",orgId); |
| | | if (ObjectUtil.isNotEmpty(orgId)) { |
| | | dingUserInfo.setPosIdList(posIds); |
| | | dingUserInfo.setRoleCodeList(roleCodeList); |
| | |
| | | postClient.addParameter("employeeCode", empCode); |
| | | String apiResult = postClient.post(); |
| | | JSONObject dataObj = parsingJson(apiResult); |
| | | log.info("任职------------->{}",dataObj); |
| | | if (null != dataObj) { |
| | | return dataObj.getJSONArray("data"); |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 拼接要发送的消息 markdown消息 |
| | | * json对象 必须 {"msgtype":"markdown","markdown":{"title":"首屏会话透出的展示内容","text":"消息内容"}} |
| | | * |
| | | * @param title 首屏会话透出的展示内容 |
| | | */ |
| | | private String getJsonMarkDown(String title, String text) { |
| | | JSONObject jsonObject = new JSONObject(); |
| | | jsonObject.put("msgtype", "markdown");//消息类型,此时固定为:markdown |
| | | JSONObject markdown = new JSONObject(); |
| | | markdown.put("title", title);//首屏会话透出的展示内容 |
| | | markdown.put("text", text);//消息内容,建议500字符以内 |
| | | jsonObject.put("markdown", markdown); |
| | | String jsonStr = JSONObject.toJSONString(jsonObject); |
| | | return jsonStr; |
| | | } |
| | | |
| | | /** |
| | | * 拼接要发送的消息 链接消息 |
| | | * |
| | | * @param messageUrl 消息点击链接地址,当发送消息为小程序时支持小程序跳转链接 |