• 小程序通用权限接口获取信息方法


    使用方法:
    函数参数interfaceName指的是小程序的接口名称,如wx.getLocation,interfaceName指的就是getLocation,当获取权限成功时通过Promise resolve(res)返回,res为对应接口的返回值(获取值可能存在失败情况,需要自己判断),而无权获取信息时通过Promise reject(r)返回,可以做后续页面显示。

    function getInterfaceInfo(interfaceName) {
      const authorizationMap = {
        'getUserInfo': 'scope.userInfo',
        'getLocation': 'scope.userLocation',
        'chooseLocation': 'scope.userLocation',
        'chooseAddress': 'scope.address',
        'chooseInvoiceTitle': 'scope.invoiceTitle',
        'chooseInvoice': 'scope.invoice',
        'getWeRunData': 'scope.werun',
        'startRecord': 'scope.record',
        'saveImageToPhotosAlbum': 'scope.writePhotosAlbum',
        'saveVideoToPhotosAlbum': 'scope.writePhotosAlbum',
        'camera': 'scope.camera'
      }
    
      return new Promise((resolve, reject) => {
        const scopeName = authorizationMap[interfaceName]
        uni.getSetting({
          success: (res) => {
            console.log(`get user ${scopeName} setting:`, res)
            if (scopeName in res.authSetting) {
              // 判断用户是否已经有权限
              if (res.authSetting[scopeName]) {
                console.log('authorize result:', res)
                uni[interfaceName]({
                  success: (res) => {
                    console.log('get user setting data success:', res)
                    resolve(res)
                  },
                  fail: (r) => {
                    console.log('get user setting data fail:', res)
                    resolve(r)
                  }
                })
              } else {
                reject(res)
              }
            } else {
              uni.authorize({
                scope: scopeName,
                success: (res) => {
                  console.log('authorize result:', res)
                  uni[interfaceName]({
                    success: (res) => {
                      console.log('get user setting data success:', res)
                      resolve(res)
                    },
                    fail: (r) => {
                      console.log('get user setting data fail:', res)
                      resolve(r)
                    }
                  })
                },
                fail: (r) => {
                  console.log('fail:', r)
                  reject(r)
                }
              })
            }
          },
          fail: (r) => {
            reject(r)
          }
        })
      })
    }
    
  • 相关阅读:
    《剑指offer》第三十五题(复杂链表的复制)
    《剑指offer》第三十四题(二叉树中和为某一值的路径)
    分支限界法
    回溯法小实例
    BUFSIZ解析
    ofstream和ifstream详细用法
    回溯法——最大团问题(Maximum Clique Problem, MCP)
    位运算应用之二——大小写转换
    回溯法——n后问题
    回溯法——批处理作业调度
  • 原文地址:https://www.cnblogs.com/li1234yun/p/11042554.html
Copyright © 2020-2023  润新知