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获取用户权限的更多相关文章

  1. 20171018 在小程序页面去获取用户的OpenID

    1. 在小程序的.js 文件中增加代码 //加载页面时到后台服务去获取openID onLoad: function (options) { //OpenId wx.login({ //获取code ...

  2. taro 微信小程序原生作用域获取

    在 Taro 的页面和组件类中,this 指向的是 Taro页面或组件实例. 但是一般我们需要获取 Taro的页面和组件 所对应的 小程序原生页面和组件实例,这个时候我们可以通过 this.$scop ...

  3. 小程序登录&授权&获取用户信息

    一 .登录 时序图如下: wx.login() 获取js_code 示例代码: App({   onLaunch: function() {     wx.login({       success: ...

  4. 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密

    后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...

  5. 逍遥云天 微信小程序开发之获取用户手机号码——使用简单php接口demo进行加密数据解密

    后边要做一个微信小程序,并要能获取用户微信绑定的手机号码.而小程序开发文档上边提供的获取手机号码的接口(getPhoneNumber())返回的是密文,需要服务器端进行解密,但是官方提供的开发文档一如 ...

  6. [小程序]微信小程序登陆并获取用户信息

    1.小程序js端调用框架登陆API,获取到一个临时code,拿着这个code去调用自己的服务端接口 2.在自己的服务器端,使用app_id app_secrect code可以获取到用户的openid ...

  7. Java springboot支付宝小程序授权,获取用户信息,支付及回调

    参考官方文档https://opendocs.alipay.com/mini/introduce/pay 支付宝小程序的支付和微信小程序的支付一样第一步都是要获取到用户的唯一标识,在微信中我们获取到的 ...

  8. 小程序wx.getUserInfo获取用户信息方案介绍

    问题模块 框架类型 问题类型 API/组件名称 终端类型 操作系统 微信版本 基础库版本 API和组件 - -   - -     背景 小程序一个比较重要的能力就是获取用户信息,也就是使用 wx.g ...

  9. C#微信小程序服务端获取用户解密信息

    using AIOWeb.Models; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.C ...

随机推荐

  1. js --装饰者模式

    定义 装饰者模式能够在补改变对象自身的基础上,在程序运行期间给对象动态的添加职责. 当看到装饰者模式的定义的时候,我们想到的js 的三大特性之一--继承,不也能够实现不改变对象自身的基础上,添加动态的 ...

  2. SQL月度统计

    select Convert ( VARCHAR(7),CreateTime,120) as Date ,sum(Money) as M FROM [LBAmmeterDB].[dbo].Am_Tas ...

  3. MySQL查询(关联查询)

    一.mysql查询与权限 (一)数据库关联查询 **内连接查询(inner join)** 查询两个表共有的数据,交集 SELECT * FROM tb1 INNER JOIN tb2 ON 条件 所 ...

  4. CentOS7清理老旧内核

    CentOS7如果是一步步内核升级上来的将会面临一个问题(一般安装了图形化界面的系统常见),由于内核增多/boot/的容量减少,导致启动变慢.这时就需要清理老旧内核,释放/boot/空间. 使用una ...

  5. 解决问题:Jupyter Notebook启动不会自动打开浏览器,每次都要自己打开浏览器输入网址

    1.找到anaconda下的anaconda prompt并打开(或者CMD)都可以 2.在anaconda prompt里面输入:jupyter notebook --generate-config ...

  6. CCS设置第一个li的元素与其他li样式不同

    <div class="ly-content-list"> <ul> <li> <div class="title"& ...

  7. python网络-HTTP协议(28)

    一.服务器和客户端介绍 1.什么是服务器? 简而言之:提供服务的机器就是服务器,至于提供什么服务不重要,重要的是要给其他人提供服务.例如:FTP服务器可以提供文件下载,SMTP服务器等等,不同的服务器 ...

  8. markdown种嵌入html标签,实现自定义样式

    转:https://www.cnblogs.com/buwuliao/p/9578918.html -------------------------------------------------- ...

  9. 配置数据源和配置jpa的yml文件

    spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver username: root password: root url: j ...

  10. vue 标签页以及标签页赋值

    背景: 使用vue增加了标签页,点击不同标签页传给后端的值不一样,用来做区分,如图: vue代码如下: 使用 form.PageA   form.PageB ,后端接收到的值 first.second ...