0


vue考试系统后台管理项目-接口封装调用

⭐️⭐️⭐️ 作者:船长在船上
🚩🚩🚩 主页:来访地址船长在船上的博客
🔨🔨🔨 简介:CSDN前端领域优质创作者,资深前端开发工程师,专注前端开发,在CSDN总结工作中遇到的问题或者问题解决方法以及对新技术的分享,欢迎咨询交流,共同学习。

🔔🔔🔔 感谢:如果觉得博主的文章不错或者对你的工作有帮助或者解决了你的问题,可以关注、支持一下博主。如有疑问可以留言、评论,看到后会及时回复。

上一篇文章 :

vue考试系统后台管理项目-登录、记住密码功能_船长在船上的博客-CSDN博客

考试系统后台管理项目介绍:

技术选型:Vue2.0+Element-ui

项目功能介绍:

  • 账户信息模块:菜单权限、角色权限设置、角色权限分配、账号设置、公司分组
  • 考试管理模块:新增/编辑/删除考试试题、成绩查看、阅卷评分、成绩记录、成绩导出
  • 题库管理模块:批量导入考题、批量删除考题、编辑录入考题、新增/编辑/删除考试分类

本项目会耗时一周到两周来完成,最近要又要辛苦加班了,项目给的时间不多,程序员太不容易了,做完项目调休好好休息一下!

此时此刻,记录一下项目实现。

发布上一篇距离今天有4天了,博主一直开发、测试别的模块功能,今天更新关于项目非常重要的一步,那就是请求后端数据调用接口部分内容,对于api接口封装处理我整理一下,分享给大家,可参考使用。

api接口封装调用

  • 新建api/axiosFun.js

封装的方法分为了3个部分,登录、文件下载、公用的方法分开来写

1.引入axios,loading加载方法,MessageBox组件弹窗提示

  1. import axios from 'axios';
  2. import { showLoading, hideLoading } from '../utils/loading';
  3. import {MessageBox} from 'element-ui';

2.登录请求方法封装

  1. // 登录请求方法
  2. const loginreq = (method, url, params) => {
  3. return axios({
  4. method: method,
  5. url: url,
  6. headers: {
  7. 'Content-Type': 'application/json',
  8. },
  9. withCredentials: true,
  10. data: params,
  11. }).then(res => {
  12. if(res.data.code===403 || res.data.code===401){
  13. localStorage.clear();
  14. }else{
  15. return res.data;
  16. }
  17. }).catch(error => {
  18. MessageBox.alert("登录失败,请检查网络!", "提示", {
  19. confirmButtonText: "确定",
  20. callback: () => {
  21. window.location.reload();
  22. }
  23. }).then(r => this.load(false));
  24. });
  25. };

3.其他通用公共方法封装

  1. // 通用公用方法
  2. const req = (method, url, params) => {
  3. showLoading();
  4. return axios({
  5. method: method,
  6. url: url,
  7. headers: {
  8. 'Content-Type': 'application/json',
  9. 'Authorization': JSON.parse(localStorage.getItem("userdata")).token
  10. },
  11. data: params,
  12. }).then(res => {
  13. hideLoading();
  14. if(res.data.code===403 || res.data.code===401){
  15. localStorage.clear();
  16. }else{
  17. return res.data;
  18. }
  19. }).catch(error => {
  20. hideLoading();
  21. });
  22. };

4.下载文件excel方法封装

  1. const downloadExcel = (method, url, params) =>{
  2. return axios({
  3. method: method,
  4. url: url,
  5. headers: {
  6. 'Content-Type': 'application/json',
  7. 'Authorization': JSON.parse(localStorage.getItem("userdata")).token
  8. },
  9. responseType: 'blob',
  10. data: params
  11. })
  12. };

5.方法导出

  1. export {
  2. loginreq,
  3. req,
  4. downloadExcel
  5. }

完整代码:

  1. import axios from 'axios';
  2. import { showLoading, hideLoading } from '../utils/loading';
  3. import {MessageBox} from 'element-ui';
  4. // 登录请求方法
  5. const loginreq = (method, url, params) => {
  6. return axios({
  7. method: method,
  8. url: url,
  9. headers: {
  10. 'Content-Type': 'application/json',
  11. },
  12. withCredentials: true,
  13. data: params
  14. }).then(res => {
  15. if(res.data.code===403 || res.data.code===401){
  16. localStorage.clear();
  17. }else{
  18. return res.data;
  19. }
  20. }).catch(error => {
  21. MessageBox.alert("登录失败,请检查网络!", "提示", {
  22. confirmButtonText: "确定",
  23. callback: () => {
  24. window.location.reload();
  25. }
  26. }).then(r => this.load(false));
  27. });
  28. };
  29. // 通用公用方法
  30. const req = (method, url, params) => {
  31. showLoading();
  32. return axios({
  33. method: method,
  34. url: url,
  35. headers: {
  36. 'Content-Type': 'application/json',
  37. 'Authorization': JSON.parse(localStorage.getItem("userdata")).token
  38. },
  39. data: params,
  40. }).then(res => {
  41. hideLoading();
  42. if(res.data.code===403 || res.data.code===401){
  43. localStorage.clear();
  44. }else{
  45. return res.data;
  46. }
  47. }).catch(error => {
  48. hideLoading();
  49. });
  50. };
  51. const downloadExcel = (method, url, params) =>{
  52. return axios({
  53. method: method,
  54. url: url,
  55. headers: {
  56. 'Content-Type': 'application/json',
  57. 'Authorization': JSON.parse(localStorage.getItem("userdata")).token
  58. },
  59. responseType: 'blob',
  60. data: params
  61. })
  62. };
  63. export {
  64. loginreq,
  65. req,
  66. downloadExcel
  67. }
  • 新建api/userMG.js

1.导入封装好的接口方法 downloadExcel,loginreq,req

2.定义线上地址、测试地址、本地地址,当然也可以写成以下写法:

  1. let url = process.env.VUE_APP_API

如果用这种写法的话,就要新建.env文件,要创建3种类型的文件(区分线上地址、测试地址、本地地址)

例如:

  • .env.development线上环境地址配置;.
  • env.test测试环境地址配置;
  • .env.development本地环境地址配置

本地环境地址配置:

  1. NODE_ENV='development'
  2. VUE_APP_FLAG = 'dev'
  3. VUE_APP_API='http://192.168.2.00:9010'
  4. VUE_APP_IMGAPI='http://36.134.22.00:8088'
  5. VUE_APP_WEBSOCKET='ws://192.168.2.00:9010'
  6. outputDir = dev

这种配置主要是为了项目打包可以很方便分开测试包,正式包;****后面会发布相应的文章介绍

3.注意接口使用的写法:get,post请求方式

  1. import axios from 'axios';
  2. import {
  3. downloadExcel,
  4. loginreq,
  5. req
  6. } from './axiosFun';
  7. // let url = "http://xxxxxxxx" //线上地址
  8. // let url = "http://xxxxxx" //测试地址
  9. let url = "http://xxxxxxxxxx" //本地地址
  10. export const baseUrl = url;
  11. /**************登录接口*********************/
  12. export const login = (params) => {
  13. return loginreq("post", url + "/sysUser/login", params)
  14. };
  15. //修改密码
  16. export const updatePwd = (params) => {
  17. return req("post", url + "/sysUser/updatePwd", params)
  18. };
  19. // 获取用户菜单
  20. export const menu = (id) => {
  21. return axios.get(url + "/sysMenu/menuTreeByUserId?id=" + id, {
  22. headers: {
  23. 'Authorization': JSON.parse(localStorage.getItem("userdata")).token
  24. }
  25. }).then(res => res.data)
  26. };
  27. ..................
  • 组件中接口引入使用

1.登录接口调用:

引入login登录接口路径

  1. import { login } from "../api/userMG";

使用:

this.ruleForm是一个账号密码对象

  1. login(this.ruleForm).then(res=>{
  2. if(res.code == 200){
  3. //信息获取
  4. }
  5. })

2.列表数据获取

  1. import {
  2. getUserNameList,//用户列表
  3. } from "../../api/userMG";
  1. getUserNameList({name:value}).then(res=>{
  2. if(res.code == 200){
  3. }
  4. })

** 3.get方式接口使用**

获取菜单数据

  1. import { menu } from "../api/userMG";
  1. // 获取菜单的数据
  2. menu(JSON.parse(localStorage.getItem("userdata")).id)
  3. .then(res => {
  4. // console.log(res, "获取菜单的数据");
  5. if (res.code == 200) {
  6. this.allmenu = res.data;
  7. } else {
  8. this.$message.error(res.msg);
  9. return false;
  10. }
  11. }).catch(err => {
  12. this.$message.error("菜单加载失败,请稍后再试!");
  13. });

** 上一篇文章 :**

vue考试系统后台管理项目-登录、记住密码功能_船长在船上的博客-CSDN博客

项目功能持续迭代...........

👉👉👉 欢迎来访船长在船上的博客,文章持续更新;**项目功能持续迭代,项目开发测试完成会把完整代码上传码云,请及时收藏关注,方便查看。 **


本文转载自: https://blog.csdn.net/SmartJunTao/article/details/126737705
版权归原作者 船长在船上 所有, 如有侵权,请联系我们删除。

“vue考试系统后台管理项目-接口封装调用”的评论:

还没有评论