186 lines
4.9 KiB
JavaScript
Raw Normal View History

2025-04-14 10:57:27 +08:00
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, time) => {
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) {
2025-07-08 10:04:47 +08:00
setTimeout(function () {
2025-04-14 10:57:27 +08:00
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,
2025-07-08 10:04:47 +08:00
success: function (res) {
2025-04-14 10:57:27 +08:00
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;
}
2025-07-08 10:04:47 +08:00
export function isNullOrEmpty(value) {
if (typeof (value) == "undefined" || value == null || value == "")
2025-04-14 10:57:27 +08:00
return true;
else
return false;
}
//获取导航栏高度
export function getNavHeight(callback) {
// 获取胶囊按钮信息
const menuButtonInfo = uni.getMenuButtonBoundingClientRect();
uni.getSystemInfo({
success: (res) => {
2025-07-08 10:04:47 +08:00
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;
2025-07-08 10:04:47 +08:00
const height = screenHeight - navHeight;
callback(height);
}
});
2025-07-08 10:04:47 +08:00
}
/**
* 将字符串转化为数组
* @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));
2025-04-14 10:57:27 +08:00
}