liusheng
2024-03-14 a3bdf2f96a06c1551bdf17c014b27f7fe527a8ab
ruoyi-project/src/main/java/com/ruoyi/project/service/impl/DingTalkServiceImpl.java
@@ -16,11 +16,14 @@
import com.ruoyi.project.domain.vo.DingTalkReqVo;
import com.ruoyi.project.service.DingTalkService;
import com.taobao.api.ApiException;
import lombok.Data;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -41,6 +44,9 @@
    @Value("${dingAppSecret}")
    private String dingAppSecret;
    @Value("${agentId}")
    private Long agentId;
    @Override
    public Boolean sendNotification(DingTalkReqVo dingTalkReqVo) {
@@ -84,11 +90,13 @@
            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);
            userIdlist = JSONObject.parseObject(str, List.class);
            if (jsonObject != null) {
                // 提取出JSONArray
                JSONArray jsonArray = new JSONArray(jsonObject.getJSONObject("result").getJSONArray("userid_list"));
                // 将JSONArray转为List列表
                String str = JSONObject.toJSONString(jsonArray);
                userIdlist = JSONObject.parseObject(str, List.class);
            }
        }
@@ -97,18 +105,27 @@
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
            request.setUseridList(urid);
            request.setAgentId(1779052814L);
            request.setAgentId(agentId);
            request.setToAllUser(false);
            //发送内容处理
            List<ConcurrentHashMap<String, String>> contents = dingTalkReqVo.getContents();
            List<ConcurrentHashMap<String, Object>> contents = dingTalkReqVo.getContents();
            ArrayList<OapiMessageCorpconversationAsyncsendV2Request.Form> objects = new ArrayList<>();
            for (int i = 0; i < contents.size(); i++) {
                ConcurrentHashMap<String, String> map = contents.get(i);
                for (Map.Entry<String, String> entry : map.entrySet()) {
                ConcurrentHashMap<String, Object> map = contents.get(i);
                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    OapiMessageCorpconversationAsyncsendV2Request.Form formSeverityDesc = new OapiMessageCorpconversationAsyncsendV2Request.Form();
                    formSeverityDesc.setKey(entry.getKey());
                    formSeverityDesc.setValue(entry.getValue());
                    if (entry.getKey().contains("审批时间")) {
                        Date date = (Date) entry.getValue();
                        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                        String format = simpleDateFormat.format(date);
                        formSeverityDesc.setValue(format);
                    } else {
                        if (entry.getValue() != null) {
                            formSeverityDesc.setValue(entry.getValue().toString());
                        }
                    }
                    objects.add(formSeverityDesc);
                }
            }
@@ -132,7 +149,7 @@
            try {
                response = client.execute(request, accessToken);
            } catch (ApiException e) {
                e.printStackTrace();
                e.getErrMsg();
            }
            //  log.info("消息任务ID"+response.getTaskId());