Ver código fonte

入职邀请加入钉钉+离职移除钉钉

jzh 1 ano atrás
pai
commit
af084886f8

+ 7 - 0
zjugis-business/src/main/java/com/zjugis/business/flow/resign/event/ResignEvent.java

@@ -15,6 +15,7 @@ import com.zjugis.framework.workflow.rpc.remote.WorkflowClient;
 import com.zjugis.framework.workflow.spring.resovler.ParamModel;
 import com.zjugis.module.adm.api.staff.StaffApi;
 import com.zjugis.module.adm.api.staff.dto.StaffStateDTO;
+import com.zjugis.module.infra.api.ding.DingApi;
 import com.zjugis.module.system.api.dept.DeptApi;
 import com.zjugis.module.system.api.dept.DeptLeaderApi;
 import com.zjugis.module.system.api.user.AdminUserApi;
@@ -79,6 +80,8 @@ public class ResignEvent extends BaseController {
     AdminUserApi adminUserApi;
     @Resource
     private DeptLeaderApi deptLeaderApi;
+    @Resource
+    private DingApi dingApi;
 
     /**
      * 设置流程描述
@@ -132,6 +135,10 @@ public class ResignEvent extends BaseController {
                 dto.setState(StaffStateConstants.RESIGN);
                 CommonResult<String> res = staffApi.updateStaffState(dto);
                 res.getCheckedData();
+
+                //钉钉移除员工
+                dingApi.resign(entity.getLoginName());
+
                 return ok("true");
             } else {
                 throw new BusinessException("执行事件出错,请联系管理员!");

+ 14 - 0
zjugis-module-adm/zjugis-module-adm-biz/src/main/java/com/zjugis/module/adm/service/staff/RecordsServiceImpl.java

@@ -19,6 +19,8 @@ import com.zjugis.module.adm.dal.dataobject.certificate.CertificateDO;
 import com.zjugis.module.adm.dal.dataobject.staff.*;
 import com.zjugis.module.adm.dal.mysql.certificate.CertMapper;
 import com.zjugis.module.adm.dal.mysql.staff.*;
+import com.zjugis.module.infra.api.ding.DingApi;
+import com.zjugis.module.infra.api.ding.dto.StaffDTO;
 import com.zjugis.module.system.api.user.AdminUserApi;
 import com.zjugis.module.system.api.user.dto.AdminUserRespDTO;
 import com.zjugis.module.system.api.user.dto.UserBaseDTO;
@@ -60,6 +62,9 @@ public class RecordsServiceImpl implements RecordsService {
     @Resource
     AdminUserApi userApi;
 
+    @Resource
+    private DingApi dingApi;
+
     @Override
     public Page<StaffRecordSDO> getRecordsPage(RecordsPageReqVO reqVO) {
 
@@ -345,6 +350,15 @@ public class RecordsServiceImpl implements RecordsService {
         }
 
 
+        //钉钉邀请
+        StaffDTO staff = new StaffDTO();
+        staff.setNickname(staffRecordSDO.getNickname());
+        staff.setMobile(staffRecordSDO.getMobilePhone());
+        staff.setDeptName(staffRecordSDO.getDeptName());
+        staff.setLoginName(staffRecordSDO.getLoginName());
+        dingApi.entry(staff);
+
+
         return user.getUserId();
     }
 

+ 10 - 0
zjugis-module-infra/zjugis-module-infra-api/src/main/java/com/zjugis/module/infra/api/ding/DingApi.java

@@ -2,12 +2,14 @@ package com.zjugis.module.infra.api.ding;
 
 import com.zjugis.framework.common.pojo.CommonResult;
 import com.zjugis.module.infra.api.ding.dto.DingMessageDto;
+import com.zjugis.module.infra.api.ding.dto.StaffDTO;
 import com.zjugis.module.infra.enums.ApiConstants;
 import io.swagger.v3.oas.annotations.Operation;
 import io.swagger.v3.oas.annotations.parameters.RequestBody;
 import io.swagger.v3.oas.annotations.tags.Tag;
 import org.springframework.cloud.openfeign.FeignClient;
 import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestParam;
 
 import javax.validation.Valid;
 
@@ -22,4 +24,12 @@ public interface DingApi {
     @Operation(summary = "发送钉钉消息")
     CommonResult<String> sendMessage(@Valid @RequestBody DingMessageDto dingMessageDto);
 
+    @PostMapping(PREFIX + "/entry")
+    @Operation(summary = "员工入职")
+    CommonResult<String> entry(@Valid @RequestBody StaffDTO dto);
+
+    @PostMapping(PREFIX + "/resign")
+    @Operation(summary = "员工离职")
+    CommonResult<String> resign(@Valid @RequestParam("loginName") String loginName);
+
 }

+ 24 - 0
zjugis-module-infra/zjugis-module-infra-api/src/main/java/com/zjugis/module/infra/api/ding/dto/StaffDTO.java

@@ -0,0 +1,24 @@
+package com.zjugis.module.infra.api.ding.dto;
+
+import lombok.Data;
+
+import javax.validation.constraints.NotNull;
+
+/**
+ * @author jzh
+ * @since 2024/7/3 16:20
+ */
+@Data
+public class StaffDTO {
+    @NotNull(message = "员工工号不能为空")
+    private String loginName;
+
+    @NotNull(message = "员工姓名不能为空")
+    private String nickname;
+
+    @NotNull(message = "手机号不能为空")
+    private String mobile;
+
+    @NotNull(message = "部门名称不能为空")
+    private String deptName;
+}

+ 47 - 24
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/api/ding/DingApiImpl.java

@@ -3,6 +3,7 @@ package com.zjugis.module.infra.api.ding;
 import com.taobao.api.ApiException;
 import com.zjugis.framework.common.pojo.CommonResult;
 import com.zjugis.module.infra.api.ding.dto.DingMessageDto;
+import com.zjugis.module.infra.api.ding.dto.StaffDTO;
 import com.zjugis.module.infra.service.ding.DingService;
 import com.zjugis.module.system.api.user.AdminUserApi;
 import com.zjugis.module.system.api.user.dto.AdminUserRespDTO;
@@ -21,30 +22,52 @@ import org.springframework.web.bind.annotation.RestController;
 @Validated
 public class DingApiImpl implements DingApi {
 
-        @Autowired
-        DingService dingService;
-
-        @Autowired
-        AdminUserApi adminUserApi;
-
-        /**
-         * @param dingMessageDto
-         * @return
-         */
-        @Override
-        public CommonResult<String> sendMessage(@RequestBody DingMessageDto dingMessageDto) {
-
-            CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(dingMessageDto.getUserId());
-            String mobile = user.getCheckedData().getMobile();
-            if(StringUtils.isBlank(mobile)){
-                    return CommonResult.error(101,"用户手机号不存在,无法获取钉钉用户");
-            }
-            try {
-                String s = dingService.sendNotification(mobile, dingMessageDto);
-                return CommonResult.success(s);
-            } catch (ApiException e) {
-                return CommonResult.error(101,"errcode:"+ e.getErrCode()+",errmsg:" + e.getErrMsg());
-            }
+    @Autowired
+    DingService dingService;
+
+    @Autowired
+    AdminUserApi adminUserApi;
+
+    /**
+     * @param dingMessageDto
+     * @return
+     */
+    @Override
+    public CommonResult<String> sendMessage(@RequestBody DingMessageDto dingMessageDto) {
+
+        CommonResult<AdminUserRespDTO> user = adminUserApi.getUser(dingMessageDto.getUserId());
+        String mobile = user.getCheckedData().getMobile();
+        if (StringUtils.isBlank(mobile)) {
+            return CommonResult.error(101, "用户手机号不存在,无法获取钉钉用户");
+        }
+        try {
+            String s = dingService.sendNotification(mobile, dingMessageDto);
+            return CommonResult.success(s);
+        } catch (ApiException e) {
+            return CommonResult.error(101, "errcode:" + e.getErrCode() + ",errmsg:" + e.getErrMsg());
+        }
+    }
+
+    @Override
+    public CommonResult<String> entry(StaffDTO dto) {
+        try {
+            String res = dingService.createStaff(dto);
+            return CommonResult.success(res);
+        } catch (ApiException e) {
+            return CommonResult.error(101, e.getMessage());
+        }
+
+
+    }
+
+    @Override
+    public CommonResult<String> resign(String loginName) {
+        try {
+            dingService.deleteStaff(loginName);
+            return CommonResult.success("ok");
+        } catch (ApiException e) {
+            return CommonResult.error(101, e.getMessage());
         }
+    }
 }
 

+ 5 - 0
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/service/ding/DingService.java

@@ -2,6 +2,7 @@ package com.zjugis.module.infra.service.ding;
 
 import com.taobao.api.ApiException;
 import com.zjugis.module.infra.api.ding.dto.DingMessageDto;
+import com.zjugis.module.infra.api.ding.dto.StaffDTO;
 
 /**
  * @author ljy
@@ -11,4 +12,8 @@ import com.zjugis.module.infra.api.ding.dto.DingMessageDto;
 public interface DingService {
     String sendNotification(String mobile, DingMessageDto dingMessageDto) throws ApiException;
 
+    String createStaff(StaffDTO dto) throws ApiException;
+
+    void deleteStaff(String loginName) throws ApiException;
+
 }

+ 104 - 18
zjugis-module-infra/zjugis-module-infra-biz/src/main/java/com/zjugis/module/infra/service/ding/DingServiceImpl.java

@@ -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());
         }
     }
 }