当前位置:网站首页>微信小程序 登录失效后处理

微信小程序 登录失效后处理

2022-06-09 10:28:00 一个假的前端男

方法一、模态框

传送门

方法二、无痕登录

还是根据方法一的请求并发处理更改 直接修改

function success(res) {
    
    console.log(res, '我成功了')
    if (res.success) {
    
        return res
    } else {
    
        if (!res.success && res.errCode == "USER50001") {
    
            wx.removeStorage({
    
                key: 'Authorization',
            })
            wx.removeStorage({
    
                key: 'Cookie',
            })

            if (!getApp().globalData.isShowModel) {
    
                getApp().globalData.isShowModel = true
                // 过期方案一 模态框
                // wx.showModal({
    
                // title: "提示",
                // content: '登录已过期,请重新登录!',
                // success: function (res) {
    
                // if (res.confirm) {
    
                // wx.reLaunch({
    
                // url: '/pages/login/index',
                // })
                // getApp().globalData.isShowModel = true

                // } else if (res.cancel) {
    
                // // console.log('用户点击取消');
                // wx.reLaunch({
    
                // url: '/pages/login/index',
                // })
                // getApp().globalData.isShowModel = true
                // }
                // }
                // })
                // 方案二 无痕登录
                wx.login({
    
                    success: res => {
    
                        isLogin(res.code)
                    }
                })
            }
        } else {
    
            wx.showToast({
    
                icon: 'none',
                title: res.msg,
            })
        }

    }
}

function isLogin(code) {
    
    wx.request({
    
        url: '', // 线上
        method: 'post',
        data: {
    
            password: '',
            isAutoLogin: false,
            login_type: 'WECHAT_MINIAPP',
            grant_type: 'password',
            appId: "appid",
            code: code,
            username: '',
        },
        header: {
    
            "Content-Type": "application/x-www-form-urlencoded",
            'Cookie': '__wpkreporterwid_=20c31724-aeb1-4d85-0fa3-67166e6379cf; token=null',
            'Authorization': 'Basic eXVuc2E6eXVuc2E='
        },
        success: function (res) {
    
            if (res.data.success) {
    
                // 直接保存 token
                wx.setStorageSync('TaoLouToken', res.data.data.accessToken)
                // 获取当前页
                var page = getCurrentPages().pop();
                if (page == undefined || page == null) return;
                // 调用当前页的onload 进行刷新
                page.onLoad(page.options)
                // 延时 防止 多次请求 
                setTimeout(() => {
    
                    getApp().globalData.isShowModel = false

                }, 60000)
            }
        },
        fail: function (err) {
    
            console.log(err)
        }
    })
}
原网站

版权声明
本文为[一个假的前端男]所创,转载请带上原文链接,感谢
https://blog.csdn.net/IT_iosers/article/details/125180727