项目与飞书数据交互,操作。

This commit is contained in:
wangzhijiang
2022-08-10 22:49:49 +08:00
parent 9f534abba0
commit 4e570ea217
@@ -1,15 +1,29 @@
package com.jero.modules.feishu.service.impl;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jero.modules.feishu.service.IFeishuService;
import com.jero.modules.feishu.vo.FeishuMsgVo;
import com.jero.modules.problemKnowledgeBase.entity.ProblemKnowledgeBaseClassifyEO;
import com.jero.modules.problemKnowledgeBase.entity.ProblemKnowledgeBaseEO;
import com.jero.modules.problemKnowledgeBase.entity.ProblemKnowledgeBaseUserEO;
import com.jero.modules.problemKnowledgeBase.enums.ShowPermissionsEnum;
import com.jero.modules.problemKnowledgeBase.service.IProblemKnowledgeBaseClassifyEOService;
import com.jero.modules.problemKnowledgeBase.service.IProblemKnowledgeBaseEOService;
import com.jero.modules.problemKnowledgeBase.service.IProblemKnowledgeBaseUserEOService;
import com.jero.modules.project.enums.JumpLinkEnum;
import com.jero.modules.system.entity.SysUser;
import com.jero.modules.system.service.ISysUserService;
import com.jero.modules.system.util.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@@ -18,6 +32,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
* @Author: liyawei
@@ -42,6 +57,17 @@ public class FeishuServiceImpl implements IFeishuService {
@Value("${Feishu.translateTextUrl}")
private String translateTextUrl;
@Value(value = "${jero.backUrl}")
private String backUrl;
@Autowired
private IProblemKnowledgeBaseClassifyEOService problemKnowledgeBaseClassifyEOService;
@Autowired
private IProblemKnowledgeBaseEOService problemKnowledgeBaseEOService;
@Autowired
private IProblemKnowledgeBaseUserEOService problemKnowledgeBaseUserEOService;
@Autowired
private ISysUserService sysUserService;
/**
* 获取token
* @return
@@ -333,7 +359,8 @@ public class FeishuServiceImpl implements IFeishuService {
" \"title\": {\n" +
" \"tag\": \"plain_text\",\n" +
" \"content\": \"点击下方的分类查看常见问题\"\n" +
" }\n" +
" },\n" +
" \"template\": \"green\"\n" +
" }\n" +
"}";
JSONObject jsonObject = JSONObject.parseObject(sendJson);
@@ -342,56 +369,28 @@ public class FeishuServiceImpl implements IFeishuService {
element.put("tag","action");
element.put("layout","bisected");
JSONArray actions = new JSONArray();
//组装推送过去的点击按钮
for (int i=0; i<5; i++){
//获取分类
List<ProblemKnowledgeBaseClassifyEO> problemKnowledgeBaseClassifyEOList = this.problemKnowledgeBaseClassifyEOService.list();
JSONArray actions = new JSONArray();
//组装推送过去的点击按钮
for (ProblemKnowledgeBaseClassifyEO problemKnowledgeBaseClassifyEO : problemKnowledgeBaseClassifyEOList) {
JSONObject buttonObject = new JSONObject();
JSONObject text = new JSONObject();
JSONObject value = new JSONObject();
buttonObject.put("tag","button");
if(i==0){
text.put("tag","plain_text");
text.put("content","content1");
text.put("tag","plain_text");
text.put("content",problemKnowledgeBaseClassifyEO.getProblemLabel());
value.put("chosen",problemKnowledgeBaseClassifyEO.getId());
value.put("chosen","chosen1");
}
if(i==1){
text.put("tag","plain_text");
text.put("content","content2");
value.put("chosen","chosen2");
}
if(i==2){
text.put("tag","plain_text");
text.put("content","content3");
value.put("chosen","chosen3");
}
if(i==3){
text.put("tag","plain_text");
text.put("content","content4");
value.put("chosen","chosen4");
}
if(i==4){
text.put("tag","plain_text");
text.put("content","content5");
value.put("chosen","chosen5");
}
if(i==5){
text.put("tag","plain_text");
text.put("content","content6");
value.put("chosen","chosen6");
}
buttonObject.put("text",text);
buttonObject.put("type","primary");
buttonObject.put("value",value);
actions.add(buttonObject);
}
element.put("actions",actions);
@@ -423,12 +422,94 @@ public class FeishuServiceImpl implements IFeishuService {
String clickStr = JSONObject.toJSONString(JSONObject.parseObject(JSONObject.toJSONString(jsonObject.get("action"))).get("value"));
log.info("用户点击的数据: " + clickStr);
String sendJson = " ";
String sendJsonStr = "{\n" +
" \"config\": {\n" +
" \"wide_screen_mode\": true\n" +
" },\n" +
" \"header\": {\n" +
" \"title\": {\n" +
" \"tag\": \"plain_text\",\n" +
" \"content\": \"问题知识库信息,点击下方标题即可跳转至详情页查看\"\n" +
" },\n" +
" \"template\": \"green\"\n" +
" }\n" +
"}";
JSONObject sendJson = JSONObject.parseObject(sendJsonStr);
JSONArray elements = new JSONArray();
//分隔行对象。
JSONObject dividingLineJson = new JSONObject();
dividingLineJson.put("tag","hr");
JSONObject clickJson = JSONObject.parseObject(clickStr);
if(StringUtils.isNotEmpty(clickJson.getString("chosen"))){
sendJson = clickJson.getString("chosen");
String problemTypeId = clickJson.getString("chosen");
String thirdId = userIds[0];
SysUser currentOperatorUser = this.sysUserService.getBaseMapper().selectOne(
new QueryWrapper<SysUser>().lambda().eq(SysUser::getThirdId, thirdId)
);
if(ObjectUtils.isNotEmpty(currentOperatorUser)){
QueryWrapper<ProblemKnowledgeBaseUserEO> userPermissionQueryWrapper = new QueryWrapper<>();
userPermissionQueryWrapper.lambda().eq(ProblemKnowledgeBaseUserEO::getUserId,currentOperatorUser.getId());
List<ProblemKnowledgeBaseUserEO> problemKnowledgeBaseUserEOList = this.problemKnowledgeBaseUserEOService.list(userPermissionQueryWrapper);
List<String> problemKnowledgeBaseIdList = problemKnowledgeBaseUserEOList.stream().map(ProblemKnowledgeBaseUserEO::getProblemKnowledgeBaseId).distinct().collect(Collectors.toList());
QueryWrapper<ProblemKnowledgeBaseEO> problemKnowledgeBaseEOQueryWrapper = new QueryWrapper<>();
problemKnowledgeBaseEOQueryWrapper.lambda().eq(ProblemKnowledgeBaseEO::getProblemType,problemTypeId);
problemKnowledgeBaseEOQueryWrapper.lambda().and(query -> {
query.eq(ProblemKnowledgeBaseEO::getShowPermissions, ShowPermissionsEnum.OPEN.getValue()).
or(o -> {
o.eq(ProblemKnowledgeBaseEO::getShowPermissions,ShowPermissionsEnum.PRIVACY.getValue());
o.in(ProblemKnowledgeBaseEO::getId,problemKnowledgeBaseIdList);
});
});
//根据发布日期倒序 查询出(公开、私密且自己是权限用户)最新的五条数据
problemKnowledgeBaseEOQueryWrapper.orderByDesc("release_time");
Integer pageNo = 1;
Integer pageSize = 5;
Page<ProblemKnowledgeBaseEO> page = new Page<ProblemKnowledgeBaseEO>(pageNo, pageSize);
IPage<ProblemKnowledgeBaseEO> pageList = problemKnowledgeBaseEOService.page(page, problemKnowledgeBaseEOQueryWrapper);
List<ProblemKnowledgeBaseEO> records = pageList.getRecords();
JSONObject element = new JSONObject();
JSONArray actions = new JSONArray();
for (ProblemKnowledgeBaseEO record : records) {
String url = backUrl + JumpLinkEnum.PROBLEM_KNOWLEDGE_BASE_DETAIL.getLink() + "?id=" + record.getId();
JSONObject action = new JSONObject();
JSONObject text = new JSONObject();
action.put("tag","button");
text.put("tag","plain_text");
text.put("content",record.getTitle());
action.put("text",text);
action.put("type","primary");
action.put("url",url);
actions.add(action);
}
element.put("tag","action");
element.put("actions",actions);
elements.add(element);
//添加一个分隔线。
elements.add(dividingLineJson);
}
}
JSONObject textFinally = new JSONObject();
textFinally.put("tag","lark_md");
textFinally.put("content","[以上都不是>>]("+backUrl + JumpLinkEnum.PROBLEM_KNOWLEDGE_BASE.getLink()+")");
JSONObject elementFinally = new JSONObject();
elementFinally.put("tag","div");
elementFinally.put("text",textFinally);
elements.add(elementFinally);
sendJson.put("elements",elements);
String token = null;
try {
@@ -440,30 +521,13 @@ public class FeishuServiceImpl implements IFeishuService {
Map<String, String> headerMap = new HashMap<>();
headerMap.put("Authorization", "Bearer " + token);
headerMap.put("Content-Type", "application/json; charset=utf-8");
// 设置消息标题
JSONObject zh_cn = new JSONObject();
zh_cn.put("title", "title");
// 设置消息内容
// 第一行
JSONObject contentOne = new JSONObject();
contentOne.put("tag", "text");
contentOne.put("text", sendJson);
List<JSONObject> contentList = new ArrayList<>();
contentList.add(contentOne);
List<List<JSONObject>> list = new ArrayList<>();
list.add(contentList);
zh_cn.put("content", list);
JSONObject post = new JSONObject();
post.put("zh_cn", zh_cn);
JSONObject contentObj = new JSONObject();
contentObj.put("post", post);
// 设置请求参数
JSONObject paramsMap = new JSONObject();
paramsMap.put("user_ids", userIds);
paramsMap.put("msg_type", "post");
paramsMap.put("content", contentObj);
paramsMap.put("msg_type", "interactive");
paramsMap.put("card", sendJson);
// 发送请求给飞书
String response = null;
try {
@@ -471,7 +535,6 @@ public class FeishuServiceImpl implements IFeishuService {
} catch (IOException e) {
e.printStackTrace();
}
// 返回结果样例:{"code":0,"data":{"invalid_department_ids":[],"invalid_open_ids":[],"invalid_union_ids":[],"invalid_user_ids":[],"message_id":"bm-d595567e4f448fafe397bc916f0be646"},"msg":"ok"}
// 获取返回结果
JSONObject tokenJson = JSONObject.parseObject(response);
if(!tokenJson.get("code").toString().equals("0")) {