Technology Sharing

Vue encapsulates request response for axios

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

1. Reasons

For example, in some business logic, for example, you need to display a loading effect before making a request, or determine whether the identity information (token) has expired when logging in, and then give corresponding prompt information based on the code returned by the server. Some logic before and after the request can be implemented by re-encapsulating the axios request.

2. Specific packaging

This is a form of axios encapsulation. There are many forms, which should be determined according to specific business needs. There are specific comments in the specific code. No more words, just go straight to the specific code for reference!

  1. // axios的封装
  2. import axios, { HttpStatusCode } from "axios";
  3. import { useRouter } from "vue-router";
  4. // 生产环境
  5. const baseURLProd = "https://mall.quanrui.cc/api/v1/backend/";
  6. // 测试环境
  7. const baseURLDev = "http://139.9.197.13:8088/api/v1/backend/";
  8. const baseURL = baseURLDev; //更改baseurl
  9. // 使用路由
  10. const router = useRouter();
  11. // 设置请求头
  12. axios.defaults.headers.post["Content-Type"] =
  13. "application/x-www-form-urlencoded;charset=UTF-8";
  14. // 创建axios示实例
  15. let instance = axios.create({
  16. baseURL: baseURL, //设置baseurl
  17. timeout: 5000, //超时时间
  18. });
  19. // 跳转到登录页面,如果没有登录,或者登录信息过期的话
  20. // 携带当前页面路由,以期在登录页面完成登录后返回当前页面
  21. const toLogin = () => {
  22. router.replace({
  23. path: "/login",
  24. query: {
  25. redirect: router.currentRoute.fullPath,
  26. },
  27. });
  28. };
  29. // 提示信息,Toast这个是第三方组件,根据使用的UI组件库不同进行更换
  30. const tip = msg => {
  31. Toast({
  32. message: msg,
  33. duration: 1000,
  34. forbidClick: true
  35. });
  36. }
  37. // 请求拦截器
  38. // (主要是在请求的时候携带请求token,以协助后端进行判断身份信息是否过期等),或者还可以在此增加业务操作,比如请求之前展示loading效果,具体可以拿个第三方UI库的一个效果过来使用
  39. axios.interceptors.request.use(
  40. config => {
  41. // 每次发送请求之前判断是否存在token,如果存在,则统一在http请求的header都加上token,不用每次请求都手动添加了
  42. // 即使本地存在token,也有可能token是过期的,所以在响应拦截器中要对返回状态进行判断
  43. const token = store.state.token;
  44. token && (config.headers.Authorization = token);
  45. return config;
  46. },
  47. error => {
  48. return Promise.error(error);
  49. }
  50. );
  51. // 响应拦截器(主要对code进行判断,提示用户进行操作)
  52. axios.interceptors.response.use(
  53. response => {
  54. if (response.status === 200) {
  55. return Promise.resolve(response);
  56. } else {
  57. return Promise.reject(response);
  58. }
  59. },
  60. // 服务器状态码不是200的情况
  61. error => {
  62. if (error.response.status) {
  63. switch (error.response.status) {
  64. // 401: 未登录
  65. // 未登录则跳转登录页面,并携带当前页面的路径
  66. // 在登录成功后返回当前页面,这一步需要在登录页操作。
  67. case 401:
  68. tip("未登录,请先登录");
  69. setTimeout(()=> {
  70. toLogin();
  71. },1000)
  72. break;
  73. // 403 token过期
  74. // 登录过期对用户进行提示
  75. // 清除本地token和清空vuex中token对象
  76. // 跳转登录页面
  77. case 403:
  78. tip("登录过期,请重新登录");
  79. // 清除token
  80. localStorage.removeItem("token"); //如果存在了浏览器的localStorage
  81. // store.commit("loginSuccess", null); //如果存在store里的登录状态,获取其它相关信息
  82. // 跳转登录页面,并将要浏览的页面fullPath传过去,登录成功后跳转需要访问的页面
  83. setTimeout(() => {
  84. toLogin();
  85. }, 1000);
  86. break;
  87. // 404请求不存在
  88. case 404:
  89. tip("网络请求不存在");
  90. break;
  91. // 其他错误,直接抛出错误提示
  92. default:
  93. tip(error.response.data.message);
  94. break;
  95. }
  96. return Promise.reject(error.response);
  97. }
  98. }
  99. );
  100. //get方法
  101. export function get(url, params){
  102. return new Promise((resolve, reject) =>{
  103. axios.get(url, {
  104. params: params
  105. })
  106. .then(res => {
  107. resolve(res.data);
  108. })
  109. .catch(err => {
  110. reject(err.data)
  111. })
  112. });
  113. }
  114. //post方法
  115. export function post(url, params) {
  116. return new Promise((resolve, reject) => {
  117. axios.post(url, JSON.stringify(params))
  118. .then(res => {
  119. resolve(res.data);
  120. })
  121. .catch(err => {
  122. reject(err.data)
  123. })
  124. });
  125. }
  126. // put方法
  127. export function put(url, params){
  128. return new Promise((resolve, reject) =>{
  129. axios.put(url, {
  130. params: params
  131. })
  132. .then(res => {
  133. resolve(res.data);
  134. })
  135. .catch(err => {
  136. reject(err.data)
  137. })
  138. });
  139. }
  140. // delete方法
  141. export function del(url, params){
  142. return new Promise((resolve, reject) =>{
  143. axios.delete(url, {
  144. params: params
  145. })
  146. .then(res => {
  147. resolve(res.data);
  148. })
  149. .catch(err => {
  150. reject(err.data)
  151. })
  152. });
  153. }
  154. // 导出实例
  155. export default instance;

3. Centralized management of requests

Create a folder to store the interface requests. When you use it, you only need to export the request method and manage it in a unified way. Otherwise, it will be a bit ugly if all the codes are piled on the page!

First, export the specific request method from our encapsulated axios

The following example:

4. Use on the page

First export the specific method of the request interface

import { pwlogin } from "../api/login";

use:

  1. getloginres() {
  2. pwlogin(data)
  3. .then((res) => {
  4. //数据处理
  5. })
  6. .catch((err) => console.log(err));
  7. }

That’s all, please feel free to point out any errors!