ny
23 小时以前 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package jnpf.message.controller;
 
import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.*;
import jnpf.base.model.synthirdinfo.DingTalkModel;
import jnpf.base.model.synthirdinfo.QyWebChatModel;
import jnpf.config.ConfigValueUtil;
import jnpf.constant.MsgCode;
import jnpf.message.SentMessageApi;
import jnpf.message.model.SentMessageForm;
import jnpf.message.service.MessageService;
import jnpf.message.service.SendConfigTemplateService;
import jnpf.message.service.SendMessageConfigService;
import jnpf.message.util.OnlineUserProvider;
import jnpf.message.util.SendFlowMsgUtil;
import jnpf.util.NoDataSourceBind;
import jnpf.util.StringUtil;
import jnpf.util.third.DingTalkUtil;
import jnpf.util.third.QyWebChatUtil;
import jnpf.util.wxutil.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.web.bind.annotation.*;
 
import jakarta.validation.Valid;
 
import java.util.*;
 
/**
 * 发送消息模型
 *
 * @版本: V3.1.0
 * @版权: 引迈信息技术有限公司(https://www.jnpfsoft.com)
 * @作者: JNPF开发平台组
 * @日期: 2021/4/21 10:28
 */
@Tag(name = "发送消息", description = "MessageAll")
@RestController
@RequestMapping("/MessageAll")
@Slf4j
public class SentMessageController implements SentMessageApi {
 
 
    @Autowired
    private MessageTemplateApi messageTemplateApi;
 
    @Autowired
    private StreamBridge streamBridge;
    @Autowired
    private MessageService messageService;
    @Autowired
    private SendFlowMsgUtil sendFlowMsgUtil;
    @Autowired
    private SendMessageConfigService sendMessageConfigService;
 
    @Autowired
    private SendConfigTemplateService sendConfigTemplateService;
    @Autowired
    private ConfigValueUtil configValueUtil;
 
 
    /**
     * @param sentMessageForm
     * @return
     */
    @Override
    @PostMapping
    public void sendMessage(@RequestBody SentMessageForm sentMessageForm) {
        sendFlowMsgUtil.sendMessage(sentMessageForm);
    }
 
    //=====================================测试企业微信、钉钉的连接=====================================
 
    /**
     * 测试企业微信配置的连接功能
     *
     * @param qyWebChatModel 企业微信配置模型
     * @return
     */
    @Operation(summary = "测试企业微信配置的连接")
    @Parameters({
            @Parameter(name = "qyWebChatModel", description = "企业微信配置模型", required = true)
    })
    @PostMapping("/testQyWebChatConnect")
    public ActionResult testQyWebChatConnect(@RequestBody @Valid QyWebChatModel qyWebChatModel) {
        // 测试发送消息、组织同步的连接
        String corpId = qyWebChatModel.getQyhCorpId();
        String agentSecret = qyWebChatModel.getQyhAgentSecret();
        String corpSecret = qyWebChatModel.getQyhCorpSecret();
        // 测试发送消息的连接
        JSONObject retMsg = QyWebChatUtil.getAccessToken(corpId, agentSecret);
        if (HttpUtil.isWxError(retMsg)) {
            return ActionResult.fail(MsgCode.SYS031.get(retMsg.getString("errmsg")));
        }
 
        // 测试发送组织同步的连接
        retMsg = QyWebChatUtil.getAccessToken(corpId, corpSecret);
        if (HttpUtil.isWxError(retMsg)) {
            return ActionResult.fail(MsgCode.SYS033.get(retMsg.getString("errmsg")));
        }
 
        return ActionResult.success(MsgCode.SYS037.get());
    }
 
 
    /**
     * 测试钉钉配置的连接功能
     *
     * @param dingTalkModel 钉钉配置模型
     * @return
     */
    @Operation(summary = "测试钉钉配置的连接")
    @Parameters({
            @Parameter(name = "dingTalkModel", description = "钉钉配置模型", required = true)
    })
    @PostMapping("/testDingTalkConnect")
    public ActionResult testDingTalkConnect(@RequestBody @Valid DingTalkModel dingTalkModel) {
        // 测试钉钉配置的连接
        String appKey = dingTalkModel.getDingSynAppKey();
        String appSecret = dingTalkModel.getDingSynAppSecret();
        String agentId = dingTalkModel.getDingAgentId();
        // 测试钉钉的连接
        JSONObject retMsg = DingTalkUtil.getAccessToken(appKey, appSecret);
        if (!retMsg.getBoolean("code")) {
            return ActionResult.fail(MsgCode.SYS036.get(retMsg.getString("error")));
        }
 
        return ActionResult.success(MsgCode.SYS037.get());
    }
 
    /**
     * 发送消息
     *
     * @param toUserIds 发送用户
     * @param title     标题
     * @param bodyText  内容
     */
    @Override
    @PostMapping("/sentMessage")
    public void sentMessage(@RequestParam("toUserIds") List<String> toUserIds, @RequestParam("title") String title, @RequestParam(value = "bodyText", required = false) String bodyText, @RequestParam("source") Integer source, @RequestParam("type") Integer type, @RequestBody UserInfo userInfo) {
        messageService.sentMessage(toUserIds, title, bodyText, userInfo, source, type, false);
    }
 
    /**
     * 退出在线的WebSocket
     *
     * @param token  token
     * @param userId 用户id
     */
    @Operation(summary = "退出在线的WebSocket")
    @Parameters({
            @Parameter(name = "token", description = "token"),
            @Parameter(name = "userId", description = "用户id")
    })
    @NoDataSourceBind
    @GetMapping("/logoutWS")
    public void logoutWebsocketByToken(@RequestParam(name = "token", required = false) String token, @RequestParam(name = "userId", required = false) String userId) {
        if (StringUtil.isNotEmpty(token)) {
            OnlineUserProvider.removeWebSocketByToken(token.split(","));
        } else {
            OnlineUserProvider.removeWebSocketByUser(userId);
        }
    }
 
    @Override
    @PostMapping("/sendScheduleMessage")
    public List<String> sendScheduleMessage(@RequestBody SentMessageForm sentMessageForm) {
        return sendFlowMsgUtil.sendScheduleMessage(sentMessageForm);
    }
 
}