使用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="{ ...
随机推荐
- JAVA Eclipse的Android的进程和生命周期是什么
安卓程序的生命周期是不受自己控制的,安卓的程序根据不同的重要性做了一些区分,最重要的进程仅仅在安卓已经崩溃或者卡死的情况下才会终止前台进程. Activity就是表现层的界面,它有三种常见的状态, ...
- apache的配置优化
[APACHE的工作方式] prefork模式(默认)这个多路处理模块(MPM)实现了一个非线程型的.预派生的web服务器,它的工作方式类似于Apache 1.3.它适合于没有线程安全库,需要避免线程 ...
- vue2.X v-model 指令
1.v-model指令 <!DOCTYPE html> <html> <head> <title></title> <script s ...
- UNP学习笔记(第十七章 ioctl操作)
ioctl相当于一个杂物箱,它一直作为那些不适合归入其他精细定义类别的特性的系统接口. 本章笔记先放着,到时候有需要再看 ioctl函数 #include <unistd.h> int i ...
- python:如何判断字符串中的内容是否都为数字并且把字符串转换为数字
使用str.isdigit();有两种使用方法 str.isdigit('12345') =====>True str.isdigit('aaaaa')======>False 或者 '1 ...
- Notepad2替换记事本--映像劫持
Image File Execution Options就是映像劫持技术,通过此种方式替换记事本,非常地绿色环保. Image File Execution Options是CreateProces ...
- Realm多线程中的那些坑...
个人在开发中遇到的一些小坑... 可能会持续更新... 1.RealmObject自带线程保护功能.仅仅能在创建它的线程中訪问.在子线程中不能訪问. 也就是说.假设你在主线程中new了一个RealmO ...
- 如何将linux服务器作为文件服务器
在开发过程中想要使用linux服务器作为文件服务器,可以通过 IP+文件名来获取文件信息,比如http://localhost/banner/a.jpg.设置过程如下 1.安装apache2 sudo ...
- matlab biplot 符号的困惑
在matlab中做Principal component Analysis 时,常要用biplot 函数来画图,表示原分量与主分量(principal component)之间的关系,以及原始观察数据 ...
- iOS项目 -- 模仿花椒直播做的第二层界面完整版
,项目开始做了,好遗憾的是,花椒app有更新了版本,