nuxtAxios.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import Vue from 'vue';
  2. import qs from 'qs';
  3. export default function ({ $axios, redirect, req, ...args }) {
  4. $axios.onRequest(config => {
  5. const contentType = config.headers['Content-Type'];
  6. const isUpload = contentType === 'multipart/form-data' || contentType === 'application/json';
  7. if (!isUpload) {
  8. config.headers['Content-Type'] = 'application/x-www-form-urlencoded';
  9. }
  10. console.log('Before, making request to ', config.url, config.baseURL)
  11. const referer = config.headers.host || (config.headers.common && config.headers.common.referer);
  12. const baseUrl = /https?/.test(referer) ? referer : 'https://' + referer;
  13. const url = config.url;
  14. // if (referer && !(/https?/.test(url))) {
  15. if (typeof window === 'undefined') {
  16. // server http
  17. config.url = baseUrl + url;
  18. config.baseURL = baseUrl;
  19. } else {
  20. // client http
  21. config.url = /https?/.test(url) ? `/${url.split('/').slice(3).join('/')}` : url;
  22. config.baseURL = '';
  23. }
  24. // stringify post data
  25. if (config.method === 'post' && !isUpload) {
  26. const data = config.data;
  27. console.log(data)
  28. if (typeof data != "string") {
  29. // const cookie = config.headers.cookie || config.headers.common.cookie || '';
  30. let urlParams = [];
  31. for(const key in data) {
  32. if(data.hasOwnProperty(key)) {
  33. const element = encodeURIComponent(data[key])
  34. urlParams.push(`${key}=${element}`)
  35. }
  36. }
  37. config.data = urlParams.join('&');
  38. }
  39. }
  40. console.log('After, making request to ', config.url, config.baseURL)
  41. return config;
  42. })
  43. $axios.onResponse(res => {
  44. const data = res.data;
  45. // 不跳转到login的页面reg list
  46. const excludePath = ['/user/register', '/cert/type/:id', '/type/vip/', '/cert/', '/user/:id'];
  47. const includePath = ['/user/register', '/cert/type/:id', '/type/vip/', '/cert/', '/user/:id'];
  48. const needLogin = path => {
  49. let result = false;
  50. excludePath.forEach(reg => {
  51. reg = reg.replace(/:id/, '');
  52. if (RegExp(reg).test(path)) {
  53. result = false;
  54. }
  55. })
  56. return result;
  57. }
  58. // req && req.url && needLogin(req.url)
  59. if(data.status === 1 || data.filename) {
  60. } else if(data.status === -99) {
  61. console.log(req && req.url)
  62. // if (req && req.url && needLogin(req.url)) {
  63. // redirect('/?loginbox=show');
  64. // }
  65. // return {code: '401', message: '请登录!'};
  66. } else {
  67. Vue.prototype.$message.error(data.info);
  68. // return data;
  69. }
  70. return res;
  71. })
  72. $axios.onError(error => {
  73. console.log('err', error);
  74. // const code = parseInt(error.response && error.response.status)
  75. // if (code === 400) {
  76. // redirect('/400')
  77. // }
  78. })
  79. }