file.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import { baseUrl, apiPath } from '@/sheep/config';
  2. import request from '@/sheep/request';
  3. const FileApi = {
  4. // 上传文件
  5. uploadFile: (file) => {
  6. // TODO 非繁人:访问令牌的接入;
  7. const token = uni.getStorageSync('token');
  8. uni.showLoading({
  9. title: '上传中',
  10. });
  11. return new Promise((resolve, reject) => {
  12. uni.uploadFile({
  13. url: baseUrl + apiPath + '/infra/file/upload',
  14. filePath: file,
  15. name: 'file',
  16. header: {
  17. // Accept: 'text/json',
  18. Accept : '*/*',
  19. 'tenant-id' :'1',
  20. // Authorization: 'Bearer test247',
  21. },
  22. success: (uploadFileRes) => {
  23. let result = JSON.parse(uploadFileRes.data);
  24. if (result.error === 1) {
  25. uni.showToast({
  26. icon: 'none',
  27. title: result.msg,
  28. });
  29. } else {
  30. return resolve(result);
  31. }
  32. },
  33. fail: (error) => {
  34. console.log('上传失败:', error);
  35. return resolve(false);
  36. },
  37. complete: () => {
  38. uni.hideLoading();
  39. },
  40. });
  41. });
  42. },
  43. // 通过图片url获得文件流
  44. getFileIO: async (fileUrl) => {
  45. const response = await fetch(import.meta.env.SHOPRO_BASE_URL+'/app-api/infra/file/downloadByUrl?fileUrl='+fileUrl, {
  46. method: 'GET',
  47. })
  48. if (!response.ok) {
  49. throw new Error('网络响应不是OK状态');
  50. }
  51. const blob = await response.blob();
  52. return blob;
  53. },
  54. };
  55. export default FileApi;