小程序原生js获取用户权限

2023-04-27,,

1.首先要有一个按钮

<view name="authorizemodal">
<view class="drawer_screen" wx:if="{{isauthorizeModal}}">
<view class="drawer_box fishqccenter phonechoosebox">
<view>
<image style="width:90%;" src="https://try.fishqc.com/img/Try-sm/authorization.png" mode="widthFix"></image>
</view>
<view>
<button class="authorization-btn" open-type="getUserInfo" bindgetuserinfo="getlogincode" style="border-radius: 1rpx"></button>
</view>
<!-- <view class="weChatAuth">授权提示</view>
<view class="phonechoosetxt">放心试小程序需授权获取微信权限信息</view>
<view class="phonechoosebtncontain">
<view class="getPhoneContain">
<button class="author-btn" open-type="getUserInfo" bindgetuserinfo="getlogincode" style="border-radius: 1rpx">授权</button>
</view>
</view> -->
</view>
</view>
</view>

2.授权的js代码

var app = getApp()
Component({
properties: { },
data: {
isauthorizeModal:false
},
methods: {
toasttips: function (text, icon, time) {
wx.showToast({
title: text == null ? '' : text,
icon: icon,
duration: time == null ? '1000' : time
})
},
getlogincode: function () {
var that = this;
wx.login({
success: function (res) {
if (res.code) {
var logincode = res.code // 弹窗授权
wx.getUserInfo({
success: function (res) {
if (res.errMsg == 'getUserInfo:ok') {
var encryptedData = res.encryptedData
var iv = res.iv;
that.setData({
isauthorizeModal: false
})
that.triggerEvent('myevent',{arg:false})
that.WeixinLogin(logincode, encryptedData, iv);
} },
fail: function (res) {
// console.log('点击确认取消')
that.checkauthorize();
}
})
} else {
// console.log('登录失败!' + res.errMsg) }
}
}); },
// 检查是否授权过
checkauthorize: function () {
var that = this;
wx.getSetting({
success(res) {
// 没有授权过的,进入如下
if (!res.authSetting['scope.userInfo']) {
that.authorizemodal();
}else{
that.setData({
isauthorizeModal:false
})
that.triggerEvent('myevent',{arg:false})
}
}
})
},
// 授权失败,弹窗
authorizemodal: function () {
var that = this;
wx.showModal({
title: '获取用户信息授权',
content: '当前功能需授权获取用户信息',
confirmText: '继续授权',
confirmColor: '#64c8bc',
success: function (res) {
if (res.confirm) {
// console.log("点击去设置")
that.authorize();
} else if (res.cancel) {
// console.log("点击取消")
// console.log(res)
} }
})
},
authorize: function () {
var that = this;
wx.openSetting({
success: function (res) {
// console.log("授权不成功")
if (!res.authSetting['scope.userInfo']) {
that.setData({
isauthorizeModal:true
})
that.triggerEvent('myevent',{arg:true}) }else{
// console.log("授权成功")
wx.login({
success: function (res) {
if (res.code) {
var logincode = res.code
// 弹窗授权
wx.getUserInfo({
success: function (res) {
// console.log('点击确认授权')
if (res.errMsg == 'getUserInfo:ok') {
var encryptedData = res.encryptedData
var iv = res.iv;
that.setData({
isauthorizeModal: false
})
that.triggerEvent('myevent',{arg:false})
that.WeixinLogin(logincode, encryptedData, iv);
} },
fail: function (res) {
}
})
} else {
// console.log('登录失败!' + res.errMsg) }
}
}); } },
fail: function () {
that.checkauthorize();
}
})
},
show:function(){
var that = this;
wx.getSetting({
success(res) {
// console.log(res)
// 没有授权过的,进入如下
if (!res.authSetting['scope.userInfo']) { var modalarg = wx.getStorageSync('modalarg')
console.log(modalarg)
if(modalarg == ''){ //没缓存,调接口
wx.request({
url: 'https://try.fishqc.com/getAb', //请求接口地址
data: {},
method:'GET',
header: {
'content-type': 'application/x-www-form-urlencoded', // 默认值
'skey': wx.getStorageSync('getstoreskey')
},
success: function(res) {
if(res.data.code == 200){ if(res.data.data.abstatus == 1){ //abstatus为1时,授权弹窗开启 wx.setStorageSync('modalarg',res.data.data) //设置已经调用过该接口的标识缓存
if(res.data.data.abvalue == 'a'){ //abvalue为a时,授权弹窗
wx.getSetting({
success(res) { // 没有授权过的,进入如下
if (!res.authSetting['scope.userInfo']) {
that.setData({
isauthorizeModal:true
})
that.triggerEvent('myevent',{arg:true})
}
}
}) }
} } }
}) }else{ //有缓存,直接判断
if(modalarg.abstatus == 1){ //abstatus为1时,授权弹窗开启
if(modalarg.abvalue == 'a'){ //abvalue为a时,授权弹窗
console.log('有缓存')
wx.getSetting({
success(res) { // 没有授权过的,进入如下
if (!res.authSetting['scope.userInfo']) {
that.setData({
isauthorizeModal:true
})
that.triggerEvent('myevent',{arg:true})
}
}
})
}
}
}
}else{ that.setData({
isauthorizeModal:false
})
that.triggerEvent('myevent',{arg:false})
}
}
}) },
WeixinLogin: function (logincode, encryptedData, iv) {
var that = this;
var arg = wx.getStorageSync('modalarg')?wx.getStorageSync('modalarg'):''
var channel = wx.getStorageSync('channel')?wx.getStorageSync('channel'):''
console.log('channel test')
console.log(channel)
wx.request({
url: 'https://product.fishqc.com/Api/WeixinLogin/weiXinMini',
data: {
code: logincode,
iv: iv,
encryptedData: encryptedData,
device_id:arg.deviceId,
abtest:'1',
is_login:0,
channel:channel },
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded' // 默认值
},
success: function (res) { }
})
}
}
})

小程序原生js获取用户权限的相关教程结束。

《小程序原生js获取用户权限.doc》

下载本文的Word格式文档,以方便收藏与打印。