chenhaozhe
2025-10-11 7f6fa7d8b4efe045ecb50e9ec22a811255214e5f
utils/common.js
@@ -1,9 +1,11 @@
class commonUtils {
    serverUrl
    audioContext // 全局音频实例
    requestLock // 请求控制锁(同步)
    constructor() {
        this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API/';
        this.audioContext = null;
        this.requestLock = false
    }
    setServerUrl(url) {
@@ -303,6 +305,42 @@
        })
    }
    // 同步执行请求 (配合await或者then)
    async doRequest2Sync({
        url,
        data,
        method,
    }) {
        if(this.requestLock){
            console.warn("该请求被锁定,已退出请求!")
            return
        }
        this.requestLock = true
        return new Promise((resolve, reject) => {
            // that = that || this;
            let errorTip = null;
            uni.showLoading({
                title: '加载中...'
            })
            uni.request({
                method: method || "GET",
                url: this.serverUrl + url,
                data: data || "",
                success: (res) => {
                    resolve(res)
                },
                fail: (err) => {
                    reject(err)
                },
                complete: () => {
                    // 释放请求锁
                    this.requestLock = false
                    uni.hideLoading()
                }
            })
        })
    }
    stringToBoolean(str) {
        // 忽略大小写的转换
        return str?.toLowerCase() === "true";