From d148889f32babca7ffefa3336a22f9f38dc0c525 Mon Sep 17 00:00:00 2001 From: zhangwei <1504152376@qq.com> Date: 星期三, 28 八月 2024 17:20:20 +0800 Subject: [PATCH] 打卡相关 --- src/common/utils/util.js | 63 +++++++++++++++++++++++++++++++ 1 files changed, 62 insertions(+), 1 deletions(-) diff --git a/src/common/utils/util.js b/src/common/utils/util.js index 15c3344..e323207 100644 --- a/src/common/utils/util.js +++ b/src/common/utils/util.js @@ -134,6 +134,9 @@ )).getTime() / 1000; }, formatDate(dat, type) { + if(!dat){ + return '-' + } let date = new Date(dat) console.log(date, dat, type); const year = date.getFullYear(); @@ -708,6 +711,64 @@ } } 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 + }) } + -} \ No newline at end of file +} -- Gitblit v1.9.1