联系我们

微信扫描上方二维码

微信插件后台需要开启自动登录模式

小程序自动授权登录


会员登录代码示例:

wx.login({
    success: function (res) {
        console.log(res);
        if (res.code) {
            wx.getUserInfo({
                success: function (userRes) {


                    //发起网络请求D
                    wx.request({
                        url: "http://www.xr.com/index.php?appid=11111&appsecret=11111111&s=weixin&c=member&m=xcx",
                        data: {
                            json: userRes.rawData,
                            js_code: res.code
                        },
                        method: 'post',
                        header: {
                            'Content-Type': 'application/x-www-form-urlencoded',
                        },
                        success: function (res) {
                            console.log(res.data);
                            if (res.data.code) {
                                if (res.data.msg == 'login') {
                                    // 登录成功
                                    console.log("登录成功了");
                                    wx.setStorageSync('member_uid', res.data.data.member.id);
                                    wx.setStorageSync('member_auth', res.data.data.auth);
                                    wx.setStorageSync('member', res.data.data.member);

                                    wx.showToast({
                                        title: "登录成功",
                                        icon: 'success',
                                        success: function () {
                                            // 跳转新页面
                                            //wx.reLaunch({ url: "../member/index" });
                                        }
                                    })

                                } else {
                                    // 绑定账号注册
                                    wx.showModal({
                                        showCancel: false,
                                        content: "迅睿cms后台微信插件中需要开启直接登录模式"
                                    })
                                }
                            } else {
                                // 失败了
                                wx.showModal({
                                    showCancel: false,
                                    content: res.data.msg
                                })
                            }
                        }
                    });


                }
            });
        } else {
            console.log('登录失败:' + res.errMsg)
        }
    }
});


把上面代码放到小程序页面js里面

/**
 * 生命周期函数--监听页面加载
 */
onLoad: function (options) {

    // 登录代码开始
    var member = wx.getStorageSync('member');
    //console.log(member);
    if (member == "") {
        // 未登录跳转登录界面,这里写登录代码
        
    }
    // 登录代码结束


},


这样在进入小程序时就会自动登录账号!

本文地址:https://www.xunruicms.com/doc/1053.html