nuxtAxios.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 proto = (referer && referer.indexOf('local.proginn.com') !==-1) ? "http://" : "https://"
  13. const baseUrl = /https?/.test(referer) ? referer : proto + referer;
  14. const url = config.url;
  15. // if (referer && !(/https?/.test(url))) {
  16. if (typeof window === 'undefined') {
  17. // server http
  18. config.url = baseUrl + url;
  19. config.baseURL = baseUrl;
  20. } else {
  21. // client http
  22. config.url = /https?/.test(url) ? `/${url.split('/').slice(3).join('/')}` : url;
  23. config.baseURL = '';
  24. }
  25. // stringify post data
  26. if (config.method === 'post' && !isUpload) {
  27. const data = config.data;
  28. console.log(data);
  29. if (typeof data != 'string') {
  30. // const cookie = config.headers.cookie || config.headers.common.cookie || '';
  31. let urlParams = [];
  32. for (const key in data) {
  33. if (data.hasOwnProperty(key)) {
  34. const element = encodeURIComponent(data[key]);
  35. urlParams.push(`${key}=${element}`);
  36. }
  37. }
  38. config.data = urlParams.join('&');
  39. }
  40. }
  41. console.log('After, making request to ', config.url, config.baseURL);
  42. return config;
  43. });
  44. $axios.onResponse((res) => {
  45. const data = res.data;
  46. /**
  47. * 设备判断
  48. */
  49. const getDeviceType = function() {
  50. const strCookie = process.server
  51. ? req.headers.cookie
  52. : document.cookie;
  53. //将多cookie切割为多个名/值对
  54. const arrCookie = strCookie.split("; ");
  55. let xAPP = "";
  56. //遍历cookie数组,处理每个cookie对
  57. for(let i=0; i<arrCookie.length; i++){
  58. const arr = arrCookie[i].split("=");
  59. //找到名称为x_app的cookie,并返回它的值
  60. if ("x_app" === arr[0]){
  61. xAPP = arr[1];
  62. break;
  63. }
  64. }
  65. if (xAPP.toLowerCase().indexOf('ios') !== -1) { //判断iPhone|iPad|iPod|iOS APP
  66. return 'ios';
  67. } else if (xAPP.toLowerCase().indexOf('android') !== -1) { //判断Android app
  68. return 'android';
  69. } else { // pc OR H5 todo 判断UA,精准识别是H5还是PC
  70. return 'pc';
  71. }
  72. }
  73. console.log('res:::::::::')
  74. // console.log(res);
  75. console.log(res.config)
  76. console.log(data)
  77. // req && req.url && needLogin(req.url)
  78. let needLogin = false
  79. if (res.config && res.config.data && typeof res.config.data === 'string') {
  80. const queryData = res.config.data
  81. //将多cookie切割为多个名/值对
  82. const arrQuery = queryData.split("&");
  83. //遍历cookie数组,处理每个cookie对
  84. for(let i=0; i<arrQuery.length; i++){
  85. const arr = arrQuery[i].split("=");
  86. //找到名称为x_app的cookie,并返回它的值
  87. if ("needLogin" === arr[0] && (arr[1] === 'true' || arr[1] === true)){
  88. needLogin = true;
  89. break;
  90. }
  91. }
  92. }
  93. if (data.status === 1 || data.filename || data.sign) {
  94. } else if (needLogin && data.status === -99) {
  95. const deviceType = getDeviceType();
  96. if (deviceType === 'ios' || deviceType === 'android') {
  97. if (process.server) {
  98. redirect('proginn://login')
  99. } else {
  100. location.href = 'proginn://login'
  101. }
  102. } else {
  103. if (process.server) {
  104. redirect('https://www.proginn.com/?loginbox=show')
  105. } else {
  106. location.href = 'https://www.proginn.com/?loginbox=show'
  107. }
  108. }
  109. console.log(req && req.url);
  110. // if (req && req.url && needLogin(req.url)) {
  111. // redirect('/?loginbox=show');
  112. // }
  113. // return {code: '401', message: '请登录!'};
  114. } else {
  115. // if (!Vue.hasMessage) {
  116. console.log(data)
  117. Vue.prototype.$message.closeAll()
  118. Vue.prototype.$message.error(data.info || '')
  119. // Vue.hasMessage = true
  120. // setTimeout(() => {
  121. // Vue.hasMessage = false
  122. // }, 3000)
  123. // }
  124. // return data;
  125. }
  126. return res;
  127. });
  128. $axios.onError((error) => {
  129. console.log('err', error);
  130. // const code = parseInt(error.response && error.response.status)
  131. // if (code === 400) {
  132. // redirect('/400')
  133. // }
  134. });
  135. }