当前位置: 首页 > news >正文

微信小程序在用户拒绝授权后无法使用wx.opensetting再次获取定位授权

 _getLocation(callback,errCall) {if (app.globalData.isLocationIng) {setTimeout(() => {this._getLocation(callback);}, 100);return;}app.globalData.isLocationIng = true;wx.getSetting({success: (resData) => {// 检查永久拒绝状态if (resData.authSetting["scope.userLocation"] === false) {app.globalData.isLocationIng = false;errCall && errCall({ type: 'permanent_deny' });return;}if (!resData.authSetting["scope.userLocation"]) {wx.authorize({scope: "scope.userLocation",success: () => this._getlocaldata(callback, errCall),fail: () => {// 新增:首次拒绝处理if (!this.data.hasTriggeredSetting) {this.setData({ hasTriggeredSetting: true });wx.showModal({title: '位置信息授权申请',content: 'xxx为了给您更好的xx服务,我们需要您的授权',confirmText: '去授权',cancelText: '残忍拒绝',success: (res) => {if (res.confirm) {wx.openSetting({success: (settingdata) => {if (settingdata.authSetting["scope.userLocation"]) {this._getlocaldata(callback);} else {app.globalData.isLocationIng = false;errCall && errCall({ type: 'permanent_deny' });}}});} else {app.globalData.isLocationIng = false;errCall && errCall({ type: 'permanent_deny' });}}});} else {app.globalData.isLocationIng = false;errCall && errCall({ type: 'permanent_deny' });}}});} else {this._getlocaldata(callback, errCall);}},fail: (msgData) => {console.log(msgData);app.globalData.isLocationIng = false;}});},_getlocaldata(callback,errCall) {if (wx.getStorageSync("address-point")) {let point = wx.getStorageSync("address-point").point;this._setaddressData({lat: point.lat,lng: point.lng,from: 'storage'},callback);} else {wx.getLocation({type: "gcj02",isHighAccuracy: true,success: (res) => {const latitude = res.latitude;const longitude = res.longitude;// const speed = res.speed;// const accuracy = res.accuracy;this._setaddressData({lat: latitude,lng: longitude,},callback);},fail: (err) => {if (err.errMsg === "getLocation:fail auth deny") {console.log("当初用户拒绝,再次发起授权");//此方法无用,因为微信不允许在异步回调中直接调用 wx.openSetting,必须由用户主动触发(如点击按钮),所以直接在_getLocation方法里进行处理wx.openSetting({success: (settingdata) => {if (settingdata.authSetting["scope.userLocation"]) {wx.showToast({icon: "none",title: "获取权限成功",duration: 1500,});this._getlocaldata(callback);} else {app.globalData.isLocationIng = false;wx.showToast({icon: "none",title: "获取权限失败,定位需要用户授权",duration: 1500,});}},fail:(err)=>{try {this.is_errCall(errCall)} catch (error) {console.log(error)}}});}else{app.globalData.isLocationIng = false;}},});}},

在myhehavior组件里无法在用户已经拒绝授权后再调用wx.opensetting()。wx.opensetting()必须由用户主动触发(如点击按钮),不能在wx.getLocation 的fail 回调中直接调用。这是为了防止开发者强制跳转设置页干扰用户,所以我直接在getlocation里直接增加二次确认弹窗,为了防止用户第一次拒绝定位授权。

http://www.dtcms.com/a/265737.html

相关文章:

  • 针孔相机模型
  • python学习打卡day59
  • 【轨物洞见】光伏机器人与组件、支架智能化协同白皮书
  • Linux操作系统之文件(二):重定向
  • Android 系统默认的Launcher3,Android 系统的导航栏(通常是屏幕底部)显示的 4 个快捷应用图标,如何替换这4个应用图标为客户想要的。
  • Fiddler中文版抓包工具在后端API调试与Mock中的巧用
  • Treap树
  • thinkphp8接管异常处理类
  • linux系统 weblogic10.3.6(jar) 下载及安装
  • 后端 Maven打包 JAR 文件、前端打包dist文件、通过后端服务访问前端页面、Nginx安装与部署
  • Josn模块的使用
  • MVC 架构设计模式
  • Docker 安装 Redis 哨兵模式
  • 【数据结构】C++的unordered_map/set模拟实现(开散列(哈希桶)作底层)
  • 机器人“触摸”水果成熟度突破:SwishFormer模型与DIGIT视触觉传感器在HelloRobot上的水果检测应用
  • TDSQL如何查出某一列中的逗号数量
  • 从 TCP/IP 协议栈角度深入分析网络文件系统 (NFS)
  • (1)手摸手-学习 Vue3 之 Vite 创建项目
  • grpc 和限流Sentinel
  • STC8G 8051内核单片机开发(GPIO)
  • 2025年6月微短剧备案分析:都市题材占四成,20-29集成主流体量
  • OS15.【Linux】gdb调试器的简单使用
  • 修改文件属主
  • 活体检测api集成方案-炫彩活体检测助力身份核验
  • 马斯克脑机接口(Neuralink)技术进展,已经实现瘫痪患者通过BCI控制电脑、玩视频游戏、学习编程,未来盲人也能恢复视力了
  • [极客时间]LangChain 实战课 -----|(10) 链(下):想学“育花”还是“插花”?用RouterChain确定客户意图
  • 预警:病毒 “黑吃黑”,GitHub 开源远控项目暗藏后门
  • 2024年INS SCI2区,强化搜索自适应大邻域搜索算法RSALNS+无人机扩展型协作多任务分配,深度解析+性能实测
  • 实现如何利用 Kafka 延时删除 用户邮箱的验证码(如何发送邮箱+源码) - 第一期
  • 前缀和算法详解