微信小程序的登录逻辑

微信小程序的登录逻辑:

1、调用wx.login获取微信code

2、将code、授权登录参数(标识不同的小程序)传给后端获取openid,unionid,customerTel。存储openid支付时使用。

包括用户在当前小程序的唯一标识(openid)、微信开放平台帐号下的唯一标识(unionid,若当前小程序已绑定到微信开放平台帐号)及本次登录的会话密钥(session_key)等。

3、选择微信授权登录,不同用户情况分类:

   1)、返回了customerTel:后端根据unionid查询到有绑定关系的customerTel返回给前端(可判断为注册过的老用户),前端再调用登录接口获取token,存储token,实现静默登录。 
           2)、 未返回customerTel: 
            后端根据unionid查询到没有绑定的customerTel(无法判断是新用户还是老用户)
            点击微信一键登录按钮,拉起手机号授权弹窗,如果用户允许授权,获取信息发给后端解密出手机号
            前端再调用登录接口获取token,存储token,实现授权登录
4、 选择验证码登录,跳转新页面
       输入手机号、验证码。首次登录执行注册+登录 
         
  wx.login({
      success: (res) => {
        api
          .getUnionId({
            code: res.code,
            wxType: env.wxType, 
          })
          .then((res) => {
            app.globalData.openId = res.responseData.openId; //微信支付需要使用openid
            app.storage.setData("openID", res.responseData.openId);
            this.data.openId = res.responseData.openId;
            this.data.unionId = res.responseData.unionId;
            this.setData({
              hideLoading: true,
            });
            if (res.responseData.customerTel) {
                if (!app.globalData.isNewCustomer) {
                  //标识新用户,'10'新用户,'20'老用户
                  app.globalData.isNewCustomer = "20";
                }
              this.data.customerTel = res.responseData.customerTel;
            } else {
                if (!app.globalData.isNewCustomer) {
                  //标识新用户,'10'新用户,'20'老用户
                  app.globalData.isNewCustomer = "10";
                }
            }
          })
          .catch((error) => {
            this.setData({
              hideLoading: true,
            });
          });
      },
    });
posted @ 2022-02-16 16:22  不特别但唯一  阅读(1675)  评论(0)    收藏  举报