小程序--授权封装
function PromiseAuth(scope){ return new Promise(function (resolve, reject) { if (wx.getSetting) { wx.getSetting({ success: function (res) { if (res.authSetting[scope]) { resolve(true); } else { confirm({ content: `请授权,才能正常使用功能!`, title: '温馨提示', options: [ {}, { callback: _ => { openSetting() } } ] }) } }, fail: function () { resolve(false); } }) } else { wx.showModal({ showCancel: false, title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。', confirmColor: '#20b976' }); reject(false); } }) } function confirm({ content = '', title = '', options = [{}, {}] }) { return new Promise((resolve, reject) => { wx.showModal({ title, content, confirmText: options[1].label || '确定', confirmColor: '#21c0ae', success: function (res) { if (res.confirm) { resolve(res) if (options[1].callback) { options[1].callback() } } else if (res.cancel) { reject(res) if (options[0].callback) { options[0].callback() } } } }) }) } function openSetting(scope, opts = {}) { return new Promise((resolve, reject) => { wx.openSetting({ success({ authSetting }) { if (scope) { resolve(authSetting[`scope.${scope}`]) opts.success && opts.success(authSetting[`scope.${scope}`]) } else { resolve(authSetting) opts.success && opts.successopts.success(authSetting) } }, fail(err) { reject(err) console.log('89080') console.log(err) opts.fail && opts.fail(err) } }) }) }
function getLocation(callback) { wx.getLocation({ type: 'gcj02', success: function (res) { callback(null, res || {}); }, fail: function (res) { // GPSNotFound(); callback('fail', {}); } }) }
module.exports ={
PromiseAuth, getLocation
}
const wxUtil = require('../../utils/wxUtil.js') var _this = this; wxUtil.getLocation(function (code, val) { if (code) { wxUtil.PromiseAuth('scope.userLocation').then((res) => { if (res) { wxUtil.getLocation(function (code, val) { if (code) { app.globalData.urlData.latitude = val.latitude; app.globalData.urlData.longitude = val.longitude; _this.getData(); } else { wx.showModal({ showCancel: false, title: '提示', content: '授权失败!', confirmColor: '#20b976' }); } }) } }); } else { app.globalData.urlData.latitude = val.latitude; app.globalData.urlData.longitude = val.longitude; _this.getData(); } })
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构