chenhaozhe
2025-07-31 b49123cf09c77425141c5fdc9297e04fbcfa832e
修复app多次退出登录/登录时,部分页面请求地址不更新的问题
2个文件已修改
501 ■■■■ 已修改文件
pages/index/login.vue 61 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
utils/common.js 440 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/index/login.vue
@@ -63,29 +63,33 @@
                    </view>
                </uni-popup-dialog>
            </uni-popup>
            <view class="btn" @tap="submit">登 录</view>
            <!-- <view class="btn apibtn" @tap="changeApi=true">服务器设置</view> -->
            <view class="btn apibtn" @click="openServerSettingHandler">服务器设置</view>
        </view>
    </view>
</template>
<script>
    import {
        setLoginData,
        setUserInfo
    } from "@/utils/auth.js";
    import hFormAlert from '@/components/h-form-alert/h-form-alert.vue';
    import {
        CommonUtils
    } from "../../utils/common";
    export default {
        components: {
            hFormAlert
        },
        data() {
            return {
                CommonUtils,
                arrayDataBases: [],
                DataBases: '',
                arrayServerOrganization: ["浙江智云迈思", "温州海诚"],
                arrayServerOrganizationValues: [
                    'http://47.96.97.237/API/',
@@ -95,7 +99,7 @@
                arrayOrganization: [],
                Organization: '',
                HOrgNameList: [],
                HOrgName: '',
                UserName: '', //admin    001
                PassWord: '', //123456
@@ -104,7 +108,7 @@
                serverUrlCache: '',
            }
        },
        onLoad() {
        onShow() {
            var logindata = uni.getStorageSync('loginData') || ''
            // console.log(logindata)
            if (logindata) {
@@ -114,9 +118,9 @@
                this.UserName = logindata.UserName
                this.PassWord = logindata.PassWord
            }
            this.serverUrlCache = this.serverUrl
            console.log('this.serverUrlCache: ',this.serverUrlCache);
            console.log('this.serverUrlCache: ', this.serverUrlCache);
            let index = this.arrayServerOrganizationValues.findIndex((e) => {
                return e == this.serverUrlCache
            })
@@ -133,11 +137,11 @@
                await this.$nextTick(() => {
                    this.Organization = this.arrayOrganization[0]
                    this.HOrgName = this.HOrgNameList[0]
                    let org = uni.getStorageSync('Organization')
                    let orgName = uni.getStorageSync('OrganizationID')
                    let orgHas = Array.from(this.arrayOrganization).findIndex((e) => e == org)
                    if(org && (orgHas != -1)) {
                    if (org && (orgHas != -1)) {
                        this.Organization = org
                        this.HOrgName = orgName
                    }
@@ -185,7 +189,7 @@
                this.DataBases = this.arrayDataBases[e.detail.value]
                uni.setStorageSync('DataBases', this.DataBases);
            },
            getOrganization() {
                uni.request({
                    url: this.serverUrl + '/Web/GetOrganizations',
@@ -268,7 +272,8 @@
                                uni.setStorageSync('login', "login");
                                uni.setStorageSync('Organization', this.Organization);
                                uni.setStorageSync('OrganizationID', this.HOrgName);
                                console.log('this.serverUrl: ', this.serverUrl);
                                this.CommonUtils.setServerUrl(this.serverUrl)
                                uni.reLaunch({
                                    url: '/pages/index/index'
                                })
@@ -288,7 +293,7 @@
        }
    }
</script>
<style lang="scss" scoped>
    .bg-img {
        position: fixed;
@@ -298,7 +303,7 @@
        left: 0;
        z-index: -1;
    }
    .container {
        width: 100%;
        height: 100%;
@@ -306,21 +311,21 @@
        flex-direction: column;
        align-items: center;
        justify-content: center;
        .title {
            margin-top: 174rpx;
            font-size: 47rpx;
            font-weight: bold;
            color: #FFFFFF;
        }
        .box {
            width: 682rpx;
            position: relative;
            top: 120rpx;
            display: flex;
            justify-content: center;
            .logo {
                width: 155rpx;
                height: 155rpx;
@@ -329,7 +334,7 @@
                z-index: 9;
                background-color: rgba(255, 255, 255, 0.5);
                border-radius: 100%;
                image {
                    width: 100%;
                    height: 100%;
@@ -337,7 +342,7 @@
                    background-color: #f0f3f7;
                }
            }
            .login {
                width: 100%;
                height: 480rpx;
@@ -348,26 +353,26 @@
                border-radius: 25rpx 0 25rpx 0;
                box-shadow: 0 6rpx 24rpx 8rpx rgba(21, 99, 210, 0.14);
                padding-top: 160rpx;
                .item {
                    display: flex;
                    align-items: center;
                    justify-content: flex-end;
                    margin-right: 38rpx;
                    margin-bottom: 70rpx;
                    .left {
                        font-size: 34rpx;
                        font-weight: 400;
                        color: #3C3C3C;
                    }
                    .right {
                        width: 441rpx;
                        height: 68rpx;
                        background: #EBEBEB;
                        border-radius: 10rpx;
                        input {
                            height: 100%;
                            padding: 0 30rpx;
@@ -375,7 +380,7 @@
                        }
                    }
                }
                // .api{
                //     margin-left: 30rpx;
                //     font-size: 27rpx;
@@ -391,7 +396,7 @@
                // }
            }
        }
        .btn {
            width: 540rpx;
            height: 92rpx;
@@ -405,26 +410,26 @@
            text-align: center;
            font-size: 36rpx;
        }
        .apibtn {
            margin-top: 50rpx;
            background: #eec18e;
            box-shadow: 0rpx 0rpx 44rpx 7rpx rgba(253, 186, 118, 0.3);
        }
    }
    .popup-content {
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        gap: 30rpx;
        .pop-item {
            border: #EBEBEB 2rpx solid;
            border-radius: 10rpx;
            padding: 14rpx 8rpx;
        }
        >picker {
            background-color: #EBEBEB;
        }
utils/common.js
@@ -1,251 +1,255 @@
class commonUtils {
    serverUrl
    serverUrl
    constructor() {
        this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API';
    }
    constructor() {
        this.serverUrl = uni.getStorageSync('serverUrl') || 'http://47.96.97.237/API/';
    }
    // 防抖函数
    debounce(func, delay, immediate = false) {
        let timer = null;
        return function() {
            const context = this;
            const args = arguments;
    setServerUrl(url) {
        this.serverUrl = url
    }
            const callNow = immediate && !timer;
            // 如果定时器已经存在,清除它
            if (timer) {
                clearTimeout(timer);
            }
    // 防抖函数
    debounce(func, delay, immediate = false) {
        let timer = null;
        return function() {
            const context = this;
            const args = arguments;
            // 设置一个新的定时器
            timer = setTimeout(() => {
                if (!immediate) {
                    func.apply(context, args);
                }
                timer = null;
            }, delay);
            const callNow = immediate && !timer;
            // 如果定时器已经存在,清除它
            if (timer) {
                clearTimeout(timer);
            }
            // 配置立即执行
            if (callNow == true) {
                func.apply(context, args)
            }
        };
    }
            // 设置一个新的定时器
            timer = setTimeout(() => {
                if (!immediate) {
                    func.apply(context, args);
                }
                timer = null;
            }, delay);
    // 判空函数
    isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false) {
        let val = value
        // 检查是否为null  或者 undefind
        if (val === null || val === undefined) {
            return true;
        }
        //如果字符串全部是由数字构成的,则转化为数字型
        if (this.isAllDigits(val) === true) {
            val = Number(val)
        }
            // 配置立即执行
            if (callNow == true) {
                func.apply(context, args)
            }
        };
    }
        // 是否是字符串类型
        if (typeof val === 'string') {
            return val.trim().length === 0;
        }
    // 判空函数
    isEmpty(value, zeroIsEmpty = false, falseIsEmpty = false) {
        let val = value
        // 检查是否为null  或者 undefind
        if (val === null || val === undefined) {
            return true;
        }
        //如果字符串全部是由数字构成的,则转化为数字型
        if (this.isAllDigits(val) === true) {
            val = Number(val)
        }
        // 是否是数组
        if (Array.isArray(val)) {
            return val.length === 0;
        }
        // 是否是字符串类型
        if (typeof val === 'string') {
            return val.trim().length === 0;
        }
        //是否是对象
        if (typeof val === 'object') {
            return Object.keys(val).length === 0;
        }
        // 是否是数组
        if (Array.isArray(val)) {
            return val.length === 0;
        }
        // 数字类型默认不为空
        if (typeof val === 'number') {
            // 数字为0视为空
            if (zeroIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false
        }
        //是否是对象
        if (typeof val === 'object') {
            return Object.keys(val).length === 0;
        }
        // 布尔类型默认不为空
        if (typeof val === 'boolean') {
            // false值视为空
            if (falseIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false;
        }
    }
        // 数字类型默认不为空
        if (typeof val === 'number') {
            // 数字为0视为空
            if (zeroIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false
        }
    // 判断是否全是数字
    isAllDigits(str) {
        return /^\d+$/.test(str);
    }
        // 布尔类型默认不为空
        if (typeof val === 'boolean') {
            // false值视为空
            if (falseIsEmpty == true) {
                if (val === 0) {
                    return true
                }
            }
            return false;
        }
    }
    isJson(str) {
        try {
            JSON.parse(str)
            return true
        } catch {
            return false
        }
    }
    // 判断是否全是数字
    isAllDigits(str) {
        return /^\d+$/.test(str);
    }
    timeClock(callback, delay) {
        let timeoutId;
        let isRunning = false;
    isJson(str) {
        try {
            JSON.parse(str)
            return true
        } catch {
            return false
        }
    }
        function interval() {
            timeoutId = setTimeout(() => {
                callback();
                clearTimeout(timeoutId); // 立即清除当前定时器ID
                if (isRunning) {
                    interval();
                }
            }, delay);
        }
    timeClock(callback, delay) {
        let timeoutId;
        let isRunning = false;
        return {
            start() {
                if (!isRunning) {
                    isRunning = true;
                    interval();
                }
            },
            stop() {
                if (isRunning) {
                    isRunning = false;
                    clearTimeout(timeoutId);
                }
            }
        };
    }
        function interval() {
            timeoutId = setTimeout(() => {
                callback();
                clearTimeout(timeoutId); // 立即清除当前定时器ID
                if (isRunning) {
                    interval();
                }
            }, delay);
        }
    deepClone(target, map = new WeakMap()) {
        // 处理原始值和函数(typeof 数组会返回object)
        if (typeof target !== 'object' || target === null) {
            return target;
        }
        return {
            start() {
                if (!isRunning) {
                    isRunning = true;
                    interval();
                }
            },
            stop() {
                if (isRunning) {
                    isRunning = false;
                    clearTimeout(timeoutId);
                }
            }
        };
    }
        // 处理循环引用
        if (map.has(target)) {
            return map.get(target);
        }
    deepClone(target, map = new WeakMap()) {
        // 处理原始值和函数(typeof 数组会返回object)
        if (typeof target !== 'object' || target === null) {
            return target;
        }
        let clone;
        // 处理循环引用
        if (map.has(target)) {
            return map.get(target);
        }
        // 处理数组
        if (Array.isArray(target)) {
            clone = [];
            map.set(target, clone);
            target.forEach((item, index) => {
                clone[index] = deepClone(item, map);
            });
            return clone;
        }
        let clone;
        // 处理日期对象
        if (target instanceof Date) {
            clone = new Date(target.getTime());
            map.set(target, clone);
            return clone;
        }
        // 处理数组
        if (Array.isArray(target)) {
            clone = [];
            map.set(target, clone);
            target.forEach((item, index) => {
                clone[index] = deepClone(item, map);
            });
            return clone;
        }
        // 处理正则表达式
        if (target instanceof RegExp) {
            clone = new RegExp(target);
            map.set(target, clone);
            return clone;
        }
        // 处理日期对象
        if (target instanceof Date) {
            clone = new Date(target.getTime());
            map.set(target, clone);
            return clone;
        }
        // 处理函数 (直接使用函数的引用)
        if (typeof target === 'function') {
            return target;
        }
        // 处理正则表达式
        if (target instanceof RegExp) {
            clone = new RegExp(target);
            map.set(target, clone);
            return clone;
        }
        // 处理其他对象(普通对象、类实例等)
        clone = Object.create(Object.getPrototypeOf(target));
        map.set(target, clone);
        // 处理函数 (直接使用函数的引用)
        if (typeof target === 'function') {
            return target;
        }
        // 获取所有属性(包括 Symbol 类型)
        const allKeys = [...Object.getOwnPropertyNames(target), ...Object.getOwnPropertySymbols(target)];
        // 处理其他对象(普通对象、类实例等)
        clone = Object.create(Object.getPrototypeOf(target));
        map.set(target, clone);
        allKeys.forEach(key => {
            const descriptor = Object.getOwnPropertyDescriptor(target, key);
            if (descriptor && descriptor.enumerable) {
                // 递归复制属性值
                clone[key] = deepClone(target[key], map);
            }
        });
        // 获取所有属性(包括 Symbol 类型)
        const allKeys = [...Object.getOwnPropertyNames(target), ...Object.getOwnPropertySymbols(target)];
        return clone;
    }
        allKeys.forEach(key => {
            const descriptor = Object.getOwnPropertyDescriptor(target, key);
            if (descriptor && descriptor.enumerable) {
                // 递归复制属性值
                clone[key] = deepClone(target[key], map);
            }
        });
    // uni-app 使用 封装请求函数 使用传统函数当作回调需要传that,箭头函数不需要
    doRequest(url, data, resFunction, errFunction, method, that) {
        that = that || this;
        let errorTip = null;
        uni.showLoading({
            title: '加载中...'
        })
        uni.request({
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问成功回调函数类型不为函数或者空!")
                }
            },
            fail: (err) => {
                console.error(err)
                errorTip = () => {
                    uni.showToast({
                        icon: "none",
                        title: err.errMsg || err.data.message || "接口异常!",
                        duration: 2000
                    })
                }
                if (typeof errFunction === 'function') {
                    errFunction.call(that, err)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问失败回调函数类型不为函数或者空!")
                }
        return clone;
    }
            },
            complete() {
                setTimeout(() => {
                    uni.hideLoading()
                    if (errorTip != null) {
                        errorTip()
                    }
                }, 1000)
            }
        })
    }
    // uni-app 使用 封装请求函数 使用传统函数当作回调需要传that,箭头函数不需要
    doRequest(url, data, resFunction, errFunction, method, that) {
        that = that || this;
        let errorTip = null;
        uni.showLoading({
            title: '加载中...'
        })
        uni.request({
            method: method || "GET",
            url: this.serverUrl + url,
            data: data || "",
            success: (res) => {
                if (typeof resFunction === 'function') {
                    resFunction.call(that, res)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问成功回调函数类型不为函数或者空!")
                }
            },
            fail: (err) => {
                console.error(err)
                errorTip = () => {
                    uni.showToast({
                        icon: "none",
                        title: err.errMsg || err.data.message || "接口异常!",
                        duration: 2000
                    })
                }
                if (typeof errFunction === 'function') {
                    errFunction.call(that, err)
                } else if (typeof errFunction === 'undefined' || errFunction === null) {
                    return
                } else {
                    throw new TypeError("访问失败回调函数类型不为函数或者空!")
                }
    // uni-app 播放音频封装
    playSound(e) {
        const innerAudioContext = uni.createInnerAudioContext();
        if (e == 1) {
            innerAudioContext.src = '/static/success.wav';
        } else {
            innerAudioContext.src = '/static/jingbao.wav';
        }
        innerAudioContext.play(); // 播放音频
    }
            },
            complete() {
                setTimeout(() => {
                    uni.hideLoading()
                    if (errorTip != null) {
                        errorTip()
                    }
                }, 1000)
            }
        })
    }
    // uni-app 播放音频封装
    playSound(e) {
        const innerAudioContext = uni.createInnerAudioContext();
        if (e == 1) {
            innerAudioContext.src = '/static/success.wav';
        } else {
            innerAudioContext.src = '/static/jingbao.wav';
        }
        innerAudioContext.play(); // 播放音频
    }
}
export const CommonUtils = Object.freeze(new commonUtils());
export const CommonUtils = new commonUtils()