欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信小程序授权问题

程序员文章站 2022-04-03 08:54:34
...
 // 查看是否授权
  getUserInfo() {
    let _this = this
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.userInfo']) {
          wx.getUserInfo({
            success: function(res) {
              console.log(res.userInfo)
              _this.setData({
                userName: res.userInfo.nickName,
                userAvatarUrl: res.userInfo.avatarUrl
              }, () => {
                console.log(_this.data.userAvatarUrl)
                _this.drawPage()
                wx.showToast({
                  title: '海报正在生成中',
                  icon: 'loading',
                  duration: 4000
                })
                setTimeout(function() {
                  _this.setData({
                    showBigImg: true
                  })
                }, 4000)
              })
            }
          })
        } else if (res.authSetting['scope.userInfo'] === undefined) {
          wx.authorize({
            scope: 'scope.userInfo',
            success() {
              wx.getUserInfo({
                success: function(res) {
                  console.log(res.userInfo)
                  _this.setData({
                    userName: res.userInfo.nickName,
                    userAvatarUrl: res.userInfo.avatarUrl
                  }, () => {
                    _this.drawPage()
                    wx.showToast({
                      title: '海报正在生成中',
                      icon: 'loading',
                      duration: 4000
                    })
                    setTimeout(function() {
                      _this.setData({
                        showBigImg: true
                      })
                    }, 4000)
                  })
                }
              })
            },
            fail() {
              wx.showToast({
                title: '您没有授权,无法保存到相册',
                icon: 'none'
              })
            }
          })
        } else {
          wx.openSetting({
            success(res) {
              if (res.authSetting['scope.userInfo']) {
                wx.getUserInfo({
                  success: function(res) {
                    console.log(res.userInfo)
                    _this.setData({
                      userName: res.userInfo.nickName,
                      userAvatarUrl: res.userInfo.avatarUrl
                    }, () => {
                      _this.drawPage()
                      wx.showToast({
                        title: '海报正在生成中',
                        icon: 'loading',
                        duration: 4000
                      })
                      setTimeout(function() {
                        _this.setData({
                          showBigImg: true
                        })
                      }, 4000)
                    })
                  }
                })
              } else {
                wx.showToast({
                  title: '您没有授权,无法保存到相册',
                  icon: 'none'
                })
              }
            }
          })
        }
      }
    })
  },
相关标签: js 小程序