nuxtAxios.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import Vue from "vue";
  2. import qs from "qs";
  3. export default function ({
  4. $axios,
  5. redirect,
  6. req,
  7. store,
  8. app,
  9. ...args
  10. }) {
  11. $axios.onRequest(config => {
  12. const contentType = config.headers["Content-Type"];
  13. const isUpload =
  14. contentType === "multipart/form-data" ||
  15. contentType === "application/json";
  16. if (!isUpload) {
  17. config.headers["Content-Type"] = "application/x-www-form-urlencoded";
  18. }
  19. console.log("nuxtAxios...........req.............req");
  20. config.baseURL = store.state.domainConfig.api;
  21. console.log("Before, making request to ", config.url, config.baseURL);
  22. // stringify post data
  23. if (config.method === "post" && !isUpload) {
  24. const data = config.data;
  25. console.log("stringify req data............");
  26. console.log(JSON.stringify(data));
  27. if (typeof data != "string") {
  28. let urlParams = [];
  29. for (const key in data) {
  30. if (data.hasOwnProperty(key)) {
  31. const element = encodeURIComponent(data[key]);
  32. urlParams.push(`${key}=${element}`);
  33. }
  34. }
  35. config.data = urlParams.join("&");
  36. }
  37. }
  38. console.log("stringify req env............",process.env.NODE_ENV);
  39. if(process.env.NODE_ENV=="development" && config.baseURL=="http://local.proginn.com")
  40. {
  41. config.baseURL="https://dev.test.proginn.com";
  42. config.headers.host="dev.test.proginn.com";
  43. }
  44. console.log("After, making request to ", config.url, config.baseURL);
  45. return config;
  46. });
  47. $axios.onResponse(res => {
  48. const data = res.data;
  49. let needLogin = false;
  50. let notShowError = false;
  51. if (res.config && res.config.data && typeof res.config.data === "string") {
  52. const queryData = res.config.data;
  53. // 将请求入参重新解析
  54. const arrQuery = queryData.split("&");
  55. // 遍历每个入参,看有没有needLogin参数,并且参数是true, 表示该接口如果服务器端返回未登录,则强制跳到登录页面
  56. for (let i = 0; i < arrQuery.length; i++) {
  57. const arr = arrQuery[i].split("=");
  58. //找到名称needLogin
  59. if ("needLogin" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  60. needLogin = true;
  61. break;
  62. }
  63. //找到名称 notShowError,如果为 true,则不做 $message.error() 展示
  64. if ("notShowError" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  65. notShowError = true;
  66. break;
  67. }
  68. }
  69. }
  70. if (data.status === 1 || data.filename || data.sign) {} else if (needLogin && data.status === -99) {
  71. if (app.$deviceType.app) {
  72. if (process.server) {
  73. redirect("proginn://login?backToPage=true");
  74. } else {
  75. location.href = "proginn://login?backToPage=true";
  76. }
  77. } else {
  78. if (process.server) {
  79. redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
  80. } else {
  81. location.href = store.state.domainConfig.siteUrl + "/?loginbox=show&next=" + encodeURIComponent(location.href);
  82. }
  83. }
  84. console.log(req && req.url);
  85. } else {
  86. console.log(JSON.stringify(data));
  87. if (!notShowError) {
  88. Vue.prototype.$message.closeAll();
  89. Vue.prototype.$message.error(data.info || data.error || "");
  90. }
  91. }
  92. return res;
  93. });
  94. $axios.onError(error => {
  95. console.log("err", error);
  96. });
  97. }