小程序原生js获取用户权限
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获取用户权限的更多相关文章
- 20171018 在小程序页面去获取用户的OpenID
1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...
- taro 微信小程序原生作用域获取
在 Taro 的页面和组件类中,this 指向的是 Taro页面或组件实例. 但是一般我们需要获取 Taro的页面和组件 所对应的 小程序原生页面和组件实例,这个时候我们可以通过 this.$scop ...
- 小程序登录&授权&获取用户信息
一 .登录 时序图如下: wx.login() 获取js_code 示例代码: App({ onLaunch: function() { wx.login({ success: ...
- 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密
后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...
- 逍遥云天 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密
后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...
- [小程序]微信小程序登陆并获取用户信息
1.小程序js端调用框架登陆API,获取到一个临时code,拿着这个code去调用自己的服务端接口 2.在自己的服务器端,使用app_id app_secrect code可以获取到用户的openid ...
- Java springboot支付宝小程序授权,获取用户信息,支付及回调
参考官方文档https://opendocs.alipay.com/mini/introduce/pay 支付宝小程序的支付和微信小程序的支付一样第一步都是要获取到用户的唯一标识,在微信中我们获取到的 ...
- 小程序wx.getUserInfo获取用户信息方案介绍
问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本 API和组件 - - - - 背景 小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.g ...
- C#微信小程序服务端获取用户解密信息
using AIOWeb.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.C ...
随机推荐
- 视频质量评估 之 VMAF
VMAF 方法: 基本想法: 面对不同特征的源内容.失真类型,以及扭曲程度,每个基本指标各有优劣.通过使用机器学习算法(支持向量机(Support Vector Machine,SVM)回归因子)将基 ...
- Flutter Animation AnimatedBuilder
Flutter AnimatedBuilder 创建动画的widget Key key, @required Listenable animation, @required this.builder, ...
- [破解版]Unity3d引擎最新稳定版本4.5.5下载(官方最新稳定版本)
来源:http://www.unitymanual.com/thread-28912-1-1.html unity4.5.5 Mac版下载地址:http://pan.baidu.com/s/1hqzi ...
- 0001-代码仓库-mvn
暂缺 基本介绍 web管理 ifsvnadmin
- MyCAT+MySQL搭建高可用企业级数据库集群视频课程
原文地址:https://www.guangboyuan.cn/mycatmysql%E6%90%AD%E5%BB%BA%E9%AB%98%E5%8F%AF%E7%94%A8%E4%BC%81%E4% ...
- Proxmox 命令使用方法
proxmox 虚拟机使用命令介绍 qm <command> <vmid> [OPTIONS] ...
- nginx 代理 websocket
nginx 代理 websocket nginx 首先确认版本必须是1.3以上 map指令的作用: 该作用主要是根据客户端请求中$http_upgrade 的值,来构造改变$connection_up ...
- 大规模异常滥用检测:基于局部敏感哈希算法——来自Uber Engineering的实践
uber全球用户每天会产生500万条行程,保证数据的准确性至关重要.如果所有的数据都得到有效利用,t通过元数据和聚合的数据可以快速检测平台上的滥用行为,如垃圾邮件.虚假账户和付款欺诈等.放大正确的数据 ...
- Java精通并发-notify方法详解及线程获取锁的方式分析
wait(): 在上一次https://www.cnblogs.com/webor2006/p/11404521.html中对于无参数的wait()方法的javadoc进行了解读,而它是调用了一个参数 ...
- Dubbo基础入门
Dubbo概述 Dubbo的背景 随着互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,亟需一个治理系统确保架构有条不紊的演进. 单一应用架构 ...