陈培均
发布于 2023-07-13 / 0 阅读 / 0 评论 / 0 点赞

<前端><微信小程序><相机>

微信小程序相机功能在正式版无法正常使用(体验版没有出现这个情况)

推测是正式版必须主动让用户授权相机,否则无法正常使用

 <camera v-if="isAuth" device-position="front" flash="off" style="width:100%;height:100%;" @error="error"></camera>
    export default {
    data() {
      return {
        isAuth: false,
        face: true,
        show: false,
      }
    },
    created() {
      this.getAuth()
    },
    methods: {
      open(currentType) {
        this.face = Number(currentType) === 1
        if (this.isAuth) {
          this.show = true
        } else {
          this.getAuth(true)
        }
      },
      close() {
        this.show = false
      },
      getAuth(show = false) {
        uni.getSetting({
          success: res => {
            if (res.authSetting['scope.camera']) {
              this.isAuth = true
              if (show) {
                this.$nextTick(() => {
                  this.show = true
                })
              }
            } else {
              wx.authorize({
                scope: 'scope.camera',
                success: () => { // 用户同意授权
                  this.isAuth = true
                  if (show) {
                    this.$nextTick(() => {
                      this.show = true
                    })
                  }
                },
                fail: () => { // 用户不同意授权
                  uni.showModal({
                    title: '提示',
                    content: `请先在设置页面打开“相机”使用权限`,
                    confirmText: '去设置',
                    cancelText: '再逛会',
                    confirmColor: '#DCEE19',
                    cancelColor: '#9C9EAA',
                    success: res => {
                      if (res.confirm) {
                        uni.openSetting({
                          success: res => {
                            if (res.authSetting['scope.camera']) {
                              this.$emit('settingSuccess')
                            }
                          }
                        })
                      }
                    }
                  })
                }
              })
            }
          }
        })
      },
      takePhoto() {
        uni.createCameraContext().takePhoto({
          quality: 'high',
          success: (res) => {
            this.close()
            this.$emit('shoot', res.tempImagePath)
          },
          fail: (res) => {
            console.log('res', res)
            // uni.showToast({ title: res.tempImagePath })
          }
        })
      },
      error(e) {}
    }
  }

必需调用这个方法 否则正式版无法正常授权


评论