使用wepy 小程序授权点击取消授权失败的方案
在wepy里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案:
//auth.js
/*
* @Author: Porco_Mar
* @Date: 2018-04-11 15:49:55
* @Last Modified by: Porco_Mar
* @Last Modified time: 2018-04-18 10:43:36
*/
import wepy from 'wepy'
export const _timer = (context) => {
return new Promise((resolve, reject) => {
let _timer = null;
clearInterval(_timer);
_timer = setInterval(() =>{
resolve(author(context))
},500)
context.data.timer = _timer;
})
}
export const author = (context) => {
return new Promise((resolve,reject) => {
var that = context;
wepy.getUserInfo({
success: (res) =>{
var userInfo = res.userInfo;
that.data.userInfo = userInfo;
resolve(res.userInfo)
},
fail: (res) =>{
console.log('.......getUserInfo fail.........')
clearInterval(context.data.timer)
wepy.showModal({
title: '警告',
content: '您点击了拒绝授权,将无法正常显示个人信息,点击确定重新获取授权。',
success:function(res){
if (res.confirm) {
wepy.openSetting({
success: (res) => {
if (res.authSetting["scope.userInfo"] || res.authSetting["scope.userLocation"]){////如果用户重新同意了授权登录
wepy.getUserInfo({
success:function(res){
resolve(res.userInfo)
that.$parent.globalData.userInfo = res.userInfo;
}
})
}
},fail: function(res){
resolve({'avatarUrl':'','nickName':'翠花'})
console.log('没有选择授权')
}
})
}else{
console.log('还是不同意授权')
}
}
})
},
complete: function (res){
}
})
})
}
let isBoolen = true;
export const location = (context) => {
return new Promise((resolve, reject) => {
if(context.$parent.globalData.location != null){
resolve(context.$parent.globalData.location)
console.log('已获取location')
}else{
console.log('没有获取到location ')
wepy.getSetting({
success(res) {
console.log(res)
if(!res.authSetting['scope.userLocation']) {
wx.showModal({
title: '温馨提醒',
content: '需要获取您的地理位置才能使用小程序',
cancelText: '不使用',
confirmText: '获取位置',
success: function(res) {
if(res.confirm) {
getLocation(context).then((res) => {
// console.log(res)
if (res.code == 1){
if(isBoolen){ //第一次不执行
isBoolen = false;
}else{
wepy.openSetting({ // 点击自带取消定位健会调用这个面板
success: (res) => {
if (res.authSetting["scope.userLocation"]){////如果用户在面板重新同意了授权地理位置
console.log('--有了scope.userLocation--')
resolve(getLocation(context)) //点击面板后再次调用getLocation返回参数
}
},fail: function(res){
console.log('--没有scope.userLocation--')
}
})
}
}else{
resolve(getLocation(context))
}
})
} else if(res.cancel) {
//resolve(getLocation(context))
//不做任何操作
}
}
})
}
}
})
}
})
}
export const getLocation = (context) => {
return new Promise((resolve, reject) => {
wx.getLocation({
type: 'wgs84',
success: function(res) {
var latitude = res.latitude
var longitude = res.longitude
var speed = res.speed
var accuracy = res.accuracy
context.$parent.globalData.location = {'code': 0, 'latitude':latitude, 'longitude':longitude, 'speed':speed, 'accuracy':accuracy}
resolve(context.$parent.globalData.location)
},
fail: function(res){
resolve({'code': 1, 'latitude':'', 'longitude':'', 'speed':'', 'accuracy':''})
}
})
})
}
// index.wepy
import wepy from 'wepy'
import {_timer, author, location} from '../utils/auth'
onShow() {
let globalDt = this.$parent.globalData
if(globalDt.userInfo.nickName && globalDt.userInfo.avatarUrl){
this.userInfo = globalDt.userInfo;
}else{
this.getValue(); // 获取userInfo
}
if(globalDt.location === null){
this.getLt(this); // 获取地理位置
}else{
console.log('当前页面获取过location了')
//console.log(globalDt.location)
this.location = globalDt.location;
}
}
async getValue () {
const datam = await _timer(this)
console.log(datam)
this.userInfo = datam;
this.$apply();
}
async getLt (context) {
const local = await location(context)
console.log(local)
this.location = local;
this.$apply()
}
来源:https://segmentfault.com/a/1190000017526582
使用wepy 小程序授权点击取消授权失败的方案的更多相关文章
- 微信小程序获取用户信息“授权失败”场景的处理
很多的时候我们在处理小程序功能的时候需要用户获取用户信息,但是呢为了信息安全,用户不授权导致授权失败场景:但是小程序第二次不在启动授权信息弹层,为了用户体验,可以用以下方式处理: function i ...
- 快速入门 WePY 小程序【转】
一.WePY介绍 WePY 是 腾讯 参考了Vue 等框架对原生小程序进行再次封装的框架,更贴近于 MVVM 架构模式, 并支持ES6/7的一些新特性. 二.WePY 使用 1.WePY的安装或更新都 ...
- 小程序点击跳转外部链接 微信小程序提示:不支持打开非业务域名怎么办 使用web-view 配置业务域名
小程序点击跳转外部页面 1.index.wxml 添加点击事件 标签可以是小程序支持的 <!-- 邀请好友 --> <cover-image src='/img/invitat ...
- wepy小程序实现选项卡
先上效果: 本文是基于前面几篇文章: 使用wepy开发微信小程序商城第一篇:项目初始化 使用wepy开发微信小程序商城第二篇:路由配置和页面结构 使用wepy开发微信小程序商城第三篇:购物车(布局篇) ...
- WePY | 小程序组件化开发框架
资源连接: WePY | 小程序组件化开发框架 WePYAWESOME 微信小程序wepy开发资源汇总 文档 GITHUB weui WebStorm/PhpStorm 配置识别 *.wpy 文件代码 ...
- 【小程序开放激励视频】--wepy小程序添加激励视频
小程序开放激励视频是对小程序开发者一个福音,小程序开发者可以完成一些变现,以增加收入! 本文章针对已经有开发经验或者正在进行小程序开发的同学~ 官方文档:激励视频广告 定义页面变量,用于创建视频实例 ...
- 【WePY小程序框架实战四】-使用async&await异步请求数据
[WePY小程序框架实战一]-创建项目 [WePY小程序框架实战二]-页面结构 [WePY小程序框架实战三]-组件传值 async await 是对promise的近一步优化,既解决了promise链 ...
- 【WePY小程序框架实战三】-组件传值
[WePY小程序框架实战一]-创建项目 [WePY小程序框架实战二]-页面结构 父子组件传值 静态传值 静态传值为父组件向子组件传递常量数据,因此只能传递String字符串类型. 父组件 (paren ...
- 微信小程序点击图片放大预览
微信小程序点击图片放大预览使用到 wx.previewImage 接口,可以放大.上/下一张 上代码 wxml代码 <view class='content-img' wx:if="{ ...
随机推荐
- GLSL逐顶点光照[转]
转载:http://blog.csdn.net/hgl868/article/details/7872350 引言 在OpenGL中有三种类型的光:方向光(directional).点光(point) ...
- 【开发总结】—— BABYLON 项目开发必备系列
前言:在公司主要使用Babylon.js作为前端引擎,我自己在开发中总结到基本上每一个新项目都会需要这些基本设置. 一.ios兼容:解决镜面反射 如果不加offline,材质中有镜面反射,苹果手机 ...
- shell中sed命令
sed -i '/cd ${LDIR_DEST}\/webextend\/pc && ln -s \/hard\/www_winclient\/bboxpc.exe ./a\ \tcd ...
- LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)
翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...
- Android · SQLiteOpenHelper实例PrivateContactsDBHelper
package privatecontact; import android.content.ContentValues; import android.content.Context; import ...
- 在C#中怎样推断线程当前所处的状态
在C#中怎样推断线程当前所处的状态 老帅 在C#中.线程对象Thread使用ThreadState属性指示线程状态.它是带Flags特性的枚举类型对象. ThreadState 为线 ...
- 介绍JSON
0x00 介绍JSON 介绍JSON :http://www.json.org/json-zh.html Introducing JSON :http://www.json.org/
- log4j入门及常用配置
<pre class="java" name="code">import org.apache.log4j.BasicConfigurator; ...
- 深入Asyncio(四)Coroutines
Coroutines asyncio在3.4版本添加到Python中,但通过async def和await关键字创建coroutines的语法是3.5才加入的,在这之前,人们把generators当作 ...
- java eclipse使用不同jdk版本
因为开发需要,两个工程分别需要使用jdk1.6(elipse indigo)和jdk1.8(eclipse neon).因为两个eclipse对于jdk版本的要求不同,若只在环境变量中配置jdk版本, ...