nuxtAxios.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. config.baseURL = store.state.domainConfig.api;
  20. if (config.method === "post" && !isUpload) {
  21. const data = config.data;
  22. if (typeof data != "string") {
  23. let urlParams = [];
  24. for (const key in data) {
  25. if (data.hasOwnProperty(key)) {
  26. const element = encodeURIComponent(data[key]);
  27. urlParams.push(`${key}=${element}`);
  28. }
  29. }
  30. config.data = urlParams.join("&");
  31. }
  32. }
  33. if(process.env.NODE_ENV=="development" && config.baseURL=="http://local.proginn.com")
  34. {
  35. config.baseURL="https://dev.test.proginn.com";
  36. config.headers.host="dev.test.proginn.com";
  37. }
  38. return config;
  39. });
  40. $axios.onResponse(res => {
  41. const data = res.data;
  42. let needLogin = false;
  43. let notShowError = false;
  44. if (res.config && res.config.data && typeof res.config.data === "string") {
  45. const queryData = res.config.data;
  46. // 将请求入参重新解析
  47. const arrQuery = queryData.split("&");
  48. // 遍历每个入参,看有没有needLogin参数,并且参数是true, 表示该接口如果服务器端返回未登录,则强制跳到登录页面
  49. for (let i = 0; i < arrQuery.length; i++) {
  50. const arr = arrQuery[i].split("=");
  51. //找到名称needLogin
  52. if ("needLogin" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  53. needLogin = true;
  54. break;
  55. }
  56. //找到名称 notShowError,如果为 true,则不做 $message.error() 展示
  57. if ("notShowError" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  58. notShowError = true;
  59. break;
  60. }
  61. }
  62. }
  63. if (data.status === 1 || data.filename || data.sign) {} else if (needLogin && data.status === -99) {
  64. if (app.$deviceType.app) {
  65. if (process.server) {
  66. redirect("proginn://login?backToPage=true");
  67. } else {
  68. location.href = "proginn://login?backToPage=true";
  69. }
  70. } else {
  71. if (process.server) {
  72. redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
  73. } else {
  74. location.href = store.state.domainConfig.siteUrl + "/?loginbox=show&next=" + encodeURIComponent(location.href);
  75. }
  76. }
  77. } else {
  78. if(typeof(res.headers["content-type"]) && res.headers["content-type"]=="image/gif")
  79. {
  80. return ;
  81. }
  82. if (!notShowError) {
  83. Vue.prototype.$message.closeAll();
  84. Vue.prototype.$message.error(data.info || data.error || "");
  85. }
  86. }
  87. return res;
  88. });
  89. $axios.onError(error => {
  90. });
  91. }