nuxtAxios.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 = (req && req.headers && req.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. /**
  46. * 设备判断
  47. */
  48. const getDeviceType = function() {
  49. const strCookie = process.server
  50. ? req.headers.cookie
  51. : document.cookie;
  52. //将多cookie切割为多个名/值对
  53. const arrCookie = strCookie.split("; ");
  54. let xAPP = "";
  55. //遍历cookie数组,处理每个cookie对
  56. for(let i=0; i<arrCookie.length; i++){
  57. const arr = arrCookie[i].split("=");
  58. //找到名称为x_app的cookie,并返回它的值
  59. if ("x_app" === arr[0]){
  60. xAPP = arr[1];
  61. break;
  62. }
  63. }
  64. if (xAPP.toLowerCase().indexOf('ios') !== -1) { //判断iPhone|iPad|iPod|iOS APP
  65. return 'ios';
  66. } else if (xAPP.toLowerCase().indexOf('android') !== -1) { //判断Android app
  67. return 'android';
  68. } else { // pc OR H5 todo 判断UA,精准识别是H5还是PC
  69. return 'pc';
  70. }
  71. }
  72. console.log('res:::::::::')
  73. // console.log(res);
  74. console.log(res.config)
  75. console.log(data)
  76. // req && req.url && needLogin(req.url)
  77. let needLogin = false
  78. if (res.config && res.config.data && typeof res.config.data === 'string') {
  79. const queryData = res.config.data
  80. //将多cookie切割为多个名/值对
  81. const arrQuery = queryData.split("&");
  82. //遍历cookie数组,处理每个cookie对
  83. for(let i=0; i<arrQuery.length; i++){
  84. const arr = arrQuery[i].split("=");
  85. //找到名称为x_app的cookie,并返回它的值
  86. if ("needLogin" === arr[0] && (arr[1] === 'true' || arr[1] === true)){
  87. needLogin = true;
  88. break;
  89. }
  90. }
  91. }
  92. if (data.status === 1 || data.filename) {
  93. } else if (needLogin && data.status === -99) {
  94. const deviceType = getDeviceType();
  95. if (deviceType === 'ios' || deviceType === 'android') {
  96. if (process.server) {
  97. redirect('proginn://login')
  98. } else {
  99. location.href = 'proginn://login'
  100. }
  101. } else {
  102. if (process.server) {
  103. redirect('https://www.proginn.com/?loginbox=show')
  104. } else {
  105. location.href = 'https://www.proginn.com/?loginbox=show'
  106. }
  107. }
  108. console.log(req && req.url);
  109. // if (req && req.url && needLogin(req.url)) {
  110. // redirect('/?loginbox=show');
  111. // }
  112. // return {code: '401', message: '请登录!'};
  113. } else {
  114. Vue.prototype.$message.error(data.info);
  115. // return data;
  116. }
  117. return res;
  118. });
  119. $axios.onError((error) => {
  120. console.log('err', error);
  121. // const code = parseInt(error.response && error.response.status)
  122. // if (code === 400) {
  123. // redirect('/400')
  124. // }
  125. });
  126. }