微信小程序开发

小程序音视频等API调用的简单封装

2020-02-04  本文已影响0人  郁南

本文主要在Taro中使用。
在小程序中引入异步

// 1、下载  yarn add @tarojs/async-await
// 2、引入 app.tsx
import "@tarojs/async-await";

1、API封装

// eslint-disable-next-line jsx-closing-bracket-location
const WxActions = {
  async fnSaveVideo(src, content = "要保存视频到本地相册吗") {
    await this.fnWxSetting("scope.writePhotosAlbum");
    await this.fnShowModal(content, "提示");
    const res = await this.fnDownFile(src);
    this.fnSaveFile(res);
  },
  /** 保存文件 */
  fnSaveFile(res) {
    const that = this;
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "加载中",
      });
      wx.saveVideoToPhotosAlbum({
        filePath: res.tempFilePath,
        success(o) {
          console.log(o.errMsg);
          wx.hideLoading();
          that.fnShowToast("保存视频成功", "none", 1500);
          resolve();
        },
        fail(err) {
          wx.hideLoading();
          console.log(err);
          that.fnShowToast("保存视频失败", "none", 3000);
          reject();
        },
      });
    });
  },
  /** 下载文件 */
  fnDownFile(src) {
    const that = this;
    return new Promise((resolve, reject) => {
      wx.showLoading({
        title: "加载中",
      });
      wx.downloadFile({
        url: src,
        success(res) {
          console.log(res);
          // 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
          if (res.statusCode === 200) {
            wx.hideLoading();
            resolve(res);
          } else {
            wx.hideLoading();
            that.fnShowToast("下载视频失败", "none", 3000);
            reject();
          }
        },
        fail(err) {
          wx.hideLoading();
          console.log(err);
          that.fnShowToast("下载视频失败", "none", 3000);
          reject();
        },
      });
    });
  },
  /** 授权检测 */
  fnWxSetting(scope) {
    const that = this;
    return new Promise((resolve, reject) => {
      wx.getSetting({
        success(res) {
          console.log(res);
          if (!res.authSetting[scope]) {
            wx.authorize({
              scope: scope,
              success() {
                that.fnShowToast("授权成功", "none", 1500);
                that.fnWxSetting(scope);
              },
              fail(err) {
                console.log(err);
                that.fnShowToast("授权失败", "none", 1500);
                reject();
              },
            });
          } else {
            resolve();
          }
        },
      });
    });
  },
  /** toast提示 */
  fnShowToast(title = "成功", icon = "success", duration = 2000) {
    wx.showToast({
      title,
      icon,
      duration,
    });
  },
  /** dialog弹窗 */
  fnShowModal(content, title = "提示") {
    const that = this;
    return new Promise((resolve, reject) => {
      wx.showModal({
        title,
        content,
        success(res) {
          if (res.confirm) {
            console.log("用户点击确定");
            resolve();
          } else if (res.cancel) {
            console.log("用户点击取消");
            that.fnShowToast("取消", "none", 1500);
            reject();
          }
        },
      });
    });
  },
};
export default WxActions;

2、使用

// 1、引入WxActions
// 2、调用
  /** 保存视频到本地 */
   saveVideo() {
    console.log(this.state.videoSrc);
    WxActions.fnSaveVideo(this.state.videoSrc);
  }
上一篇 下一篇

猜你喜欢

热点阅读