123456789101112131415161718192021222324252627282930313233343536373839 |
- import {wxToPromise} from "./wx";
- import config from "../model/config";
- import Http from "./http";
- import Route from "../model/route";
- import User from "../model/user";
- class FileUploader extends Http {
- static async upload(filePath, key = 'file',
- errorHandler = async (code, msg) => {
- if (code == 401) {
- await User.logout();
- Route.redirectLogin();
- }
- const title = Http._showError(code, msg)
- throw new Error(title)
- }) {
- let res
- try {
- res = await wxToPromise('uploadFile', {
- url: `${config.API}/v1/file`,
- filePath: filePath,
- name: key,
- })
- } catch (e) {
- console.log(e)
- await errorHandler(-100)
- }
- const serverData = JSON.parse(res.data)
- if (res.statusCode !== 201) {
- await errorHandler(serverData.code, serverData.msg)
- }
- return serverData.data
- }
- }
- export default FileUploader
|