美文网首页
用户登录注册相关

用户登录注册相关

作者: xueyueshuai | 来源:发表于2020-01-19 18:44 被阅读0次

    wxml

    <button  open-type="getUserInfo" bindgetuserinfo="getUserInfo"> 授权登录 </button>
    
    // 用户点击授权
      getUserInfo: function (e) {
        console.log('getUserInfo e:')
        console.log(e)
        console.log('')
        var that = this;
    
        if (e.detail.userInfo) {
          //用户按了允许授权按钮
    
          var nickname = e.detail.userInfo.nickName
          var headimg = e.detail.userInfo.avatarUrl
          var sex = e.detail.userInfo.gender
    
          wx.showLoading({
            title: '登录中',
          });
    
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              var data = {
                nickname: nickname,
                headimg: headimg,
                sex: sex,
                appid: that.data.appid,
              };
              // 获取到code
              data.code = res.code;
    
              console.log('p_data');
              console.log(data);
              //插入登录的用户的相关信息到数据库
    
              wx.request({
                url: app.globalData.api_host + '/index.php/api/v1_0_0.login/do_login',
                data: data,
                success: function (d) {
                  console.log(d)
                  wx.hideLoading();
                  console.log(d.data)
                  var res = d.data;
    
                  var code = res.code;
                  var msg = res.msg;
                  var data = res.data;
    
                  if (code == 200) {
                    var userInfo = {
                      user_id: data.user_id,
                      user_token: data.user_token,
                      openid: data.openid,
                      nickname: nickname,
                      headimg: headimg,
                      sex: sex,
                    };
                    console.log(userInfo);
                    app.globalData.userInfo = userInfo;
    
                    wx.setStorageSync('userInfo', userInfo)
                    
                  } else if (code == 201) {
                    app.toast_msg(msg)
                  } else if (code == 202) {
                    app.re_login()
                  } else {
    
                  }
    
                }
              });
            }
          })
        } else {
          //用户按了拒绝按钮
          wx.showModal({
            title: '警告',
            content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
            showCancel: false,
            confirmText: '返回授权',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击了“返回授权”')
              }
            }
          })
        }
      },
    

    相关文章

      网友评论

          本文标题:用户登录注册相关

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