WePY 小程序授权点击取消授权失败的方案

摘要:
今天,我看到了一个微信小程序wepy框架,用于获取用户信息授权并记录下来以备将来使用。

今天看到一个微信小程序 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()
  }

免责声明:文章转载自《WePY 小程序授权点击取消授权失败的方案》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇Linux性能监控FPGA 移交代码学习——Map错误 ODDR2 使用问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JNDI简介和简单示例

什么是JNDI? The Java Naming and Directory Interface是访问不同名字和目录服务的统一API接口。 不同的服务使用不同的名字格式。 Java程序需要以相同的格式访问数据库,文件,目录,对象和网络。 JNID有两部分接口:应用程序接口和提供服务的接口。在应用程序中使用API来访问名字或目录服务,在一个新的服务中使用SP...

在ASP.NET Core跨平台应用程序开发中如何捕获并处理全局异常

问题描述 在传统的ASP.NET Web Api 应用程序开发中,我们处理全局异常的方法通常是实现一个ExceptionFilterAttribute的子类,如下: public classErrorHandlingFilter : ExceptionFilterAttribute { public override voidOnException...

控制Goroutine并发量的解决方案

前言 Go语言虽然开并发Goroutine特别简单,但是实际中如果不控制并发的数量会导致资源的浪费以及同时占用大量服务资源(http连接、数据库连接、文件句柄等)导致服务性能下降! 笔者之前总结过一篇在业务代码中控制并发数量的文章:Go控制协裎并发数量的用法及实际中的一个案例 ants库实现链接池的效果控制并发量 今天介绍另外一个控制并发数量的第三方库:a...

微信小程序开发五:案例实践

5.1天气预报 在这一节中,我们将开发一个天气预报的小程序,使用的数据接口为百度天气预报的接口,该接口可以根据经纬度坐标查询所在地天气。准备工作使用百度接口需要预先申请。在本书第4章中有百度ak的申请方法以及百度天气预报接口介绍。所不同的是第4章使用城市名称查询天气,而本节中使用坐标进行查询。在小程序中,将会向该地址发起请求,需要预先将百度接口所在域...

数据存储

iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3Core Data应用沙盒每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒应用沙盒的文件系统目录,如下图所示(假设应...

android绘图—Paint path 旋转

http://meteor6789.blog.163.com/blog/static/35040733201111193535153/ Piant 看一段代码: mPaint = new Paint();mPaint.setAntiAlias(true);//锯齿mPaint.setDither(true);//mPaint.setColor(0xFF3...