111 lines
2.9 KiB
JavaScript
111 lines
2.9 KiB
JavaScript
/**
|
||
* 延迟执行
|
||
* @param {Number} ms
|
||
* @returns
|
||
*/
|
||
export function sleep(ms) {
|
||
return new Promise(resolve => setTimeout(resolve, ms));
|
||
}
|
||
/**
|
||
* 解析查询字符串
|
||
* @param {string} urlOrQueryString
|
||
* @returns {Object} 查询参数对象
|
||
*/
|
||
export function parseQueryString(urlOrQueryString) {
|
||
// 如果传入的是完整URL(如 "/path?name=value"),提取查询部分
|
||
let queryString = urlOrQueryString;
|
||
const questionMarkIndex = queryString.indexOf('?');
|
||
if (questionMarkIndex !== -1) {
|
||
queryString = queryString.slice(questionMarkIndex + 1);
|
||
}
|
||
|
||
const params = {};
|
||
if (!queryString) return params; // 如果没有查询参数,返回空对象
|
||
|
||
const pairs = queryString.split('&');
|
||
for (const pair of pairs) {
|
||
const [key, value] = pair.split('=');
|
||
// 解码 URI 组件,并处理无值情况(如 "key" 而不是 "key=value")
|
||
params[key] = value ? decodeURIComponent(value) : '';
|
||
}
|
||
|
||
return params;
|
||
}
|
||
|
||
/**
|
||
* 显示确认弹窗
|
||
* @param {Object} options 弹窗选项
|
||
* @param {String} options.title 弹窗标题
|
||
* @param {String} options.content 弹窗内容
|
||
* @param {String} options.confirmText 确认按钮文字
|
||
* @param {String} options.cancelText 取消按钮文字
|
||
* @returns {Promise} 返回Promise对象,resolve中返回{confirm: Boolean},表示是否点击了确认按钮
|
||
*/
|
||
export function showModal(options = {}) {
|
||
return new Promise((resolve) => {
|
||
uni.showModal({
|
||
title: options.title || '提示',
|
||
content: options.content || '',
|
||
confirmText: options.confirmText || '确定',
|
||
cancelText: options.cancelText || '取消',
|
||
success(res) {
|
||
resolve(res);
|
||
},
|
||
fail() {
|
||
resolve({ confirm: false });
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 显示提示信息
|
||
* @param {*} title 提示信息
|
||
* @param {*} icon 图标
|
||
* @param {*} duration 提示时长
|
||
* @returns
|
||
*/
|
||
export function showToast(title, icon = "none", duration = 1500) {
|
||
return new Promise((resolve) => {
|
||
uni.showToast({
|
||
title: title || '',
|
||
icon: icon || 'none',
|
||
duration: duration,
|
||
success: () => {
|
||
resolve(true);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 显示加载中
|
||
* @param {String} title 加载中文字
|
||
* @returns {Promise} 返回Promise对象,resolve中返回true
|
||
*/
|
||
export function showLoading(title = "加载中...") {
|
||
return new Promise((resolve) => {
|
||
uni.showLoading({
|
||
title: title,
|
||
success: () => {
|
||
resolve(true);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* 隐藏加载中
|
||
* @returns {Promise} 返回Promise对象,resolve中返回true
|
||
*/
|
||
export function hideLoading() {
|
||
return new Promise((resolve) => {
|
||
uni.hideLoading({
|
||
success: () => {
|
||
resolve(true);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|