资讯专栏INFORMATION COLUMN

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

baihe / 2700人阅读

摘要:在里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案警告您点击了拒绝授权将无法正常显示个人信息点击确定重新获取授权。

在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://www.ucloud.cn/yun/100441.html

相关文章

  • 程序 wepy 用户取消授权以及取消获取地理位置后处理方法

    摘要:在里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案警告您点击了拒绝授权将无法正常显示个人信息点击确定重新获取授权。 在wepy里使用进行小程序页面授权,里面包含了用户点击取消的重新授权方案: //auth.js /* * @Author: Porco_Mar * @Date: 2018-04-11 15:49:55 * @Last Modified by: Por...

    wqj97 评论0 收藏0
  • 记录微信程序

    摘要:除官方外的参考文章微信小程序实例创建下发模板消息实例手把手教你开发微信小程序之模版消息开发教你突破小程序模板消息的推送限制获取用户信息接口的废弃问题接口是获取用户信息昵称,头像等的接口,在官方文档上写是即将废弃。 ----------------更新-------------- 2018年10月10日官网3个接口废弃的通知: 1、分享监听接口分享消息给好友时,开发者将无法从callba...

    EastWoodYang 评论0 收藏0
  • 做完程序项目、老板给我加了6k薪资~

    摘要:大家好,我是平头哥联盟的首席填坑官苏南,今天要给大家分享的是最近公司做的一个小程序项目,过程中的一些好的总结和遇到的坑,希望能给其他攻城狮带来些许便利,更希望能做完之后老板给你加薪今天是中秋节的第一天,假日的清晨莫名的醒的特别早,不知道为什 showImg(https://segmentfault.com/img/bVbhAYf?w=1278&h=722);   大家好,我是@IT·平...

    z2xy 评论0 收藏0
  • 程序如何生成海报分享朋友圈

    摘要:项目需求写完有一段时间了,但是还是想回过来总结一下,一是对项目的回顾优化等,二是对坑的地方做个记录,避免以后遇到类似的问题。需求利用微信强大的社交能力通过小程序达到裂变的目的,拉取新用户。摘要: 小程序开发必备技能啊... 原文:小程序如何生成海报分享朋友圈 作者:小白 Fundebug经授权转载,版权归原作者所有。 项目需求写完有一段时间了,但是还是想回过来总结一下,一是对项目的回顾优...

    lemon 评论0 收藏0

发表评论

0条评论

baihe

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<