From a2ddd3bec584b6eb91ab8845353fdb40c9b37ebe Mon Sep 17 00:00:00 2001 From: zhangwei <1504152376@qq.com> Date: 星期一, 02 九月 2024 17:22:17 +0800 Subject: [PATCH] 初步完成 --- src/common/utils/util.js | 70 +++++++++++++++++++++++++++++++++-- 1 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/common/utils/util.js b/src/common/utils/util.js index 15c3344..0c7407c 100644 --- a/src/common/utils/util.js +++ b/src/common/utils/util.js @@ -134,17 +134,19 @@ )).getTime() / 1000; }, formatDate(dat, type) { + if(!dat){ + return '-' + } let date = new Date(dat) - console.log(date, dat, type); const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); const hours = date.getHours().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0'); if (type) { - return `${year}-${month}-${day} ${hours}:${minutes}`; + return `${year}/${month}/${day} ${hours}:${minutes}`; } - return `${year}-${month}-${day}`; + return `${year}/${month}/${day}`; }, /** * 鍊掕鏃� @@ -708,6 +710,66 @@ } } typeof callback == 'function' && callback(theRequest); + }, + + /** + * 璺緞杞琤ase64 + * @param {Object} string + */ + pathToBase64(path) { + if (/^data:/.test(path)) return path + return new Promise((resolve, reject) => { + // #ifdef H5 + let image = new Image(); + image.setAttribute("crossOrigin", 'Anonymous'); + image.onload = function() { + let canvas = document.createElement('canvas'); + canvas.width = this.naturalWidth; + canvas.height = this.naturalHeight; + canvas.getContext('2d').drawImage(image, 0, 0); + let result = canvas.toDataURL('image/png') + resolve(result); + canvas.height = canvas.width = 0 + } + image.src = path + '?v=' + Math.random() + image.onerror = (error) => { + reject(error); + }; + // #endif + + // #ifdef MP + if (uni.canIUse('getFileSystemManager')) { + uni.getFileSystemManager().readFile({ + filePath: path, + encoding: 'base64', + success: (res) => { + resolve('data:image/png;base64,' + res.data) + }, + fail: (error) => { + reject(error) + } + }) + } + // #endif + + // #ifdef APP-PLUS + plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), (entry) => { + entry.file((file) => { + const fileReader = new plus.io.FileReader() + fileReader.onload = (data) => { + resolve(data.target.result) + } + fileReader.onerror = (error) => { + reject(error) + } + fileReader.readAsDataURL(file) + }, reject) + }, reject) + // #endif + }) + }, + roundToTwo(num){ + return Math.round((num + Number.EPSILON) * 100) / 100; } -} \ No newline at end of file +} -- Gitblit v1.9.1