项目实战 | 微信小程序用户授权

作者: 白晓明 | 来源:发表于2019-07-17 11:17 被阅读19次

我们需要与现有系统的用户数据进行授权绑定,因此我们需要现有系统的用户登录信息及微信提供的唯一标识,授权绑定后我们再次访问小程序时,并不需要去认证登录,通过唯一标识来贯彻整个业务系统。我们使用官方提供的最新授权方法。

页面中使用按钮并绑定获取用户信息

<button type='primary' open-type="getUserInfo" lang="zh_CN" bindgetuserinfo="bindGetUserInfo">绑定授权</button>

JS中来监听按钮事件

    bindGetUserInfo: function(e) {
        if (e.detail.userInfo) {
            var that = this;
            //绑定用户信息
            this.bindEventUserInfo();
        } else {
            //用户点击了取消授权按钮
            app.util.showModalTip('提示', '您点击了拒绝授权,将无法使用小程序的更多功能,请授权后再体验!', '返回授权');
        }
    },
    bindEventUserInfo: function(e) {
        var that = this;
        var key = wx.getStorageSync('ZHPT_KEY');
        if (key == "" || key == null) {
            app.util.showModalTip('提示', '授权信息已失效,请退出应用重新打开!', '确定');
        }
        that.setData({
            _wxh: key
        })
        //需要对用户录入信息进行验证
        var flag = that.checkInputValue();
        if (flag) {
            var sqgzUrl = app.util.getServerUrlPath();
            wx.request({
                url: sqgzUrl,
                data: that.data,
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                method: 'POST',
                dataType: 'json',
                success: res => {
                    if (res.statusCode === 200 && res.data.status) {
                        console.log(res.data);
                        wx.setStorageSync('ZHPT_KEY', key);
                        that.getUserInfoByKey(key);
                    } else {
                        app.util.showModalTip('系统提示', res.data.msg, '确定');
                    }
                }
            });
        }
    },
    getUserInfoByKey: function(_key) {
        var getUserInfoUrl = app.util.getServerUrlPath();
        wx.request({
            url: getUserInfoUrl,
            data: {
                _wxh: _key
            },
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            method: 'POST',
            success: function(res) {
                console.log(res);
                if (res.statusCode === 200) {
                    if (res.data.status) {
                        wx.setStorageSync('userInfo', res.data.data);
                        wx.redirectTo({
                            url: '/pages/home/home',
                        });
                    } else {
                        app.util.showModalTip('警告', res.data.msg, '确定');
                    }
                } else {
                    app.util.showModalTip('警告', '获取信息失败,请稍后尝试!', '确定');
                }
            }
        });
    }

验证用户输入信息是否完整

    jzsjh: function(e) {
        this.setData({
            _sjhm_sfz: e.detail.value
        });
    },
    xssfzh: function(e) {
        this.setData({
            _xs_sfzh: e.detail.value
        });
    },
    xsxm: function(e) {
        this.setData({
            _xs_name: e.detail.value
        });
    },
    checkInputValue: function() {
        if (this.data._sjhm_sfz == "") {
            app.util.showModalTip('警告', '请输入您的手机号或身份证号码!', '确定');
            return false;
        }
        if (this.data._xs_sfzh == "") {
            app.util.showModalTip('警告', '请输入您孩子的身份证号码!', '确定');
            return false;
        }
        if (this.data._xs_name == "") {
            app.util.showModalTip('警告', '请输入您孩子的姓名!', '确定');
            return false;
        }
        return true;
    },

相关文章

网友评论

    本文标题:项目实战 | 微信小程序用户授权

    本文链接:https://www.haomeiwen.com/subject/ypfqlctx.html