报表打印wps改造

This commit is contained in:
zhangqinbin
2024-01-09 16:34:08 +08:00
parent 63b1595825
commit 5ec3178bea
4 changed files with 302 additions and 36 deletions

View File

@ -5,50 +5,50 @@ import com.alibaba.fastjson.JSON;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.api.AttachmentClient;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentDetail;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.AttachmentEntity;
import com.chinaunicom.ebtp.mall.cloud.attachment.sdk.model.UploadObject;
import com.chinaunicom.mall.ebtp.cloud.security.starter.entity.SecurityEntity;
import com.chinaunicom.mall.ebtp.common.base.entity.BaseResponse;
import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum;
import com.chinaunicom.mall.ebtp.common.log.OperationLogDetail;
import com.chinaunicom.mall.ebtp.common.log.enums.EbtpLogBusinessModule;
import com.chinaunicom.mall.ebtp.common.log.enums.EbtpLogType;
import com.chinaunicom.mall.ebtp.extend.crypconfigure.crypto.tenderfee.test;
import com.chinaunicom.mall.ebtp.extend.export.bean.BizExportDict;
import com.chinaunicom.mall.ebtp.extend.export.service.dict.IBizExportDictService;
import com.chinaunicom.mall.ebtp.extend.feign.client.ProcessFeignService;
import com.chinaunicom.mall.ebtp.extend.feign.client.UnicomOAuthClient;
import com.chinaunicom.mall.ebtp.extend.shortmessage.utils.RSA;
import com.chinaunicom.mall.ebtp.extend.feign.entity.BizAssessRoom;
import com.chinaunicom.mall.ebtp.extend.wps.entity.KingSoftFileSaveVo;
import com.chinaunicom.mall.ebtp.extend.wps.enums.WpsExceptionEnum;
import com.chinaunicom.mall.ebtp.extend.wps.service.WpsService;
import com.chinaunicom.mall.ebtp.extend.wps.util.HttpsUtils;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsClientInVo;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsClientInVo2;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsResultVo;
import feign.Body;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.bag.SynchronizedSortedBag;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLStreamHandler;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.AUTHORIZATION_HEADER;
import static com.chinaunicom.mall.ebtp.cloud.security.starter.common.Constants.COOKIE_TOKEN_CODE;
@Slf4j
@ -68,6 +68,12 @@ public class WpsController {
private String kswpsurl;
@Value("${KingSoftWps.tempDocFileId}")
private String tempDocFileId;
@Value("${KingSoftWps.host}")
private String kswhost;
@Autowired
private ProcessFeignService processFeignService;
@Autowired
private IBizExportDictService iBizExportDictService;
/* 附件工具 */
@Autowired
@ -162,13 +168,7 @@ public class WpsController {
String r11 = referer.substring(r1.length(),referer.length());
String r2 = r11.substring(0,r11.indexOf("/")>=0?r11.indexOf("/"):r11.length());
String host = r1+r2;
// String host = request.getHeader("Host");
//
// if(host.indexOf("18022")>=0){
// host = "http://"+host;
// }else if(host.indexOf("18023")>=0){
// host = "https://"+host;
// }
log.info("getWpsUrlBy host:"+host);
String authToken = cookie.get().getValue();
@ -187,6 +187,53 @@ public class WpsController {
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("无效请求",true);
}
}
/**
* (金山)获得编辑或查看链接 报表打印使用
*
* @return
*/
@ApiOperation("(金山)获得编辑或查看链接 报表打印使用")
@GetMapping("/kingSoft/getWpsUrl2")
public void getWpsUrlBy2(HttpServletRequest request, HttpServletResponse response,
@RequestParam("key") String key,
@RequestParam("fileName") String fileName) {
//cookie传输token
Optional<Cookie> cookie = Optional.ofNullable(request.getCookies())
.flatMap(cookies -> Stream.of(cookies)
.filter(item -> StringUtils.equals(item.getName(), COOKIE_TOKEN_CODE)
).findFirst());
if (cookie.isPresent()) {
log.info(String.valueOf(request.getRequestURL()));
String host = "";
if(request.getRequestURL()!=null&&!"".equals(request.getRequestURL())) {
String referer = request.getHeader("Referer");
String r1 = referer.substring(0, referer.indexOf("//") + 2);
String r11 = referer.substring(r1.length(), referer.length());
String r2 = r11.substring(0, r11.indexOf("/") >= 0 ? r11.indexOf("/") : r11.length());
host = r1 + r2;
}else{
host = kswhost;
}
log.info("getWpsUrlBy host:"+host);
String authToken = cookie.get().getValue();
WpsClientInVo2 in = this.wpsService.getKSWpsUrlDecrypt2(key,fileName);
in.setUserToken(authToken);
in.setHost(host);
String str = this.wpsService.getKSWpsUrl2(in);
try {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println(str);
}catch (Exception e){
e.printStackTrace();
}
}else{
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("无效请求",true);
}
}
/**
* (金山)获得编辑或查看链接
*
@ -241,7 +288,7 @@ public class WpsController {
*/
@ApiOperation("(金山)沃文档回调-查询文档")
@GetMapping("/kingSoft/getSaveFile")
public boolean getKSFile2( @RequestParam("fileId") String fileId) {
public boolean getSaveFile( @RequestParam("fileId") String fileId) {
List<String> businessIdList = new ArrayList<>();
businessIdList.add(fileId);
@ -264,18 +311,7 @@ public class WpsController {
}
return buf.length>10000;
}
/**
* (金山)沃文档回调-查询文档
* 编辑 permission=write
* 预览 permission=read
* @param
* @return
*/
@ApiOperation("(金山)沃文档回调-查询文档")
@GetMapping("/kingSoft/file")
public byte[] getKSFile(@RequestParam("key") String key) {
return this.wpsService.getKSWpsFile(key);
}
/**
* (金山)沃文档回调-查询文档
* 编辑 permission=write
@ -304,8 +340,8 @@ public class WpsController {
* @return
*/
@ApiOperation("(金山)沃文档回调-查询文档")
@GetMapping("/kingSoft/file2")
public boolean getKSFile2(HttpServletRequest request,HttpServletResponse response, @RequestParam("key") String key) {
@GetMapping("/kingSoft/file")
public boolean getKSFile(HttpServletRequest request,HttpServletResponse response, @RequestParam("key") String key) {
//带后缀
String fileIdName = this.wpsService.downloadDecrypt(key);
log.info(" file2 fileIdName"+fileIdName);
@ -381,6 +417,48 @@ public class WpsController {
return true;
}
/**
* (金山)沃文档回调-查询文档 报表打印使用
* 编辑 permission=write
* 预览 permission=read
* @param
* @return
*/
@ApiOperation("(金山)沃文档回调-查询文档 报表打印使用")
@GetMapping("/kingSoft/file2")
public byte[] getKSFile2(HttpServletRequest request, HttpServletResponse response,@RequestParam("key") String key) {
String[] value = this.wpsService.downloadDecrypt2(key);
BizAssessRoom room = processFeignService.getById(value[0]).getData();
BizExportDict dict = iBizExportDictService.getById(value[1]);
String dataUrl = kswhost+"/"+dict.getUrl() + "?id=" + dict.getId() + "&projectId=" + room.getTpId() + "&sectionId=" + room.getSectionId() + "&assessRoomId=" + room.getId();
System.out.println("dataUrl:"+dataUrl);
try {
URL videoUrl = new URL(dataUrl);
HttpURLConnection connection = (HttpURLConnection)videoUrl.openConnection();
connection.setRequestProperty("Authorization", "Bearer " + value[2]);
InputStream videoStream = connection.getInputStream();
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Length", "9999");
response.setHeader("Content-Disposition", "attachment; filename=".concat(new String(dict.getDicName().getBytes(StandardCharsets.UTF_8), "ISO8859-1")));
try (InputStream inputStream = videoStream;
ServletOutputStream outputStream = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
}
}catch (Exception e){
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("报表打印使用文件下载异常",true);
}
return this.wpsService.getKSWpsFile(key);
}
/**
* 沃文档回调-保存文档
* @return

View File

@ -3,6 +3,7 @@ package com.chinaunicom.mall.ebtp.extend.wps.service;
import com.chinaunicom.mall.ebtp.extend.wps.entity.KingSoftFileSaveVo;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsClientInVo;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsClientInVo2;
import com.chinaunicom.mall.ebtp.extend.wps.vo.WpsResultVo;
import java.io.File;
@ -29,4 +30,10 @@ public interface WpsService{
String downloadDecrypt(String key);
Map<String,String> saveKSFile(KingSoftFileSaveVo vo);
WpsClientInVo2 getKSWpsUrlDecrypt2(String key, String fileName);
String[] downloadDecrypt2(String key);
public String getKSWpsUrl2(WpsClientInVo2 in);
}

View File

@ -118,6 +118,8 @@ public class WpsServiceImpl implements WpsService {
private String appName;
@Value("${KingSoftWps.downloadUrl}")
private String kswdownloadUrl;
@Value("${KingSoftWps.downloadUrl2}")
private String kswdownloadUrl2;
@Value("${KingSoftWps.publicKey}")
private String kswpublicKey;
@Value("${KingSoftWps.privateKey}")
@ -665,13 +667,139 @@ public class WpsServiceImpl implements WpsService {
if(newDateLong-inDateLong>Long.valueOf(kswTimeLimit)){//
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌已失效-",true);
}
// SecurityEntity securityEntity = unicomOAuthClient.get(token);
// log.info("securityEntity:"+securityEntity);
// CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌无效",securityEntity.getStaffId()==null||"".equals(securityEntity.getStaffId()));
return fileId;
}
//打印报表使用
@Override
public WpsClientInVo2 getKSWpsUrlDecrypt2(String key,String fileName){
String val = RSA.decrypt(key,kswprivateKey);
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("请求参数无效",val.indexOf("_")<0);
//fileId_yyyyMMddHHmmss_type_doc
String[] value = val.split("_");
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("请求参数无效",value.length<4);
String roomId = value[0];
String exportId = value[1];
String time = value[2];
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
long newDateLong = System.currentTimeMillis();
long inDateLong = 0;
try {
Date inDate = format.parse(time);
inDateLong = inDate.getTime();
}catch (Exception e){
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("请求时间戳错误",true);
}
//time_limit = "120000";
log.info("newDateLong:"+newDateLong);
log.info("inDateLong:"+inDateLong);
if(newDateLong-inDateLong>Long.valueOf(kswTimeLimit)){//
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("请求已失效-",true);
}
WpsClientInVo2 in = new WpsClientInVo2();
in.setExportId(exportId);
in.setRoomId(roomId);
in.setFileName(fileName);
in.setType("0");
in.setFileExtension("");
return in;
}
//报表打印使用
private String getDownloadEncrypt2(String roomId,String exportId,String token){
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
//fileId+ebtp+token+time md5加密
String mdKey = MdConstants.getSha256(time+roomId+exportId+appName+token);
log.info("mdKey:"+mdKey);
//fileId|token|yyyyMMddHHmmss|md5加密
String rsaKey = roomId+"_"+exportId+"_"+token+"_"+time+"_"+mdKey;
log.info("rsaKey:"+rsaKey);
//RSA加密
String key = RSA.encrypt(rsaKey.length()>117?rsaKey.substring(0,117):rsaKey,kswpublicKey);
log.info("key:"+key);
return key;
}
//报表打印使用
@Override
public String[] downloadDecrypt2(String key){
String val = RSA.decrypt(key,kswprivateKey);
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌无效",val.indexOf("_")<0);
String[] value = val.split("_");
String roomId = value[0];
String exportId = value[1];
String token = value[2];
String time = value[3];
String md = value[4];
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌无效",val.length()<4);
String o_key = MdConstants.getSha256(time+roomId+exportId+appName+token);//
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌无效",!o_key.substring(0,md.length()).equals(md));
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
long newDateLong = System.currentTimeMillis();
long inDateLong = 0;
try {
Date inDate = format.parse(time);
inDateLong = inDate.getTime();
}catch (Exception e){
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌时间戳错误",true);
}
//time_limit = "120000";
log.info("newDateLong:"+newDateLong);
log.info("inDateLong:"+inDateLong);
if(newDateLong-inDateLong>Long.valueOf(kswTimeLimit)){//
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("令牌已失效-",true);
}
return value;
}
@Override
public String getKSWpsUrl2(WpsClientInVo2 in){
String url = "";
String dataJson = "";
String callBackUrl = "";
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
try {
callBackUrl = kswdownloadUrl2+"?key="+URLEncoder.encode(getDownloadEncrypt2(in.getRoomId(),in.getExportId(),in.getUserToken()), "utf-8");
}catch (UnsupportedEncodingException e){
log.error("url转换失败",e);
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.customValidName("url获取失败", true);
}
url = kswpsurl_preview;
KingSoftWpsPreviewVo vo = new KingSoftWpsPreviewVo();
vo.setSrcId(7);
vo.setSrcWpsNgProxy(in.getHost());
vo.setStaffCode("ebtp");
vo.setFileKey(in.getExportId());
vo.setFileName(in.getFileName());
vo.setFileUrl(callBackUrl);
vo.setFileSize(9999);
vo.setFileCreator("ebtp");
vo.setFileCreateTime(time);
vo.setFileLastModifiedTime(time);
dataJson = JSON.toJSONString(vo);
String retJson = HttpUtils.httpPost(url, dataJson);//httpPost(kswpsurl,json);
log.info("ret:"+retJson);
//String retJson = HttpUtils.httpPost(kswpsurl,json);
KingSoftWpsRet ret = JSON.parseObject(retJson,KingSoftWpsRet.class);
return ret.getData();
}
private String getSaveDownloadEncrypt(String fileId){
String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
@ -786,6 +914,13 @@ public class WpsServiceImpl implements WpsService {
wpsService.kswpublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCtUm+H78uT39TOsgS5XdbIuC0BRLiktqbuI3F9VFbPWDcX15Ptb2u2yTCSOB0Gl+yI+hqpClyV9nyjnu0LgoWpsIDZSLVLdrltYKz9N8jwXKhybomT2EuYU7hzWT1Ea4fkIBfYMCl+BQYSkXD6eqObg4mq2/wUFUVX/+/vjsMShwIDAQAB";
wpsService.kswprivateKey = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAK1Sb4fvy5Pf1M6yBLld1si4LQFEuKS2pu4jcX1UVs9YNxfXk+1va7bJMJI4HQaX7Ij6GqkKXJX2fKOe7QuChamwgNlItUt2uW1grP03yPBcqHJuiZPYS5hTuHNZPURrh+QgF9gwKX4FBhKRcPp6o5uDiarb/BQVRVf/7++OwxKHAgMBAAECgYA77iY8hdEJx7qo4es6QVVUQELnjmCeK74a18yVPoVp2qHqL5Op5P4EzRprZNW8gum6D2kKjFepTvqYBQsIENCwY3a6iKQf+8kwJ/GhVP+7uT3bnxdQFNXA99ujVu7Q2nFQZYAYJWiuMOQKGWrVJperlli0Ip3htCNmtTaviVfAcQJBANTAbX210K1zWe40nSxuuc6ONBqK/YF5hkqKpWB/XzfgFgQ4tPOfvWnnp0pt9rWUV9PzO6NYr2ya60yu2Y6aE+kCQQDQjhfdIid458UOYbjAixY5ONZOcswFQi77pk6MB5Cp8aHn8gHtapDnXt5DiHhqzJCya31FYYoqGtp/4fIntRzvAkA1N2Dr5fOpv0XKcLyz5YpyoV1lGkko/sXZlW6igZnGtzWqnqicm+NcP0a4NmNUriN978SGKmPO7b1XtBDVO7MpAkA/Q1sp0Tp4CpZUDoCkp4xOP1eC5H/O+IyX+ZLKF2Fiq9GZMmgzRKhABx8VDUi53OPnu4N8kGIA87RkJn32XZIxAkAtL+skd+5wkL71VZVnZafLIjWlJM/RKxqyaLbF8irT2sq2+ae8A19hfXj3zT7CeLn5xjKP1/cuGakeOBL8cNaw";
wpsService.appName = "biz-service-ebtp-extend";
wpsService.kswTimeLimit = "20000";
String jm = wpsService.getDownloadEncrypt2("1743166333208461312","100001","9f07d66a-9b09-41ea-8d51-149b5ba677b0");
String[] values = wpsService.downloadDecrypt2(jm);
for(String value : values){
System.out.println(value);
}
//String jm = RSA.encrypt("1538803364727304192_20231219095538_0_doc_客户端测试子项目1-XY926020101-服务费通知书-杭州安恒信息技术股份有限公司",wpsService.kswpublicKey);
//System.out.println(jm);

View File

@ -0,0 +1,46 @@
package com.chinaunicom.mall.ebtp.extend.wps.vo;
import lombok.Data;
@Data
public class WpsClientInVo2 {
/**
*评审室id
*/
private String roomId;
/**
* 报表id
*/
private String exportId;
/**
* 前端文档名名称:可能是项目名、标段名、评标报告等
*/
private String fileName;
/**
*请求沃文档的url
*/
private String permission;
/**
*业务id文档所属数据的id
*/
private String id;
/**
*当前用户token
*/
private String userToken;
/**
*0 查看 9527007 编辑
*/
private String type;
/**
* 文件后缀 初次编辑需要传 doc docx xls ...
*/
private String fileExtension;
/**
* 前端 host
*/
private String host;
}