设置API:wx.openSetting,wx.getSetting使用说明(示例:地图授权与取消授权后的重新授权)

这个API解决了过去一个长久以来无法解决的问题,如何让用户重复授权;

打开小程序的设置界面:就是主动调取授权

目前资料极少,但是已经可以让大家先看看了;

官方文档地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/setting.html#opensettingobject

基础库版本 1.1.0 开始支持,低版本需做兼容处理

调起客户端小程序设置界面,返回用户设置的操作结果

目前的授权影响范围:

scope 说明:

scope 对应接口
scope.userInfo wx.getUserInfo
scope.userLocation wx.getLocation, wx.chooseLocation
scope.address wx.chooseAddress
scope.record wx.startRecord

相关文章:微信小程序重新调起授权用户信息,扫码进入小程序获取二维码携带参数wx.openSetting,wx.setClipboardData,wx.getClipboardData
微信小程序最新更新--api测试一览

微信小程序之新版本测试

相关讨论:
wx.openSetting()怎么判断success里面 用户有没有授权了我写的

[AppleScript] 纯文本查看 复制代码

?


1

2

3

4

5

6

7

8

9

wx.openSetting({

      success:(res)=>{

          console.log("授权结果..")

        console.log(res)

        if(!res.authSetting.scope.userInfo || !res.authSetting.scope.userLocation){

            applyNotice()

        }

      }

    })

不正常啊,res的authSetting两个对象,两个对象的键名是scope.userInfo和scope.userLocation都是带了 . 的,怎么判断用户都授权了呢?我的小程序需要用户授权才能使用服务呢。做的事基于LBS的共享服务,没有定位和用户登录,就无法获取服务器上的区域划分的数据。用户就看不了自己周围的地图和自己的用户信息

[AppleScript] 纯文本查看 复制代码

?


1

2

3

4

5

6

7

8

9

wx.openSetting({

      success:(res)=>{

          console.log("授权结果..")

        console.log(res)

        if(!res.authSetting["scope.userInfo"] || !res.authSetting["scope.userLocation"]){

            applyNotice()

        }

      }

    })

试试上述改动。

打印出来的console的可以判断了,对象还可以像数组一样键值调用,谢谢啦!

相关讨论:
用户取消后如何再次调出允许授权的modal?
如果用户错点了拒绝,就没办法再继续进入使用小程序了。能重新再调出许可的小窗让用户重新再选择吗?
答:林超

使用 wx.openSetting接口

相关讨论:

关于用户授权界面的唤醒
用户如果不小心拒绝了授权,之前是过十分钟之后还可以被唤醒,最近发现唤不醒了,一直是:"getUserInfo:fail auth deny",该如何应对?
答:Tïedào

//如下,在getUserInfo失败后调用wx.openSetting即可

如下是我的真实示例:

// pages/contactus/contactus.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        loadingHidden: false,
        latitude: 23.099994,
        longitude: 113.324520,
        markers: [{
            iconPath: ‘../../images/location.png‘,
            id: 1,
            latitude: 31.245442,
            longitude: 121.506337,
            title: ‘千卉摄影‘,
            width: 50,
            height: 50,
            callout: {
                content: ‘千卉摄影‘,
                color: ‘#ff00000‘,
                fontSize: ‘18‘,
                borderRadius: ‘5‘,
                bgColor: ‘#ffffff‘,
                padding: ‘10‘,
                display: ‘ALWAYS‘,
                textAlign: ‘center‘
            }
        }],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        wx.getSystemInfo({
            success: function (res) {
                // console.log(res)
                that.setData({
                    scrollHeight: res.windowHeight
                });
            }
        });
      wx.getSetting({
        success(res) {
          console.log(!res.authSetting[‘scope.userLocation‘]);
          if (!res.authSetting[‘scope.userLocation‘]) {
            wx.authorize({
              scope: ‘scope.userLocation‘,
              success() {
                // 用户已经同意
                //其他操作...
                console.log("用户已经同意位置授权");
              },
              fail() {
                console.log("用户已经拒绝位置授权");
                that.openConfirm();//如果拒绝,在这里进行再次获取授权的操作
              }
            })
          }
          //其他操作..
        }
      });
    },
  //当用户第一次拒绝后再次请求授权
  openConfirm: function () {
    wx.showModal({
      content: ‘检测到您没打开此小程序的定位权限,是否去设置打开?‘,
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        //点击“确认”时打开设置页面
        if (res.confirm) {
          console.log(‘用户点击确认‘)
          wx.openSetting({
            success: (res) => { }
          })
        } else {
          console.log(‘用户点击取消‘)
        }
      }
    });
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function (e) {
        this.mapCtx = wx.createMapContext(‘myMap‘)
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        var that = this
        app.reqGetfunc.reqGet(‘qh_contact.html‘, {}, function (res) {
            // console.log(res)
            that.setData({
                tel: res.tel,
                address: res.address,
                map: res.map,
                qq: res.qq,
                wechat: res.wechat,
                code: res.code,
                loadingHidden: true
            })

        })
    },
    getLocation:function(){
        var that = this
        wx.getSetting({
          success(res) {
            console.log(res)
            if (!res.authSetting[‘scope.userLocation‘]) {
              wx.authorize({
                scope: ‘scope.userLocation‘,
                success() {
                  // 用户已经同意小程序使用此功能,后续调用 wx.getLocation 接口不会弹窗询问
                  wx.getLocation({
                    type: ‘gcj02‘,
                    success: function (res) {
                      //console.log(res)
                      var latitude = res.latitude
                      var longitude = res.longitude
                      var speed = res.speed
                      var accuracy = res.accuracy
                      console.log("latitude:" + latitude)
                      console.log("longitude:" + longitude)
                      console.log("speed:" + speed)
                      console.log("accuracy:" + accuracy)
                      wx.openLocation({
                        name: ‘山东省‘,
                        address: ‘山东省临沂市千卉摄影‘,
                        //latitude: latitude,
                        //longitude: longitude,
                        latitude: Number(that.data.map.latitude),
                        longitude: Number(that.data.map.longitude),
                        scale: 28
                      })
                    }
                  })
                },
                fail() {
                  console.log("用户已经拒绝位置授权");
                  that.openConfirm();//如果拒绝,在这里进行再次获取授权的操作
                }
              })
            }
          }
        })
    },
    chooseLocation:function(){
      wx.getSetting({
        success(res) {
          if (!res.authSetting[‘scope.userLocation‘]) {
            wx.authorize({
              scope: ‘scope.userLocation‘,
              success(){
                wx.chooseLocation({
                  success: function (res) {
                    var name = res.name
                    var address = res.address
                    var latitude = res.latitude
                    var longitude = res.longitude
                  }
                })
              }

            })
          }
        },
        fail() {
          console.log("用户已经拒绝位置授权");
          that.openConfirm();//如果拒绝,在这里进行再次获取授权的操作
        }
      })

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})

原文地址:https://www.cnblogs.com/zmdComeOn/p/11778225.html

时间: 2024-08-30 01:55:02

设置API:wx.openSetting,wx.getSetting使用说明(示例:地图授权与取消授权后的重新授权)的相关文章

打开小程序设置页(wx.openSetting),注意点

注意:bindtap的处理函数中,可以直接调用 wx.openSetting 资料 网址 wx.openSetting(调起客户端小程序设置界面,返回用户设置的操作结果) https://developers.weixin.qq.com/miniprogram/dev/api/open-api/setting/wx.openSetting.html 打开小程序设置页(wx.openSetting)接口调整 https://developers.weixin.qq.com/community/de

微信 小程序 drawImage wx.canvasToTempFilePath wx.saveFile 获取设备宽高 尺寸问题

以下问题测试环境为微信开发者0.10.102800,手机端iphone6,如有不对敬谢指出. 根据我的测试,context.drawImage,在开发者工具中并不能画出来,只有预览到手机中显示. wx.canvasToTempFilePath wx.saveFile 官方文档中只有一行,真是坑爹啊,原来 wx.canvasToTempFilePath参数为一个对象包括canvasID,success,fail,complete,和wx.saveFile差不多: wx.canvasToTempFi

创建ArcGIS API for JavaScript的第一个示例程序

原文:创建ArcGIS API for JavaScript的第一个示例程序 在上一篇博客中已经介绍了如何搭建ArcGIS API for JavaScript开发环境,如果您还没有搭建好开发环境的话,参考博客:http://blog.csdn.net/zdw_wym/article/details/48678913. 如果开发环境搭建好了的话,那么今天我们继续来搭建我们的第一个ArcGIS API for JavaScript应用程序. 下面首先将代码贴出来,复制到VS2012中新建的html

css设置背景固定不滚动效果的示例

css设置背景固定不滚动效果的示例 背景固定不滚动各位看到最多的无非就是QQ空间了,我们在很多的空间都可以看到内容滚动而北京图片不滚动了,下文整理了几个关于背景固定不滚动css代码. 一.css设置背景不滚动的效果 <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">

drf的throttle设置api的访问速率

drf的throttle设置api的访问速率,在setting中设置,登录用户和匿名用户的请求频率做限制,限制单位天.时.分等都可以 在views中: from rest_framework.throttling import UserRateThrottle,AnonRateThrottle 视图类中 throttle_class = (UserRateThrottle,AnonRateThrottle)

ArcGIS API for Silverlight之配准JPG图片地图文字倾斜解决方案

原文:ArcGIS API for Silverlight之配准JPG图片地图文字倾斜解决方案 根据实际JPG图片进行配准后,发布的地图,利用ArcGIS API for Silverlight在网页上显示的时候,原先的文字总有倾斜的现象,如何解决? 图一.配准后有文字倾斜现象的地图 解决方案如下: <esri:Map x:Name="myMap" IsLogoVisible="False" ZoomDuration="0:00:01" E

【百度地图API】如何制作一张魔兽地图!!——CS地图也可以,哈哈哈

原文:[百度地图API]如何制作一张魔兽地图!!--CS地图也可以,哈哈哈 摘要: 你玩魔兽不?你知道如何做一张魔兽地图不?! 快来看此文吧! --------------------------------------------- 一.制作魔兽地图的准备工作 其实,你只需要准备一些地图的瓦片图,就可以了. 用切片工具,把一张完整的图片(最好大一些,清楚一些),切成小片.(下周我贴这个工具的下载链接出来.留个悬念,哈哈.) 正式公布切图工具链接: http://openapi.baidu.co

百度地图API快速调用,一键生成百度地图

对于在网站或者网页之内插入百度地图插件,可能很多编程技术人员都知道如何来做,但是做起来所花费的时间都比较长,那么下面跟大家分享一下如何快速调用百度地图API,一件生成百度地图的方法,这种方法适用于任何的编程语言,所以有兴趣的编程开发者可以保存一下.body之间源代码如下:<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true&quo

WPF技术触屏上的应用系列(二): 嵌入百度地图、API调用及结合本地数据库在地图上进行自定义标点的实现

原文:WPF技术触屏上的应用系列(二): 嵌入百度地图.API调用及结合本地数据库在地图上进行自定义标点的实现 去年某客户单位要做个大屏触屏应用,要对档案资源进行展示之用.客户端是Window7操作系统,54寸大屏电脑电视一体机.要求有很炫的展示效果,要有一定的视觉冲击力,可触控操作.当然满足客户的要求也可以有其它途径.但鉴于咱是搞 .NET技术的,首先其冲想到的微软WPF方面,之前对WPF的了解与学习也只是停留在比较浅的层面,没有进一步深入学习与应用.所以在项目接来以后,也就赶鸭子上架了,经过