技术碎片
技术记录
帮助交流
recommend
微信小程序登录接口适配示例
//登录
  login: function(cb) {
    var that = this;
    var token = wx.getStorageSync('token'|| '';
    //调用登录接口
    wx.login({
      success: function(res) {
       var code = res.code;
        if (res.code && !that.globalData.userInfo{
          //发起网络请求
          wx.showModal({
            title: '温馨提示',
            content: '登录需要请求您的个人信息',
            success(res) {
              if (res.confirm{
                wx.getUserProfile({
                desc: "获取你的昵称、头像、地区及性别",
                success: ures => {
                  console.log(ures.rawData)
                  console.log(code)
                  let wxUserInfo = ures.userInfo;
                  wx.request({
                    url: that.apiUrl + 'user/login',
                    data: {
                      code: code,
                      rawData: ures.rawData,
                      token: token
                    },
                    method: 'post',
                    header: {
                      "Content-Type": "application/x-www-form-urlencoded",
                    },
                    success: function(lres) {
                      console.log(lres)
                      var response = lres.data
                      if (response.code == 1{
                        that.globalData.userInfo = response.data.userInfo;
                        wx.setStorageSync('token', response.data.userInfo.token);
                        typeof cb == "function" && cb(that.globalData.userInfo);
                      } else {
                        wx.setStorageSync('token', '');
                        console.log("用户登录失败")
                        that.showErrorModal('用户登录失败');
                      }
                    }
                  });
                },
                fail: res => {
                  //拒绝授权
                  that.showErrorModal('您拒绝了请求');
                  return;
                }
              })} else if (res.cancel{
                //拒绝授权 showErrorModal是自定义的提示
                that.showErrorModal('您拒绝了请求');
                return;
              }
            }
          })

          // wx.getUserInfo({
          //   success: function(ures) {
          //     wx.request({
          //       url: that.apiUrl + 'user/login',
          //       data: {
          //         code: res.code,
          //         rawData: ures.rawData,
          //         token: token
          //       },
          //       method: 'post',
          //       header: {
          //         "Content-Type": "application/x-www-form-urlencoded",
          //       },
          //       success: function(lres) {
          //         var response = lres.data
          //         if (response.code == 1) {
          //           that.globalData.userInfo = response.data.userInfo;
          //           wx.setStorageSync('token', response.data.userInfo.token);
          //           typeof cb == "function" && cb(that.globalData.userInfo);
          //         } else {
          //           wx.setStorageSync('token', '');
          //           console.log("用户登录失败")
          //           that.showLoginModal(cb);
          //         }
          //       }
          //     });
          //   },
          //   fail: function(res) {
          //     that.showLoginModal(cb);
          //   }
          // });
        }
      }
    });
  },
  showErrorModal(msg) {
    wx.showToast({
    title: msg,
    icon: 'none',
    duration: 1500,
    mask: false
    });
  },
如有疑问或者交流问题请加:QQ 2137087126

转载请注明来源地址:www.zhyunxuan.com 更新时间 2023-02-01  1931
梦想有多远,路就有多远