parent
0baa3620c1
commit
14e03415f6
10 changed files with 188 additions and 203 deletions
@ -1,33 +1,33 @@ |
|||||||
//登录接口需要携带参数ts类型
|
//登录接口需要携带参数ts类型
|
||||||
export interface loginForm{ |
export interface loginForm { |
||||||
username:string, |
username: string |
||||||
password:string |
password: string |
||||||
} |
} |
||||||
|
|
||||||
interface dataType{ |
interface dataType { |
||||||
token:string |
token: string |
||||||
} |
} |
||||||
//登录接口返回数据类型
|
//登录接口返回数据类型
|
||||||
export interface loginResponseDate{ |
export interface loginResponseDate { |
||||||
code:number, |
code: number |
||||||
date:dataType |
date: dataType |
||||||
} |
} |
||||||
//定义服务器返回用户信息相关数据类型
|
//定义服务器返回用户信息相关数据类型
|
||||||
interface userInfo{ |
interface userInfo { |
||||||
userId:number, |
userId: number |
||||||
avatar:string, |
avatar: string |
||||||
username:string, |
username: string |
||||||
password:string, |
password: string |
||||||
desc:string, |
desc: string |
||||||
roles:string[], |
roles: string[] |
||||||
buttons:string[], |
buttons: string[] |
||||||
routes:string[], |
routes: string[] |
||||||
token:string |
token: string |
||||||
} |
} |
||||||
interface user{ |
interface user { |
||||||
checkUser:userInfo |
checkUser: userInfo |
||||||
} |
} |
||||||
export interface userReponseData{ |
export interface userReponseData { |
||||||
code:number, |
code: number |
||||||
date:user |
date: user |
||||||
} |
} |
@ -1,11 +1,5 @@ |
|||||||
<template> |
<template></template> |
||||||
|
|
||||||
</template> |
<script setup></script> |
||||||
|
|
||||||
<script setup> |
<style scoped></style> |
||||||
|
|
||||||
</script> |
|
||||||
|
|
||||||
<style scoped> |
|
||||||
|
|
||||||
</style> |
|
||||||
|
@ -1,17 +1,16 @@ |
|||||||
//引入项目中全部的全局组件
|
//引入项目中全部的全局组件
|
||||||
import SvgIcon from './SvgIcon/index.vue' |
import SvgIcon from './SvgIcon/index.vue' |
||||||
import Pagination from'./Pagination/index.vue' |
import Pagination from './Pagination/index.vue' |
||||||
//全局对象
|
//全局对象
|
||||||
const allGloablComponent={SvgIcon,Pagination }; |
const allGloablComponent = { SvgIcon, Pagination } |
||||||
//对外暴露插件对象
|
//对外暴露插件对象
|
||||||
export default { |
export default { |
||||||
//务必叫做insta1l方法
|
//务必叫做insta1l方法
|
||||||
install(app) { |
install(app) { |
||||||
//注册项目全部的全局组件
|
//注册项目全部的全局组件
|
||||||
Object.keys(allGloablComponent).forEach(key => { |
Object.keys(allGloablComponent).forEach((key) => { |
||||||
//注册为全局组件
|
//注册为全局组件
|
||||||
|
app.component(key, allGloablComponent[key]) |
||||||
app.component(key, allGloablComponent[key]); |
}) |
||||||
}); |
}, |
||||||
} |
|
||||||
} |
} |
@ -1,46 +1,48 @@ |
|||||||
//进行axios二次封装:使用请求与响应拦截器
|
//进行axios二次封装:使用请求与响应拦截器
|
||||||
//1:使用请求拦截器,可以在请求拦截器中处理一些业务(开始进度条、请求头携带公共参数)
|
//1:使用请求拦截器,可以在请求拦截器中处理一些业务(开始进度条、请求头携带公共参数)
|
||||||
// 2:使用响应拦截器,可以在响应拦截器中处理一些业务(进度条结束、简化服务器返回的数据、处理http网络错误)
|
// 2:使用响应拦截器,可以在响应拦截器中处理一些业务(进度条结束、简化服务器返回的数据、处理http网络错误)
|
||||||
import axios from "axios"; |
import axios from 'axios' |
||||||
import { ElMessage } from "element-plus"; |
import { ElMessage } from 'element-plus' |
||||||
//创建axios实例
|
//创建axios实例
|
||||||
let request = axios.create({ |
let request = axios.create({ |
||||||
baseURL: import.meta.env.VITE_APP_BASE_API, |
baseURL: import.meta.env.VITE_APP_BASE_API, |
||||||
timeout: 5000 |
timeout: 5000, |
||||||
}) |
}) |
||||||
//请求拦截器
|
//请求拦截器
|
||||||
request.interceptors.request.use(config => { |
request.interceptors.request.use((config) => { |
||||||
//config配置对象,headers属性请求头,经常给服务器端携带公共参数
|
//config配置对象,headers属性请求头,经常给服务器端携带公共参数
|
||||||
return config; |
return config |
||||||
}); |
}) |
||||||
//响应拦截器
|
//响应拦截器
|
||||||
request.interceptors.response.use((response) => { |
request.interceptors.response.use( |
||||||
return response.data; |
(response) => { |
||||||
}, (error) => { |
return response.data |
||||||
|
}, |
||||||
|
(error) => { |
||||||
//处理网络错误
|
//处理网络错误
|
||||||
let msg = ''; |
let msg = '' |
||||||
let status = error.response.status; |
let status = error.response.status |
||||||
switch (status) { |
switch (status) { |
||||||
case 401: |
case 401: |
||||||
msg = "token过期"; |
msg = 'token过期' |
||||||
break; |
break |
||||||
case 403: |
case 403: |
||||||
msg = '无权访问'; |
msg = '无权访问' |
||||||
break; |
break |
||||||
case 404: |
case 404: |
||||||
msg = "请求地址错误"; |
msg = '请求地址错误' |
||||||
break; |
break |
||||||
case 500: |
case 500: |
||||||
msg = "服务器出现问题"; |
msg = '服务器出现问题' |
||||||
break; |
break |
||||||
default: |
default: |
||||||
msg = "无网络"; |
msg = '无网络' |
||||||
|
|
||||||
} |
} |
||||||
ElMessage({ |
ElMessage({ |
||||||
type: 'error', |
type: 'error', |
||||||
message: msg |
message: msg, |
||||||
}) |
}) |
||||||
return Promise.reject(error); |
return Promise.reject(error) |
||||||
}); |
}, |
||||||
export default request; |
) |
||||||
|
export default request |
||||||
|
Loading…
Reference in new issue