From 5a87dc95cb6c8b96b5eabb1ad28662c0b733b174 Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Wed, 18 Apr 2018 19:59:22 +0800 Subject: [PATCH 1/3] JS Interface --- libs/core.js | 29 ++++++----------------------- libs/utils.js | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/libs/core.js b/libs/core.js index a01080a5..a49f131d 100644 --- a/libs/core.js +++ b/libs/core.js @@ -216,29 +216,7 @@ core.prototype.init = function (coreData, callback) { if (window.FileReader) { core.platform.fileReader = new FileReader(); core.platform.fileReader.onload = function () { - var content=core.platform.fileReader.result; - var obj=null; - if(content.slice(0,4)==='data'){ - if (core.isset(core.platform.successCallback)) - core.platform.successCallback(content); - return; - } - try { - obj=JSON.parse(content); - if (core.isset(obj)) { - if (core.isset(core.platform.successCallback)) - core.platform.successCallback(obj); - return; - } - } - catch (e) { - console.log(e); - } - alert("不是有效的JSON文件!"); - - if (core.isset(core.platform.errorCallback)) - core.platform.errorCallback(); - + core.readFileContent(core.platform.fileReader.result); }; core.platform.fileReader.onerror = function () { if (core.isset(core.platform.errorCallback)) @@ -1078,6 +1056,11 @@ core.prototype.readFile = function (success, error, readType) { core.utils.readFile(success, error, readType); } +////// 读取本地文件完毕 ////// +core.prototype.readFileContent = function (content) { + core.utils.readFileContent(content); +} + ////// 下载文件到本地 ////// core.prototype.download = function (filename, content) { core.utils.download(filename, content); diff --git a/libs/utils.js b/libs/utils.js index aaf82da3..67f3022b 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -321,6 +321,31 @@ utils.prototype.readFile = function (success, error, readType) { core.platform.fileInput.click(); } +////// 读取文件完毕 ////// +utils.prototype.readFileContent = function (content) { + var obj=null; + if(content.slice(0,4)==='data'){ + if (core.isset(core.platform.successCallback)) + core.platform.successCallback(content); + return; + } + try { + obj=JSON.parse(content); + if (core.isset(obj)) { + if (core.isset(core.platform.successCallback)) + core.platform.successCallback(obj); + return; + } + } + catch (e) { + console.log(e); + } + alert("不是有效的JSON文件!"); + + if (core.isset(core.platform.errorCallback)) + core.platform.errorCallback(); +} + ////// 下载文件到本地 ////// utils.prototype.download = function (filename, content) { From 7b34994df7339c2d6a976c5c76b0d4bac5ea1418 Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Wed, 18 Apr 2018 19:59:27 +0800 Subject: [PATCH 2/3] JS Interface --- libs/utils.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/libs/utils.js b/libs/utils.js index 67f3022b..fcc6a561 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -285,6 +285,14 @@ utils.prototype.isset = function (val) { ////// 读取一个本地文件内容 ////// utils.prototype.readFile = function (success, error, readType) { + core.platform.successCallback = success; + core.platform.errorCallback = error; + + if (core.isset(window.jsinterface)) { + window.jsinterface.readFile(); + return; + } + // step 0: 不为http/https,直接不支持 if (!core.platform.isOnline) { alert("离线状态下不支持文件读取!"); @@ -409,6 +417,12 @@ utils.prototype.download = function (filename, content) { ////// 复制一段内容到剪切板 ////// utils.prototype.copy = function (data) { + + if (core.isset(window.jsinterface)) { + window.jsinterface.copy(filename, content); + return true; + } + if (!core.platform.supportCopy) return false; var textArea = document.createElement("textarea"); From a238f631fd476ff03706ec240957ef7f5a373f02 Mon Sep 17 00:00:00 2001 From: ckcz123 Date: Wed, 18 Apr 2018 20:00:05 +0800 Subject: [PATCH 3/3] JS Interface --- libs/utils.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/utils.js b/libs/utils.js index fcc6a561..34ff3517 100644 --- a/libs/utils.js +++ b/libs/utils.js @@ -324,8 +324,6 @@ utils.prototype.readFile = function (success, error, readType) { } } - core.platform.successCallback = success; - core.platform.errorCallback = error; core.platform.fileInput.click(); }