| | |
| | | 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); |
| | | 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); |
| | | } |
| | | DingUserInfo dingUserInfo = BeanUtil.toBean(resObj, DingUserInfo.class); |
| | | String accId = DingEncodeUtil.getMD5(resObj.getString("employeeCode").getBytes()); |
| | | dingUserInfo.setUserId(Long.parseLong(accId)); |
| | | return dingUserInfo; |
| | | } |
| | | DingUserInfo dingUserInfo = BeanUtil.toBean(resObj, DingUserInfo.class); |
| | | String accId = DingEncodeUtil.getMD5(resObj.getString("employeeCode").getBytes()); |
| | | dingUserInfo.setUserId(Long.parseLong(accId)); |
| | | return dingUserInfo; |
| | | } |
| | | return null; |
| | | } |
| | |
| | | * 根据员工Code查询详情 |
| | | * |
| | | * @param employeeCode 员工code列表 |
| | | * @param tenantId 租户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); |
| | | 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字以内 |
| | | * @param text 消息,最好500字以内 |
| | | * @return |
| | | */ |
| | | public JSONObject sendDingMsgMarkDown(String receiverIds, String title,String text) { |
| | | 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)); |
| | | postClient.addParameter("msg", getJsonMarkDown(title, text)); |
| | | String apiResult = postClient.post(); |
| | | log.info("apiResult------>{}",apiResult); |
| | | log.info("apiResult------>{}", apiResult); |
| | | JSONObject resObj = parsingJson(apiResult); |
| | | if (null != resObj) { |
| | | resObj = resObj.getJSONObject("data"); |
| | | } |
| | | return resObj; |
| | | } |
| | | |
| | | /** |
| | | * 发送链接消息 |
| | | * |
| | |
| | | */ |
| | | public List<UmsDepart> getOrgInfo(JSONArray deptTopArr) { |
| | | List<UmsDepart> orgList = new ArrayList<>(); |
| | | if(CollectionUtils.isEmpty(deptTopArr)){ |
| | | if (CollectionUtils.isEmpty(deptTopArr)) { |
| | | return null; |
| | | } |
| | | //获取全部部门code |
| | |
| | | } |
| | | } |
| | | //补充accountId |
| | | if(CollectionUtils.isNotEmpty(employeeCodes)) { |
| | | if (CollectionUtils.isNotEmpty(employeeCodes)) { |
| | | log.info("开始补充accountId"); |
| | | JSONArray users = getAccountId(employeeCodes); |
| | | if(CollectionUtils.isNotEmpty(users)) { |
| | | 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()){ |
| | | if (first.isPresent()) { |
| | | DingUserInfo dingUserInfo = first.get(); |
| | | dingUserInfo.setAccountId(Long.parseLong(accountId)); |
| | | } |
| | |
| | | redisTemplate.opsForValue().set("userJsonList", arr.toJSONString()); |
| | | return dingUserInfoList; |
| | | } |
| | | |
| | | /** |
| | | * 根据员工code 获取员工accountId |
| | | * |
| | |
| | | * @return 钉钉用户体系实体对象 |
| | | */ |
| | | private JSONArray getAccountId(List<String> employeeCodes) { |
| | | log.info("Code参数集合------>{}",employeeCodes); |
| | | log.info("Code参数集合------>{}", employeeCodes); |
| | | PostClient postClient = executableClient.newPostClient(POST_ACCOUNTID_BY_EMPLOYEECODE); |
| | | for (String code : employeeCodes) { |
| | | postClient.addParameter("employeeCodes",code); |
| | | postClient.addParameter("employeeCodes", code); |
| | | } |
| | | postClient.addParameter("tenantId", dingConfig.getTenantId()); |
| | | String apiResult = postClient.post(); |
| | |
| | | // if (null != orgDetail) { |
| | | // String orgId = orgDetail.getString("organizationCode"); |
| | | // String orgName = orgDetail.getString("organizationName"); |
| | | Long orgId = Long.parseLong(DingEncodeUtil.getMD5(orgCode.getBytes())); |
| | | 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); |
| | | } |
| | |
| | | String jsonStr = JSONObject.toJSONString(jsonObject); |
| | | return jsonStr; |
| | | } |
| | | |
| | | /** |
| | | * 拼接要发送的消息 markdown消息 |
| | | * json对象 必须 {"msgtype":"markdown","markdown":{"title":"首屏会话透出的展示内容","text":"消息内容"}} |
| | | * |
| | | * @param title 首屏会话透出的展示内容 |
| | | */ |
| | | private String getJsonMarkDown(String title,String text) { |
| | | 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字符以内 |
| | | markdown.put("text", text);//消息内容,建议500字符以内 |
| | | jsonObject.put("markdown", markdown); |
| | | String jsonStr = JSONObject.toJSONString(jsonObject); |
| | | return jsonStr; |
| | | } |
| | | |
| | | /** |
| | | * 拼接要发送的消息 链接消息 |
| | | * |