yfs/common/util.js
2025-05-20 00:29:56 +08:00

51 lines
1.3 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.

/**
* 延迟执行
* @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 {string} msg
*/
export function msg(msg) {
let title = msg || ''
let icon = 'none'
let mask = false
let duration = 1500
let position = 'center'
uni.showToast({
title,
icon,
mask,
duration,
position,
});
};