小程序--授权封装

复制代码
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();
      }
    })
复制代码

 

posted @   梦蝶庄周  阅读(960)  评论(1编辑  收藏  举报
编辑推荐:
· .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语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示