3***@qq.com
3***@qq.com
  • 发布:2020-01-19 09:46
  • 更新:2021-06-21 11:55
  • 阅读:2563

使用uni-app storage 无法保存登录状态,每次关闭后打开需要重新登录

分类:uni-app

使用uni-app 的微信登录功能,在回调函数里面 设置了 key


                uni.login({  
                    provider: 'weixin',  
                    success: function(loginRes) {  
                        // 获取用户信息  
                        uni.getUserInfo({  
                            provider: 'weixin',  
                            success: async function(infoRes) {  
                                let userInfo = infoRes.userInfo;  
                                //获取 client_id 传递给服务器  
                                //调用接口 获取用户信息  
                                let client_id = uni.getStorageSync('client_id');  
                                userInfo.client_id = client_id;  
                                let res = await api.wx_login(userInfo);  
                                if (res.code != 200) {  
                                    //提示错误信息  
                                    uni.showModal({  
                                        title: '错误',  
                                        content: res.datas.error,  
                                        showCancel: false,  
                                        success: function() {  

                                        },  
                                    });  

                                    return false;  
                                }  

                                **uni.setStorageSync('key', res.datas.token);**  
                                **uni.setStorageSync('mp_key', res.datas.mp_token);**  

                                uni.redirectTo({  
                                    url: "/pages/index/index"  
                                })  
                            }  
                        });  
                    },  
                    fail: function(e) {  
                        console.log(e, '+++++')  
                    }  
                });

然后在程序的 APP.VUE 页面 onLaunch 钩子里面 检测登录状态

            checkLogin: function() {  
                let key = uni.getStorageSync('key')  
                if (!key) {  
                    uni.showToast({  
                        title:'请先登录!',  
                        icon:'none',    
                        success:function(){  

                            uni.redirectTo({  
                                url:'/pages/login/login'  
                            })  
                        }  
                    });  
                }  
            },

每次后台关闭APP 后再次打开就会提示登录
请问这可能是哪里的问题?

2020-01-19 09:46 负责人:无 分享
已邀请:
2***@qq.com

2***@qq.com

大佬解决了吗

该问题目前已经被锁定, 无法添加新回复