小程序授权登录

小程序授权登录

<button class="l_btn" open-type="getUserInfo" lang="zh_CN" @getuserinfo="bindGetUserInfo" @click="getUserInfoClick">微信登录 </button>

   getUserInfoClick() {}, //不要删除
            bindGetUserInfo: function (e) {
                var that = this;
                var sessionId  =  wx.getStorageSync("sessionId");
                if (e.mp.detail.userInfo) {
                    var userInfo = e.mp.detail.userInfo;
                    userInfo.encryptedData = e.mp.detail.encryptedData;
                    userInfo.iv = e.mp.detail.iv;
                    wx.setStorageSync("userInfo", userInfo);
                    wx.switchTab({
                        url: "/pages/home/main"
                    });
                } else {
                    wx.showModal({
                        content: '请允许使用微信登录',
                        confirmText: "确认",
                        cancelText: "取消",
                        success: function (res) {
                            //点击“确认”时打开设置页面
                            if (res.confirm) {
                                wx.openSetting({
                                    success: (data) => {
                                        if (data.authSetting["scope.userInfo"] == true){
                                            wx.getUserInfo({
                                                // withCredentials: false,
                                                lang:"zh_CN",
                                                success: function (datares) {
                                                    var userInfo = datares.userInfo;
                                                    userInfo.encryptedData = datares.encryptedData;
                                                    userInfo.iv = datares.iv;
                                                    wx.setStorageSync("userInfo", userInfo);
                                                        wx.switchTab({
                                                            url: "/pages/home/main"
                                                        });
                                                }})
                                        }
                                    }
                                })
                            } else {
                                console.log('用户点击取消')
                            }
                        }
                    });
                }
            },
posted @ 2019-09-16 09:36  星光下  阅读(453)  评论(0编辑  收藏  举报