/**
* @description 防抖
* const debounceFn = debounce(function() { }[, 1000])
* debounceFn(args)
* or
* handleFn: debounce(function() {this.fn()}, 1000)
* @param {Function} fn
* @param {Number} [delay] 500
* @return {Function}
*/
export const debounce = (fn, delay = 500) => {
let flag = true,
timer = null;
return function(...args) {
if (flag) {
fn.apply(this, args);
flag = false;
}
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
clearTimeout(timer);
flag = true;
}, delay);
};
};
/**
* @description 节流
* const throttleFn = throttle(function() { }[, 1000])
* throttleFn(args)
* or
* handleFn: throttle(function() {this.fn()}, 1000)
* @param {Function} fn
* @param {Number} [delay] 500
* @return {Function}
*/
export const throttle = (fn, delay = 500)=> {
var timer = null;
var startTime = Date.now();
return function(...args) {
var currentTime = Date.now();
var remaining = delay - (currentTime - startTime);
clearTimeout(timer);
if (remaining <= 0) {
fn.apply(this, args);
startTime = Date.now();
} else {
timer = setTimeout(() => {
fn.apply(this, args);
}, remaining);
}
};
};
/**
* @description 获取 URL 参数
* getUrlParams("http://abc.com?id=1", "id")
* OR
* getUrlParams("http://abc.com?id=1").id
* @param {String} url 普通链接
* @param {String} [key] 需要获取的参数;不传返回所有参数(Object)
* @return {String | Object}
*/
export const getUrlParams = (url, key) => {
const params = {};
if (url) {
const paramsStr = url.split("?")[1];
if (paramsStr) {
const paramsArr = paramsStr.split("&"),
len = paramsArr.length;
let i = 0;
for (; i < len; i++) {
let arr = paramsArr[i].split("=");
if (arr[0] === key) {
return arr[1];
} else {
params[arr[0]] = arr[1];
}
}
}
};
return params;
}
/**
* @description object 数据拼接为 URL 参数
* const url ="/pages/index/index?" + buildUrlParams({ a: 1, b: 2})
* @param {Object} options
* @return {String}
*/
export const buildUrlParams = (options) => {
let parts = [];
for (const [key, value] of Object.entries(options)) {
if (key !== 'path') {
parts.push(`${key}=${value}`);
}
}
return parts.join("&");
}
/**
* @description 版本比较
* compareVersion("1.0.0", "2.0.0")
* @param {Sting} v1
* @param {String} v2
* @return {Number} 0: v1 = v2 | 1: v1 > v2 | -1: v1 < v2
*/
export const compareVersion = (v1, v2) =>{
v1 = v1.split(".");
v2 = v2.split(".");
const len = Math.max(v1.length, v2.length);
while (v1.length < len) {
v1.push("0");
}
while (v2.length < len) {
v2.push("0");
}
for (let i = 0; i < len; i++) {
const n1 = parseInt(v1[i]);
const n2 = parseInt(v2[i]);
if (n1 > n2) {
return 1;
} else if (n1 < n2) {
return -1;
}
}
return 0;
}
import { debounce,
throttle,
getUrlParams,
buildUrlParams,
compareVersion, } from "@/utils/utils.js" 引入所需的js
防抖例子
// 领取前
onReceiveBefore: debounce(async function() {
if (!this.guiderData.id) {
uni.showModal({
title: "无法领取",
content: "该店铺下还未有导购",
showCancel: false,
})
return;
};
const isAttention = this.idata.isAttention === true ? await this.getWeChatWorkAttentionStatus() : true;
if (!isAttention) {
this.$emit("attention", this.idata)
return;
}
uni.requestSubscribeMessage({
tmplIds: [subscribeMessage.receiveSuccess],
success: (res) => {
console.log(res)
},
fail: (err) => {
console.log(err)
},
complete: () => {
this.onReceive();
}
})
}, 2000),
解码网络连接获取需要的参数
this.giftDetailId = getUrlParams(decodeURIComponent(this.options.q), "giftDetailId")
const url = decodeURIComponent(options.q);
const params = getUrlParams(url);
跳转连接传参
onToRobot() {
uni.navigateTo({
url: `/pages/robot/robot?${buildUrlParams(this.options)}`,
})
}