|
@@ -1,5 +1,9 @@
|
|
|
package com.zjugis.module.infra.service.ding;
|
|
|
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
import com.dingtalk.api.DefaultDingTalkClient;
|
|
|
import com.dingtalk.api.DingTalkClient;
|
|
|
import com.dingtalk.api.request.OapiGettokenRequest;
|
|
@@ -9,10 +13,7 @@ import com.dingtalk.api.response.OapiGettokenResponse;
|
|
|
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
|
|
|
import com.dingtalk.api.response.OapiUserGetByMobileResponse;
|
|
|
import com.taobao.api.ApiException;
|
|
|
-import com.zjugis.module.infra.api.ding.dto.DingMessageDto;
|
|
|
-import com.zjugis.module.infra.api.ding.dto.Form;
|
|
|
-import com.zjugis.module.infra.api.ding.dto.Link;
|
|
|
-import com.zjugis.module.infra.api.ding.dto.OA;
|
|
|
+import com.zjugis.module.infra.api.ding.dto.*;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
@@ -20,7 +21,9 @@ import org.springframework.stereotype.Service;
|
|
|
import org.springframework.validation.annotation.Validated;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* @author ljy
|
|
@@ -30,7 +33,7 @@ import java.util.List;
|
|
|
@Service
|
|
|
@Validated
|
|
|
@Slf4j
|
|
|
-public class DingServiceImpl implements DingService{
|
|
|
+public class DingServiceImpl implements DingService {
|
|
|
|
|
|
@Value("${zjugis.ding.agentid}")
|
|
|
private String agentId;
|
|
@@ -52,31 +55,114 @@ public class DingServiceImpl implements DingService{
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
|
|
|
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
|
|
|
request.setAgentId(Long.valueOf(agentId));
|
|
|
- String userId = getUserIdByPhone(mobile,token);
|
|
|
+ String userId = getUserIdByPhone(mobile, token);
|
|
|
request.setUseridList(userId);
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
|
|
|
- switch (dingMessageDto.getMsgType()){
|
|
|
+ switch (dingMessageDto.getMsgType()) {
|
|
|
case LINK:
|
|
|
- buildLink(msg,dingMessageDto.getLink());
|
|
|
+ buildLink(msg, dingMessageDto.getLink());
|
|
|
break;
|
|
|
case OA:
|
|
|
- buildOA(msg,dingMessageDto.getOa());
|
|
|
+ buildOA(msg, dingMessageDto.getOa());
|
|
|
break;
|
|
|
default:
|
|
|
- buildText(msg,dingMessageDto.getContent());
|
|
|
+ buildText(msg, dingMessageDto.getContent());
|
|
|
}
|
|
|
request.setMsg(msg);
|
|
|
OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, token);
|
|
|
return rsp.toString();
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public String createStaff(StaffDTO dto) throws ApiException {
|
|
|
+ String accessToken = getDingTalkToken();
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("access_token", accessToken);
|
|
|
+ paramMap.put("userid", dto.getLoginName());
|
|
|
+ paramMap.put("name", dto.getNickname());
|
|
|
+ paramMap.put("mobile", dto.getMobile());
|
|
|
+ paramMap.put("dept_id_list", getDeptList(dto.getDeptName(), accessToken));
|
|
|
+ String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/v2/user/create", paramMap);
|
|
|
+ Map map = JSONUtil.toBean(res, Map.class);
|
|
|
+ String errorCode = map.get("errcode").toString();
|
|
|
+ String errMsg = map.get("errmsg").toString();
|
|
|
+ if (!errorCode.equals("40103")) {
|
|
|
+ throw new ApiException(errMsg);
|
|
|
+ }
|
|
|
+ return errMsg;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void deleteStaff(String loginName) throws ApiException {
|
|
|
+ String accessToken = getDingTalkToken();
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("access_token", accessToken);
|
|
|
+ paramMap.put("userid", loginName);
|
|
|
+ String res = HttpUtil.post("https://oapi.dingtalk.com/topapi/v2/user/delete", paramMap);
|
|
|
+ Map map = JSONUtil.toBean(res, Map.class);
|
|
|
+ String errorCode = map.get("errcode").toString();
|
|
|
+ if (!errorCode.equals("0")) {
|
|
|
+ throw new ApiException(map.get("errmsg").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getDeptList(String staffDeptName, String accessToken) throws ApiException {
|
|
|
+
|
|
|
+ Map<String, Object> paramMap = new HashMap<>();
|
|
|
+ paramMap.put("access_token", accessToken);
|
|
|
+ paramMap.put("dept_id", "");
|
|
|
+ String res = HttpUtil.get("https://oapi.dingtalk.com/topapi/v2/department/listsub", paramMap);
|
|
|
+ Map map = JSONUtil.toBean(res, Map.class);
|
|
|
+ JSONArray jsonArray = JSONUtil.parseArray(map.get("result"));
|
|
|
+ Map<String, String> masterDept = new HashMap<>();
|
|
|
+ for (Object o : jsonArray) {
|
|
|
+ JSONObject object = (JSONObject) o;
|
|
|
+ String deptId = object.get("dept_id").toString();
|
|
|
+ String deptName = object.get("name").toString();
|
|
|
+ masterDept.put(deptName, deptId);
|
|
|
+ }
|
|
|
+
|
|
|
+ Map<String, String> subDeptMap = new HashMap<>();
|
|
|
+ if (masterDept.size() > 0) {
|
|
|
+ masterDept.forEach((k, v) -> {
|
|
|
+ Map<String, Object> subMap = new HashMap<>();
|
|
|
+ subMap.put("access_token", accessToken);
|
|
|
+ subMap.put("dept_id", v);
|
|
|
+ String subCompany = HttpUtil.get("https://oapi.dingtalk.com/topapi/v2/department/listsub", subMap);
|
|
|
+ Map jMap = JSONUtil.toBean(subCompany, Map.class);
|
|
|
+ JSONArray ja = JSONUtil.parseArray(jMap.get("result"));
|
|
|
+ for (Object o : ja) {
|
|
|
+ JSONObject object = (JSONObject) o;
|
|
|
+ String deptId = object.get("dept_id").toString();
|
|
|
+ String deptName = object.get("name").toString();
|
|
|
+ subDeptMap.put(deptName, deptId);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+// 先去子部门匹配
|
|
|
+ if (subDeptMap.size() > 0) {
|
|
|
+ String subDeptId = subDeptMap.get(staffDeptName);
|
|
|
+ String deptId = masterDept.get(staffDeptName);
|
|
|
+ if (subDeptId != null) {
|
|
|
+ return subDeptId;
|
|
|
+ } else if (deptId != null) {
|
|
|
+ return deptId;
|
|
|
+ } else {
|
|
|
+ throw new ApiException("部门不匹配 请联系管理员");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
private void buildOA(OapiMessageCorpconversationAsyncsendV2Request.Msg msg, OA oa) {
|
|
|
msg.setMsgtype("oa");
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.OA entity = new OapiMessageCorpconversationAsyncsendV2Request.OA();
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.Head head = new OapiMessageCorpconversationAsyncsendV2Request.Head();
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.Body body = new OapiMessageCorpconversationAsyncsendV2Request.Body();
|
|
|
List<Form> forms = oa.getBody().getForm();
|
|
|
- if(forms != null) {
|
|
|
+ if (forms != null) {
|
|
|
List<OapiMessageCorpconversationAsyncsendV2Request.Form> dingForms = new ArrayList<>(forms.size());
|
|
|
for (Form form : forms) {
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.Form dingForm = new OapiMessageCorpconversationAsyncsendV2Request.Form();
|
|
@@ -87,14 +173,14 @@ public class DingServiceImpl implements DingService{
|
|
|
}
|
|
|
entity.setMessageUrl(oa.getMessageUrl());
|
|
|
entity.setPcMessageUrl(oa.getPcMessageUrl());
|
|
|
- BeanUtils.copyProperties(oa.getHead(),head);
|
|
|
- BeanUtils.copyProperties(oa.getBody(),body);
|
|
|
+ BeanUtils.copyProperties(oa.getHead(), head);
|
|
|
+ BeanUtils.copyProperties(oa.getBody(), body);
|
|
|
entity.setHead(head);
|
|
|
entity.setBody(body);
|
|
|
msg.setOa(entity);
|
|
|
}
|
|
|
|
|
|
- private void buildText(OapiMessageCorpconversationAsyncsendV2Request.Msg msg,String content){
|
|
|
+ private void buildText(OapiMessageCorpconversationAsyncsendV2Request.Msg msg, String content) {
|
|
|
msg.setMsgtype("text");
|
|
|
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
|
|
|
msg.getText().setContent(content);
|
|
@@ -103,7 +189,7 @@ public class DingServiceImpl implements DingService{
|
|
|
private void buildLink(OapiMessageCorpconversationAsyncsendV2Request.Msg msg, Link link) throws ApiException {
|
|
|
msg.setMsgtype("link");
|
|
|
OapiMessageCorpconversationAsyncsendV2Request.Link entity = new OapiMessageCorpconversationAsyncsendV2Request.Link();
|
|
|
- BeanUtils.copyProperties(link,entity);
|
|
|
+ BeanUtils.copyProperties(link, entity);
|
|
|
msg.setLink(entity);
|
|
|
}
|
|
|
|
|
@@ -118,16 +204,16 @@ public class DingServiceImpl implements DingService{
|
|
|
return rsp.getAccessToken();
|
|
|
}
|
|
|
|
|
|
- private String getUserIdByPhone(String phone,String token) throws ApiException {
|
|
|
+ private String getUserIdByPhone(String phone, String token) throws ApiException {
|
|
|
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile");
|
|
|
OapiUserGetByMobileRequest req = new OapiUserGetByMobileRequest();
|
|
|
req.setMobile(phone);
|
|
|
req.setHttpMethod("GET");
|
|
|
OapiUserGetByMobileResponse rsp = client.execute(req, token);
|
|
|
- if("ok".equals(rsp.getErrmsg())){
|
|
|
+ if ("ok".equals(rsp.getErrmsg())) {
|
|
|
return rsp.getUserid();
|
|
|
} else {
|
|
|
- throw new ApiException(rsp.getErrorCode()+rsp.getErrmsg());
|
|
|
+ throw new ApiException(rsp.getErrorCode() + rsp.getErrmsg());
|
|
|
}
|
|
|
}
|
|
|
}
|