import Base64 from './base64'; export const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` } export const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } export const getGUID = () => { let guid = ""; for (let ii = 1; ii <= 32; ii++) { let n = Math.floor(Math.random() * 16.0).toString(16).toUpperCase(); guid += n; if ((ii == 8) || (ii == 12) || (ii == 16) || (ii == 20)) guid += "-"; } return guid.toUpperCase(); } // 显示消息提示框 export const showMessage = (title, icon = 'none', time = 1500) => { uni.showToast({ title: title, icon: icon, duration: time }); } // 显示 loading 提示框。需主动调用 uni.hideLoading 才能关闭提示框 export const loading = (title, time) => { if (time == null) time = 5000; uni.showLoading(title); if (time && time != false) { setTimeout(function () { uni.hideLoading(); }, time); } } // 显示模态提示框 export const showModelMessage = (content, title, showCancel, confirmText = "确定") => { if (title == null || title == "") title = "提示"; if (showCancel == null) showCancel = false; return new Promise((resolve, reject) => { uni.showModal({ title, content, showCancel, confirmText, success: function (res) { resolve(res); } }); }); } export function dataFormat(date, fmt) { var o = { "M+": date.getMonth() + 1, //月份 "d+": date.getDate(), //日 "H+": date.getHours(), //小时 "m+": date.getMinutes(), //分 "s+": date.getSeconds(), //秒 "q+": Math.floor((date.getMonth() + 3) / 3), //季度 "S": date.getMilliseconds() //毫秒 }; if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length)); for (var k in o) { if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (( "00" + o[k]).substr(("" + o[k]).length))); } return fmt; } //格式话长日期 export function longToDateFormat(value) { return value ? dataFormat((new Date(value)), 'yyyy-MM-dd') : ""; }; //格式话长日期(有时间) export function longToDateFormatTime(value) { return value ? dataFormat((new Date(value)), 'yyyy-MM-dd HH:mm:ss') : ""; }; export function base64Encode(text) { return Base64.encode(text); } export function base64Decode(text) { return Base64.decode(text); } export function getUrlParams(url) { const paramsRegex = /[?&]+([^=&]+)=([^&]*)/gi; const params = {}; let match; while (match = paramsRegex.exec(url)) { params[match[1]] = match[2]; } return params; } export function isNullOrEmpty(value) { if (typeof (value) == "undefined" || value == null || value == "") return true; else return false; } //获取导航栏高度 export function getNavHeight(callback) { // 获取胶囊按钮信息 const menuButtonInfo = uni.getMenuButtonBoundingClientRect(); uni.getSystemInfo({ success: (res) => { const statusBarHeight = res.statusBarHeight; const navHeight = statusBarHeight + menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2; callback(navHeight); } }); } export function getScreenHeightNoTabBar(callback) { // 获取胶囊按钮信息 const menuButtonInfo = uni.getMenuButtonBoundingClientRect(); uni.getSystemInfo({ success: (res) => { const { statusBarHeight, windowHeight, screenHeight } = res; const navHeight = statusBarHeight + menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2; const height = screenHeight - navHeight; callback(height); } }); } /** * 将字符串转化为数组 * @param {string} codeString - 要转换的字符串 * @return {Array} - 转换后的数组 */ export function stringToArray(codeString, returnVal = []) { // 处理数据不存在或非字符串类型的情况 if (codeString === undefined || codeString === null) { return returnVal; // 返回空数组表示没有数据 } // 确保输入是字符串类型 const inputString = String(codeString).trim(); // 处理空字符串或无效格式 if (inputString === '' || inputString === 'null' || inputString === 'undefined') { return returnVal; } // 替换全角逗号、中文逗号等非标准逗号为标准英文逗号 const normalizedString = inputString .replace(/,/g, ',') // 中文全角逗号 .replace(/,/g, ',') // 中文半角逗号(如果存在) .replace(/、/g, ',') // 日语顿号 .replace(/٫/g, ','); // 阿拉伯语逗号 // 使用标准逗号分割字符串并去除首尾空格 const resultArray = normalizedString .split(',') .map(code => code.trim()) .filter(code => code.length > 0); // 过滤空字符串 // 深克隆结果数组 return JSON.parse(JSON.stringify(resultArray)); } /** * 手机号脱敏处理 * @param {string} phone - 原始手机号 * @returns {string} 脱敏后的手机号,如138****5678 */ export function maskPhoneNumber(phone) { // 检查输入是否为有效的11位数字手机号 if (!/^1[3-9]\d{9}$/.test(phone)) { console.warn('请输入有效的11位手机号'); return phone; // 若无效则返回原始值 } // 保留前3位和后4位,中间4位用*替换 return phone.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2'); } // 封装一个高阶函数,接收一个函数作为参数,返回一个新的只执行一次的函数 export function once(fn) { let executed = false; // 标记函数是否已执行 return function (...args) { if (!executed) { executed = true; // 首次执行时标记为已执行 // 调用原函数并返回其结果,使用apply确保this指向正确 return fn.apply(this, args); } return; }; }