微信小程序(简易授权界面)

/*登入界面 login.js*/
const app = getApp()

Page({
  data: { 
    userIcon: "../../images/default_icon.gif",
    userName: "未登录",
    settingName: "授权"
  }, 
  onShow: function () {
    let that = this;
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.userInfo']) {
          console.info("用户未授权");
          wx.openSetting();
        } else {
          console.info("用户已经授权");
          that.setUserInfo();
        }
      }
    })
  }, 
  setUserInfo: function () {
    let that = this;
    wx.getUserInfo({
      success: function (userRes) {
        that.setData({  //设置数据
          userIcon: userRes.userInfo.avatarUrl,
          userName: userRes.userInfo.nickName,
          settingName: "已授权",
          haveLogin: true
        })
      }
    });
  },//打开权限设置 
  openSetting: function () { //打开授权界面
    let that = this;
    wx.openSetting()
  }
})
/*login.wxml*/

<view class="container">
   <view class='avatar'><image src="{{userIcon}}"></image></view>
   <view class="user-name">{{userName}}</view>
   <button bindtap="openSetting"  class='btn'>{{settingName}}</button>
</view>

 

/*login.wxss*/

.avatar{
  text-align: center;
}

.avatar image{
  margin: 0px auto;
  width: 100px;
  height: 100px;
  border-radius: 50px;
}
.user-name{
   text-align: center;
  margin-top: 10px;
}

.btn{
  margin: 50px 20px 0px 20px;
  background:red;
  color: #fff;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值