使用wepy 小程序授权点击取消授权失败的方案

软件结
• 阅读 2524

在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()
  }
点赞
收藏
评论区
推荐文章
待兔 待兔
1年前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
微信小程序部分api 会触发 onShow onHide
解决部分api触发小程序onShowonHide首先要明白微信小程序的onShow()onHide()分为页面级的和应用级的,应用级的就是app.js里面的那几个,页面级的就是pages里的当使用了下列api时,均会触发页面级和应用级的onShowonHide1.点击右上角小圆点关闭小程序。2.图片预览:wx.preview
Souleigh ✨ Souleigh ✨
5年前
快速入门 WePY 小程序
一、WePY介绍WePY是 _腾讯_参考了Vue等框架对原生小程序进行再次封装的框架,更贴近于MVVM架构模式,并支持ES6/7的一些新特性。二、WePY使用1、WePY的安装或更新都通过npm进行:npminstallgwepyc
Dax Dax
4年前
微信小程序不支持wx.getUserInfo授权的解决方法
微信小程序最近被吐槽最多的一个更改,就是用户使用wx.getUserInfo(开发和体验版)时不会弹出授权,正式版不受影响。现在授权方式是需要引导用户点击一个授权按钮,然后再弹出授权。我最近围绕这个做了一些研究,来看看我是如何做好这个授权。1.用户进来一个页面时,按照微信小程序的生命周期,开始解析onLoad里面的内容。所以我们在第一步,就把代码放在这里
Stella981 Stella981
4年前
IdentityServer4在Asp.Net Core中的应用(一)
  IdentityServer4是一套身份授权以及访问控制的解决方案,专注于帮助使用.Net技术的公司为现代应用程序建立标识和访问控制解决方案,包括单点登录、身份管理、授权和API安全。  下面我将具体介绍如何在.NetCore中实现OAuth授权,从最简单的授权模式开始,在上一篇对OAuth2.0的详细描述中,在客户端模式中,我们说它在严
Wesley13 Wesley13
4年前
mysql报错 常见 1045 10061
报错1045:远程没有设置用户远程访问的权限解决方案:进行授权(红色是你的密码)如果想root用户使用password从任何主机连接到mysql服务器的话。GRANTALLPRIVILEGESON\.\TO'root'@'%'IDENTIFIEDBY'123123'WITHGRANTOPTION;如
Stella981 Stella981
4年前
Kerberos无约束委派的攻击和防御
 0x00前言简介当ActiveDirectory首次与Windows2000Server一起发布时,Microsoft就提供了一种简单的机制来支持用户通过Kerberos对Web服务器进行身份验证并需要授权用户更新后端数据库服务器上的记录的方案。这通常被称为Kerberosdoublehopissue(双跃点问题),
Wesley13 Wesley13
4年前
MySQL.授权管理
查看权限:showgrantsfor'用户’@‘IP地址’  授权:grant权限on数据库.表to‘用户’@‘IP地址’取消权限:revoke权限on数据库.表from‘用户’@‘IP地址’常用权限:allprivileges除grant外的所有权限     select  仅查权限     select,i
Wesley13 Wesley13
4年前
C#开发——网站应用微信登录开发
1\.在微信开放平台注册开发者账号,并有一个审核已通过的网站应用,并获得相对应的AppID和AppSecret,申请通过登陆后,方可开始接入流程。2.微信OAuth2.0授权登录目前支持authorization\_code模式,适用于拥有server端的应用授权。该模式整体流程为:1.第三方发起微信授权登录请求,微信用户允许授权第三方应
软件结
软件结
Lv1
你在我这里无法保存,因为你过于庞大,内存不足。
文章
6
粉丝
0
获赞
0