From 7a6d66c564108c297d922a20e696ae2c0edcc5af Mon Sep 17 00:00:00 2001
From: 819527061@qq.com <123456>
Date: 星期二, 12 九月 2023 16:26:49 +0800
Subject: [PATCH] 支付成功和失败页面
---
src/libs/word.js | 385 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 366 insertions(+), 19 deletions(-)
diff --git a/src/libs/word.js b/src/libs/word.js
index 8990427..9b52e2a 100644
--- a/src/libs/word.js
+++ b/src/libs/word.js
@@ -1,37 +1,128 @@
-import PizZip from 'pizzip'
import docxtemplater from 'docxtemplater'
+import PizZip from 'pizzip'
import JSZipUtils from 'jszip-utils'
-import { saveAs } from 'file-saver'
+import {saveAs} from 'file-saver'
let docx = require("docx-preview");
const ImageModule = require("docxtemplater-image-module-free");
+const base64Regex =
+ /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
+
//涓嬭浇word锛屼笉甯﹀浘鐗�
-export const exportDoc = (e,path,dname) => {
- for(let i in e) {
- if(e[i] == null) {
- e[i] = ''
+export const exportDoc = (e, path , dname) => {
+ for (let attr in e) {
+ if (e[attr] == null) {
+ e[attr] = "";
+ }
+ }
+ let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
+ let docxname = dname; //瀵煎嚭鏂囦欢鐨勫悕瀛�
+ // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
+ JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+ // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+ // 鎶涘嚭寮傚父
+ if (error) {
+ throw error;
+ }
+
+ // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+ let zip = new PizZip(content);
+ // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
+ let doc = new docxtemplater().loadZip(zip);
+ // 璁剧疆妯℃澘鍙橀噺鐨勫��
+ doc.setData(e);
+
+ try {
+ //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+ doc.render();
+ } catch (error) {
+ let e = {
+ message: error.message,
+ name: error.name,
+ stack: error.stack,
+ properties: error.properties
+ };
+ console.log(JSON.stringify({error: e}));
+ throw error;
+ }
+
+ // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+ let out = doc.getZip().generate({
+ type: "blob",
+ mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ });
+ // 灏嗙洰鏍囨枃浠跺璞′繚瀛樹负鐩爣绫诲瀷鐨勬枃浠讹紝骞跺懡鍚�
+ saveAs(out, docxname);
+ });
+};
+
+//涓嬭浇word锛屽甫鍥剧墖
+export const exportDocWidthImg = (e, path , dname, imgSize) => {
+ for (let attr in e) {
+ if (e[attr] == null) {
+ e[attr] = "";
}
}
let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
let docxname = dname; //瀵煎嚭鏂囦欢鐨勫悕瀛�
// 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
- JSZipUtils.getBinaryContent(docxsrc,function (error, content) {
- if (error) throw error // 鎶涘嚭寮傚父
- let zip = new PizZip(content) // 鍒涘缓涓�涓狫SZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
- let doc = new docxtemplater().loadZip(zip) // 鍒涘缓骞跺姞杞絛ocxtemplater瀹炰緥瀵硅薄
- doc.setData(e) // 璁剧疆妯℃澘鍙橀噺鐨勫��
+ JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+ // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+ // 鎶涘嚭寮傚父
+ if (error) {
+ throw error;
+ }
+
+ // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+ let zip = new PizZip(content);
+ // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
+ let doc = new docxtemplater().loadZip(zip).setOptions({
+ linebreaks:true,
+ paragraphLoop: true,
+ nullGetter:function () {
+ return ""
+ }
+ });
+
+ // // 鍥剧墖澶勭悊
+ let opts = {}
+ opts = {
+ // 鍥惧儚鏄惁灞呬腑
+ centered: false
+ };
+ opts.fileType = "docx";
+ opts.getImage = (chartId) =>{
+ return base64DataURLToArrayBuffer(chartId);
+ }
+
+ opts.getSize = function(img, tagVlaue, tagName){ //鍥剧墖size
+ if(imgSize.hasOwnProperty(tagName)) {
+ return imgSize[tagName]
+ }else {
+ return [300,300]
+ }
+ }
+ let imageModule = new ImageModule(opts);
+ doc.attachModule(imageModule);
+
+ // 璁剧疆妯℃澘鍙橀噺鐨勫��
+ doc.setData(e);
+ console.log(doc,'涓嬭浇---甯﹀浘鐗囩殑---')
+
try {
- doc.render() // 鐢ㄦā鏉垮彉閲忕殑鍊兼浛鎹㈡墍鏈夋ā鏉垮彉閲�
- } catch(error) {
+ //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+ doc.render();
+ } catch (error) {
let e = {
message: error.message,
name: error.name,
stack: error.stack,
properties: error.properties
- }
- console.log(JSON.stringify({ error: e }))
- throw error // 鎶涘嚭寮傚父
+ };
+ console.log(JSON.stringify({error: e}));
+ throw error;
}
+
// 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
let out = doc.getZip().generate({
type: "blob",
@@ -39,17 +130,18 @@
});
// 灏嗙洰鏍囨枃浠跺璞′繚瀛樹负鐩爣绫诲瀷鐨勬枃浠讹紝骞跺懡鍚�
saveAs(out, docxname);
- })
+ },2000);
}
-//棰勮word锛屼笉甯﹀浘鐗囩殑
-export const viewD = (e, path , continer) => {
+//涓嬭浇word锛屽甫鍥剧墖
+export const exportDocWidthImg2 = (e, path , dname, arry, imgSize) => {
for (let attr in e) {
if (e[attr] == null) {
e[attr] = "";
}
}
let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
+ let docxname = dname; //瀵煎嚭鏂囦欢鐨勫悕瀛�
// 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
// docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
@@ -62,6 +154,24 @@
let zip = new PizZip(content);
// 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
let doc = new docxtemplater().loadZip(zip);
+
+ // // 鍥剧墖澶勭悊
+ const opts = {}
+ opts.fileType = "docx";
+ opts.getImage = (chartId) =>{
+ return base64DataURLToArrayBuffer(chartId);
+ }
+
+ opts.getSize = function(img, tagVlaue, tagName){ //鍥剧墖size
+ if(imgSize.hasOwnProperty(tagName)) {
+ return imgSize[tagName]
+ }else {
+ return [300,300]
+ }
+ }
+ let imageModule = new ImageModule(opts);
+ doc.attachModule(imageModule);
+
// 璁剧疆妯℃澘鍙橀噺鐨勫��
doc.setData(e);
@@ -84,6 +194,243 @@
type: "blob",
mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
});
+ // 灏嗙洰鏍囨枃浠跺璞′繚瀛樹负鐩爣绫诲瀷鐨勬枃浠讹紝骞跺懡鍚�
+ saveAs(out, docxname);
+ },2000);
+}
+
+//棰勮word锛屼笉甯﹀浘鐗囩殑
+export const viewD = (e, path , continer) => {
+ for (let attr in e) {
+ if (e[attr] == null) {
+ e[attr] = "";
+ }
+ }
+ let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
+ // 璇诲彇骞惰幏寰楁ā鏉挎枃浠剁殑浜岃繘鍒跺唴瀹�
+ JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+ // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+ // 鎶涘嚭寮傚父
+ if (error) {
+ throw error;
+ }
+
+ // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+ let zip = new PizZip(content);
+ // 鍒涘缓骞跺姞杞絛ocx templater瀹炰緥瀵硅薄
+ let doc = new docxtemplater().loadZip(zip);
+ // 璁剧疆妯℃澘鍙橀噺鐨勫��
+ doc.setData(e);
+
+ try {
+ //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+ doc.render();
+ } catch (error) {
+ let e = {
+ message: error.message,
+ name: error.name,
+ stack: error.stack,
+ properties: error.properties
+ };
+ throw error;
+ }
+
+ // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+ let out = doc.getZip().generate({
+ type: "blob",
+ mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ });
+ docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+ });
+};
+
+//棰勮甯﹀浘鐗囩殑word
+export const viewWithImg = (e, path , continer, imgSize) => {
+ for (let attr in e) {
+ if (e[attr] == null) {
+ e[attr] = "";
+ }
+ }
+ let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
+ JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+ // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+ // 鎶涘嚭寮傚父
+ if (error) {
+ throw error;
+ }
+
+ const doc = new docxtemplater();
+ // // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+ const zip = new PizZip(content);
+ doc.loadZip(zip)
+ // // 鍥剧墖澶勭悊
+ const opts = {}
+ //opts.centered = true;
+ opts.fileType = "docx";
+ opts.getImage = (chartId) =>{
+ return base64DataURLToArrayBuffer(chartId);
+ }
+
+ opts.getSize = function(img, tagVlaue, tagName){ //鍥剧墖size
+ if(imgSize.hasOwnProperty(tagName)) {
+ return imgSize[tagName]
+ }else {
+ return [70,40]
+ }
+ }
+
+ let imageModule = new ImageModule(opts);
+ doc.attachModule(imageModule);
+
+ doc.setData(e);
+ console.log(doc,'棰勮甯﹀浘鐗�====')
+
+ try {
+ // //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+ doc.render();
+ } catch (error) {
+ let e = {
+ message: error.message,
+ name: error.name,
+ stack: error.stack,
+ properties: error.properties
+ };
+ console.log(JSON.stringify({error: e}));
+ // throw error;
+ }
+
+ // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+ let out = doc.getZip().generate({
+ type: "blob",
+ mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ });
+ docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
+ });
+};
+
+//棰勮甯︾鍚嶅拰鎸夋墜鍗扮殑word
+export const viewWithImg2 = (e, path , continer, imgSize) => {
+ for (let attr in e) {
+ if (e[attr] == null) {
+ e[attr] = "";
+ }
+ }
+ let docxsrc = path; //妯℃澘鏂囦欢鐨勪綅缃�
+ JSZipUtils.getBinaryContent(docxsrc, function (error, content) {
+ // docxsrc鏄ā鏉裤�傛垜浠湪瀵煎嚭鐨勬椂鍊欙紝浼氭牴鎹妯℃澘鏉ュ鍑哄搴旂殑鏁版嵁
+ // 鎶涘嚭寮傚父
+ if (error) {
+ throw error;
+ }
+
+ const doc = new docxtemplater();
+ // // 鍒涘缓涓�涓狿izZip瀹炰緥锛屽唴瀹逛负妯℃澘鐨勫唴瀹�
+ const zip = new PizZip(content);
+ doc.loadZip(zip)
+ // // 鍥剧墖澶勭悊
+ const opts = {}
+ //opts.centered = true;
+ opts.fileType = "docx";
+ opts.getImage = (chartId) =>{
+ return base64DataURLToArrayBuffer(chartId);
+ }
+
+ opts.getSize = function(img, tagVlaue, tagName){ //鍥剧墖size
+ if(imgSize.hasOwnProperty(tagName)) {
+ return imgSize[tagName]
+ }else {
+ return [100,100]
+ }
+ }
+
+ let imageModule = new ImageModule(opts);
+ doc.attachModule(imageModule);
+ doc.setData(e);
+
+ try {
+ // //鏇挎崲鎵�鏈夋ā鏉垮彉閲�
+ doc.render();
+ } catch (error) {
+ let e = {
+ message: error.message,
+ name: error.name,
+ stack: error.stack,
+ properties: error.properties
+ };
+ // throw error;
+ }
+
+ // 鐢熸垚涓�涓唬琛╠ocxtemplater瀵硅薄鐨剒ip鏂囦欢锛堜笉鏄竴涓湡瀹炵殑鏂囦欢锛岃�屾槸鍦ㄥ唴瀛樹腑鐨勮〃绀猴級
+ let out = doc.getZip().generate({
+ type: "blob",
+ mimeType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
+ });
docx.renderAsync(out, continer); // 娓叉煋鍒伴〉闈㈤瑙�
});
};
+
+function base64DataURLToArrayBuffer(dataURL) {
+ const base64Regex = /^data:image\/(png|jpg|svg|svg\+xml);base64,/;
+ if (!base64Regex.test(dataURL)) {
+ return false;
+ }
+ const stringBase64 = dataURL.replace(base64Regex, "");
+ let binaryString;
+ if (typeof window !== "undefined") {
+ binaryString = window.atob(stringBase64);
+ } else {
+ binaryString = new Buffer(stringBase64, "base64").toString("binary");
+ }
+ const len = binaryString.length;
+ const bytes = new Uint8Array(len);
+ for (let i = 0; i < len; i++) {
+ const ascii = binaryString.charCodeAt(i);
+ bytes[i] = ascii;
+ }
+ return bytes.buffer;
+}
+
+
+export const urlToBase64 = (url, callback) => {
+ let image = new Image();
+ //瑙e喅璺ㄥ煙闂
+ image.setAttribute('crossOrigin', '*');
+ image.src = url
+ image.onload = () => {
+ var canvas = document.createElement("canvas");
+ canvas.width = image.width;
+ canvas.height = image.height;
+ var context = canvas.getContext('2d');
+ context.drawImage(image, 0, 0, image.width, image.height);
+ var quality = 0.8;
+ //杩欓噷鐨刣ataurl灏辨槸base64绫诲瀷
+ var dataURL = canvas.toDataURL("image/png", quality);
+ callback ? callback(dataURL) : null; //璋冪敤鍥炶皟鍑芥暟
+ }
+};
+
+
+function base64Parser(dataURL) {
+ if (
+ typeof dataURL !== "string" ||
+ !base64Regex.test(dataURL)
+ ) {
+ return false;
+ }
+ const stringBase64 = dataURL.replace(base64Regex, "");
+
+ // For nodejs
+ if (typeof Buffer !== "undefined" && Buffer.from) {
+ return Buffer.from(stringBase64, "base64");
+ }
+
+ // For browsers :
+ const binaryString = window.atob(stringBase64);
+ const len = binaryString.length;
+ const bytes = new Uint8Array(len);
+ for (let i = 0; i < len; i++) {
+ const ascii = binaryString.charCodeAt(i);
+ bytes[i] = ascii;
+ }
+ return bytes.buffer;
+}
--
Gitblit v1.9.1