lilishop-uniapp/utils/tools.js

119 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-05-13 11:03:32 +08:00
/**
* 解析url参数
* @example ?id=12345&a=b
* @return Object {id:12345,a:b}
*/
function urlParse(url) {
2021-06-07 11:15:47 +08:00
let obj = {};
let reg = /[?&][^?&]+=[^?&]+/g;
let arr = url.match(reg);
if (arr) {
2022-11-30 11:32:45 +08:00
arr.forEach(item => {
2021-06-07 11:15:47 +08:00
let tempArr = item.substring(1).split("=");
let key = decodeURIComponent(tempArr[0]);
let val = decodeURIComponent(tempArr.splice(1).join("="));
obj[key] = val;
});
}
return obj;
}
2021-05-13 11:03:32 +08:00
const getNetworkType = () => {
2021-06-07 11:15:47 +08:00
uni.getNetworkType({
2022-11-30 11:32:45 +08:00
success: res => {
2021-06-07 11:15:47 +08:00
if (res.networkType === "none") {
uni.showToast({
title: "网络好像有点问题,请检查后重试!",
duration: 2000,
2022-11-30 11:32:45 +08:00
icon: "none"
2021-06-07 11:15:47 +08:00
});
let pages = getCurrentPages();
if (pages.length) {
let route = pages[pages.length - 1].route;
if (route !== "pages/empty/empty") {
uni.navigateTo({
2022-11-30 11:32:45 +08:00
url: `/pages/empty/empty?type=wifi`
2021-06-07 11:15:47 +08:00
});
}
} else {
uni.navigateTo({
2022-11-30 11:32:45 +08:00
url: `/pages/empty/empty?type=wifi`
2021-06-07 11:15:47 +08:00
});
}
}
2022-11-30 11:32:45 +08:00
}
2021-06-07 11:15:47 +08:00
});
};
2021-05-13 11:03:32 +08:00
const throttle = (fn, that, gapTime) => {
2021-06-07 11:15:47 +08:00
// 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;
}
};
2021-05-13 11:03:32 +08:00
/**
* 计算传秒数的倒计时
* @param seconds
* @returns {{day : *, hours : *, minutes : *, seconds : *}}
*/
2022-11-30 11:32:45 +08:00
const countTimeDown = seconds => {
const leftTime = time => {
2021-06-07 11:15:47 +08:00
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)),
2022-11-30 11:32:45 +08:00
seconds: leftTime(parseInt(seconds % 60, 10))
2021-06-07 11:15:47 +08:00
};
};
2021-05-13 11:03:32 +08:00
/**
* 计算当前时间到第二天0点的倒计时[]
* @returns {number}
*/
const theNextDayTime = () => {
2021-06-07 11:15:47 +08:00
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);
};
2022-11-30 11:32:45 +08:00
// 防抖
const debounce = (fn, delay) => {
// 时间期限
var delays = delay || 200;
var timer;
// 闭包
return function() {
// 考虑作用域上下文环境apply需要用到this对象
var th = this;
// 接收的参数用 ES6 中的 rest 参数统一存储到变量 args 中。arguments就是传入的参数数组,而且个数可以不确定的传回给fn不确定函数到底有多少个参数用arguments来接收
var args = arguments;
// 判断还在定时,说明当前正在一个计时过程中,并且又触发了相同事件。所以要取消当前的计时,重新开始计时
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(function() {
timer = null;
// 执行方法
fn.apply(th, args);
}, delays);
};
2021-06-07 11:15:47 +08:00
};
2022-11-30 11:32:45 +08:00
export { getNetworkType, throttle, countTimeDown, theNextDayTime, debounce };