liusheng
2023-05-25 bed24ea3fdeee959b85b699a82d53c5112adb219
ruoyi-admin/src/main/java/com/ruoyi/web/controller/smartor/DingTalkController.java
@@ -11,11 +11,14 @@
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.ruoyi.common.core.controller.BaseController;
import com.smartor.domain.DingTalkReqVo;
import com.smartor.service.DingTalkService;
import com.taobao.api.ApiException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@@ -28,103 +31,27 @@
 */
@RestController
@RequestMapping("/smartor/dingtalk")
@PropertySource(value = {"classpath:application-druid.yml"})
public class DingTalkController extends BaseController {
    @Value("appid")
    private String dingAppid = "dingn8iip5ubj7clrrsv";
    @Value("${dingAppid}")
    private String dingAppid;
    @Value("appSecret")
    private String dingAppSecret = "qlEK8D3oOVwGPOTiBQIBYTqQVlAfy9S_qQizEQFjJdSScwemWFryg4gbneu-NqWD";
    @Value("${dingAppSecret}")
    private String dingAppSecret;
    @Autowired
    private DingTalkService dingTalkService;
    /**
     * 发送钉钉消息
     *
     * @param mobile
     * @param park
     * @param alarmCode
     * @param severityDesc
     * @param dingTalkReqVo 发送内容
     * @throws ApiException
     */
    public void sendNotification(String mobile, String park, String alarmCode, String severityDesc) throws ApiException {
        // Log.info("发送钉钉通知");
        String accessToken = getAccessToken();
        if (StringUtils.isBlank(mobile)) {
            return;
        }
        DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
        OapiUserListidRequest request2 = new OapiUserListidRequest();
        request2.setDeptId(675909059L); // 设置要获取的部门 ID,这里以根部门为例
        // 设置部门 ID,如果获取所有员工手机号,可以设置为根部门的 ID
        OapiUserListidResponse response2 = client3.execute(request2, accessToken);
        String body = response2.getBody();
        // 解析为JSONObject
        JSONObject jsonObject = JSONObject.parseObject(body);
        // 提取出JSONArray
        JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list"));
        // 将JSONArray转为List列表
        String str = JSONObject.toJSONString(jsonArray);
        List<String> list = JSONObject.parseObject(str, List.class);
        //userid数组
        for (String urid : list) {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
            request.setUseridList(urid);
            request.setAgentId(1779052814L);
            request.setToAllUser(false);
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
            msg.getOa().setMessageUrl("www.baidu.com");
            msg.getOa().setPcMessageUrl("www.baidu.com");
            msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
            msg.getOa().getHead().setText("text");
            msg.getOa().getHead().setBgcolor("FFBBBBBB");
            msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
            msg.getOa().getBody().setContent("msg信息");
            OapiMessageCorpconversationAsyncsendV2Request.Form formPark = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formPark.setKey(null);
            formPark.setValue(null);
            OapiMessageCorpconversationAsyncsendV2Request.Form formAlarmCode = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formAlarmCode.setKey("工工工a ");
            formAlarmCode.setValue(alarmCode);
            OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formSeverityDesc.setKey("林要要要要");
            formSeverityDesc.setValue(severityDesc);
            OapiMessageCorpconversationAsyncsendV2Request.Form formConner = new OapiMessageCorpconversationAsyncsendV2Request.Form();
            formConner.setKey("厅在在");
            ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
            objects.add(formPark);
            objects.add(formSeverityDesc);
            objects.add(formAlarmCode);
            objects.add(formConner);
            msg.getOa().getBody().setForm(objects);
            msg.setMsgtype("oa");
            request.setMsg(msg);
            //  log.info("获取发送通知消息体和获取发送通知人完成");
            OapiMessageCorpconversationAsyncsendV2Response response = client.execute(request, accessToken);
            //  log.info("发送消息是否成功"+response.isSuccess());
            System.out.println(response.isSuccess());
            //  log.info("消息任务ID"+response.getTaskId());
            System.out.println(response.getTaskId());
        }
    }
    /**
     * 获取 access_token
     */
    private String getAccessToken() throws ApiException {
        DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        //Appkey
        request.setAppkey(dingAppid);
        //Appsecret
        request.setAppsecret(dingAppSecret);
        /*请求方式*/
        request.setHttpMethod("GET");
        OapiGettokenResponse response = client.execute(request);
        return response.getAccessToken();
    @PostMapping("/sendNotification")
    public void sendNotification(@RequestBody DingTalkReqVo dingTalkReqVo) throws ApiException {
        dingTalkService.sendNotification(dingTalkReqVo);
    }
}