美文网首页uni-app
uni-app统一处理登录失效重新登录

uni-app统一处理登录失效重新登录

作者: LingSun | 来源:发表于2020-11-25 19:11 被阅读0次
    if ((_res.data.msg + '').indexOf('路由鉴权未通过') > -1) {
        uni.showModal({
            itle: '登录异常',
            content: `${_res.data.msg},请重新登录`,
            confirmText: '重新登录',
            success: function (__res) {
            if (__res.confirm) {
                uni.reLaunch({
                    url: '/pages/login/index'  // 这里的pages前面一定要加上/,不然它不跳转的,我试的时候是这样子的
                })
            } else if (__res.cancel) {
                console.log('用户点击取消');
            }
        }
    })
    

    接口配置完整代码如下:

    apiUrl.js

    let baseUrl = ""
    let baseH5Url = ""
    if(process.env.NODE_ENV === 'development'){
        // 开发环境
        baseUrl = 'http://***.***.***.***:****' 
        baseH5Url = 'http://***.***.***.***:****';
    }else{
        // 生产环境
        baseUrl = 'http://***.***.***.***:****' 
        baseH5Url = 'http://***.***.***.***:****';
    }
    export default {
        baseUrl,
        baseH5Url
    }
    

    index.js

    import request from './request.js'
    
    const api = {
        // 登录
        login: params => {
            return request({ 
                url: '/webToken/login', 
                methods: 'POST',
                data: params
            })
        }
    }
    

    request.js

    import apiUrl from './apiUrl.js'   // 这个是调接口时的域名配置
    
    const headers = {}
        
    const request = (options) => {
        const url = apiUrl.baseUrl + options.url
        return uni.request({
            url: url,
            method: options.methods || 'GET',
            data: options.data,
            header: {
                ...headers,
                token: uni.getStorageSync('token'),
                ...options.headers
            }
        }).then(res => {
            const _res = res[1]
            if (_res.statusCode === 200) {
                if ((_res.data.msg + '').indexOf('路由鉴权未通过') > -1) {
                    uni.showModal({
                        title: '登录异常',
                        content: `${_res.data.msg},请重新登录`,
                        confirmText: '重新登录',
                        success: function (__res) {
                            if (__res.confirm) {
                                uni.reLaunch({
                                    url: '/pages/login/index'
                                })
                            } else if (__res.cancel) {
                                console.log('用户点击取消');
                            }
                        }
                })
                    
                    return
                }
                if (_res.data.code === 9999) {
                    uni.showToast({
                        icon: 'none',
                        title: _res.data.msg,
                    })
                }
                return _res.data
            } else{
                uni.showToast({
                    icon: 'none',
                    title: _res.data.msg,
                })
                throw _res.data
            }
        }).catch(err => {
            console.log(err)
            switch (err.code) {
                case 401:
                uni.clearStorageSync()
                break
                default:
                uni.showToast({
                    title: err.message,
                    icon: 'none',
                })
                return Promise.reject()
                break
            }
        })
    } 
    
    export default request
    

    相关文章

      网友评论

        本文标题:uni-app统一处理登录失效重新登录

        本文链接:https://www.haomeiwen.com/subject/dnoniktx.html