yusijie
2025-11-12 66799de4f91612bdfecefc9ef6fdf3d2d5f52d31
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,70 @@
        })
    }
    // 同步执行请求 (配合await或者then)
    async doRequest2Sync({
        url,
        data,
        method,
    }) {
        if (this.requestLock) {
            console.log("该请求被锁定,不能重复请求!!!")
            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()
                }
            })
        })
    }
    async doRequest2Async({
        url,
        data,
        method,
    }) {
        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: () => {
                    uni.hideLoading()
                }
            })
        })
    }
    stringToBoolean(str) {
        // 忽略大小写的转换
        return str?.toLowerCase() === "true";
@@ -319,14 +385,14 @@
        innerAudioContext.play(); // 播放音频
        innerAudioContext.onPlay(() => {
            console.log('开始播放');
            //console.log('开始播放');
        });
        innerAudioContext.onError((res) => {
            console.log(res.errMsg);
            console.log(res.errCode);
        });
        innerAudioContext.onPause(function() {
            console.log('播放出现错误,销毁');
            //console.log('播放暂停,销毁');
            innerAudioContext.destroy();
        })
    }
@@ -362,6 +428,29 @@
    // }
    showTips({
        type,
        message,
        title,
        duration
    }) {
        if (!message) {
            return
        }
        if (message.length < 20) {
            return uni.showToast({
                icon: type || 'none',
                title: message
            })
        }
        return uni.showModal({
            title: title,
            content: message,
            showCancel: false
        })
    }
    replaceWithFunction(str, handler) {
        return str.replace(/\{(.+?)\}/g, (match, key) => {