file-uploader.js 809 B

12345678910111213141516171819202122232425262728293031
  1. import {wxToPromise} from "./wx";
  2. import config from "../model/config";
  3. import Http from "./http";
  4. class FileUploader extends Http {
  5. static async upload(filePath, key = 'file') {
  6. let res
  7. try {
  8. res = await wxToPromise('uploadFile', {
  9. url: `${config.API}/v1/file`,
  10. filePath: filePath,
  11. name: key,
  12. })
  13. } catch (e) {
  14. console.log(e)
  15. FileUploader._showError(-100)
  16. throw new Error(e.errMsg)
  17. }
  18. const serverData = JSON.parse(res.data)
  19. if (res.statusCode !== 201) {
  20. FileUploader._showError(serverData.code, serverData.msg)
  21. throw new Error(serverData.msg)
  22. }
  23. return serverData.data
  24. }
  25. }
  26. export default FileUploader