Browse Source

优化装载字体文件,并行处理

TonyKang 5 years ago
parent
commit
f8e79cd3df
1 changed files with 32 additions and 9 deletions
  1. 32 9
      app/public/report/js/rpt_main.js

+ 32 - 9
app/public/report/js/rpt_main.js

@@ -29,6 +29,8 @@ let rptTplObj = {
             dynamicLoadJs('/public/jspdf/Arial Narrow-italic.js');
             dynamicLoadJs('/public/jspdf/Arial Narrow-bolditalic.js');
 
+            rptControlObj.loadPDFFonts();
+
             // dynamicLoadJs('/public/jspdf/SmartSimsun-normal.js');
             // dynamicLoadJs('/public/jspdf/SmartSimsun-bold.js', me.pdfFontSimsunCallBack);
         }
@@ -451,6 +453,7 @@ let rptControlObj = {
     currentOutputType: "Excel",
     currentDownloadUrl: null,
     currentDownloadIdx: 0,
+    isLoading: false,
     getCurrentPageSize: function() {
         // let rst = "A4";
         let rst = document.getElementById("btnRptPageSize").innerHTML.trim();
@@ -579,25 +582,45 @@ let rptControlObj = {
             me.getAllIndividualExcelBook();
         }
     },
+    getPdfFontCallbackLight: function(fontProperty) {
+        rptTplObj.pdfFont['SmartSimsun'].push(fontProperty);
+        if (rptTplObj.pdfFont['SmartSimsun'].length === 2) {
+            rptControlObj.isLoading = false;
+        }
+    },
     getPdfFontCallback: function(fontProperty) {
         let me = rptControlObj;
-        rptTplObj.pdfFont['SmartSimsun'].push(fontProperty);
+        if (rptTplObj.pdfFont['SmartSimsun'].indexOf(fontProperty) < 0) {
+            rptTplObj.pdfFont['SmartSimsun'].push(fontProperty);
+        }
         if (rptTplObj.pdfFont['SmartSimsun'].length === 2) {
             $.bootstrapLoading.end();
             me.getPDFEx();
         }
     },
+    loadPDFFonts: function () {
+        //这里尝试下异步加载字体文件
+        let me = rptControlObj;
+        me.isLoading = true;
+        dynamicLoadJs('/public/jspdf/SmartSimsun-normal.js',"normal", me.getPdfFontCallbackLight);
+        dynamicLoadJs('/public/jspdf/SmartSimsun-bold.js',"bold", me.getPdfFontCallbackLight);
+    },
     getPDFPre: function () {
         let me = rptControlObj;
-        // showWaitingView();
-        if (rptTplObj.pdfFont['SmartSimsun'].length === 2) {
-            me.getPDFEx();
-        } else {
+        if (me.isLoading) {
             $.bootstrapLoading.start();
-            dynamicLoadJs('/public/jspdf/SmartSimsun-normal.js',"normal", me.getPdfFontCallback);
-            dynamicLoadJs('/public/jspdf/SmartSimsun-bold.js',"bold", me.getPdfFontCallback);
-            // dynamicLoadJs('http://d2.smartcost.com.cn/cach/SmartSimsun-normal.js', "normal", me.getPdfFontCallback('normal'));
-            // dynamicLoadJs('http://d2.smartcost.com.cn/cach/SmartSimsun-bold.js', "normal", me.getPdfFontCallback('bold'));
+            console.log('fonts are loading...');
+            setTimeout(me.getPDFPre, 3000); //延时3秒
+        } else {
+            if (rptTplObj.pdfFont['SmartSimsun'].length === 2) {
+                me.getPDFEx();
+            } else {
+                $.bootstrapLoading.start();
+                dynamicLoadJs('/public/jspdf/SmartSimsun-normal.js',"normal", me.getPdfFontCallback);
+                dynamicLoadJs('/public/jspdf/SmartSimsun-bold.js',"bold", me.getPdfFontCallback);
+                // dynamicLoadJs('http://d2.smartcost.com.cn/cach/SmartSimsun-normal.js', "normal", me.getPdfFontCallback('normal'));
+                // dynamicLoadJs('http://d2.smartcost.com.cn/cach/SmartSimsun-bold.js', "normal", me.getPdfFontCallback('bold'));
+            }
         }
     },
     getPDFEx: async function () {