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. 【转载】C#中使用Average方法对List集合中相应元素求平均值

    在C#的List集合操作中,有时候需要对List集合元素进行汇总求平均值,如数值类型的List集合元素,有时候对象类型的List集合也需要对集合中的元素的某个对象进行汇总求平均值,此时都可以使用到Av ...

  2. Postgres 多实例实例部署方式

    Postgres 数据库在原有示例正常运行情况下,新增一个端口示例,主要目的解决新的项目和原有项目的数据库部署不在冲突,可以独立运行,备份和还原数据互不影响,主要用的的命令有  initdb 数据库初 ...

  3. springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)

    一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...

  4. jenkens docker启动

    docker run \ -u root \ --rm \ -d \ -p 8080:8080 \ -p 50000:50000 \ -v jenkins-data:/var/jenkins_home ...

  5. thinkphp概述

    thinkphp框架是一个免费的,开源,快速,简单的面向对象的轻量级PHP开发框架. 了解什么是thinkphp概述,thinkphp项目目录结构,thinkphp的控制器,视图,thinkphp项目 ...

  6. 运维开发笔记整理-URL配置

    运维开发笔记整理-URL配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.URL路由 对于高质量的Web应用来说,使用简洁,优雅的URL的路由是一个非常值得重视的细节.Dja ...

  7. rest-assured-doc接口自动化测试,数据驱动测试平台

    原文:https://github.com/rest-assured/rest-assured/wiki/Usage 本文github地址:https://github.com/RookieTeste ...

  8. 洛谷 P1280 尼克的任务题解

    题目链接:https://www.luogu.org/problem/P1280 题目描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每 ...

  9. Centos7 安装谷歌浏览器

    配置下载yum源 cd /etc/yum.repos.d vim google-chrome.repo [google-chrome] name=google-chrome baseurl=http: ...

  10. Dubbo源码分析(4):Protocol

    Protocol接口是Dubbo框架的核心组件.Dubbo框架启动protocol接口实现类,由spring的xml文件配置决定.RegistryProtocol协议是Protocol协议的核心,它负 ...