npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zegoweb/wx-upload

v1.0.4

Published

Meida web break-point upload for Javascript in wechat miniprogram

Downloads

13

Readme

媒资断点续传 - 微信小程序 SDK

1、 安装

# 方式一:npm/pnpm/yarn 安装
npm install @zegoweb/wx-upload -S
# 方式二:本地SDK包,接入使用

2、使用

// ES6
const token = "zego-token";
const apiPrefix = "zego-apiPrefix";
import { WXUpload } from "@zegoweb/wx-upload";
const uploader = new WXUpload(
  { token, apiPrefix },
  { chunkSize: 1024 * 1024 * 5 }
);

// UMD
const token = "zego-token";
const apiPrefix = "zego-apiPrefix";
const { WXUpload } = require("@zegoweb/wx-upload");
const uploader = new WXUpload(
  { token, apiPrefix },
  { chunkSize: 1024 * 1024 * 5 }
);

3、错误码

  • 协议层:(常见)

    | 错误码 | 错误信息 | | ------ | ----------------- | | 400 | 请求错误 | | 401 | 未授权,请登录 | | 403 | 拒绝访问 | | 404 | 请求地址出错 | | 408 | 请求超时 | | 500 | 服务器内部错误 | | 501 | 服务未实现 | | 502 | 网关错误 | | 503 | 服务不可用 | | 504 | 网关超时 | | 505 | HTTP 版本不受支持 |

  • 业务层:(常见)

    | 错误码 | 错误信息 | | ------ | ---------------- | | -1 | 网络异常 | | -2 | 网络超时 | | -3 | 请求失败 | | -100xx | 后端其它错误信息 |

4、API 说明

// npm 安装使用
import { WXUpload } from '@zegoweb/wx-upload';
// 初始化上传
const uploader = new WXUpload(uplodConfig[,uploadOptions]);
// 开始上传
uploader.startUpload(WXFile)
// 暂停上传
uploader.pauseUpload()
// 续传
uploader.resumeUpload()
// 取消
uploader.cancelUpload();
// 获取上传状态、进度条、错误
uploader.emitter.on("*", (type, info) => {
  if(type === 'data') {
    // 获取上传状态、进度条等信息
    console.log(info);
  }
  if(type === 'error') {
    // 获取错误信息
    console.log(info);
  }
})

相关参数说明:

初始化: new WXUpload(uplodConfig[,uploadOptions]);

  • uploadConfig:

    • 类型:Object
    • 说明:文件上传初始化参数

    | 参数 | 参数类型 | 是否必填 | 参数说明 | | --------- | -------- | -------- | ----------------------------------- | | token | string | 是 | 请求 Token | | apiPrefix | string | 是 | 上传后端 API 接口前缀 | | timeout | number | 否 | 上传请求超时时间,单位毫秒,默认:0 |

  • uploadOptions

    • 类型:Object

    • 说明:上传额外参数

    • 备注:默认(auto),将采取动态处理分片大小,处理规则如下:

      • <10M 单个分片大小为 5M;

      • >50M 且<200M 单个分片大小为 10M;

      • >200M 且<1G 单个分片大小为 50M;

      • >1G 且<2G 单个分片为 100M;

      • >2G 单个分片为 200M

    | 参数 | 参数类型 | 是否必填 | 参数说明 | | --------- | ---------------- | -------- | ------------------------------------------------------ | | chunkSize | number | ‘auto’ | 否 | 文件单个分片大小,单位 Byte,必须大于 5M,默认: ‘auto’ |

const token = "710524909276631040",
  apiPrefix = "http://mm-test.zegonetwork.com:30002/api/v1/storage/breakpoint",
  chunkSize = 1024 * 1024 * 10;
const uploader = new WXUpload({ token, apiPrefix }, { chunkSize });

上传文件:uploader.startUpload(File)

  • File:
    • 类型:File
    • 说明:选取文件(单个)

监听数据:uploader.emitter.on("*", (type, info) => {}

  • 监听 type:

    • 类型:enum

    • 说明:监听的错误类型

      | 枚举值 | 说明 | | ------ | -------------------- | | data | 上传过程中,正确类型 | | error | 上传过程中,报错类型 |

  • 监听 info:

    • 类型: Object

    • 说明:上传状态、进度条、错误等信息

    • 正确值:

      | 值 | 类型 | 说明 | | ---------- | ------ | ------------------------------------------------------------------------------------------------ | | statusCode | number | 状态值: 0:切片解析中;1: 解析完成 2:上传中;3:暂停上传中;4:暂停分片中;5:取消上传;6:上传完成; | | statusText | string | 状态值对应文案 | | progress | number | 进度百分比 | | spend | number | 耗时(秒) | | fileInfo | object | 上传完成文件信息 |

addFile(files: IWXUpload.WXFile[]) {
  const { fileList, token, apiPrefix } = this.data;
  // 先清空文件
  this.setData({ fileList: [] });
  for (let index = 0, len = files.length; index < len; index++) {
    // 初始化文件上传管理器,并监听
    const uploader = new WXUpload(
      { token, apiPrefix },
      { chunkSize: 1024 * 1024 * 5 }
    );
    const pos = fileList.length;
    fileList.push({
      ...files[index],
      index: pos,
      uploader,
      name: files[index].name,
      size: files[index].size,
      file: files[index],
      progress: 0,
      statusCode: 0,
      statusText: 0,
    });
    this.setData(
      {
        fileList: [...fileList],
      },
      () => {
        const { fileList } = this.data as any;
        uploader.emitter.on("*", (type, info) => {
          if (type === "data") {
            const { statusText, progress } = info as IWXUpload.EventData;
            [fileList[pos].statusText, fileList[pos].progress] = [
              statusText,
              progress,
            ];
            this.setData({
              fileList: [...fileList],
            });
            if (progress === 100) {
              console.log(`${fileList[index].name} 上传完成!`);
            }
          }
          if (type === "error") {
            console.error(info as IWXUpload.EventError);
          }
        });
      }
    );
    uploader.startUpload(files[index]);
  }
},

操作事件:uploader.pauseUpload()/uploader.resumeUpload()/uploader.cancelUpload()

  • 多个文件上传
  async handlePause(e: any) {
    const index: number = e.currentTarget.dataset.index;
    console.log('handlePause');
    const { fileList } = this.data;
    try {
      const res = await fileList[index].uploader.pauseUpload();
      const { statusText, progress } = res;
      [fileList[index].statusText, fileList[index].progress] = [
        statusText,
        progress,
      ];
    } catch (error) {
      console.log({ error });
    }
  },

  async handleResume(e: any) {
    const index: number = e.currentTarget.dataset.index;
    console.log('handleResume');
    const { fileList } = this.data;
    try {
      await fileList[index].uploader.resumeUpload();
    } catch (error) {
      console.log({ error });
    }
  },

  async handleCancel(e: any) {
    const index: number = e.currentTarget.dataset.index;
    console.log('handleCancel');
    const { fileList } = this.data;
    try {
      const res = await fileList[index].uploader.cancelUpload();
      const { statusText, progress } = res;
      [fileList[index].statusText, fileList[index].progress] = [
        statusText,
        progress,
      ];
    } catch (error) {
      console.log({ error });
    }
  }

案例

案例请参考源码的 example