微信小程序> 微信公众号授权,微信小程序如何按钮授权获取用户信息、请求用户授权获取手机号码

微信公众号授权,微信小程序如何按钮授权获取用户信息、请求用户授权获取手机号码

浏览量:1081 时间: 来源:loverxy丶Du
微信小程序如何按钮授权获取用户信息
微信登录授权
请求用户授权获取手机号码
微信小程序获取用户手机号码
微信小程序获取用户手机号码
说明一下,isLogin用来判断是否已授权登录,如果没有登录则显示按钮,已登录则显示用户头像和昵称。那么isLogin如何判断呢?
index.js里代码如下:
constapp=getApp()Page({data:{isLogin:true,//一开始没有授权,所以显示授权按钮授权canIUse:wx.canIUse('button.open-type.getUserInfo')},onLoad:function(options){//查看是否授权letthat=this;wx.getSetting({success(res){if(res.authSetting['scope.userInfo']){//已经授权,可以直接调用getUserInfo获取头像昵称//that.data.isHide=false;//that.setData({//isHide:false//});wx.getUserInfo({success(res){console.log(res.userInfo)that.setData({userInfo:res.userInfo});}})}else{that.setData({isHide:true});}}}),},onShow(){},bindGetUserInfo(e){//授权后的处理console.log(e.detail.userInfo)//获取加密信息iv等wx.getUserInfo({withCredentials:true,success:function(res_user){console.log(res_user,'加密信息iv等')}})this.setData({isLogin:false});}})button组件有很多用法,详情可参考:
简单说一下,open-type:微信开放能力,要求用户基础库版本1.1.0+
其中bindgetuserinfo表示:用户点击该按钮时,会返回获取到的用户信息,回调的detail数据与wx.getUserInfo返回的一致,生效的时机是:open-type="getUserInfo",
登陆页排版布局
mlviewwx:if="{{isHide}}"viewwx:if="{{canIUse}}"class='sq_first'viewclass='header'imagesrc='../../../we7/resource/images/login.png'/image/viewviewclass='content'text申请获取以下权限/texttext获得你的公开信息(昵称,头像等)/text/viewviewbuttontype="primary"open-type="getUserInfo"lang="zh_CN"bindgetuserinfo="bindGetUserInfo"授权登录/buttonbuttontype="primary"lang="zh_CN"open-type='getPhoneNumber'bindgetphonenumber="getPhoneNumber"手机号授权/button/view/viewviewwx:else请升级微信版本/view/viewjs:data:{canIUse:wx.canIUse('button.open-type.getUserInfo'),isHide:false,//一开始是隐藏,如果没有授权登陆就改为trueflag:true,open_id:''},bindGetUserInfo(e){//授权后的处理console.log(e.detail.userInfo,'用户信息')//获取加密信息iv等wx.getUserInfo({withCredentials:true,success:function(res_user){console.log(res_user,'加密信息iv等')}})this.setData({flag:false,isHide:false,});},wxss:/授权页面/.sq_first{position:fixed;width:100%;height:100%;z-index:9;overflow:hidden;/background:rgba(0,0,0,0.5);/background:#fff;left:0;top:0;}.header{margin:90rpx090rpx50rpx;border-bottom:1pxsolid#ccc;text-align:center;width:650rpx;height:300rpx;line-height:450rpx;}.headerimage{width:200rpx;height:200rpx;}.content{margin-left:50rpx;margin-bottom:90rpx;}.contenttext:nth-child(1){color:#333333;font-size:36rpx;}.contenttext{display:block;color:#9d9d9d;margin-top:20rpx;font-size:30rpx;padding-left:20rpx;}.hide{border-radius:80rpx;margin:70rpx50rpx;font-size:35rpx;display:none;}.show{display:block;border-radius:80rpx;margin:70rpx50rpx;font-size:35rpx;}

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎