Mini Program: Authorization Login Process

1. When the user enters the applet, we first determine whether the user authorizes the applet

wx.getSetting({(查看是否授权)
    success:res=>{//调用成功的回调函数	
        if (res.authSetting['scope.userInfo']) {
            // 有值时,代表已授权
        } else{
           //没有授权
        }
    }
})

2. No authorization, we need to authorize through the button

In the WeChat applet development document, we can know that when the open-type="getUserInfo" of the button component is combined with the bindgetuserinfo event, user information (authorized or not) can be obtained

<button open-type="getUserInfo" bindgetuserinfo="getuserinfo">授权</button>

3. Through the bindgetuserinfo event, we can obtain personal information, encrypted offset data, and encrypted user information (e.detail acquisition)

getuserinfo(e){
    //可以获取到个人的信息、加密偏移数据、加密用户信息
    console.log(e.detail);
}

4. Users can authorize login or cancel authorization

// 1、登录:授权
wx.login({
    success: (res) => {//成功的回调
        // console.log(res.code);//获取临时登录凭证code
        if (res.code) {//当有临时登录凭证code码时,我们请求登录接口
            //请求登录接口
        }
    }
})

// 2、取消授权:显示模态框
wx.showModal({
    title: '提示',
    content: '请先授权之后再进入',
    showCancel: false,
    confirmText: '返回授权',
    success: function (res) {
        // 用户没有授权成功,不需要改变 isHide 的值
        if (res.confirm) {
            console.log('用户点击了“返回授权”');
        }
    }
})

5. According to the code code returned by the login interface, determine whether the user is a new user

//根据登录返回的code码,我们在需要注册的里面,请求注册接口,并配置好参数、请求方式等
//例如:登录接口code码返回10000时,代表未注册,返回0时,代表注册过
if (data.code == 10000) {
     //注册请求接口
 } else if (data.code == 0) {
    wx.setStorage({//保存token
        data: token,
        key: 'token'
    })
 }

6. After the user registers successfully, call the login interface and save the token. Token is required on some pages

7. In step 1, when we authorize, we need to check whether the token exists

8. When the token exists, we directly execute the logic code

9. When the token does not exist, we need to log in. After logging in, we judge the code code returned, and judge whether the user is a new user according to the code code. Finally save the token

Guess you like

Origin blog.csdn.net/u013592575/article/details/131704352