小程序相册授权后保存图片到相册

小程序相册授权和保存图片到相册

  //保存图片
  saveImgToAlbum: function () {
    const _this = this;
    console.log('has.this.data.tempFilePath', _this.data.tempFilePath);
    if (!!_this.data.tempFilePath) {
      tt.saveImageToPhotosAlbum({
        filePath: _this.data.tempFilePath,
        success(rres) {
          console.log(`tt.saveImageToPhotosAlbum.success`, rres);

          //成功提示
          tt.showToast({
            title: "图片结果保存成功",
            duration: 3000,
            icon: 'success',
            success(tres) {
              console.log(`${tres}`);
            },
            fail(tres) {
              console.log(`showToast调用失败`, tres);
            }
          });
        },
        complete(rres) {
          console.log(`tt.saveImageToPhotosAlbum.complete`, rres);
        },
        fail(rres) {
          console.log(`tt.saveImageToPhotosAlbum.fail`, rres);
        }
      });
    } else {
      console.error('this.data.tempFilePath.isEmpty', _this.data.tempFilePath);
    }
  },
  //相册授权
  saveImgAuth: function () {
    const _this = this;
    tt.getSetting({
      success: function (res) {
        console.log('tt.getSetting.success', res);

        console.log('is.scope.album', res.authSetting['scope.album']);
        if (res.authSetting['scope.album'] && res.authSetting['scope.album'] == true) {//已授权
          console.log('has.scope.album');

          console.log('start.saveImg');
          //保存图片
          _this.saveImgToAlbum();
        } else {//未授权
          console.error('non.scope.album');

          console.log('start.tt.authorize');
          //授权
          tt.authorize({
            scope: "scope.album",
            success(rres) {
              // 用户同意授权
              console.log('tt.authorize.success', rres);

              console.log('start.saveImg');
              //保存图片
              _this.saveImgToAlbum();
            },
            complete(rres) {
              console.log('tt.authorize.complete', rres);
            },
            fail(rres) {
              // 用户取消授权 或 取消过授权
              console.error('tt.authorize.fail', rres);

              tt.showModal({
                title: "系统提示",
                content: "打开相册权限才能保存图片哦!",
                success(mres) {
                  if (mres.confirm) {
                    // console.log("confirm, continued");
                    console.log('start.tt.openSetting');
                    // 取消过授权需要打开设置页面
                    tt.openSetting({
                      success: function (rrres) {
                        console.log('tt.openSetting.success', rrres);
                        if (rrres.authSetting['scope.album'] && rrres.authSetting['scope.album'] == true) {//已授权
                          console.log('start.saveImg');
                          //保存图片
                          _this.saveImgToAlbum();
                        }
                      },
                      complete: function (rrres) {
                        console.log('tt.openSetting.complete', rrres);
                      },
                      fail: function (rrres) {
                        console.log('tt.openSetting.fail', rrres);
                      },
                    });
                  } else if (mres.cancel) {
                    // console.log("cancel, cold");
                    //成功提示
                    tt.showToast({
                      title: "图片结果保存失败",
                      duration: 3000,
                      icon: 'fail',
                      success(tres) {
                        console.log(`${tres}`);
                      },
                      fail(tres) {
                        console.log(`showToast调用失败`, tres);
                      }
                    });
                  } else {
                    // what happend?
                  }
                },
                fail(mres) {
                  console.log(`showModal调用失败`);
                }
              });
            },
          });
        }
      },
      complete: function (res) {
        console.log('tt.getSetting.complete', res);
      },
      fail: function (res) {
        console.error('tt.getSetting.fail', res);
      },
    });
  },

本文地址:

https://www.cnblogs.com/stumpx/p/13183779.html

posted @ 2020-06-23 18:36  stumpx  阅读(652)  评论(0编辑  收藏  举报