app/utils/tools.js
2025-05-12 14:51:35 +08:00

314 lines
7.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 解析url参数
* @example ?id=12345&a=b
* @return Object {id:12345,a:b}
*/
function urlParse(url) {
let obj = {};
let reg = /[?&][^?&]+=[^?&]+/g;
let arr = url.match(reg);
if (arr) {
arr.forEach((item) => {
let tempArr = item.substring(1).split("=");
let key = decodeURIComponent(tempArr[0]);
let val = decodeURIComponent(tempArr.splice(1).join("="));
obj[key] = val;
});
}
return obj;
}
const getNetworkType = () => {
uni.getNetworkType({
success: (res) => {
if (res.networkType === "none") {
uni.showToast({
title: "网络好像有点问题,请检查后重试!",
duration: 2000,
icon: "none",
});
let pages = getCurrentPages();
if (pages.length) {
let route = pages[pages.length - 1].route;
if (route !== "pages/empty/empty") {
uni.navigateTo({
url: `/pages/empty/empty?type=wifi`,
});
}
} else {
uni.navigateTo({
url: `/pages/empty/empty?type=wifi`,
});
}
}
},
});
};
const throttle = (fn, that, gapTime) => {
// export function throttle(fn, gapTime) {
if (gapTime == null || gapTime == undefined) {
gapTime = 1800;
}
let _lastTime = that.lastTime;
let _nowTime = +new Date();
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(that, arguments); //将this和参数传给原函数
that.lastTime = _nowTime;
}
};
/**
* 计算传秒数的倒计时【天、时、分、秒】
* @param seconds
* @returns {{day : *, hours : *, minutes : *, seconds : *}}
*/
const countTimeDown = (seconds) => {
const leftTime = (time) => {
if (time < 10) time = "0" + time;
return time + "";
};
return {
day: leftTime(parseInt(seconds / 60 / 60 / 24, 10)),
hours: leftTime(parseInt((seconds / 60 / 60) % 24, 10)),
minutes: leftTime(parseInt((seconds / 60) % 60, 10)),
seconds: leftTime(parseInt(seconds % 60, 10)),
};
};
/**
* 计算当前时间到第二天0点的倒计时[秒]
* @returns {number}
*/
const theNextDayTime = () => {
const nowDate = new Date();
const time =
new Date(
nowDate.getFullYear(),
nowDate.getMonth(),
nowDate.getDate() + 1,
0,
0,
0
).getTime() - nowDate.getTime();
return parseInt(time / 1000);
};
const graceNumber = (number) => {
// if (number == 0) {
// return "0";
// } else if (number > 999 && number <= 9999) {
// return (number / 1000).toFixed(1) + "k";
// } else if (number > 9999 && number <= 99999) {
// return (number / 10000).toFixed(1) + "w";
// } else if (number > 99999) {
// return "10w+";
// }
// return number;
if (number == 0) {
return "0";
}
if (number < 1000) {
return number.toString();
} else if (number < 10000) {
return (number / 1000).toFixed(1).replace(/\.0$/, '') + "k";
} else if (number < 100000000) {
return (number / 10000).toFixed(1).replace(/\.0$/, '') + "w";
} else {
return (number / 100000000).toFixed(1).replace(/\.0$/, '') + "亿+";
}
}
// 时间格式化时间为: 多少分钟前、多少天前
// time 2020-09-10 20:20:20
const getDateBeforeNow = (stringTime = '') => {
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
// 获取当前时间戳
const currentTime = new Date().getTime();
// 如果没有传入时间参数,使用当前时间
const time = stringTime ? new Date(stringTime.replace(/-/g, "/")).getTime() : currentTime;
const difference = currentTime - time; // 计算时间差
let result = null;
// 如果时间差小于0未来的时间返回原始时间
if (difference < 0) {
result = stringTime;
} else if (difference >= month) {
result = Math.floor(difference / month) + "月前";
} else if (difference >= week) {
result = Math.floor(difference / week) + "周前";
} else if (difference >= day) {
result = Math.floor(difference / day) + "天前";
} else if (difference >= hour) {
result = Math.floor(difference / hour) + "小时前";
} else if (difference >= minute) {
result = Math.floor(difference / minute) + "分钟前";
} else {
result = "刚刚";
}
return result;
// let minute = 1000 * 60;
// let hour = minute * 60;
// let day = hour * 24;
// let week = day * 7;
// let month = day * 30;
// let time1 = new Date().getTime(); //当前的时间戳
// if (stringTime == '') {
// stringTime = time1
// } else {
// stringTime = new Date(stringTime.replace(/-/g, "/"));
// }
// let time2 = Date.parse(new Date(stringTime)); //指定时间的时间戳
// let time = time1 - time2;
// let result = null;
// if (time < 0) {
// result = stringTime;
// } else if (time / month >= 1) {
// result = parseInt(time / month) + "月前";
// } else if (time / week >= 1) {
// result = parseInt(time / week) + "周前";
// } else if (time / day >= 1) {
// result = parseInt(time / day) + "天前";
// } else if (time / hour >= 1) {
// result = parseInt(time / hour) + "小时前";
// } else if (time / minute >= 1) {
// result = parseInt(time / minute) + "分钟前";
// } else {
// result = "刚刚";
// }
// return result;
}
// 判断是否为空
const isStrEmpty = (str) => {
if (
str == null ||
str == undefined ||
(typeof str == "object" && str == {}) ||
(typeof str == "string" && str.trim() == "")
) {
return true;
} else {
return false;
}
}
// 获得星座
const getAstro = (m, d) => {
return "魔羯水瓶双鱼白羊金牛双子巨蟹狮子处女天秤天蝎射手魔羯".substr(
m * 2 - (d < "102223444433".charAt(m - 1) - -19) * 2,
2
);
}
// 获得生肖
const getAnimal = (year) => {
year = year % 12;
let animal = "";
switch (year) {
case 1:
animal = "鸡";
break;
case 2:
animal = "狗";
break;
case 3:
animal = "猪";
break;
case 4:
animal = "鼠";
break;
case 5:
animal = "牛";
break;
case 6:
animal = "虎";
break;
case 7:
animal = "兔";
break;
case 8:
animal = "龙";
break;
case 9:
animal = "蛇";
break;
case 10:
animal = "马";
break;
case 11:
animal = "羊";
break;
case 0:
animal = "猴";
break;
}
return animal;
}
// 日期格式化
const dateFormat = (fmt, date) => {
let ret;
const opt = {
"Y+": date.getFullYear().toString(), // 年
"M+": (date.getMonth() + 1).toString(), // 月
"D+": date.getDate().toString(), // 日
"H+": date.getHours().toString(), // 时
"m+": date.getMinutes().toString(), // 分
"s+": date.getSeconds().toString(), // 秒
};
for (let k in opt) {
ret = new RegExp("(" + k + ")").exec(fmt);
if (ret) {
fmt = fmt.replace(
ret[1],
ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, "0")
);
}
}
return fmt;
}
const clickFeedBack = (fn) => {
// #ifdef APP-PLUS
if (uni.getSystemInfoSync().platform == 'ios') {
let UIImpactFeedbackGenerator = plus.ios.importClass('UIImpactFeedbackGenerator');
let impact = new UIImpactFeedbackGenerator();
impact.prepare();
impact.init(2);
impact.impactOccurred();
}
if (uni.getSystemInfoSync().platform == 'android') {
// vibrateShort
// uni.vibrateLong({
uni.vibrateShort({
success: () => {
console.log('点击震动');
}
});
}
// #endif
}
export {
getNetworkType,
throttle,
countTimeDown,
theNextDayTime,
graceNumber,
getDateBeforeNow,
isStrEmpty,
getAstro,
getAnimal,
dateFormat,
clickFeedBack
};