当前位置: 首页 > news >正文

Vue3 + Vite + TS + Element-Plus + Pinia项目(5)对axios进行封装

1、在src文件夹下新建config文件夹后,新建baseURL.ts文件,用来配置http主链接

2、在src文件夹下新建http文件夹后,新建request.ts文件,内容如下

import axios from "axios"
import { ElMessage } from 'element-plus'
import router from "../router/index.ts"
import {baseURL_dev} from '../config/baseURL.ts'
import useUser from '../store/user.ts'const userStore=useUser()
const Service = axios.create({timeout:8000,baseURL:baseURL_dev,headers:{"Content-type":"application/json;charset=utf-8","Authorization":""}
})
// 添加请求拦截器
Service.interceptors.request.use(function (config) {// 在发送请求之前做些什么if (config.headers.Authorization == null ||  config.headers.Authorization== "") {config.headers.Authorization = "Bearer "+userStore.userInfo.token;}return config;}, function (error) {// 对请求错误做些什么return Promise.reject(error);});// 添加响应拦截器
Service.interceptors.response.use(function (response) {const data = response.data;if(data.code!=undefined){if(data.code!=200 && data.code!=201){if(data.code==401){userStore.clearUser();router.push('/')return data;}ElMessage.error(data.errorMsg||"服务器出错")          return data}}return data}, function (error) {// 超出 2xx 范围的状态码都会触发该函数。// 对响应错误做点什么return Promise.reject(error);});// get请求
export const get = async (config:any)=>
{let ret = await Service({...config,method:"get",params:config.data})return ret
}// post请求
export const post=async (config:any)=>{let ret = await Service({...config,method:"post",data:config.data})return ret
}// put请求
export const put=async (config:any)=>{let ret = await Service({...config,method:"put",data:config.data})return ret
}// delete请求
export const del=async (config:any)=>{let ret = await Service({...config,method:"delete"})return ret
}

3、配置api,新建api.ts

import {post,get,put,del} from './request.ts'//登录方法
export const loginApi=async (data: object)=>{ let ret= post({url:"/Login/CheckLogin",data})//console.log(ret)return ret
}// 获取信息
export const getOutPatientsApi=async (data: object)=>{ let ret= get({url:`/Patient/GetOutPatients`,data})return ret
} 
// 更改患者信息
export const changePatientInfoApi=(data: object)=>{ return put({url:`/Patient/ChangePatientInfo`,data})
} // 删除用户
export const userDeleteApi=(data: object)=>{ return del({url:`/User/DeleteUserById/${data.userId}`})
} 

4、使用api

import { getPatientsApi, addPatientApi, changePatientInfoApi } from "../../http/api";getPatientsApi(searchParams).then((res) => {if (res) {if (res.data) {// console.log("用户数据", res);data.patientList = res.data.dataList;data.total = res.data.totalCount;}}});

http://www.lryc.cn/news/327609.html

相关文章:

  • 【Rust】——编写自动化测试(一)
  • 第十二章 微服务核心(一)
  • MySQL索引18连问,谁能顶住
  • [flink 实时流基础系列]揭开flink的什么面纱基础一
  • 开放平台 - 互动玩法演进之路
  • Linux之进程控制进程终止进程等待进程的程序替换替换函数实现简易shell
  • RegSeg 学习笔记(待完善)
  • Qt中常用宏定义
  • 【计算机网络】第 9 问:四种信道划分介质访问控制?
  • Rust编程(五)终章:查漏补缺
  • LLM漫谈(五)| 从q star视角解密OpenAI 2027年实现AGI计划
  • 【echart】数据可视化+vue+vite遇到问题
  • mac m1安装和使用nvm的问题
  • git泄露
  • Java项目:78 springboot学生宿舍管理系统的设计与开发
  • ArcGis Pro Python工具箱教程 03 工具箱中工具自定义
  • 【C++初阶】之类和对象(中)
  • Vue2(十一):脚手架配置代理、github案例、插槽
  • 在宝塔面板中,为自己的云服务器安装SSL证书,为所搭建的网站启用https(主要部分攻略)
  • 学习JavaEE的日子 Day32 线程池
  • @Transactional 注解使用的注意事项
  • 电商系列之库存
  • Apache HBase(二)
  • 【设计模式】原型模式详解
  • 企微侧边栏开发(内部应用内嵌H5)
  • 如何确定最优的石油管道位置
  • FPGA 图像边缘检测(Canny算子)
  • 2024.3.28学习笔记
  • 33.HarmonyOS App(JAVA)鸿蒙系统app数据库增删改查
  • 寄主机显示器被快递搞坏了怎么办?怎么破?