• 小程序--授权封装


    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();
          }
        })
  • 相关阅读:
    JAVA CookieUtil
    Maven打包时提示No runnable methods的解决方案
    Spring MVC中Junit测试简单讲解
    C# 连接MongoDB,含用户验证
    Spring中同一个service中方法相互调用事务不生效问题解决方案
    004. vue html模板字符串转为ast(js对象)
    03事件模型-发布订阅模式
    000 学习vue源码里面一些处理技巧
    02 响应式原理
    my-ts-axios
  • 原文地址:https://www.cnblogs.com/mengdiezhuangzhou/p/9707369.html
Copyright © 2020-2023  润新知