From b7ae3f69ae6706a692284d2f12835d213debfb1a Mon Sep 17 00:00:00 2001
From: zhangwei <1504152376@qq.com>
Date: 星期二, 07 四月 2026 17:21:41 +0800
Subject: [PATCH] -
---
src/common/utils/util.js | 217 +++++++++++++++++++++++++++++++++++++++++++++++++++--
1 files changed, 207 insertions(+), 10 deletions(-)
diff --git a/src/common/utils/util.js b/src/common/utils/util.js
index db1eae5..70a4f8a 100644
--- a/src/common/utils/util.js
+++ b/src/common/utils/util.js
@@ -117,6 +117,52 @@
}
},
/**
+ * 鏍稿績宸ュ叿鍑芥暟锛氭牴鎹敓鏃ュ瓧绗︿覆璁$畻骞撮緞
+ * @param {string} birthdayStr - 鐢熸棩瀛楃涓诧紙鏀寔 YYYY-MM-DD / YYYY/MM/DD锛�
+ * @returns {number} 鍛ㄥ瞾锛�-1 琛ㄧず鏃ユ湡鏃犳晥锛�
+ */
+ calculateAgeFromBirthday(birthdayStr) {
+ console.log(birthdayStr,'銆併�併�併�併�併�併�併�併�併�佲��');
+ if (!birthdayStr) return -1
+
+ // 1. 缁熶竴鏍煎紡骞剁簿鍑嗚В鏋愮敓鏃ワ紙瑙e喅 1/26 杩欑被鏃犺ˉ闆舵棩鏈熺殑瑙f瀽闂锛�
+ const normalized = birthdayStr.replace(/[./]/g, '-')
+ console.log(normalized,'normalized鈥�');
+ const [birthYear, birthMonth, birthDay] = normalized.split('-').map(Number)
+ // 鐩存帴鐢ㄦ暟瀛楀垱寤� Date 瀵硅薄锛岄伩鍏嶅瓧绗︿覆瑙f瀽鐨勬涔夛紙鏈堜唤浠� 0 寮�濮嬶級
+ const birthDate = new Date(birthYear, birthMonth - 1, birthDay)
+
+ // 2. 鏍¢獙鏃ユ湡鍚堟硶鎬�
+ if (isNaN(birthDate.getTime())) return -1
+ // 鍙嶅悜楠岃瘉锛氱‘淇濊緭鍏ョ殑骞存湀鏃ュ拰 Date 瀵硅薄涓�鑷达紙閬垮厤 2010/2/30 杩欑被鏃犳晥鏃ユ湡锛�
+ if (
+ birthDate.getFullYear() !== birthYear ||
+ birthDate.getMonth() + 1 !== birthMonth ||
+ birthDate.getDate() !== birthDay
+ ) {
+ return -1
+ }
+
+ // 3. 鑾峰彇褰撳墠鏃ユ湡锛堢簿鍑嗗埌鏃ワ紝鎺掗櫎鏃跺垎绉掑共鎵帮級
+ const now = new Date()
+ const currentYear = now.getFullYear()
+ const currentMonth = now.getMonth() + 1 // 杞垚 1-12 鏈�
+ const currentDay = now.getDate()
+
+ // 4. 鏍稿績锛氱簿鍑嗚绠楀懆宀�
+ let age = currentYear - birthYear
+ // 鍒ゆ柇锛氬綋鍓嶆棩鏈� < 鐢熸棩鏃ユ湡 鈫� 骞撮緞鍑� 1锛堝彧鏈夊綋鍓嶆棩鏈� >= 鐢熸棩鏃ユ湡锛屾墠婊″搴斿懆宀侊級
+ if (
+ currentMonth < birthMonth || // 鏈堜唤娌″埌
+ (currentMonth === birthMonth && currentDay < birthDay) // 鏈堜唤鍒颁簡锛屼絾鏃ユ湡娌″埌
+ ) {
+ age--
+ }
+
+ // 5. 杈圭晫澶勭悊锛氬勾榫勪笉鑳戒负璐燂紙鎺掗櫎鏈潵鐢熸棩锛�
+ return age
+ },
+ /**
* 鏃ユ湡鏍煎紡杞椂闂存埑
* @param {Object} timeStamp
*/
@@ -133,20 +179,29 @@
parseInt(t[2], 10) || null
)).getTime() / 1000;
},
- formatDate(dat, type) {
- if(!dat){
+ 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)
+ 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');
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 type=='riqi' ? `${month}-${day}`: `${year}-${month}-${day} ${hours}:${minutes}`;
}
- return `${year}/${month}/${day}`;
+ return `${year}-${month}-${day}`;
},
/**
* 鍊掕鏃�
@@ -711,7 +766,7 @@
}
typeof callback == 'function' && callback(theRequest);
},
-
+
/**
* 璺緞杞琤ase64
* @param {Object} string
@@ -736,7 +791,7 @@
reject(error);
};
// #endif
-
+
// #ifdef MP
if (uni.canIUse('getFileSystemManager')) {
uni.getFileSystemManager().readFile({
@@ -751,7 +806,7 @@
})
}
// #endif
-
+
// #ifdef APP-PLUS
plus.io.resolveLocalFileSystemURL(getLocalFilePath(path), (entry) => {
entry.file((file) => {
@@ -767,7 +822,149 @@
}, 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()
+ }
+ });
+ },
+ //鐢熸垚鏃堕棿鎴�+闅忔満鏁�
+ // 鐢熸垚鏃堕棿鎴�+闅忔満鏁扮殑鍑芥暟
+ generateTimestampWithRandom(filename) {
+ // 鑾峰彇褰撳墠鏃ユ湡鍜屾椂闂�
+ const now = new Date();
+ // 鑾峰彇骞翠唤锛岀‘淇濅负鍥涗綅鏁�
+ const year = now.getFullYear();
+ // 鑾峰彇鏈堜唤锛岀‘淇濅负涓や綅鏁�
+ const month = String(now.getMonth() + 1).padStart(2, '0');
+ // 鑾峰彇鏃ユ湡锛岀‘淇濅负涓や綅鏁�
+ const day = String(now.getDate()).padStart(2, '0');
+ // 鑾峰彇灏忔椂锛岀‘淇濅负涓や綅鏁�
+ const hours = String(now.getHours()).padStart(2, '0');
+ // 鑾峰彇鍒嗛挓锛岀‘淇濅负涓や綅鏁�
+ const minutes = String(now.getMinutes()).padStart(2, '0');
+ // 鑾峰彇绉掓暟锛岀‘淇濅负涓や綅鏁�
+ const seconds = String(now.getSeconds()).padStart(2, '0');
+ // 鎷兼帴鏃堕棿鎴�
+ const timestamp = `${year}${month}${day}${hours}${minutes}${seconds}`;
+ // 鐢熸垚涓�涓� 0 鍒� 9999 涔嬮棿鐨勯殢鏈烘暟锛屽苟鏍煎紡鍖栦负鍥涗綅鏁�
+ const randomNumber = String(Math.floor(Math.random() * 10000)).padStart(4, '0');
+ // 鎷兼帴鏃堕棿鎴冲拰闅忔満鏁�
+ return `cylsg/${timestamp}_${randomNumber}` + filename.match(/\.[^.]+$/);
+ },
+ getFileExtensionWithDotRegex(filename) {
+ const match = filename.match(/\.[^.]+$/);
+ return match ? match[0] : '';
+ },
+ isEmpty(value) {
+ return (
+ value === null ||
+ value === undefined ||
+ value === '' ||
+ (Array.isArray(value) && value.length === 0) ||
+ (typeof value === 'object' &&!Array.isArray(value) && Object.keys(value).length === 0)
+ );
+ },
-
-}
+ removeEmptyValuesRecursive(obj) {
+ const newObj = {};
+ for (const key in obj) {
+ if (obj.hasOwnProperty(key)) {
+ const value = obj[key];
+ if (!this.isEmpty(value)) {
+ if (typeof value === 'object' &&!Array.isArray(value)) {
+ newObj[key] = removeEmptyValuesRecursive(value);
+ } else {
+ newObj[key] = value;
+ }
+ }
+ }
+ }
+ return newObj;
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.1