nuxtAxios.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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("Before, making request to ", config.url, config.baseURL);
  20. console.log("nuxtAxios...........req.............req");
  21. config.baseURL = store.state.domainConfig.api;
  22. console.log("Before, making request to ", config.url, config.baseURL);
  23. // stringify post data
  24. if (config.method === "post" && !isUpload) {
  25. const data = config.data;
  26. console.log("stringify req data............");
  27. console.log(JSON.stringify(data));
  28. if (typeof data != "string") {
  29. let urlParams = [];
  30. for (const key in data) {
  31. if (data.hasOwnProperty(key)) {
  32. const element = encodeURIComponent(data[key]);
  33. urlParams.push(`${key}=${element}`);
  34. }
  35. }
  36. config.data = urlParams.join("&");
  37. }
  38. }
  39. console.log("After, making request to ", config.url, config.baseURL);
  40. return config;
  41. });
  42. $axios.onResponse(res => {
  43. const data = res.data;
  44. console.log("res:::::::::");
  45. console.log(app.$deviceType);
  46. // console.log(res);
  47. console.log(res.config.url);
  48. console.log(JSON.stringify(data));
  49. // req && req.url && needLogin(req.url)
  50. let needLogin = 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. }
  64. }
  65. if (data.status === 1 || data.filename || data.sign) {} else if (needLogin && data.status === -99) {
  66. if (app.$deviceType.app) {
  67. if (process.server) {
  68. redirect("proginn://login?backToPage=true");
  69. } else {
  70. location.href = "proginn://login?backToPage=true";
  71. }
  72. } else {
  73. if (process.server) {
  74. redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
  75. } else {
  76. location.href = store.state.domainConfig.siteUrl + "/?loginbox=show";
  77. }
  78. }
  79. console.log(req && req.url);
  80. } else {
  81. console.log(JSON.stringify(data));
  82. Vue.prototype.$message.closeAll();
  83. Vue.prototype.$message.error(data.info || "");
  84. }
  85. return res;
  86. });
  87. $axios.onError(error => {
  88. console.log("err", error);
  89. });
  90. }