nuxtAxios.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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("After, making request to ", config.url, config.baseURL);
  39. return config;
  40. });
  41. $axios.onResponse(res => {
  42. const data = res.data;
  43. let needLogin = false;
  44. let notShowError = false;
  45. if (res.config && res.config.data && typeof res.config.data === "string") {
  46. const queryData = res.config.data;
  47. // 将请求入参重新解析
  48. const arrQuery = queryData.split("&");
  49. // 遍历每个入参,看有没有needLogin参数,并且参数是true, 表示该接口如果服务器端返回未登录,则强制跳到登录页面
  50. for (let i = 0; i < arrQuery.length; i++) {
  51. const arr = arrQuery[i].split("=");
  52. //找到名称needLogin
  53. if ("needLogin" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  54. needLogin = true;
  55. break;
  56. }
  57. //找到名称 notShowError,如果为 true,则不做 $message.error() 展示
  58. if ("notShowError" === arr[0] && (arr[1] === "true" || arr[1] === true)) {
  59. notShowError = true;
  60. break;
  61. }
  62. }
  63. }
  64. if (data.status === 1 || data.filename || data.sign) {} else if (needLogin && data.status === -99) {
  65. if (app.$deviceType.app) {
  66. if (process.server) {
  67. redirect("proginn://login?backToPage=true");
  68. } else {
  69. location.href = "proginn://login?backToPage=true";
  70. }
  71. } else {
  72. if (process.server) {
  73. redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
  74. } else {
  75. location.href = store.state.domainConfig.siteUrl + "/?loginbox=show&next=" + encodeURIComponent(location.href);
  76. }
  77. }
  78. console.log(req && req.url);
  79. } else {
  80. console.log(JSON.stringify(data));
  81. if (!notShowError) {
  82. Vue.prototype.$message.closeAll();
  83. Vue.prototype.$message.error(data.info || data.error || "");
  84. }
  85. }
  86. return res;
  87. });
  88. $axios.onError(error => {
  89. console.log("err", error);
  90. });
  91. }