微信公众号开发

小程序登录

本文主要是介绍小程序登录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

获取基本用户数据

    profile(e)
    {
        try{
            wx.getUserProfile({

                desc: '用于完善会员资料',//后续会进行展示

                success: res => {
                    wx.setStorage({
                        key:"userInfo",
                        data:JSON.stringify(res.userInfo)
                    })
                    this.get_openid(); //继续获取openid
                },

                fail: res =>{
                    wx.showModal({
                        title: '警告',
                        content: '您拒绝了授权,将无法进入测评,请授权后进入!',
                        showCancel: false,
                        confirmText: '返回授权',
                        success: function (res) {
                            console.log('用户点击了“返回授权”');
                        }
                    });
                }
            })
        }

        catch (e){
            wx.getUserInfo({

                success: res => {
                    wx.setStorage({
                        key:"userInfo",
                        data:JSON.stringify(res.userInfo)
                    })

                    this.get_openid(); //继续获取openid
                },

                fail: res =>{
                    wx.showModal({
                        title: '警告',
                        content: '您拒绝了授权,将无法进入测评,请授权后进入!',
                        showCancel: false,
                        confirmText: '返回授权',
                        success: function (res) {
                            console.log('用户点击了“返回授权”');
                        }
                    });
                }
            })
        }

    },

 

获取用户openid

    get_openid()
    {
        wx.login({
            success: res => {
                //获取到jsCode
                // console.log(res.code);

                //根据jsCode获取用户openId
                wx.request({
                    url: "http://localhost/personalities/server/index.php",
                    method: 'GET',
                    data: {
                        head:'wx_login',
                        v1: res.code
                    },
                    success: res => {
                        wx.setStorage({
                            key:"openid",
                            data:res.data.openid,
                            success: res => {
                                this.merge_data();
                            }
                        })
                    }
                })
            }
        })
    },

 

php: 根据jsCode获取用户openId

function login()
{
    try {

        $code = $_REQUEST['v1'];

        $app = Factory::miniProgram(WX_APP);

        return json_encode($app->auth->session($code),JSON_UNESCAPED_UNICODE);

    } catch (\EasyWeChat\Kernel\Exceptions\InvalidConfigException $e) {

        return json_encode($e,JSON_UNESCAPED_UNICODE);
    }
}

 

小程序获取完数据就拼接,返回首页

    merge_data()
    {
        let openid = wx.getStorageSync('openid');
        let user = JSON.parse(wx.getStorageSync('userInfo'));
        user.openid = openid;

        wx.setStorage({
            key:"user",
            data:JSON.stringify(user),
        })

        wx.reLaunch({
            url: '../index/index'
        });

        wx.showToast({
            title: '登录成功',
            icon: 'success',
        })
    }

 

这篇关于小程序登录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!