nuxtAxios.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import Vue from "vue";
  2. import qs from "qs";
  3. export default function({ $axios, redirect, req, store, app, ...args }) {
  4. $axios.onRequest(config => {
  5. const contentType = config.headers["Content-Type"];
  6. const isUpload =
  7. contentType === "multipart/form-data" ||
  8. contentType === "application/json";
  9. if (!isUpload) {
  10. config.headers["Content-Type"] = "application/x-www-form-urlencoded";
  11. }
  12. console.log("Before, making request to ", config.url, config.baseURL);
  13. console.log("nuxtAxios...........req.............req");
  14. console.log(req);
  15. config.baseURL = store.state.domainConfig.api;
  16. // stringify post data
  17. if (config.method === "post" && !isUpload) {
  18. const data = config.data;
  19. console.log("stringify req data............");
  20. console.log(JSON.stringify(data));
  21. if (typeof data != "string") {
  22. let urlParams = [];
  23. for (const key in data) {
  24. if (data.hasOwnProperty(key)) {
  25. const element = encodeURIComponent(data[key]);
  26. urlParams.push(`${key}=${element}`);
  27. }
  28. }
  29. config.data = urlParams.join("&");
  30. }
  31. }
  32. console.log("After, making request to ", config.url, config.baseURL);
  33. return config;
  34. });
  35. $axios.onResponse(res => {
  36. const data = res.data;
  37. console.log("res:::::::::");
  38. console.log(app.$deviceType);
  39. // console.log(res);
  40. console.log(res.config.url);
  41. console.log(JSON.stringify(data));
  42. // req && req.url && needLogin(req.url)
  43. let needLogin = 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. }
  57. }
  58. if (data.status === 1 || data.filename || data.sign) {
  59. } else if (needLogin && data.status === -99) {
  60. if (app.$deviceType.app) {
  61. if (process.server) {
  62. redirect("proginn://login");
  63. } else {
  64. location.href = "proginn://login";
  65. }
  66. } else {
  67. if (process.server) {
  68. redirect(store.state.domainConfig.siteUrl + "/?loginbox=show");
  69. } else {
  70. location.href = store.state.domainConfig.siteUrl + "/?loginbox=show";
  71. }
  72. }
  73. console.log(req && req.url);
  74. } else {
  75. console.log(JSON.stringify(data));
  76. Vue.prototype.$message.closeAll();
  77. Vue.prototype.$message.error(data.info || "");
  78. }
  79. return res;
  80. });
  81. $axios.onError(error => {
  82. console.log("err", error);
  83. });
  84. }