liusheng
2023-06-15 77d7257c02fc811a53b8d9207e4239f69c8a600c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package com.smartor.service.impl;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiUserListidRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiUserListidResponse;
import com.smartor.domain.DingTalkReqVo;
import com.smartor.service.DingTalkService;
import com.smartor.service.IntelligenceCenterService;
import com.taobao.api.ApiException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * 智能中心
 *
 * @author ls
 * @date 2023-05-23
 */
@Service
public class DingTalkServiceImpl implements DingTalkService {
 
 
    @Value("${dingAppid}")
    private String dingAppid;
 
    @Value("${dingAppSecret}")
    private String dingAppSecret;
 
    @Override
    public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) {
        Boolean result = false;
        // Log.info("发送钉钉通知");
        String accessToken = null;
        try {
            accessToken = getAccessToken();
        } catch (ApiException e) {
            e.printStackTrace();
        }
        DingTalkClient client3 = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listid");
 
        OapiUserListidRequest request2 = new OapiUserListidRequest();
        request2.setDeptId(675909059L); // 设置要获取的部门 ID,这里以根部门为例
        // 设置部门 ID,如果获取所有员工手机号,可以设置为根部门的 ID
        OapiUserListidResponse response2 = null;
        try {
            response2 = client3.execute(request2, accessToken);
        } catch (ApiException e) {
            e.printStackTrace();
        }
        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);
 
            List<String> contents = dingTalkReqVo.getContents();
            ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
            for (int i = 0; i < contents.size(); i++) {
                OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
                formSeverityDesc.setKey("问题" + i);
                formSeverityDesc.setValue(contents.get(i).toString());
                objects.add(formSeverityDesc);
            }
 
            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("待办事宜");
            msg.getOa().getHead().setBgcolor("00409eff");
            msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
            msg.getOa().getBody().setTitle("尊敬的 " + "AAA" + "(项目经理)");
            msg.getOa().getBody().setContent("待审批:" + "123" + "条");
 
            msg.getOa().getBody().setForm(objects);
            msg.setMsgtype("oa");
            request.setMsg(msg);
            //  log.info("获取发送通知消息体和获取发送通知人完成");
            OapiMessageCorpconversationAsyncsendV2Response response = null;
            try {
                response = client.execute(request, accessToken);
            } catch (ApiException e) {
                e.printStackTrace();
            }
 
            //  log.info("消息任务ID"+response.getTaskId());
            System.out.println(response.getTaskId());
 
            result = response.isSuccess();
        }
        return result;
    }
 
 
    /**
     * 获取 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();
    }
}