소스 검색

WPS文档对接

sakuya 3 년 전
부모
커밋
65694e9220

+ 142 - 0
eladmin-system/src/main/java/me/zhengjie/application/admin/controller/AdminDocEditorController.java

@@ -0,0 +1,142 @@
+package me.zhengjie.application.admin.controller;
+
+import cfca.com.itextpdf.text.pdf.BaseFont;
+import com.alibaba.fastjson.JSONObject;
+import lombok.RequiredArgsConstructor;
+import me.zhengjie.annotation.rest.AnonymousGetMapping;
+import me.zhengjie.base.util.HttpUtil;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.xhtmlrenderer.pdf.ITextFontResolver;
+import org.xhtmlrenderer.pdf.ITextRenderer;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.text.SimpleDateFormat;
+import java.util.*;
+
+@Controller
+@RequiredArgsConstructor
+@RequestMapping("${fqgz.admin.web.url-prefix}/doc-editor")
+public class AdminDocEditorController {
+
+    private static final String APP_ID = "";
+    private static final String APP_KEY = "";
+    private static final String ACCESS_HOST = "https://openapi.wps.cn";
+
+    @AnonymousGetMapping("/edit-url")
+    public void edit(String appToken, String fileId) {
+        String uri = "/weboffice/v2/url";
+        Map<String, String> header = getHeader(uri, null);
+        //
+        Map<String, String> query = new HashMap<>();
+        query.put("app_token", appToken);
+        query.put("file_id", fileId);
+        query.put("type", "w");
+        query.put("scene_id", "134134");
+        String url = ACCESS_HOST + uri + getQueryString(query);
+        //
+        JSONObject message = HttpUtil.doGetWithHeader(url, header);
+        if (message != null && "0".equals(message.getString("result"))) {
+            String editUrl = message.getString("url");
+            System.out.println(editUrl);
+        }
+    }
+
+    @AnonymousGetMapping("/create")
+    public void create(@RequestParam String appToken) {
+        JSONObject body = new JSONObject();
+        body.put("file_name", "test.docx");
+        //
+        String uri = "/appfile/v1/files/new";
+        Map<String, String> header = getHeader(uri, body);
+        //
+        Map<String, String> query = new HashMap<>();
+        query.put("app_token", appToken);
+        String url = ACCESS_HOST + uri + getQueryString(query);
+        //
+        JSONObject message = HttpUtil.doPostWithHeaderAndBody(url, header, body);
+        if (message != null && "0".equals(message.getString("result"))) {
+            String fileId = message.getJSONObject("data").getString("file_id");
+            System.out.println(fileId);
+        }
+    }
+
+    @AnonymousGetMapping("/login")
+    public String login() {
+        String uri = "/auth/v1/app/inscope/token";
+        Map<String, String> header = getHeader(uri, null);
+        //
+        Map<String, String> query = new HashMap<>();
+        query.put("app_id", APP_ID);
+        query.put("scope", "file_edit");
+        String url = ACCESS_HOST + uri + getQueryString(query);
+        //
+        JSONObject message = HttpUtil.doGetWithHeader(url, header);
+        if (message != null && "0".equals(message.getString("result"))) {
+            String appToken = message.getJSONObject("token").getString("app_token");
+            System.out.println(appToken);
+            return appToken;
+        }
+        return "";
+    }
+
+    private String getQueryString(Map<String, String> query) {
+        String queryStr = "";
+        Iterator keyList = query.keySet().iterator();
+        while (keyList.hasNext()) {
+            String key = (String) keyList.next();
+            String value = query.get(key);
+            queryStr += "&" + key + "=" + value;
+        }
+        if (queryStr.length() > 0) {
+            queryStr = "?" + queryStr.substring(1);
+        }
+        return queryStr;
+    }
+
+    private Map<String, String> getHeader(String uri, JSONObject body) {
+        String contentType = "application/json";
+        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
+        dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
+        String date = dateFormat.format(new Date());
+        String contentMd5 = body != null ? body.toString() : "";
+        contentMd5 = DigestUtils.md5Hex(contentMd5);
+        String auth = "WPS-3:" + APP_ID + ":" + DigestUtils.sha1Hex(APP_KEY + contentMd5 + uri + contentType + date);
+        //
+        Map<String, String> header = new HashMap<>();
+        header.put("Content-Md5", contentMd5);
+        header.put("Content-Type", contentType);
+        header.put("Date", date);
+        header.put("X-Auth", auth);
+        //
+        return header;
+    }
+
+    public static void main(String[] args) {
+        try {
+            htmlToPdf("/Users/sakuya/Downloads/H001.html", "/Users/sakuya/Downloads/H001.pdf");
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    private static void htmlToPdf(String inputFile, String outputFile) throws Exception {
+        String url = new File(inputFile).toURI().toURL().toString();
+        System.out.println(url);
+        OutputStream os = new FileOutputStream(outputFile);
+        ITextRenderer renderer = new ITextRenderer();
+        renderer.setDocument(url);
+        // 解决中文支持问题
+        ITextFontResolver fontResolver = renderer.getFontResolver();
+        fontResolver.addFont("/Users/sakuya/Downloads/simsun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+//        fontResolver.addFont("/Users/sakuya/Downloads/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
+        // 解决图片的相对路径问题
+        renderer.layout();
+        renderer.createPDF(os);
+        os.close();
+    }
+}

+ 64 - 0
eladmin-system/src/main/java/me/zhengjie/base/util/HttpUtil.java

@@ -6,6 +6,7 @@ import java.net.*;
 import java.text.MessageFormat;
 import java.util.*;
 
+import cfca.trustsign.connector.HttpClient;
 import com.alibaba.fastjson.JSONObject;
 
 import cfca.trustsign.common.vo.cs.SignKeywordVO;
@@ -365,4 +366,67 @@ public class HttpUtil {
 //		System.out.println("用户近6个月平均话费评分:" + result);
 
 	}
+
+	/**
+	 * 发送带Header信息的Get请求
+	 *
+	 * @param url
+	 * @param header
+	 * @return
+	 */
+	public static JSONObject doGetWithHeader(String url, Map<String, String> header) {
+		HttpURLConnection connection = null;
+		HttpClient httpClient = new HttpClient();
+		JSONObject returnMsg = null;
+		try {
+			connection = httpClient.connect(url, "GET");
+			if (null != header) {
+				for (String key : header.keySet()) {
+					connection.setRequestProperty(key, header.get(key));
+				}
+			}
+			int responseCode = httpClient.send(connection, (byte[]) null);
+			System.out.println("responseCode:" + responseCode);
+			byte[] fileBytes = null;
+			if (responseCode == 200) {
+				fileBytes = httpClient.receive(connection);
+			}
+			String message = new String(fileBytes);
+			returnMsg = JSONObject.parseObject(message);
+			System.out.println(returnMsg);
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			httpClient.disconnect(connection);
+		}
+		return returnMsg;
+	}
+
+	public static JSONObject doPostWithHeaderAndBody(String url, Map<String, String> header, JSONObject body) {
+		HttpURLConnection connection = null;
+		HttpClient httpClient = new HttpClient();
+		JSONObject returnMsg = null;
+		try {
+			connection = httpClient.connect(url, "POST");
+			if (null != header) {
+				for (String key : header.keySet()) {
+					connection.setRequestProperty(key, header.get(key));
+				}
+			}
+			int responseCode = httpClient.send(connection, body.toString().getBytes());
+			System.out.println("responseCode:" + responseCode);
+			byte[] fileBytes = null;
+			if (responseCode == 200) {
+				fileBytes = httpClient.receive(connection);
+			}
+			String message = new String(fileBytes);
+			returnMsg = JSONObject.parseObject(message);
+			System.out.println(returnMsg);
+		} catch (Exception e) {
+			e.printStackTrace();
+		} finally {
+			httpClient.disconnect(connection);
+		}
+		return returnMsg;
+	}
 }