request.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import axios from 'axios'
  2. // import store from '@/store'
  3. import qs from 'qs'
  4. const service = axios.create({
  5. timeout: 30000 // 请求超时时间
  6. })
  7. function isJSON (val) {
  8. if (typeof val !== 'string') {
  9. return false
  10. }
  11. try {
  12. const obj = JSON.parse(val)
  13. if (Object.prototype.toString.call(obj) === '[object Object]') {
  14. return true
  15. } else {
  16. return false
  17. }
  18. } catch (e) {
  19. return false
  20. }
  21. }
  22. service.interceptors.request.use(
  23. config => {
  24. // if (!config.headers.Authorization) {
  25. // config.headers.Authorization = token // 让每个请求携带自定义token 请根据实际情况自行修改
  26. // }
  27. if (config.headers['Content-Type'] === 'application/x-www-form-urlencoded' && config.data && Object.prototype.toString.call(config.data) === '[object Object]') {
  28. config.data = qs.stringify(config.data)
  29. }
  30. return config
  31. },
  32. err => {
  33. return Promise.reject(err)
  34. }
  35. )
  36. // respone拦截器 处理数据
  37. service.interceptors.response.use(
  38. response => {
  39. const { data } = response
  40. const resData = isJSON(data) ? JSON.parse(data) : data
  41. if (typeof resData === 'object') {
  42. return resData
  43. } else {
  44. // 针对返回 res 是二进制数据流
  45. return response
  46. }
  47. },
  48. error => {
  49. return Promise.reject(error)
  50. }
  51. )
  52. export function request (_param) {
  53. const {
  54. method = 'get',
  55. // `responseType` 表示服务器响应的数据类型,可以是 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'
  56. responseType = 'json', // 默认的
  57. headers = {},
  58. url = '',
  59. params,
  60. data,
  61. ...otherData
  62. } = _param
  63. if (!url) {
  64. return new Promise((resolve, reject) => {
  65. // eslint-disable-next-line prefer-promise-reject-errors
  66. reject('url is null')
  67. })
  68. }
  69. const _method = method.toLowerCase()
  70. console.info('_param', _param)
  71. if (_method === 'get') {
  72. return axios({
  73. responseType,
  74. url,
  75. headers,
  76. method,
  77. params: params || data || otherData
  78. })
  79. }
  80. if (_method === 'post') {
  81. // 处理
  82. // if (!headers.hasOwnProperty('Content-Type')) {
  83. // headers["Content-Type"] = "application/x-www-form-urlencoded"
  84. // }
  85. if (params && data) {
  86. return axios({
  87. responseType,
  88. url,
  89. headers,
  90. method,
  91. params,
  92. data
  93. })
  94. } else {
  95. const { start, limit, ...resetData } = otherData
  96. return axios({
  97. responseType,
  98. url,
  99. headers,
  100. method,
  101. params: params || { start, limit },
  102. data: data || resetData
  103. })
  104. }
  105. }
  106. }
  107. export default service