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

  • 相关阅读:
    2020年终总结
    vi编辑器使用基本操作
    为什么Windows下重载Nginx配置不生效
    JAVA、C#中使用正则表达式替换字符串
    领域事件、集成事件、事件总线区别与关系
    asp.net core中使用Serilog以及自定义Enricher
    C语言----文件(高阶篇二十八)
    C语言----指针与一维数组(高阶篇二十七)
    C语言----位运算(进阶篇二十六)
    C语言----指针基础(进阶篇二十五)
  • 原文地址:https://www.cnblogs.com/joe235/p/14569762.html
Copyright © 2020-2023  润新知