weicd
2023-09-28 d0c51ac702c57f2cae84a8d879a5b605f2dab630
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
package me.zhengjie.modules.quartz.service.impl;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiAttendanceListRequest;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiAttendanceListResponse;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.quartz.service.OpaiDingTalkService;
import me.zhengjie.modules.quartz.service.dto.OapiDingTalkAccessToken;
import me.zhengjie.modules.quartz.service.dto.OapiDingTalkRecordResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
 
import java.sql.Timestamp;
import java.util.List;
 
/**
 * @author zlh
 * @date 2022-02-23
 */
@RequiredArgsConstructor
@Service(value = "opaiDingTalkService")
public class OpaiDingTalkServiceImpl implements OpaiDingTalkService {
 
    private final String appkey = "dingqcailhf40maaaibg";
 
    private final String appsecret = "tAsp4xCPcLDCVY_SGIO_wVfQf_u2s0QQdNlBLuZD2DKwLBos9VVSdpZgnfEndDlW";
 
    private final Long agentId = 1455808586L;
 
    @Override
    public OapiDingTalkAccessToken getAccessToken() throws Exception {
        DingTalkClient clientToken = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
        OapiGettokenRequest request = new OapiGettokenRequest();
        request.setAppkey(appkey);
        request.setAppsecret(appsecret);
        request.setHttpMethod("GET");
        OapiGettokenResponse response = clientToken.execute(request);
        JSONObject jsonObjectToken = JSONObject.parseObject(response.getBody());
        OapiDingTalkAccessToken oapiDingTalkAccessToken = new OapiDingTalkAccessToken();
        oapiDingTalkAccessToken.setErrcode(jsonObjectToken.getInteger("errcode"));
        oapiDingTalkAccessToken.setErrmsg(jsonObjectToken.getString("errmsg"));
        oapiDingTalkAccessToken.setAccessToken(jsonObjectToken.getString("access_token"));
        oapiDingTalkAccessToken.setExpiresIn(jsonObjectToken.getInteger("expires_in"));
        return oapiDingTalkAccessToken;
    }
 
    @Override
    public List<OapiDingTalkRecordResult> getAttendanceList(List<OapiDingTalkRecordResult> oapiDingTalkRecordResultList, String access_token, String workDate, List<String> userIdList, Long offset, Long limit) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/attendance/list");
        OapiAttendanceListRequest request = new OapiAttendanceListRequest();
        request.setWorkDateFrom(workDate);
        request.setWorkDateTo(workDate);
        request.setUserIdList(userIdList);
        request.setOffset(offset);
        request.setLimit(limit);
        OapiAttendanceListResponse rsp = client.execute(request, access_token);
        JSONObject jsonObject = JSONObject.parseObject(rsp.getBody());
        if (0 == jsonObject.getInteger("errcode")) {
            JSONArray recordresult = jsonObject.getJSONArray("recordresult");
            for (int i = 0; i < recordresult.size(); i++) {
                OapiDingTalkRecordResult oapiDingTalkRecordResult = new OapiDingTalkRecordResult();
                JSONObject result = recordresult.getJSONObject(i);
                oapiDingTalkRecordResult.setId(result.getInteger("id"));
                oapiDingTalkRecordResult.setSourceType(result.getString("sourceType"));
                oapiDingTalkRecordResult.setBaseCheckTime((Timestamp) result.getTimestamp("baseCheckTime"));
                oapiDingTalkRecordResult.setUserCheckTime((Timestamp) result.getTimestamp("userCheckTime"));
                oapiDingTalkRecordResult.setProcInstId(result.getString("procInstId"));
                oapiDingTalkRecordResult.setApproveId(result.getInteger("approveId"));
                oapiDingTalkRecordResult.setLocationResult(result.getString("locationResult"));
                oapiDingTalkRecordResult.setTimeResult(result.getString("timeResult"));
                oapiDingTalkRecordResult.setCheckType(result.getString("checkType"));
                oapiDingTalkRecordResult.setUserId(result.getString("userId"));
                oapiDingTalkRecordResult.setWorkDate((Timestamp) result.getTimestamp("workDate"));
                oapiDingTalkRecordResult.setRecordId(result.getInteger("recordId"));
                oapiDingTalkRecordResult.setPlanId(result.getInteger("planId"));
                oapiDingTalkRecordResult.setGroupId(result.getInteger("groupId"));
                oapiDingTalkRecordResultList.add(oapiDingTalkRecordResult);
            }
            if (jsonObject.getBoolean("hasMore") == true) {
                this.getAttendanceList(oapiDingTalkRecordResultList, access_token, workDate, userIdList, offset + 1, limit);
            } else {
                return oapiDingTalkRecordResultList;
            }
        } else if (1 == jsonObject.getInteger("errcode")) {
            this.getAttendanceList(oapiDingTalkRecordResultList, access_token, workDate, userIdList, offset + 1, limit);
        } else {
            return oapiDingTalkRecordResultList;
        }
        return oapiDingTalkRecordResultList;
    }
 
    @Override
    public void sendNotice(List<String> userIdList, String title, String content, String type) throws Exception {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(agentId);
        request.setUseridList(StringUtils.join(userIdList, ","));
        request.setToAllUser(false);
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        if ("markdown".equals(type)) {
            msg.setMsgtype("markdown");
            msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
            msg.getMarkdown().setText(content);
            msg.getMarkdown().setTitle(title);
        }
        if ("card".equals(type)) {
            msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
            msg.getActionCard().setTitle(title);
            msg.getActionCard().setMarkdown(content);
            msg.getActionCard().setSingleTitle("详情");
            msg.getActionCard().setSingleUrl("http://pms-upgrade.yhzhg.cn/");
            msg.setMsgtype("action_card");
        }
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, getAccessToken().getAccessToken());
//        System.out.println(rsp.getBody());
    }
 
 
}