You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
4.6 KiB
157 lines
4.6 KiB
// import { isAsync } from "./isPending";
|
|
|
|
const _promiseThrottleMap = {};
|
|
|
|
/**
|
|
* 节流函数
|
|
* @description 本函数接收一个Promise,当Promise在pending状态中,会自动阻止下次继续触发,在finally后释放。
|
|
* 同时允许传入一个时限,如果达到时限Promise还未finally,会执行onTime回调
|
|
* @param {string|number} promise_id 节流ID,同ID的内部节流,不同ID的互不干扰
|
|
* @param {number} time x毫秒后触发onTime,如果在此之前Promise已经finally,则不执行onTime
|
|
* @param {Function} promiseFunction 要节流的Promise函数,该函数应该返回一个Promise
|
|
* @param {Function} onTime 事件
|
|
* @returns {Promise} 节流后的Promise
|
|
*/
|
|
export function promiseThrottle(promise_id, time = 500, promiseFunction, onTime) {
|
|
if (_promiseThrottleMap[promise_id] === true)
|
|
return Promise.reject(new Error("请求正在进行中"));
|
|
|
|
let timeout;
|
|
if (typeof time == "number" && onTime)
|
|
timeout = setTimeout(onTime, time);
|
|
|
|
_promiseThrottleMap[promise_id] = true;
|
|
return promiseFunction().finally(() => {
|
|
_promiseThrottleMap[promise_id] = false;
|
|
clearTimeout(timeout);
|
|
});
|
|
}
|
|
|
|
export function sleep(timeout = 3000) {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve();
|
|
}, timeout);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* status 状态栏 | menu 小程序胶囊 | screen 屏幕
|
|
* @returns {{
|
|
* status: number,
|
|
* menu: number,
|
|
* screen: number
|
|
* }} - safeHeight
|
|
*/
|
|
export function useHeight() {
|
|
const safeHeight = {};
|
|
|
|
safeHeight.status = uni.getSystemInfoSync().statusBarHeight || 0;
|
|
// #ifdef APP
|
|
uni.hideTabBar();
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
const menuButton = uni.getMenuButtonBoundingClientRect();
|
|
// menuHeight.value = menuButton.height;
|
|
safeHeight.menu = menuButton.height;
|
|
safeHeight.status = menuButton.top;
|
|
// #endif
|
|
|
|
// #ifdef APP-PlUS
|
|
safeHeight.status = uni.getSystemInfoSync().statusBarHeight || 45;
|
|
// #endif
|
|
|
|
const info = uni.getWindowInfo();
|
|
safeHeight.screen = info.screenHeight;
|
|
safeHeight.bottom = info.screenHeight - info.safeArea.bottom;
|
|
safeHeight.menu = safeHeight.menu || 0;
|
|
|
|
return safeHeight;
|
|
}
|
|
|
|
/**
|
|
* 通过类或者id 获取元素高度
|
|
* @param {string} idOrClass
|
|
* @returns {Promise{number}} 元素高度
|
|
*/
|
|
export function getHeight(idOrClass) {
|
|
return new Promise((resolve) => {
|
|
const query = uni.createSelectorQuery();
|
|
query.select(idOrClass).boundingClientRect((data) => {
|
|
if ("height" in data) {
|
|
resolve(data.height);
|
|
}
|
|
}).exec();
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 返回上一页或首页
|
|
*/
|
|
export function gotoBack() {
|
|
// uni.navigateBack();
|
|
// 获取当前页面栈信息
|
|
const pages = getCurrentPages();
|
|
|
|
// 页面栈长度为1时,说明当前是首页或无法返回,直接跳转到首页
|
|
if (pages.length <= 1) {
|
|
uni.switchTab({
|
|
url: "/pages/home/home", // 替换为你的首页路径
|
|
});
|
|
}
|
|
else {
|
|
// 页面栈长度大于1时,执行返回操作
|
|
uni.navigateBack({
|
|
delta: 1, // 返回的页面数
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* toast配置
|
|
* @typedef {object} toastConfig
|
|
* @property {string} title - toast标题
|
|
* @property {string} icon - toast图标,默认none
|
|
* @property {boolean} returnBoolean - 是否返回字符串,默认返回布尔值
|
|
*/
|
|
|
|
/**
|
|
* 校验器
|
|
* @example
|
|
* const validators = [
|
|
* () => !email && "请输入邮箱", // && if true: return msge
|
|
* ];
|
|
* validates(validators);
|
|
* @param {Array<Function>|Function} validators 校验函数数组,每个函数返回校验错误信息或空字符串
|
|
* @param {toastConfig} toastConfig 校验失败时的toast配置
|
|
* @returns {string|boolean} 校验错误信息或空字符串
|
|
*/
|
|
export function validates(validators, toastConfig = { icon: "none", returnBoolean: true }) {
|
|
const validatorsFlat = Array.isArray(validators) ? validators : [validators];
|
|
// const hasAsync = validatorsFlat.some(item => isAsync(item));
|
|
// if (!hasAsync) {
|
|
// 同步校验:顺序执行,遇到第一条错误立即返回
|
|
for (const validator of validatorsFlat) {
|
|
const msg = validator();
|
|
if (msg) {
|
|
console.log(msg, "校验失败");
|
|
return toastConfig.returnBoolean ? false : msg;
|
|
}
|
|
}
|
|
return toastConfig.returnBoolean ? true : "";
|
|
// }
|
|
// else {
|
|
// // 异步校验:顺序执行,遇到第一条错误立即返回
|
|
// return (async () => {
|
|
// for (const validator of validatorsFlat) {
|
|
// const msg = await validator();
|
|
// if (msg) {
|
|
// console.log(msg, "校验失败");
|
|
// return toastConfig.returnBoolean ? false : msg;
|
|
// }
|
|
// }
|
|
// return toastConfig.returnBoolean ? true : "";
|
|
// })();
|
|
// }
|
|
}
|