This commit is contained in:
2024-11-15 13:47:07 +08:00
parent 563285eb77
commit a86e6d4009
3 changed files with 58 additions and 49 deletions

View File

@@ -35,6 +35,6 @@ export const downloadTemplate = () => {
} }
//导入被检设备 //导入被检设备
export const importPqDev=(params: Device.ReqPqDevParams)=>{ export const importPqDev=(params: Device.ReqPqDevParams)=>{
return http.post(`/pqDev/import`, params) return http.upload(`/pqDev/import`, params)
} }

View File

@@ -1,12 +1,12 @@
import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from "axios"; import axios, { AxiosInstance, AxiosError, AxiosRequestConfig, InternalAxiosRequestConfig, AxiosResponse } from 'axios'
import { showFullScreenLoading, tryHideFullScreenLoading } from "@/components/Loading/fullScreen"; import { showFullScreenLoading, tryHideFullScreenLoading } from '@/components/Loading/fullScreen'
import { LOGIN_URL } from "@/config"; import { LOGIN_URL } from '@/config'
import { ElMessage } from "element-plus"; import { ElMessage } from 'element-plus'
import { ResultData } from "@/api/interface"; import { ResultData } from '@/api/interface'
import { ResultEnum } from "@/enums/httpEnum"; import { ResultEnum } from '@/enums/httpEnum'
import { checkStatus } from "./helper/checkStatus"; import { checkStatus } from './helper/checkStatus'
import { useUserStore } from "@/stores/modules/user"; import { useUserStore } from '@/stores/modules/user'
import router from "@/routers"; import router from '@/routers'
export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig { export interface CustomAxiosRequestConfig extends InternalAxiosRequestConfig {
loading?: boolean; loading?: boolean;
@@ -20,15 +20,15 @@ const config = {
// 跨域时候允许携带凭证 // 跨域时候允许携带凭证
withCredentials: true, withCredentials: true,
// post请求指定数据类型以及编码 // post请求指定数据类型以及编码
headers: { 'Content-Type': 'application/json;charset=utf-8' } headers: { 'Content-Type': 'application/json;charset=utf-8' },
//headers : {'Content-Type': 'multipart/form-data'} }
};
class RequestHttp { class RequestHttp {
service: AxiosInstance; service: AxiosInstance
public constructor(config: AxiosRequestConfig) { public constructor(config: AxiosRequestConfig) {
// 创建实例 // 创建实例
this.service = axios.create(config); this.service = axios.create(config)
/** /**
* @description 请求拦截器 * @description 请求拦截器
@@ -37,19 +37,19 @@ class RequestHttp {
*/ */
this.service.interceptors.request.use( this.service.interceptors.request.use(
(config: CustomAxiosRequestConfig) => { (config: CustomAxiosRequestConfig) => {
const userStore = useUserStore(); const userStore = useUserStore()
// 当前请求不需要显示 loading在 api 服务中通过指定的第三个参数: { loading: false } 来控制 // 当前请求不需要显示 loading在 api 服务中通过指定的第三个参数: { loading: false } 来控制
config.loading ?? (config.loading = true); config.loading ?? (config.loading = true)
config.loading && showFullScreenLoading(); config.loading && showFullScreenLoading()
if (config.headers && typeof config.headers.set === "function") { if (config.headers && typeof config.headers.set === 'function') {
config.headers.set("x-access-token", userStore.token); config.headers.set('x-access-token', userStore.token)
} }
return config; return config
}, },
(error: AxiosError) => { (error: AxiosError) => {
return Promise.reject(error); return Promise.reject(error)
} },
); )
/** /**
* @description 响应拦截器 * @description 响应拦截器
@@ -57,58 +57,66 @@ class RequestHttp {
*/ */
this.service.interceptors.response.use( this.service.interceptors.response.use(
(response: AxiosResponse) => { (response: AxiosResponse) => {
const { data } = response; const { data } = response
const userStore = useUserStore(); const userStore = useUserStore()
tryHideFullScreenLoading(); tryHideFullScreenLoading()
// 登陆失效 // 登陆失效
if (data.code == ResultEnum.OVERDUE) { if (data.code == ResultEnum.OVERDUE) {
userStore.setToken(""); userStore.setToken('')
router.replace(LOGIN_URL); router.replace(LOGIN_URL)
ElMessage.error(data.message); ElMessage.error(data.message)
return Promise.reject(data); return Promise.reject(data)
} }
// 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错) // 全局错误信息拦截(防止下载文件的时候返回数据流,没有 code 直接报错)
if (data.code && data.code !== ResultEnum.SUCCESS) { if (data.code && data.code !== ResultEnum.SUCCESS) {
ElMessage.error(data.message); ElMessage.error(data.message)
return Promise.reject(data); return Promise.reject(data)
} }
// 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑) // 成功请求(在页面上除非特殊情况,否则不用处理失败逻辑)
return data; return data
}, },
async (error: AxiosError) => { async (error: AxiosError) => {
const { response } = error; const { response } = error
tryHideFullScreenLoading(); tryHideFullScreenLoading()
// 请求超时 && 网络错误单独判断,没有 response // 请求超时 && 网络错误单独判断,没有 response
if (error.message.indexOf("timeout") !== -1) ElMessage.error("请求超时!请您稍后重试"); if (error.message.indexOf('timeout') !== -1) ElMessage.error('请求超时!请您稍后重试')
if (error.message.indexOf("Network Error") !== -1) ElMessage.error("网络错误!请您稍后重试"); if (error.message.indexOf('Network Error') !== -1) ElMessage.error('网络错误!请您稍后重试')
// 根据服务器响应的错误状态码,做不同的处理 // 根据服务器响应的错误状态码,做不同的处理
if (response) checkStatus(response.status); if (response) checkStatus(response.status)
// 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面 // 服务器结果都没有返回(可能服务器错误可能客户端断网),断网处理:可以跳转到断网页面
if (!window.navigator.onLine) router.replace("/500"); if (!window.navigator.onLine) router.replace('/500')
return Promise.reject(error); return Promise.reject(error)
} },
); )
} }
/** /**
* @description 常用请求方法封装 * @description 常用请求方法封装
*/ */
get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> { get<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
return this.service.get(url, { params, ..._object }); return this.service.get(url, { params, ..._object })
} }
post<T>(url: string, params?: object | string, _object = {}): Promise<ResultData<T>> { post<T>(url: string, params?: object | string, _object = {}): Promise<ResultData<T>> {
return this.service.post(url, params, _object); return this.service.post(url, params, _object)
} }
put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> { put<T>(url: string, params?: object, _object = {}): Promise<ResultData<T>> {
return this.service.put(url, params, _object); return this.service.put(url, params, _object)
} }
delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> { delete<T>(url: string, params?: any, _object = {}): Promise<ResultData<T>> {
return this.service.delete(url, { params, ..._object }); return this.service.delete(url, { params, ..._object })
} }
download(url: string, params?: object, _object = {}): Promise<BlobPart> { download(url: string, params?: object, _object = {}): Promise<BlobPart> {
return this.service.post(url, params, { ..._object, responseType: "blob" }); return this.service.post(url, params, { ..._object, responseType: 'blob' })
}
upload(url: string, params?: object, _object = {}): Promise<BlobPart> {
return this.service.post(url, params, { ..._object, headers: { 'Content-Type': 'multipart/form-data' } })
} }
} }
export default new RequestHttp(config); export default new RequestHttp(config)

View File

@@ -82,6 +82,7 @@ const downloadTemp = () => {
// 文件上传 // 文件上传
const uploadExcel = async (param: UploadRequestOptions) => { const uploadExcel = async (param: UploadRequestOptions) => {
debugger
let excelFormData = new FormData(); let excelFormData = new FormData();
excelFormData.append("file", param.file); excelFormData.append("file", param.file);
isCover.value && excelFormData.append("isCover", isCover.value as unknown as Blob); isCover.value && excelFormData.append("isCover", isCover.value as unknown as Blob);