youdas/common/utils.js
2025-07-08 14:10:30 +08:00

167 lines
3.8 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.

import {
getUserWxInfo
} from '@/common/server/user.js'
/**
* 延迟执行
* @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);
}
});
});
}
/**
* 微信APP登录
* @returns {Promise} 返回Promise对象resolve中返回{unionId, nickName, avatarUrl, loginRes, infoRes}
*/
export function wxAppLogin() {
return new Promise((resolve) => {
uni.showLoading({})
uni.login({
provider: 'weixin',
onlyAuthorize: true,
success: async function(loginRes) {
console.log("loginResloginResloginRes", loginRes);
if (loginRes.code) {
const userInfo = await getUserWxInfo(loginRes.code);
uni.hideLoading();
console.log(userInfo);
if (userInfo) {
resolve({
unionId: userInfo.unionid,
nickName: userInfo.nickname,
avatarUrl: userInfo.headimgurl,
openid: userInfo.openid
// loginRes: loginRes,
// infoRes: infoRes
});
return;
}
}
return;
},
fail: function(e) {
console.log(e);
uni.hideLoading();
}
});
});
}
/**
*
*/
export function getOS() {
// #ifdef APP-PLUS
const systemInfo = uni.getSystemInfoSync();
return systemInfo.platform === 'ios' ? 'ios' : 'android';
// #endif
// 非 App 环境返回 undefined 或其他值
return undefined;
}