丽知:对接钉钉

This commit is contained in:
zhengyf 2024-10-26 15:20:32 +08:00
parent f5a880243b
commit 6bf73a3c9d
2 changed files with 100 additions and 17 deletions

View File

@ -1,16 +1,29 @@
package com.hzya.frame.plugin.lets.dingtalk.push.serivce.impl;
import cn.hutool.core.lang.Assert;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponseBody;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.hzya.frame.plugin.lets.dingtalk.push.serivce.PushU8CService;
import com.hzya.frame.plugin.lets.dingtalk.utils.DingTalkUtils;
import com.hzya.frame.plugin.lets.dingtalk.utils.FileUtil;
import com.hzya.frame.plugin.lets.dingtalk.vo.DjFileVO;
import com.hzya.frame.plugin.lets.dingtalk.vo.DjzbVO;
import com.hzya.frame.plugin.lets.dingtalk.vo.FileModuleVO;
import com.hzya.frame.plugin.lets.dingtalk.vo.SysFileVO;
import com.hzya.frame.plugin.lets.plugin.adjust.AdjustInPluginInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
@ -22,16 +35,57 @@ import java.util.List;
@Service("PushU8CService")
public class PushU8CServiceImpl implements PushU8CService {
@Value("${DING.CorpId}")
private String CorpId;
@Value("${DING.APIToken}")
private String APIToken;
@Value("${DING.APPKEY}")
private String APPKEY;
@Value("${DING.AppSecret}")
private String AppSecret;
@Value("${DING.SPACEID}")
private String SPACEID;
@Value("${DING.APPROVE_CODE.SPECIAL_SERVICE_PROCESSING}")
private String SPECIAL_SERVICE_PROCESSING;
@Value("${DING.APPROVE_FILE_SPACE.SPECIAL_SERVICE_PROCESSING}")
private String SPECIAL_SERVICE_PROCESSING_SPACE;
@Value("${DING.APPROVE_CODE.PURCHASE_PAYMENT_REQUEST_NEW}")
private String PURCHASE_PAYMENT_REQUEST_NEW;
@Value("${DING.APPROVE_FILE_SPACE.PURCHASE_PAYMENT_REQUEST_NEW}")
private String PURCHASE_PAYMENT_REQUEST_NEW_SPACE;
@Value("${DING.APPROVE_CODE.PURCHASE_PAYMENT_REQUEST_SHOP}")
private String PURCHASE_PAYMENT_REQUEST_SHOP;
@Value("${DING.APPROVE_FILE_SPACE.PURCHASE_PAYMENT_REQUEST_SHOP}")
private String PURCHASE_PAYMENT_REQUEST_SHOP_SPACE;
Logger logger = LoggerFactory.getLogger(PushU8CServiceImpl.class);
/** 委外采购(付款单)->采购付款申请(新) */
@Override
public String outSource(DjFileVO djFileVO) {
//校验参数
checkParameter(djFileVO);
System.out.println(djFileVO);
try{
//
//获取钉钉参数
GetAccessTokenResponseBody tokenBody = DingTalkUtils.getTokenBody(APPKEY, AppSecret);
//制单人的钉钉unionid通过手机号获取钉钉的userid在获取unionid 13783530043
OapiV2UserGetbymobileResponse userByMobile = DingTalkUtils.getUserByMobile(tokenBody.getAccessToken(), "13783530043");
String userid = userByMobile.getResult().getUserid();
OapiV2UserGetResponse userById = DingTalkUtils.getUserById(tokenBody.getAccessToken(), userid);
String unionid = userById.getResult().getUnionid();
//base转文件流-上传钉盘-拿到钉盘信息可能是List集合
List<FileModuleVO> fileModuleVOList = baseTransformFile2uploadDing(djFileVO.getSysFileVOS(),tokenBody.getAccessToken(),unionid,SPECIAL_SERVICE_PROCESSING_SPACE,SPECIAL_SERVICE_PROCESSING_SPACE);
System.out.println(fileModuleVOList);
}catch (Exception e){
e.printStackTrace();
@ -67,15 +121,41 @@ public class PushU8CServiceImpl implements PushU8CService {
/**
* 文件转换
* 将base64转为文件上传钉盘并返回钉钉的 List<FileModuleVO>
* @return
* 将base64转为文件流上传钉盘并返回钉钉的 List<FileModuleVO>
* @Param fileVOS 文件集合
* @Param token token
* @Param unionid unionid
* @Param spaceId 空间信息
* @Param parentSpaceId 父级空间id
*/
public List<File> baseTransformFile(List<SysFileVO> fileBase64s){
List<File> fileList=new ArrayList<>();
// for (String fileBase64 : fileBase64s) {
// // 解码Base64字符串
// byte[] decodedBytes = Base64.getDecoder().decode(fileBase64);
// }
return fileList;
public List<FileModuleVO> baseTransformFile2uploadDing(List<SysFileVO> fileVOS,String token,String unionid,String spaceId,String parentSpaceId) throws IOException {
List<FileModuleVO> fileModuleVOList=new ArrayList<>();
for (SysFileVO fileVO : fileVOS) {
// 指定输出文件路径
Path outputPath = Paths.get(fileVO.getFileName()+"."+fileVO.getType());
// 1. 解码Base64字符串并写入文件
byte[] decodedBytes = Base64.getDecoder().decode(fileVO.getBaseCode());
Files.write(outputPath, decodedBytes);
logger.info("文件已成功创建: " + outputPath);
// 2. 将文件内容读取到ByteArrayOutputStream
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try (InputStream inputStream = Files.newInputStream(outputPath)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, bytesRead);
}
}catch(Exception e){
e.printStackTrace();
}
// 3. 打印ByteArrayOutputStream的内容可选
byte[] fileContent = byteArrayOutputStream.toByteArray();
// 4. 文件流上传钉盘
FileModuleVO dingDileModuleVO = FileUtil.get4DingDileModuleVO(token, unionid, spaceId, fileVO, byteArrayOutputStream, parentSpaceId);
fileModuleVOList.add(dingDileModuleVO);
}
return fileModuleVOList;
}
}

View File

@ -11,6 +11,7 @@ import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
@ -19,6 +20,7 @@ import java.net.URL;
import java.util.List;
import java.util.Map;
@Component
public class FileUtil {
protected static Logger logger = LogManager.getLogger(FileUtil.class);
@ -32,23 +34,24 @@ public class FileUtil {
* @param dingFileUrlID 钉盘审批实例中附件地址
* @param fileVO 的文件信息
* @param stream 文件读取的字节流
* @param parentSpaceId 父空间id
* @return
*/
public static FileModuleVO get4DingDileModuleVO(String accessToken, String unionid, String dingFileUrlID, SysFileVO fileVO, ByteArrayOutputStream stream){
public static FileModuleVO get4DingDileModuleVO(String accessToken, String unionid, String dingFileUrlID, SysFileVO fileVO, ByteArrayOutputStream stream,String parentSpaceId){
FileModuleVO fileModuleVO = new FileModuleVO();
try {
//钉钉--上传附件
//1.调用服务端API-获取审批钉盘空间信息接口获取审批空间spaceId
System.out.println(spaceId);
System.out.println(dingFileUrlID);
//2.调用服务端API-获取文件上传信息接口获取审批空间文件上传信息并执行上传
/////////////////////////////////////////////////////////////////////////////////////////////////此处很容易调权限钉钉问题添加权限
AddPermissionResponse addPermissionResponse = DingTalkUtils.addFileStorageByUnionId(accessToken, spaceId, unionid);
AddPermissionResponse addPermissionResponse = DingTalkUtils.addFileStorageByUnionId(accessToken, parentSpaceId, unionid);
if(addPermissionResponse.getBody().getSuccess()!=true){
Assert.state(false,"钉钉添加审批实例附件到钉盘权限失败");
}
/////////////////////////////////////////////////////////////////////////////////////////////////此处很容易调权限钉钉问题添加权限
//2.1调用本接口获取上传文件需要的resourceUrls和headers参数值
GetFileUploadInfoResponse headerSignature = DingTalkUtils.getFileUploadInfo(accessToken, spaceId, unionid, "HEADER_SIGNATURE", false);
GetFileUploadInfoResponse headerSignature = DingTalkUtils.getFileUploadInfo(accessToken, parentSpaceId, unionid, "HEADER_SIGNATURE", false);
String uploadKey = headerSignature.getBody().getUploadKey();
List<String> resourceUrls = headerSignature.getBody().getHeaderSignatureInfo().getResourceUrls();
if (resourceUrls.size() > 1) {
@ -88,13 +91,13 @@ public class FileUtil {
commitFileRequestOptionAppProperties.setVisibility("PUBLIC");//属性可见性 - PUBLIC所有应用都可见 - PRIVATE仅限当前应用可见
//3.提交文件
CommitFileResponse commitFileResponse = DingTalkUtils.submitFile(accessToken, spaceId, unionid, uploadKey, fileVO.getFileName(), dingFileUrlID, commitFileRequestOptionAppProperties);
CommitFileResponse commitFileResponse = DingTalkUtils.submitFile(accessToken, parentSpaceId, unionid, uploadKey, fileVO.getFileName(), parentSpaceId, commitFileRequestOptionAppProperties);
//钉盘文件唯一id
String dingFileUniqueId = commitFileResponse.getBody().getDentry().getId();
//4.组装钉钉文件控件-->"[{\"spaceId\": \"163xxxx658\", \"fileName\": \"2644.JPG\", \"fileSize\": \"333\", \"fileType\": \"jpg\", \"fileId\": \"643xxxx140\"}]"
fileModuleVO.setSpaceId(spaceId);
fileModuleVO.setSpaceId(parentSpaceId);
fileModuleVO.setFileName(fileVO.getFileName());
fileModuleVO.setFileSize(String.valueOf(fileVO.getFileSize()));
String s = StringUtils.substringAfter(fileVO.getFileName(), ".");