package com.product.file.controller;
|
|
import cn.hutool.core.text.UnicodeUtil;
|
import com.alibaba.fastjson.JSON;
|
import com.product.common.lang.StringUtils;
|
import com.product.core.config.CoreConst;
|
import com.product.core.config.Global;
|
import com.product.core.controller.support.AbstractBaseController;
|
import com.product.core.entity.DataTableEntity;
|
import com.product.core.entity.FieldSetEntity;
|
import com.product.core.entity.RequestParameterEntity;
|
import com.product.core.exception.BaseException;
|
import com.product.core.sign.SignUtil;
|
import com.product.core.spring.context.SpringMVCContextHolder;
|
import com.product.file.config.CmnConst;
|
import com.product.file.config.FileCode;
|
import com.product.file.service.FileManagerService;
|
import com.product.file.service.OnlineDocumentEditService;
|
import com.product.module.sys.entity.SystemUser;
|
import com.product.module.sys.version.ApiVersion;
|
import com.product.util.BaseUtil;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.ui.Model;
|
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.File;
|
import java.io.IOException;
|
import java.io.OutputStream;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* @Author cheng
|
* @Date 2022/4/6 13:57
|
* @Desc
|
*/
|
@RequestMapping("api/office")
|
@Controller
|
public class WebOfficeController extends AbstractBaseController {
|
|
@Autowired
|
FileManagerService fileManagerService;
|
|
@Autowired
|
OnlineDocumentEditService onlineDocumentEditService;
|
|
/**
|
* weboffice 获取文件
|
*
|
* @param response
|
* @param request
|
*/
|
@PostMapping("/get-nest-red-file/{version}")
|
@ApiVersion(1)
|
@ResponseBody
|
public String getNestRedTemplate(HttpServletResponse response, HttpServletRequest request) {
|
|
try {
|
//获取参数
|
FieldSetEntity fse = null;
|
Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
File file = null;
|
if (bean != null) {
|
RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
fse = reqp.getFormData();
|
file = reqp.getFiles().values().toArray(new File[1])[0];
|
}
|
onlineDocumentEditService.nestRedDocument(fse, file);
|
// byte[] document_templates = fileManagerService.getFileContent("1".equals(location) ? fse.getString("document_template") : fse.getString("document_template_tail"));
|
// IoUtil.write(response.getOutputStream(), true, document_templates);
|
return OK();
|
} catch (BaseException e) {
|
e.printStackTrace();
|
return error(e);
|
} catch (Exception e) {
|
e.printStackTrace();
|
SpringMVCContextHolder.getSystemLogger().error(e);
|
return error(FileCode.NEST_RED_DOCUMENT_FAIL);
|
}
|
}
|
|
/**
|
* weboffice 获取文件
|
*
|
* @param response
|
* @param request
|
*/
|
@GetMapping("/get-file/{version}")
|
@ApiVersion(1)
|
public void getFile(HttpServletResponse response, HttpServletRequest request) {
|
try {
|
//获取参数
|
FieldSetEntity fse = null;
|
Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
if (bean != null) {
|
RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
fse = reqp.getFormData();
|
}
|
String uuid = fse.getString("uuid");
|
onlineDocumentEditService.getFile(response, uuid);
|
// IoUtil.write(response.getOutputStream(), true, fileManagerService.getFileContent(uuid));
|
} catch (Exception e) {
|
try {
|
OutputStream outputStream = response.getOutputStream();
|
outputStream.flush();
|
outputStream.close();
|
} catch (IOException ex) {
|
ex.printStackTrace();
|
}
|
e.printStackTrace();
|
SpringMVCContextHolder.getSystemLogger().error(e);
|
}
|
}
|
|
/**
|
* weboffice 保存文件(自动、手动)
|
*
|
* @param request
|
* @return
|
*/
|
@PostMapping("/save-office/{version}")
|
@ApiVersion(1)
|
@ResponseBody
|
public String saveOffice(HttpServletRequest request) {
|
try {
|
//获取参数
|
FieldSetEntity fse = null;
|
Map<String, File> files = null;
|
Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
if (bean != null) {
|
RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
files = reqp.getFiles();
|
fse = reqp.getFormData();
|
}
|
if (fse == null || StringUtils.isEmpty(fse.getUUID()) || files == null || files.size() <= 0 || StringUtils.isEmpty(fse.getString("key"))) {
|
return error(FileCode.GET_DATA_FAIL);
|
}
|
onlineDocumentEditService.uploadFile(fse.getUUID(), files);
|
return BaseUtil.success(null);
|
} catch (BaseException e) {
|
return error(e);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return error(FileCode.OFFICE_SAVE_FILE_FAIL);
|
}
|
}
|
|
/**
|
* weboffice 跳转 mvc
|
*
|
* @param model
|
* @param request
|
* @return
|
*/
|
@GetMapping("/open-office/{version}")
|
@ApiVersion(1)
|
public String openOffice(Model model, HttpServletRequest request) {
|
|
try {
|
//获取参数
|
FieldSetEntity fse = null;
|
Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
if (bean != null) {
|
RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
fse = reqp.getFormData();
|
// System.out.println(formJson);
|
System.out.println(SignUtil.getHmacSHA1(reqp.getFormJson(), Global.getSystemConfig("signature.key", "")));
|
}
|
//文件名(窗口的名字)
|
String file_title = fse.getString("fileName");
|
FieldSetEntity fieldSetEntity = fileManagerService.getBaseDao().getFieldSetEntity("product_sys_attachments", fse.getString("uuid"), false);
|
if (!FieldSetEntity.isEmpty(fieldSetEntity)) {
|
file_title = fieldSetEntity.getString("file_name");
|
}
|
// if (file_title == null) {
|
// file_title = "";
|
// } else {
|
// file_title = UnicodeUtil.toString(file_title);
|
// }
|
SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
|
//
|
Boolean allowNestRed = false;
|
String node_uuid = fse.getString("nodeUuid");
|
String fileType = fse.getString("fileType");
|
List<Object> nestRedDocumentParams = new ArrayList<>();
|
// 判断是否为 流程节点中打开的文档 且是 word 类型
|
if (!StringUtils.isEmpty(node_uuid) && ("doc".equals(fileType) || "docx".equals(fileType) || "wps".equals(fileType) || "wpt".equals(fileType))) {
|
//获取流程节点信息
|
FieldSetEntity node = onlineDocumentEditService.getBaseDao().getFieldSetEntity("product_sys_flow_node", node_uuid, false);
|
if (!FieldSetEntity.isEmpty(node)) {
|
//允许套红
|
allowNestRed = node.getBoolean("allow_nest_red");
|
if (allowNestRed) {
|
//查询套红配置模板
|
DataTableEntity dt = onlineDocumentEditService.getBaseDao().listTable("product_sys_document_nest_red_config");
|
if (!DataTableEntity.isEmpty(dt)) {
|
for (int i = 0; i < dt.getRows(); i++) {
|
FieldSetEntity fs = dt.getFieldSetEntity(i);
|
Map<Object, Object> values = fs.getValues();
|
|
values.put("token", SpringMVCContextHolder.getCurrentUser().getToken_info());
|
values.put("~table~", "product_sys_document_nest_red_config");
|
values.put("file_uuid", fse.getUUID());
|
//将模板配置信息 转为字符串
|
HashMap<Object, Object> v = (HashMap<Object, Object>) values;
|
v = (HashMap<Object, Object>) v.clone();
|
v.remove("title");
|
v.remove("description");
|
String params = JSON.toJSONString(v);
|
// 配置信息签名 (用于套红时 GET 请求)
|
String signature = SignUtil.getHmacSHA1(params, Global.getSystemConfig("signature.key", ""));
|
values.put("signature", signature);
|
values.put("formJson", params);
|
nestRedDocumentParams.add(values);
|
}
|
}
|
}
|
}
|
}
|
model.addAttribute("nestDocument", nestRedDocumentParams);
|
model.addAttribute("allow_nest_red", allowNestRed);
|
model.addAttribute("file_title", file_title);
|
model.addAttribute(CmnConst.UUID, fse.getUUID());
|
model.addAttribute("user_name", currentUser.getUser_name());
|
//不能修改此参数,参数和签名绑定修改参数需要更新签名
|
model.addAttribute(CoreConst.API_POST_REQUEST_DATA + "Temp", request.getParameter("formData"));
|
//使用第一次传输过来的签名用于获取文件 、保存文件
|
model.addAttribute("signature", request.getParameter("signature"));
|
model.addAttribute("transportProtocols", "http");
|
model.addAttribute("port", Global.getSystemConfig("server.port", ""));
|
return "webOffice";
|
} catch (Exception e) {
|
e.printStackTrace();
|
return "404";
|
}
|
}
|
|
/**
|
* 计划编辑文档
|
*
|
* @param request
|
* @return
|
*/
|
@PostMapping("/document-edit-plan/{version}")
|
@ResponseBody
|
@ApiVersion(1)
|
public String documentEditPlan(HttpServletRequest request) {
|
try {
|
//获取参数
|
FieldSetEntity fse = null;
|
Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
if (bean != null) {
|
RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
fse = reqp.getFormData();
|
}
|
if (fse == null || StringUtils.isEmpty(fse.getUUID())) {
|
return error(FileCode.GET_DATA_FAIL);
|
}
|
/**
|
* 使用同步锁 防止并发打开相同的文件
|
*/
|
synchronized (fse.getUUID().intern()) {
|
onlineDocumentEditService.signDocumentEdit(fse.getUUID());
|
}
|
return OK();
|
} catch (BaseException e) {
|
return error(e);
|
} catch (Exception e) {
|
e.printStackTrace();
|
return error(FileCode.SIGN_DOCUMENT_EDIT_STATUS_FAIL);
|
}
|
}
|
|
// /**
|
// * 重置文档编辑时间
|
// *
|
// * @param request
|
// * @return
|
// */
|
// @PostMapping("/reset-document-edit-time/{version}")
|
// @ResponseBody
|
// @ApiVersion(1)
|
// public String resetDocumentExpirationTime(HttpServletRequest request) {
|
// try {
|
// //获取参数
|
// FieldSetEntity fse = null;
|
// Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA);
|
// if (bean != null) {
|
// RequestParameterEntity reqp = (RequestParameterEntity) bean;
|
// fse = reqp.getFormData();
|
// }
|
// if (fse == null || StringUtils.isEmpty(fse.getUUID())) {
|
// return error(FileCode.GET_DATA_FAIL);
|
// }
|
// onlineDocumentEditService.resetDocumentEepirationTime(fse.getUUID());
|
// return OK();
|
// } catch (BaseException e) {
|
// return error(e);
|
// } catch (Exception e) {
|
// e.printStackTrace();
|
// return error(FileCode.SIGN_DOCUMENT_EDIT_STATUS_FAIL);
|
// }
|
// }
|
}
|