file.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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: (fileUrl) => {
  45. return request({
  46. url: '/infra/file/downloadByUrl?fileUrl='+fileUrl,
  47. method: 'GET',
  48. responseType: 'blob'
  49. });
  50. }
  51. };
  52. export default FileApi;