index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { service } from './service'
  2. import { config } from './config'
  3. const { default_headers } = config
  4. const request = (option) => {
  5. const { url, method, params, data, headersType, responseType, ...config } = option
  6. return service({
  7. url: url,
  8. method,
  9. params,
  10. data,
  11. ...config,
  12. responseType: responseType,
  13. headers: {
  14. 'If-None-Match': false,
  15. 'Cache-Control': 'no-store', // 全局设置不使用缓存
  16. 'Content-Type': headersType || default_headers
  17. }
  18. })
  19. }
  20. export default {
  21. get: async (option) => {
  22. const res = await request({ method: 'GET', ...option })
  23. return res.data
  24. },
  25. post: async (option) => {
  26. const res = await request({ method: 'POST', ...option })
  27. const result = res ? res.data : {}
  28. return result
  29. },
  30. postOriginal: async (option) => {
  31. const res = await request({ method: 'POST', ...option })
  32. return res
  33. },
  34. delete: async (option) => {
  35. const res = await request({ method: 'DELETE', ...option })
  36. return res.data
  37. },
  38. put: async (option) => {
  39. const res = await request({ method: 'PUT', ...option })
  40. return res.data
  41. },
  42. download: async (option) => {
  43. const res = await request({ method: 'GET', responseType: 'blob', ...option })
  44. return res
  45. },
  46. upload: async (option) => {
  47. option.headersType = 'multipart/form-data'
  48. const res = await request({ method: 'POST', ...option })
  49. return res
  50. }
  51. }