微信小程序的开发之授权功能

最近刚好在做一个小程序的简单开发,故准备将其中的一些要点记录下,以备日后参考
相信最近在开发小程序的小伙伴们都清楚,官方将授权功能修改了(一句mmp不知道能不能引起共鸣~)。害我找了半天的原因….
老式授权功能:
var openId = (wx.getStorageSync('openId'));
    if (openId) {
      wx.getUserInfo({
        success: function (res) {
          wx.request({
            url: '',
            data: res,
            method: 'POST',
            success: function (res) {
              console.log('submit success');
              // 判断是否为渠道商
              if (is) {
                app.editTabBar2();
              } else {
                app.editTabBar();
              }
            },
            fail: function (res) {
              console.log('submit fail');
            },
            complete: function (res) {
              console.log('submit complete');
            }
          })
          that.setData({
            nickName: res.userInfo.nickName,
            avatarUrl: res.userInfo.avatarUrl,
          })
        },
        fail: function () {
          // fail
          console.log("获取失败!")
        },
        complete: function () {
          // complete
          console.log("获取用户信息完成!")
        }
      })
    } else {
      wx.login({
        success: function (res) {
          console.log(res);
          if (res.code) {
            console.log(111);
            wx.getUserInfo({
              withCredentials: true,
              success: function (res_user) {
                console.log(res_user);
                wx.request({

                  // 后台接口地址
                  url: '',
                  data: {
                    code: res.code,
                    encryptedData: res_user.encryptedData,
                    iv: res_user.iv
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  },
                  success: function (res) {
                    // 判断是否为渠道商
                    if (is) {
                      app.editTabBar2();
                    } else {
                      app.editTabBar();
                    }
                    // this.globalData.userInfo = JSON.parse(res.data);
                    that.setData({
                      nickName: res.data.nickName,
                      avatarUrl: res.data.avatarUrl,
                    })
                    wx.setStorageSync('openId', res.data.openId);

                  }
                })
              }, fail: function () {
                wx.showModal({
                  title: '警告通知',
                  content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
                  success: function (res) {
                    console.log(res);
                    if (res.confirm) {
                      wx.openSetting({
                        success: (res) => {
                          console.log(res);
                          // 如果用户重新同意了授权登录
                          if (res.authSetting["scope.userInfo"]) {
                            console.log(22)
                            wx.login({
                              success: function (res_login) {
                                if (res_login.code) {
                                  wx.getUserInfo({
                                    withCredentials: true,
                                    success: function (res_user) {
                                      console.log(res_user)
                                      wx.request({
                                        url: '',
                                        data: {
                                          code: res_login.code,
                                          encryptedData: res_user.encryptedData,
                                          iv: res_user.iv
                                        },
                                        method: 'GET',
                                        header: {
                                          'content-type': 'application/json'
                                        },
                                        success: function (res) {
                                          // 判断是否为渠道商
                                          if (is) {
                                            app.editTabBar2();
                                          } else {
                                            app.editTabBar();
                                          }
                                          that.setData({
                                            nickName: res.data.nickName,
                                            avatarUrl: res.data.avatarUrl,

                                          })
                                          wx.setStorageSync('openId', res.data.openId);
                                        }
                                      })
                                    }
                                  })
                                }
                              }
                            });
                          }
                        }, fail: function (res) {
                          wx.showModal({
                            title: '提示',
                            content: '您点击了拒绝授权,将只能允许程序浏览功能',
                            success: function (res) {
                              if (res.confirm) {
                                console.log('用户点击确定')
                              }
                            }
                          })
                        }
                      })

                    }
                  }
                })
              }, complete: function (res) {


              }
            })
          }
        }
      })

    }
新式授权功能(美名曰:提升用户的体验,不过也确实如此哦~)
需要事件操作调用才能授权
wxml

js

/**
   * 获取用户信息
   */
  onGotUserInfo: function (e) {
    wx.setStorageSync("userInfo",e);
    console.log(e);
    console.log(e.detail)
    console.log(e.detail.errMsg)
    console.log(e.detail.userInfo)
    console.log(e.detail.rawData)
  },
代码确实简洁太多了~~~

你可能感兴趣的:(微信小程序)