小程序调用图片,视频,相机功能

发布时间 2023-04-19 15:49:29作者: shuihanxiao
//调用图片和相机
 choosePic() {
    console.log("table")
    var _this = this
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数,默认9
      sizeType: ['original', 'compressed'], // original 原图,compressed 压缩图,默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图,camera 使用相机,默认二者都有
      success: function (res) {
        // success
        console.log(res)
        _this.setData({
          src: res.tempFilePaths
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
//调用图片视频
  onChoosePic() {  
    console.log("onDoor")
    wx.getSetting({
      success: (res) => {
        let authStatus = res.authSetting['scope.album'] && res.authSetting['scope.camera'];
        // 如果没有授权相册
        if (!authStatus) {
          //向用户发起授权请求
          wx.authorize({
            scope: "scope.camera",
            success: () => {
              wx.showActionSheet({
                title: '选择上传类型',
                itemList: ['图片', '视频'],
                success: res => {
                  console.log(res);
                  if (res.tapIndex == 0) {
                    this.chooseImages();
                  } else {
                    this.chooseVideo();
                  }
                }
              });
            },
            fail: () => {
              wx.showModal({
                title: '授权失败',
                content: '需要从您的相机或相册获取图片,请在设置界面打开相关权限',
                success: (res) => {
                  if (res.confirm) {
                    wx.openSetting()
                  }
                }
              })
            }
          })
        } else {
          wx.showActionSheet({
            title: '选择上传类型',
            itemList: ['图片', '视频'],
            success: res => {
              console.log(res);
              if (res.tapIndex == 0) {
                this.chooseImages();
              } else {
                this.chooseVideo();
              }
            }
          });
        }
      }
    })
  },