|
@@ -3,7 +3,14 @@ import router from '@router/index.js'
|
|
import store from '@store/index.js'
|
|
import store from '@store/index.js'
|
|
import notification from 'ant-design-vue/es/notification'
|
|
import notification from 'ant-design-vue/es/notification'
|
|
|
|
|
|
-// 创建 axios 实例
|
|
|
|
|
|
+/**
|
|
|
|
+ * 创建 axios 实例
|
|
|
|
+ * baseURL: API 接口的基础 URL
|
|
|
|
+ * withCredentials: 跨域请求时是否需要携带 cookie
|
|
|
|
+ * timeout: 请求超时时间
|
|
|
|
+ * maxBodyLength: 请求体最大长度
|
|
|
|
+ * maxContentLength: 响应体最大长度
|
|
|
|
+ */
|
|
const service = axios.create({
|
|
const service = axios.create({
|
|
baseURL: 'https://qlaiapi.raycos.com.cn', // api base_url
|
|
baseURL: 'https://qlaiapi.raycos.com.cn', // api base_url
|
|
withCredentials: true,
|
|
withCredentials: true,
|
|
@@ -12,51 +19,139 @@ const service = axios.create({
|
|
maxContentLength: 5242880, // 响应的最大长度,单位 B ,5MB,针对文件或者表格数据大量返回 注意和nginx配置保持一致
|
|
maxContentLength: 5242880, // 响应的最大长度,单位 B ,5MB,针对文件或者表格数据大量返回 注意和nginx配置保持一致
|
|
})
|
|
})
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * 统一错误处理函数
|
|
|
|
+ * @param {Error} error - 错误对象
|
|
|
|
+ */
|
|
const err = (error) => {
|
|
const err = (error) => {
|
|
if (error.response) {
|
|
if (error.response) {
|
|
const data = error.response.data
|
|
const data = error.response.data
|
|
- if (error.response.status === 401) { // 登录失效
|
|
|
|
- router.push({path: '/login'})
|
|
|
|
- } else if (error.response.status === 402) { // 缺少权限
|
|
|
|
- notification.error({
|
|
|
|
- message: '您没有权限查看当前信息',
|
|
|
|
- description: '请联系管理员获取数据权限',
|
|
|
|
- duration: null
|
|
|
|
- })
|
|
|
|
- } else if (error.response.status === 403) {
|
|
|
|
- notification.error({
|
|
|
|
- message: 'Forbidden',
|
|
|
|
- description: data.message
|
|
|
|
- })
|
|
|
|
- } else if (error.response.status === 504) { // 请求超时
|
|
|
|
- notification.error({
|
|
|
|
- message: '提示',
|
|
|
|
- description: '请求时间较长,请稍后查看',
|
|
|
|
- duration: null
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
|
|
+ const status = error.response.status
|
|
|
|
+
|
|
|
|
+ // 错误状态码处理
|
|
|
|
+ const errorMap = {
|
|
|
|
+ 400: {
|
|
|
|
+ message: '请求错误',
|
|
|
|
+ description: data.message || '请求参数异常'
|
|
|
|
+ },
|
|
|
|
+ 401: {
|
|
|
|
+ message: '未授权',
|
|
|
|
+ description: '登录状态已过期,请重新登录',
|
|
|
|
+ callback: () => {
|
|
|
|
+ router.push({path: '/login'})
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ 402: {
|
|
|
|
+ message: '权限不足',
|
|
|
|
+ description: '请联系管理员获取数据权限'
|
|
|
|
+ },
|
|
|
|
+ 403: {
|
|
|
|
+ message: '拒绝访问',
|
|
|
|
+ description: data.message || '无权限访问该资源'
|
|
|
|
+ },
|
|
|
|
+ 404: {
|
|
|
|
+ message: '请求错误',
|
|
|
|
+ description: '请求的资源不存在'
|
|
|
|
+ },
|
|
|
|
+ 408: {
|
|
|
|
+ message: '请求超时',
|
|
|
|
+ description: '请求超时,请稍后重试'
|
|
|
|
+ },
|
|
|
|
+ 500: {
|
|
|
|
+ message: '服务器错误',
|
|
|
|
+ description: '服务器出现问题,请稍后重试'
|
|
|
|
+ },
|
|
|
|
+ 504: {
|
|
|
|
+ message: '网关超时',
|
|
|
|
+ description: '请求时间较长,请稍后查看'
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ const errorInfo = errorMap[status] || {
|
|
|
|
+ message: `请求错误 ${status}`,
|
|
|
|
+ description: '未知错误,请联系技术支持'
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ notification.error({
|
|
|
|
+ message: errorInfo.message,
|
|
|
|
+ description: errorInfo.description,
|
|
|
|
+ duration: null
|
|
|
|
+ })
|
|
|
|
+
|
|
|
|
+ if (errorInfo.callback) {
|
|
|
|
+ errorInfo.callback()
|
|
|
|
+ }
|
|
|
|
+ } else if (error.request) {
|
|
|
|
+ // 请求已经发出,但没有收到响应
|
|
|
|
+ notification.error({
|
|
|
|
+ message: '网络异常',
|
|
|
|
+ description: '服务器无响应,请检查网络连接',
|
|
|
|
+ duration: null
|
|
|
|
+ })
|
|
} else {
|
|
} else {
|
|
- // 请求超时
|
|
|
|
|
|
+ // 请求配置发生的错误
|
|
notification.error({
|
|
notification.error({
|
|
- message: '提示',
|
|
|
|
- description: '请求时间较长,请稍后查看',
|
|
|
|
|
|
+ message: '请求错误',
|
|
|
|
+ description: error.message || '请求发生错误,请稍后重试',
|
|
duration: null
|
|
duration: null
|
|
- });
|
|
|
|
|
|
+ })
|
|
}
|
|
}
|
|
return Promise.reject(error)
|
|
return Promise.reject(error)
|
|
}
|
|
}
|
|
|
|
|
|
-// 请求拦截,设置token
|
|
|
|
-service.interceptors.request.use((config) => {
|
|
|
|
- config.headers['Access-Token'] = store.state.app.token || ''
|
|
|
|
- return config
|
|
|
|
-}, err)
|
|
|
|
|
|
+/**
|
|
|
|
+ * 请求拦截器
|
|
|
|
+ * 在请求发送之前做一些处理
|
|
|
|
+ */
|
|
|
|
+service.interceptors.request.use(
|
|
|
|
+ (config) => {
|
|
|
|
+ // 设置请求头中的token
|
|
|
|
+ const token = store.state.app.token
|
|
|
|
+ if (token) {
|
|
|
|
+ config.headers['Access-Token'] = token
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 处理 GET 请求参数
|
|
|
|
+ if (config.method === 'get') {
|
|
|
|
+ config.params = {
|
|
|
|
+ _t: Date.now(), // 添加时间戳防止缓存
|
|
|
|
+ ...config.params
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
|
|
-// 处理响应
|
|
|
|
-service.interceptors.response.use((response) => {
|
|
|
|
- return response.data
|
|
|
|
-}, err)
|
|
|
|
|
|
+ return config
|
|
|
|
+ },
|
|
|
|
+ err
|
|
|
|
+)
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 响应拦截器
|
|
|
|
+ * 在接收到响应数据之后做一些处理
|
|
|
|
+ */
|
|
|
|
+service.interceptors.response.use(
|
|
|
|
+ (response) => {
|
|
|
|
+ const res = response.data
|
|
|
|
+
|
|
|
|
+ // 这里可以根据后端约定的状态码,做相应的处理
|
|
|
|
+ if (res.code === 200) {
|
|
|
|
+ return res
|
|
|
|
+ } else {
|
|
|
|
+ notification.error({
|
|
|
|
+ message: '请求失败',
|
|
|
|
+ description: res.message || '未知错误',
|
|
|
|
+ duration: 4
|
|
|
|
+ })
|
|
|
|
+ return Promise.reject(new Error(res.message || '未知错误'))
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ err
|
|
|
|
+)
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * 封装请求方法
|
|
|
|
+ * @param {Object} options - axios 配置项
|
|
|
|
+ * @returns {Promise} axios 请求Promise
|
|
|
|
+ */
|
|
function requests(options = {}) {
|
|
function requests(options = {}) {
|
|
return service.request({ ...options })
|
|
return service.request({ ...options })
|
|
}
|
|
}
|