找不到帮助文档?你想要哪方面的帮助教程?提交文档建议 >

小程序自动授权登录

更新时间:2020-12-21 加入收藏
0

帝云CMS(DiYunCMS)通过API接口开发微信小程序时实现微信自动授权登录的方法介绍以及示例代码

方法如下:

后台微信插件填写AppID与AppSecret并且开启自动登录模式,如下图所示:

{title}


授权登录代码示例:

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


                    //发起网络请求D
                    wx.request({
                        url: "http://www.xxxxxx.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 == "") {
        // 未登录跳转登录界面,这里写登录代码
        
    }
    // 登录代码结束


},


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


--
有帮助
--
没帮助
0
收藏
0
打赏
相关文档
Hello,欢迎来咨询~
点击在线咨询