在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()
}