使用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="{ ...
随机推荐
- flask的日志输出current_app.logger.debug
环境部署方式:nginx+supervisord+gunicorn在/etc/supervisord.conf中配置日志的输出路径stdout_logfile=/home/admin/workspac ...
- 读取配置文件包含properties和xml文件
读取properties配置文件 /** * 读取配置文件 * @author ll-t150 */ public class Utils { private static Properties pr ...
- opengl加载多个3ds模型失败记
VC6 下载 http://blog.csdn.net/bcbobo21cn/article/details/44200205 opengl环境配置 http://blog.csdn.net/bcbo ...
- swoole新手教程01-环境搭建及扩展安装
写在前面的废话 <swoole源代码分析>已经写了13章,整个swoole的核心架构基本都分析的差点儿相同了.于是心里一直以来想整理swoole的文档并写一份教程的想法就再度浮了出来. 实 ...
- SAS学习经验总结分享:篇四—SQL过程
SQL过程 SQL过程是实现对数据集或关系数据库的表进行操作的过程,对数据集或关系数据库的表进行查询.修改.创建表.删除数据.插入数据和更新数据等功能.提现了SAS对大型数据库管理系统通用的SQL语言 ...
- Java NIO之Charset类字符编码对象
介绍 java中使用Charset来表示编码对象 This class defines methods for creating decoders and encoders and for retri ...
- C语言 结构体篇
结构体:是一种构造类型 它是由若干成员组成的 其中每一个成员都可以是一个基本数据类型或者又是一个构造类型 定义结构体变量后,系统就会为其自动分配内存 为了便于更大的程序便于修改和使用 常常将结构体类 ...
- UML类图简明教程
作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...
- python创建迅雷批量任务
其实不是真的创建了批量任务,而是用python创建一个文本文件,每行一个要下载的链接,然后打开迅雷,复制文本文件的内容,迅雷监测到剪切板变化,弹出下载全部链接的对话框~~ 实际情况是这样的,因为用py ...
- C# 杀掉后台进程
var p = Process.GetProcessesByName("WINWORD"); if (p.Any()) { for (int i = 0; i < p.Len ...