upload.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import request from '@/common/request.js'
  2. import config from '../config.js'
  3. import {
  4. getToken
  5. } from '../common/auth.js'
  6. /**
  7. * 单一文件上传接口
  8. * @file 文件地址
  9. * @url 上传路径
  10. * @formData 上传数据
  11. */
  12. export function UploadOne(file, formData = {}) {
  13. let header = {}
  14. if (getToken()) {
  15. header['Authorization'] = 'Bearer ' + getToken()
  16. }
  17. return new Promise((resolve, reject) => {
  18. try {
  19. uni.uploadFile({
  20. url: '/api/file/upload',
  21. filePath: file,
  22. name: 'file',
  23. formData: formData,
  24. header: header,
  25. success: (res) => {
  26. let r = JSON.parse(res.data)
  27. resolve(r)
  28. },
  29. fail: (err) => {
  30. uni.showToast({
  31. title: '服务器休息中,请稍后再试',
  32. icon: "none",
  33. position: "center",
  34. duration: 3000
  35. });
  36. },
  37. complete: (e) => {
  38. }
  39. })
  40. } catch {
  41. uni.showToast({
  42. title: '网速不好哦!在试试',
  43. icon: "none",
  44. position: "center",
  45. duration: 3000
  46. })
  47. uni.hideLoading()
  48. }
  49. })
  50. }
  51. export function UploadSomeFile(file, formData = {}) {
  52. let header = {}
  53. if (getToken()) {
  54. header['Authorization'] = 'Bearer ' + getToken()
  55. }
  56. return new Promise((resolve, reject) => {
  57. try {
  58. uni.uploadFile({
  59. url: '/api/file/uploadForm',
  60. // filePath: file,
  61. // name: 'file',
  62. files:file,
  63. formData: formData,
  64. header: header,
  65. success: (res) => {
  66. let r = JSON.parse(res.data)
  67. resolve(r)
  68. },
  69. fail: (err) => {
  70. uni.showToast({
  71. title: '服务器休息中,请稍后再试',
  72. icon: "none",
  73. position: "center",
  74. duration: 3000
  75. });
  76. },
  77. complete: (e) => {
  78. }
  79. })
  80. } catch {
  81. uni.showToast({
  82. title: '网速不好哦!在试试',
  83. icon: "none",
  84. position: "center",
  85. duration: 3000
  86. })
  87. uni.hideLoading()
  88. }
  89. })
  90. }
  91. export function UploadSome(data) {
  92. return request({
  93. url: '/api/file/uploadMul',
  94. method: 'post',
  95. data: data
  96. })
  97. }