upload.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { getToken } from '@/utils/auth'
  2. import {
  3. store
  4. } from '@/store/index.js'
  5. // import baseURL from '@/api/base/path.js'
  6. function upload({url, data, method="POST"}) {
  7. let token = 'Bearer ' + getToken();
  8. let header = {
  9. Authorization: token
  10. }
  11. console.log(data)
  12. return new Promise((resolve, reject)=>{
  13. const urlList = uni.getStorageSync('baseUrl')
  14. uni.uploadFile({
  15. url: JSON.parse(urlList).baseUrl + url,
  16. method,
  17. header,
  18. timeout: 600000,
  19. filePath:data.filePath,
  20. name: 'file',
  21. sslVerify: false,
  22. success: (res) => {
  23. if (res.statusCode === 200) {
  24. //请求成功
  25. resolve(res.data);
  26. } else if (res.statusCode === 401) {
  27. uni.showToast({
  28. icon: 'none',
  29. title: "未登录或登录状态已超时",
  30. duration: 1500
  31. });
  32. } else if (res.statusCode === 405) {
  33. uni.showToast({
  34. icon: 'none',
  35. title: "请求方法错误",
  36. duration: 1500
  37. });
  38. } else {
  39. uni.showToast({
  40. icon: 'none',
  41. title: "请求错误:" + res.statusCode,
  42. duration: 1500
  43. });
  44. }
  45. },
  46. fail: (err) => {
  47. uni.showToast({
  48. icon: 'none',
  49. title: err.errMsg,
  50. duration: 1500
  51. });
  52. reject(err);
  53. }
  54. })
  55. })
  56. }
  57. export default {upload}