Commit 355307d8 by hzc

集成企业微信消息通知

parent 8a7fa600
......@@ -74,7 +74,10 @@ public class NoticeInfoCtrl {
@ApiOperation("创建消息")
@PostMapping("createNotice")
public Result createNotice(@RequestBody NoticeInfo noticeInfo){
noticeInfoService.insertNotice(noticeInfo);
List<NoticeInfo> noticeInfos = new ArrayList<>();
noticeInfos.add(noticeInfo);
//noticeInfoService.insertNotice(noticeInfo);
noticeInfoService.createNotices(noticeInfos);
return Result.genOkResult();
}
@ApiOperation("批量创建消息(异步)")
......
......@@ -21,6 +21,9 @@ public class RoleRefUserModel {
@ApiModelProperty("在此角色里面对应的指标归属部门")
private String refIndDept;
@ApiModelProperty("登录账号")
private String userId;
@Override
public boolean equals(Object o) {
if (this == o) {
......
package com.keymobile.indicators.service;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
......@@ -73,5 +74,11 @@ public interface SystemAuthService {
*/
@GetMapping("/smartBi/clearAllPools")
void clearAllPools();
/**
* 根据用户ID获取登录账号
*
* @return 登录账号用|分隔
*/
@GetMapping("/user/getUserById")
String getUserByIds(@RequestParam List<String> userIds) ;
}
......@@ -739,6 +739,7 @@ public class TaskServiceImpl implements TaskService {
user.setId(jo.getString("id"));
user.setDisName(jo.getString("disname"));
user.setRefIndDept(jo.getString("refIndDept"));
user.setUserId(jo.getString("userId"));
String refIndDept = user.getRefIndDept();
if (StringUtils.isBlank(refIndDept)) {
continue;
......
package com.keymobile.indicators.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.keymobile.indicators.constant.Constants;
import com.keymobile.indicators.model.entity.NoticeInfo;
import com.keymobile.indicators.model.mapper.indicators.NoticeInfoMapper;
import com.keymobile.indicators.service.NoticeInfoService;
import com.keymobile.indicators.service.SystemAuthService;
import com.keymobile.indicators.utils.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Slf4j
@Service
public class NoticeInfoServiceImpl implements NoticeInfoService {
@Autowired
private NoticeInfoMapper noticeInfoMapper;
@Value("${weChart.url}")
private String weChart_url;
@Value("${weChart.corpID}")
private String weChart_corpID;
@Value("${weChart.secret}")
private String weChart_secret;
@Value("${weChart.agentId}")
private String weChart_agentId;
@Value("${weChart.isSend}")
private Boolean isSend;
@Autowired
private SystemAuthService systemAuthService;
@Override
public void insertNotice(NoticeInfo noticeInfo) {
......@@ -26,6 +49,50 @@ public class NoticeInfoServiceImpl implements NoticeInfoService {
@Override
@Async
public void createNotices(List<NoticeInfo> notices) {
//
if(BooleanUtils.isTrue(isSend)){
log.info("准备发送湘烟通消息");
try {
String weChartToken = getWeChartToken();
if(weChartToken!=null){
//获取用户id,用于查登录账号
List<String> users = new ArrayList<>();
notices.stream().forEach(item->{
users.add(item.getToUserId());
});
String weChartContent = getWeChartContent(notices.get(0));
Map<String,String> textMap = new HashMap<>();
textMap.put("content",weChartContent);
JSONObject jsonObject = new JSONObject();
//获取用户--已经封装好格式-userid|userId
String toUsers =systemAuthService.getUserByIds(users);// users.stream().collect(Collectors.joining("|"));
jsonObject.put("touser",toUsers);
jsonObject.put("msgtype","text");
jsonObject.put("agentid",weChart_agentId);
jsonObject.put("toparty","");
jsonObject.put("totag","");
jsonObject.put("text",textMap);
StringBuilder url = new StringBuilder(weChart_url);
url.append("cgi-bin/message/send?access_token=");
url.append(weChartToken);
log.info("开始发送消息->url:{}",url);
log.info("发送消息->param:{}",jsonObject.toString());
String resp = HttpUtil.jsonPost(url.toString(),jsonObject.toString());
JSONObject respJson = JSONObject.parseObject(resp);
Integer errcode = respJson.getInteger("errcode");
if(errcode==0){
log.info("========发送成功=========");
}else{
log.info("发送失败:获取JSON:{}",respJson);
}
}
}catch (Exception e){
log.info("发送湘烟通接口失败===");
e.printStackTrace();
}
}
log.info("开始发pc端消息。。。。");
noticeInfoMapper.batchInsert(notices);
}
......@@ -67,4 +134,29 @@ public class NoticeInfoServiceImpl implements NoticeInfoService {
params.put("noticeClass",noticeClass);
noticeInfoMapper.updateToReadByToUsersAndTaskIdAndNoticeClass(params);
}
private String getWeChartToken(){
String url=weChart_url+"cgi-bin/gettoken";
String param = "corpid="+weChart_corpID+"&corpsecret="+weChart_secret;
log.info("获取token->url:{}",url);
log.info("获取token->param:{}",param);
String s = HttpUtil.sendGet(url, param);
JSONObject jsonObject = JSONObject.parseObject(s);
String access_token = jsonObject.getString("access_token");
log.info("weChartToken:{}",access_token);
return access_token;
}
private String getWeChartContent(NoticeInfo noticeInfo){
StringBuilder content=new StringBuilder("【对标系统】");
String title = noticeInfo.getTitle();
Map<Integer,String> noticeClassMap = new HashMap<>();
noticeClassMap.put(1,"数据填报");
noticeClassMap.put(2,"数据审核");
noticeClassMap.put(3,"对标对象锁定");
String noticeClass = noticeClassMap.get(noticeInfo.getNoticeClass());
content.append(noticeClass);
content.append("<br>");
content.append(title);
return content.toString();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment