收藏
评论

微信小程序登陆


<view class="page">
    <view class="page__bd">
        <view class="container">
            <view class="userinfo">
                <image class="userinfo-avatar" src="http://wx.qlogo.cn/mmhead/uI5pczeERTYJa6gSPNTNT8WksxJr2E3uv5ZJpNib8WF7IzsWkdicCbhQ/132" background-size="cover"></image>
                <text class="userinfo-nickname">七街城酒</text>
            </view>
            <view class="usermotto">
                <button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授权登录</button>
                <view wx:else>请升级微信版本</view>
            </view>
        </view>
    </view>
</view>
login: function () {
      if (this.data.logged) return
 
      util.showBusy('正在登录')
      var that = this
 
      // 调用登录接口
      qcloud.login({
          success(result) {
              if (result) {
                  util.showSuccess('登录成功')
 
                  wx.switchTab({
                      url: '/pages/index/index'

                  })


                  that.setData({
                      userInfo: result,
                      logged: true
                  })
              } else {
                  // 如果不是首次登录,不会返回用户信息,请求用户信息接口获取
                  qcloud.request({
                      url: config.service.requestUrl,
                      login: true,

                      success(result) {


                          util.showSuccess('登录成功')


                          wx.switchTab({

                              url: '/pages/index/index'
                          })
 
                           
                          that.setData({
                              userInfo: result.data.data.user,
                              logged: true
                          })
                      },
 
                      fail(error) {
                          util.showModel('请求失败', error)
                          console.log('request fail', error)
                      }
                  })
              }
          },
 
          fail(error) {
              util.showModel('登录失败', error)
              console.log('登录失败', error)
          }
      })
  },
onLoad: function (options) {
     var that = this;
      // 查看是否授权
      wx.getSetting({
          success: function (res) {
              console.log(res.authSetting)
              if (res.authSetting['scope.userInfo']) {
                  // 已经授权,可以直接调用 getUserInfo 获取头像昵称
                  that.login();
              }
          }
      })
  },


收藏

5 个评论

  • C浩
    C浩
    2018-05-19

    这个设计也是没谁了。。。。官方感觉一上来就弹窗影响用户体验,你这一上来直接整个页面要求用户点击授权,怕是赔夫人又折兵啊

    2018-05-19
    赞同 1
    回复
  • 陈
    2018-06-19

    关注一下

    2018-06-19
    赞同
    回复
  • 李刚
    李刚
    2018-05-15

    可以给我这个页面源码吗 ,和app.js

    我是小程序前端,谢谢

    2018-05-15
    赞同
    回复
  • 李刚
    李刚
    2018-05-15

    我把你这代码复制了一份怎么不行啦,

    2018-05-15
    赞同
    回复
  • 我是芒果鸭
    我是芒果鸭
    2018-05-15

    官方的要求是 第一眼就能看到 首页,其次这个体验极差--

    2018-05-15
    赞同
    回复 1
    • Brave 🎈
      Brave 🎈
      2018-05-23
      如果首页就需要用户信息呢
      2018-05-23
      回复
登录 后发表内容