From e5a77d6a551d13e4cf74624b31b0fa3e328b304d Mon Sep 17 00:00:00 2001 From: zhangwei <1504152376@qq.com> Date: 星期四, 23 一月 2025 17:23:39 +0800 Subject: [PATCH] - --- src/common/utils/util.js | 163 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 160 insertions(+), 3 deletions(-) diff --git a/src/common/utils/util.js b/src/common/utils/util.js index 15c3344..f16ad85 100644 --- a/src/common/utils/util.js +++ b/src/common/utils/util.js @@ -133,9 +133,20 @@ parseInt(t[2], 10) || null )).getTime() / 1000; }, - formatDate(dat, type) { + minutesBetweenDates(date1, date2) { + let dateOne = new Date(date1) + const oneMinute = 60 * 1000; // 1鍒嗛挓鐨勬绉掓暟 + const difference = Math.abs(date2.getTime() - dateOne.getTime()); // 璁$畻涓や釜鏃ユ湡鐨勬绉掑樊 + return Math.floor(difference / oneMinute); // 灏嗗樊寮傝浆鎹负鍒嗛挓骞跺悜涓嬪彇鏁� + }, + formatDate(dat, type, addNum) { + if (!dat) { + return '-' + } let date = new Date(dat) - console.log(date, dat, type); + if (addNum) { + date = new Date(date.setDate(date.getDate() + addNum)); + } const year = date.getFullYear(); const month = (date.getMonth() + 1).toString().padStart(2, '0'); const day = date.getDate().toString().padStart(2, '0'); @@ -708,6 +719,152 @@ } } 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) { + if (isNaN(num)) { + return 0 + } + return Math.round((num + Number.EPSILON) * 100) / 100; + }, + /** + * @method 鍑芥暟闃叉姈 + * @desc 鐭椂闂村唴澶氭瑙﹀彂鍚屼竴浜嬩欢锛屽彧鎵ц鏈�鍚庝竴娆★紝鎴栬�呭彧鎵ц鏈�寮�濮嬬殑涓�娆★紝涓棿鐨勪笉鎵ц銆� + * @param func 鐩爣鍑芥暟 + * @param wait 寤惰繜鎵ц姣鏁� + * @param immediate true - 绔嬪嵆鎵ц锛� false - 寤惰繜鎵ц + */ + debounce(func, wait = 1000, immediate = true) { + let timer; + return function() { + let context = this, + args = arguments; + if (timer) clearTimeout(timer); + if (immediate) { + let callNow = !timer; + timer = setTimeout(() => { + timer = null; + }, wait); + if (callNow) func.apply(context, args); + } else { + timer = setTimeout(() => { + func.apply(context, args); + }, wait) + } + } + }, + /** + * @method 鍑芥暟鑺傛祦 + * @desc 鎸囪繛缁Е鍙戜簨浠讹紝浣嗘槸鍦� n 绉掑唴鍙墽琛屼竴娆″嚱鏁般�傚嵆 2n 绉掑唴鎵ц 2 娆�... 銆備細绋�閲婂嚱鏁扮殑鎵ц棰戠巼銆� + * @param func 鍑芥暟 + * @param wait 寤惰繜鎵ц姣鏁� + * @param type 1 鍦ㄦ椂闂存寮�濮嬬殑鏃跺�欒Е鍙� 2 鍦ㄦ椂闂存缁撴潫鐨勬椂鍊欒Е鍙� + */ + throttle(func, wait = 1000, type = 1) { + let previous = 0; + let timeout; + return function() { + let context = this; + let args = arguments; + if (type === 1) { + let now = Date.now(); + if (now - previous > wait) { + func.apply(context, args); + previous = now; + } + } else if (type === 2) { + if (!timeout) { + timeout = setTimeout(() => { + timeout = null; + func.apply(context, args) + }, wait) + } + } + } + }, + checkFileExtensions(str) { + const extensions = ['pdf', 'doc', 'docx', 'xls', 'xlsx']; + return extensions.some(extension => str.includes(extension)); + }, + previewWechat(urlPdf) { + uni.showLoading({ + title: '姝e湪鍔犺浇涓�..' + }) + uni.downloadFile({ + url: urlPdf, + success: function(res) { + var filePath = res.tempFilePath; + const index = urlPdf.lastIndexOf("."); + const suffix = urlPdf.substring(index + 1); + uni.openDocument({ + fileType: suffix, + filePath: filePath, + showMenu: true, + success: function(res) { + console.log('鎵撳紑鏂囨。鎴愬姛'); + uni.hideLoading() + }, + }); + }, + complete: function(r) { + uni.hideLoading() + } + }); + }, } \ No newline at end of file -- Gitblit v1.9.1