upload.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import {getTokenHeader} from '../request/header'
  2. import {getObjValue} from "js-fast-way";
  3. import {getAppApiUrl} from "@/config/envApi";
  4. export const getUploadApi = () => {
  5. let url = '';
  6. //#ifdef H5
  7. url = '/api/';
  8. //#endif
  9. //#ifdef APP-PLUS
  10. url = getAppApiUrl()
  11. //#endif
  12. return url
  13. }
  14. export const uploadApi = async (file, form= {}) => {
  15. const url = getUploadApi() + 'blade-resource/oss/endpoint/upload-file';
  16. return uploadFileApi(url, file, form)
  17. }
  18. export const uploadApi2 = async (file, form= {}) => {
  19. const url = getUploadApi() + 'blade-resource/oss/endpoint/put-file';
  20. return uploadFileApi(url, file, form)
  21. }
  22. export const uploadFileApi = async (url, file, form= {}) => {
  23. return new Promise((resolve) => {
  24. uni.uploadFile({
  25. url: url,
  26. name: 'file',
  27. formData: form,
  28. header: getTokenHeader(),
  29. filePath: file,
  30. success:(res) => {
  31. const {code, msg, data} = JSON.parse(res?.data)
  32. if (code === 200) {
  33. resolve({error: false, msg: msg, data: getObjValue(data)})
  34. } else {
  35. resolve({error: true, msg: msg, data: {}})
  36. }
  37. },
  38. fail:()=> {
  39. resolve({error: true, msg: '上传失败', data: {}})
  40. }
  41. });
  42. })
  43. }