171 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2025-04-28 15:37:57 +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-08-01 09:00:22 +08:00
setTimeout(function () {
2025-04-28 15:37:57 +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-08-01 09:00:22 +08:00
success: function (res) {
2025-04-28 15:37:57 +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') : "";
};
2025-05-09 16:36:41 +08:00
export function jkcBaseEncode(text) { //加密
// 确保输入是字符串类型
if (typeof text !== 'string') {
throw new Error('Input must be a string');
}
const jkcBase = base64Encode("jkcBase")
// 第一次 Base64 编码
let firstEncode = Base64.encode(text);
// 将 "jkcBase" 插入到第一次编码结果的第二个字母后
let modifiedFirstEncode = firstEncode.slice(0, 2) + jkcBase + firstEncode.slice(2);
// 第二次 Base64 编码
let secondEncode = Base64.encode(modifiedFirstEncode);
return secondEncode;
}
export function jkcBaseDecode(text) { //解密
const jkcBase = base64Encode("jkcBase")
// 确保输入是字符串类型
if (typeof text !== 'string') {
throw new Error('Input must be a string');
}
// 第一次 Base64 解码
let firstDecode = Base64.decode(text);
// 移除 "jkcBase",它在第一次编码后的第二个字符后
let modifiedText = firstDecode.replace(jkcBase, '');
// 第二次 Base64 解码
let originalText = Base64.decode(modifiedText);
return originalText;
}
2025-04-28 15:37:57 +08:00
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-05-09 16:36:41 +08:00
export function isNullOrEmpty(value) {
2025-08-01 09:00:22 +08:00
if (typeof (value) == "undefined" || value == null || value == "")
2025-04-28 15:37:57 +08:00
return true;
else
return false;
2025-08-01 09:00:22 +08:00
}
/**
* 防抖函数
* @param {Function} fn 需要防抖的函数
* @param {Number} delay 延迟时间毫秒
* @param {Boolean} immediate 是否立即执行
* @returns {Function} 防抖后的函数
*/
export function debounce(fn, delay = 300, immediate = false) {
let timer = null;
return function (...args) {
const context = this;
if (immediate && !timer) {
fn.apply(context, args);
}
// 清除定时器
if (timer) clearTimeout(timer);
// 设置新的定时器
timer = setTimeout(() => {
if (!immediate) {
fn.apply(context, args);
}
timer = null;
}, delay);
};
2025-04-28 15:37:57 +08:00
}