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 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 sendScheduleMessage(@RequestBody SentMessageForm sentMessageForm) { return sendFlowMsgUtil.sendScheduleMessage(sentMessageForm); } }