Mini program guides user authorization ideas and project implementation methods (with code)

不言
Release: 2018-12-14 11:05:15
forward
4046 people have browsed it

What this article brings to you is about the ideas and project implementation methods for mini programs to guide user authorization (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .

User information authorization

For users who are not authorized by the mini program, the official cancels the direct call of the wx.getUserInfo method. The first authorization must actively trigger the custom button before the official authorization component can be activated

The information that can be obtained is: nickname, avatar, gender, country, province, city, gender, language

Things and steps

1. wx.getSetting to check whether it is authorized

2. Authorized to use wx.getUserInfo to obtain user information, save

3. Unauthorized to display a custom page with button, bindGetUserInfo will return user information, and the button will call WeChat official authorization

<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">允许用户授权</button>
Copy after login

4. Authorization is completed and user information is saved

Project implementation

1.app.js----I put it after the login method

// 查看是否授权,保存授权状态
    wx.getSetting({
        success: function(res) {
            if (res.authSetting['scope.userInfo']) {
                wx.setStorageSync('isAuthorize', 'true');
                wx.getUserInfo({
                    success: function(res) {
                        wx.setStorageSync('userInfo', res.rawData);
                    }
                })
            } else {
                wx.setStorageSync('isAuthorize', 'false');
            }
        }
    })
Copy after login

2.main .wxml------Project main page

<!-- 小程序授权组件 -->
<authorize id="authorize"></authorize>
Copy after login

3, main.js------onload to determine whether to display a custom button

// 已授权隐藏弹框,未授权显示弹框
this.authorize = this.selectComponent("#authorize");
if (wx.getStorageSync('isAuthorize')=='true'){
    this.authorize.hideDialog()
}
Copy after login

4,main .json-----Main page configuration parameters

"usingComponents": {
    "authorize": "自定义授权组件的路径"
}
Copy after login

5. authorize.js------Customize the page/pop-up component with button authorize, only the js part is posted here

/*authorize.js*/
Component({
    options: {
        multipleSlots: true
    },

    data: {
        isHide: false,
        canIUse: wx.canIUse('button.open-type.getUserInfo')
    },

    methods: {

        //隐藏弹框
        hideDialog() {
            this.setData({
                isHide: true
            })
        },
        // 授权信息保存
        bindGetUserInfo(e){
            wx.setStorageSync('isAuthorize', 'true');
            wx.setStorageSync('userInfo', JSON.stringify(e.detail.userInfo));
            this.hideDialog()
        }

    }
})
Copy after login

This way the entire authorization is completed!

The above is the detailed content of Mini program guides user authorization ideas and project implementation methods (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!