微信小程序如何实现登录

200 阅读1分钟

方法封装如下:

getLoginPermission() {  
//获取用户登录权限  
var that = this;  
//查看是否授权  
uni.getSetting({  
successfunction(res) {  
//微信登录接口  
uni.login({  
    successres => {  
            that.code = res.code;  
            //请求微信后台接口返回openid 和 sessionKey  
            uni.request({  
                    url: that.public_ip + '/assets/login/wxLogin?code=' + res.code,  
                    header: {  
                            'content-type''application/x-www-form-urlencoded' //'application/json'  
                    },  
                    method'POST',  
                    successres => {  
                            that.setUserInfoGlobal2(res.data.openid);  
                            that.setUserInfoGlobal11(res.data.Appid);  
                            that.setUserInfoGlobal3(res.data.session_key);  
                    },  
                    failres => {  
                            uni.showModal({  
                                    content'接口请求失败',  
                                    showCancelfalse  
                            });  
                    },  
                    complete() => {}  
            });  
    }  
});  
}  
});  
},