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. Steam之两个list间交集、并集、差集

    public static void main(String[] args) { List<String> list1 = new ArrayList(); list1.add(" ...

  2. 1.工厂模式(Factory Method)

    注:图片来源于 https://www.cnblogs.com/-saligia-/p/10216752.html 工厂UML图解析: 工厂模式:client用户需要三步: 1.创建工厂: 2.生产产 ...

  3. MySql注释的写法

    每一种语言都有它的注释方式,代码量少的时候还可以,随着代码量越来越多,代码注释的重要性也越发凸显. 在mysql中主要有三种方式: 1.常用的方式,跟在css中那些注释一样 :/* 内容 */ /* ...

  4. Linux 基础学习1

    目录 Linux 基础学习 用户登录 终端 交互式接口 bash 修改ssh连接慢的步骤 命令提示符 显示提示符格式 命令 别名 命令格式 获取命令的帮助信息 man bash 快捷键 tab 键 引 ...

  5. layui.js源码分析

      /*! @Title: Layui @Description:经典模块化前端框架 @Site: www.layui.com @Author: 贤心 @License:MIT */ ;!functi ...

  6. UIAlertController 修改文字显示实现方法

    UIAlertController修改文字显示 不废话先上完整代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 UIAlertControll ...

  7. stm32 USART_IT_IDLE中断 一帧数据

    USART_IT_IDLE中断,是串口收到一帧数据后,发生的中断.也可以叫做一包数据 USART_IT_IDLE和USART_IT_RXNE区别 当接收到1个字节,会产生USART_IT_RXNE中断 ...

  8. java数据库数据导入excel

    data导出入excel中 controller: package com.longfor.hrssc.api.controller; import com.longfor.hrssc.api.mod ...

  9. PHP SplQueue 实现队列

    $que = new SplQueue(); $que->enqueue("a");//入队列 $que->enqueue("b"); $que-& ...

  10. jmeter+python+sh执行优化报告(一)

    缘由: 1)jmeter生成的html报告容量偏大 2)jmeter生成的报告,没有历史统计 3)此外,该目录整体可以整合的自动化平台内 故:做了调整~ 一.目录结构 1)scriptPy文件夹:主要 ...