当前位置:   article > 正文

axios全局拦截+请求响应处理+路由拦截_export function updatetask(data) { return request(

export function updatetask(data) { return request({ baseurl: defaultsettings

1:下载依赖axios

npm install axios -s

2:在utils文件夹目录中创建文件request.js

3: 打开request文件引入axios和Message组件

  1. import axios from 'axios';
  2. import store from '@/store';
  3. import { getToken } from '@/utils/auth';
  4. import { Message } from 'element-ui';

4:创建axios实例

  1. const service = axios.create({
  2. baseURL: 'http://www.qingmu.com/api', //自行修改
  3. withCredentials: true,
  4. timeout: 5000
  5. });

5:请求拦截器,里面逻辑自行处理,我是每次请求接口,请求头携带token用户信息

  1. service.interceptors.request.use(
  2. config => {
  3. config.headers['token'] = getToken() || 0;
  4. return config;
  5. },
  6. error => {
  7. // 处理请求错误
  8. console.log(error); // for debug
  9. return Promise.reject(error);
  10. }
  11. );

6:详情拦截器以及服务器异常code处理

  1. service.interceptors.response.use(
  2. response => {
  3. const res = response.data;
  4. // 未设置状态码则默认成功状态
  5. const code = res.errCode || 0;
  6. // if the custom code is not 20000, it is judged as an error.
  7. if (code !== 0) {
  8. if (code === 3001 || code === 3002) {
  9. Message({
  10. message: res.errMsg,
  11. type: 'error',
  12. duration: 5 * 1000
  13. });
  14. } else {
  15. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  16. if (code === 1004 || code === 1005 || code === 1006) {
  17. // to re-login
  18. store.dispatch('user/resetToken').then(() => {
  19. location.reload();
  20. });
  21. } else {
  22. Message({
  23. message: res.errMsg || 'Error',
  24. type: 'error',
  25. duration: 5 * 1000
  26. });
  27. }
  28. }
  29. return Promise.reject(new Error(res.errMsg || 'Error'));
  30. } else {
  31. return res;
  32. }
  33. },
  34. error => {
  35. if (error && error.response) {
  36. switch (error.response.status) {
  37. case 302:error.message = '接口重定向了!'; break;
  38. case 400:error.message = '请求错误'; break;
  39. case 401:error.message = '未授权,请登录'; break;
  40. case 403:error.message = '拒绝访问'; break;
  41. case 404:error.message = `请求地址出错: ${error.response.config.url}`; break;
  42. case 408:error.message = '请求超时'; break;
  43. case 409:error.message = '系统已存在相同数据!'; break;
  44. case 500:error.message = '服务器内部错误'; break;
  45. case 501:error.message = '服务未实现'; break;
  46. case 502:error.message = '网关错误'; break;
  47. case 503:error.message = '服务不可用'; break;
  48. case 504:error.message = '网关超时'; break;
  49. case 505:error.message = 'HTTP版本不受支持'; break;
  50. default:error.message = '异常问题,请联系管理员!'; break;
  51. }
  52. }
  53. Message({
  54. message: error || 'Error',
  55. type: 'error',
  56. duration: 5 * 1000
  57. });
  58. return Promise.reject(error);
  59. }
  60. );

7:把service整个模块导出

export default service;

8:开始使用:创建user.js文件维护接口信息

  1. import request from '@/utils/request';
  2. // 列表
  3. export function showUser(data) {
  4. return request({
  5. url: '/user/queryUser',
  6. method: 'post',
  7. data: data
  8. });
  9. }
  10. // 人员新增弹框
  11. export function queryaddProduct(data) {
  12. return request({
  13. url: '/user/queryProduct',
  14. method: 'get',
  15. params: data
  16. });
  17. }
  18. // 删除
  19. export function delUser(id) {
  20. return request({
  21. url: `/user/delUserById?uid=${id}`,
  22. method: 'post'
  23. });
  24. }
  25. // 更新状态
  26. export function updateUser(data) {
  27. return request({
  28. url: 'user/updateState',
  29. method: 'post',
  30. data: data
  31. });
  32. }
  33. // 模糊查询
  34. export function searchName(id) {
  35. return request({
  36. url: `/user/username/search?usernameQuery=${id}`,
  37. method: 'get'
  38. });
  39. }
  40. // 批量删除
  41. export function batchDeleteUser(data) {
  42. return request({
  43. url: '/user/delUserByIds',
  44. method: 'post',
  45. data: data
  46. });
  47. }
  48. export function addUser(data) {
  49. return request({
  50. url: '/user/add',
  51. method: 'POST',
  52. data: data
  53. });
  54. }
  55. // 登录
  56. export function login(data) {
  57. return request({
  58. url: '/userinfo/login',
  59. method: 'post',
  60. data
  61. });
  62. }
  63. export function getRouters() {
  64. return request({
  65. url: '/menuInfo/router',
  66. method: 'get'
  67. });
  68. }

9:在vue文件中开始使用

9-1:引入需要使用的接口

import { showUser } from '@/api/user';

9-2:掉接口

  1. // 获取列表
  2. async getList() {
  3. this.listLoading = true;
  4. const res = await showUser(this.listQuery);
  5. if (!res) {
  6. return;
  7. }
  8. const data = res.data || {};
  9. this.list = data.data || [];
  10. this.total = data.totalCount;
  11. this.listLoading = false;
  12. },

request.js完整代码贴出

  1. import axios from 'axios';
  2. import { Message } from 'element-ui';
  3. import store from '@/store';
  4. import { getToken } from '@/utils/auth';
  5. import config from '@/api/config';
  6. // 创建axios实例
  7. const service = axios.create({
  8. baseURL: 'http://www.qingmu.com/api',
  9. withCredentials: true,
  10. timeout: 5000
  11. });
  12. // 请求拦截器
  13. service.interceptors.request.use(
  14. config => {
  15. config.headers['token'] = getToken() || 0;
  16. return config;
  17. },
  18. error => {
  19. // 处理请求错误
  20. console.log(error); // for debug
  21. return Promise.reject(error);
  22. }
  23. );
  24. // 响应拦截器
  25. service.interceptors.response.use(
  26. response => {
  27. const res = response.data;
  28. // 未设置状态码则默认成功状态
  29. const code = res.errCode || 0;
  30. // if the custom code is not 20000, it is judged as an error.
  31. if (code !== 0) {
  32. if (code === 3001 || code === 3002) {
  33. Message({
  34. message: res.errMsg,
  35. type: 'error',
  36. duration: 5 * 1000
  37. });
  38. } else {
  39. // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
  40. if (code === 1004 || code === 1005 || code === 1006) {
  41. // to re-login
  42. store.dispatch('user/resetToken').then(() => {
  43. location.reload();
  44. });
  45. } else {
  46. Message({
  47. message: res.errMsg || 'Error',
  48. type: 'error',
  49. duration: 5 * 1000
  50. });
  51. }
  52. }
  53. return Promise.reject(new Error(res.errMsg || 'Error'));
  54. } else {
  55. return res;
  56. }
  57. },
  58. error => {
  59. if (error && error.response) {
  60. switch (error.response.status) {
  61. case 302:error.message = '接口重定向了!'; break;
  62. case 400:error.message = '请求错误'; break;
  63. case 401:error.message = '未授权,请登录'; break;
  64. case 403:error.message = '拒绝访问'; break;
  65. case 404:error.message = `请求地址出错: ${error.response.config.url}`; break;
  66. case 408:error.message = '请求超时'; break;
  67. case 409:error.message = '系统已存在相同数据!'; break;
  68. case 500:error.message = '服务器内部错误'; break;
  69. case 501:error.message = '服务未实现'; break;
  70. case 502:error.message = '网关错误'; break;
  71. case 503:error.message = '服务不可用'; break;
  72. case 504:error.message = '网关超时'; break;
  73. case 505:error.message = 'HTTP版本不受支持'; break;
  74. default:error.message = '异常问题,请联系管理员!'; break;
  75. }
  76. }
  77. Message({
  78. message: error || 'Error',
  79. type: 'error',
  80. duration: 5 * 1000
  81. });
  82. return Promise.reject(error);
  83. }
  84. );
  85. export default service;
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/299835
推荐阅读
相关标签
  

闽ICP备14008679号