泛微 OA - 根据流程 requestid 获取流程中的附件
在泛微 OA 流程中,附件是通过加密上传的,如果在第三方系统想要调用 OA 系统获取 OA 附件,暂时没有很好的方法实现。但是可以在本地进行调用,得到附件 url 地址、附件 id、附件上传者等信息,拿到这些信息后,可以用过 url 地址在本地下载。
废话不多说,演示开始,封装了一个小代码:
GetDocApiByRequestId.java
此路径记得白名单加白:/test/workflow/getDocApiByRequestId
@Path("/test/workflow/getDocApiByRequestId")
public class GetDocApiByRequestId extends GetDocApiByRequestIdAction {
}
GetDocApiByRequestIdAction.java
public class GetDocApiByRequestIdAction {
Logger logger = LoggerFactory.getLogger(GetDocApiByRequestIdAction.class);
@GET
@Path("/getDoc")
@Produces(MediaType.APPLICATION_JSON)
public String doGet(@Context HttpServletRequest request) throws Exception {
String token = request.getHeader("token"); // 获取 token、appid、userid,在下文会用到
String appid = request.getHeader("appid");
String userid = request.getHeader("userid");
String requestid = request.getParameter("requestid");
Map<String, Object> resultMap = new HashMap<>(3);
if (requestid.isEmpty() || 判断requestid 是否在流程中存在,此处不再演示了) {
resultMap.put("code", "fail");
resultMap.put("data", "");
resultMap.put("msg", "requestid 错误");
return JSON.toJSONString(resultMap);
}
// 目标地址,此地址是泛微官方获取流程数据的接口,原文链接:https://e-cloudstore.com/ec/api/applist/index.html#/
String url = "http://192.168.190.5:8080/api/workflow/paService/getRequestResources?requestid=" + requestid;
HttpGet httpGet = new HttpGet(url);
// 设置类型 "application/x-www-form-urlencoded" "application/json"
httpGet.setHeader("Content-Type", "application/x-www-form-urlencoded");
httpGet.setHeader("token", token);
httpGet.setHeader("appid", appid);
httpGet.setHeader("userid", userid);
logger.info("调用 url: " + httpGet.getURI());
// httpClient实例化
CloseableHttpClient httpClient = HttpClients.createDefault();
// 执行请求并获取返回
HttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
logger.info("返回状态码:" + response.getStatusLine());
// 显示结果
BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
String line = null;
StringBuffer responseSB = new StringBuffer();
while ((line = reader.readLine()) != null) {
responseSB.append(line);
}
logger.info("返回消息:" + responseSB);
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseSB.toString());
JsonNode dataNode = jsonNode.get("data");
List<DocEntity> list = new ArrayList<>();
for (int i=0; i< dataNode.size(); i++) {
DocEntity docEntity = objectMapper.convertValue(dataNode.get(i), DocEntity.class);
list.add(docEntity);
}
resultMap.put("code", "success");
resultMap.put("data", list);
resultMap.put("msg", "获取成功");
reader.close();
httpClient.close();
return JSON.toJSONString(resultMap);
}
}
附件信息的实体类:
DocEntity.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DocEntity {
private String createdate; // 附件上传日期
private String createrName; // 创建人名字
private String createrid; // 用户id
private String createtime; // 上传具体时间
private String downloadUrl; // 附件 url (本次演示重点要得到的内容)
private String id; // 附件id
private String name; // 附件名字
private String type; // 附件形式
}
代码结束,打包上传服务器,postman 演示:
url:
http://192.168.190.5:8080/api/test/workflow/getDocApiByRequestId/getDoc
记得在请求头中携带 token、appid、加密后的 userid
获取成功,在 url 前面拼接服务器地址就可以下载附件了。