zoukankan      html  css  js  c++  java
  • 小程序--授权封装

    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();
          }
        })
  • 相关阅读:
    maven之私服搭建
    maven之自定义archetype
    maven之自定义插件
    任务调度之 Elastic Job
    雪花算法原理解析
    基于 zxing 的二维码生成、解析
    spring-cloud-oauth2 认证授权
    spring security 自定义短信验证登录
    spring security session管理
    JDK1.8之HashMap实现原理
  • 原文地址:https://www.cnblogs.com/mengdiezhuangzhou/p/9707369.html
Copyright © 2011-2022 走看看