1
This commit is contained in:
24
src/utils/format.ts
Normal file
24
src/utils/format.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 日期格式化
|
||||
* @param {Date} date
|
||||
* @param {string} format 格式
|
||||
*/
|
||||
export function formatDate(date: any = new Date(), format = 'YYYY-MM-DD hh:mm:ss') {
|
||||
if (!(date instanceof Date)) {
|
||||
date = new Date(date);
|
||||
}
|
||||
const YYYY = date.getFullYear().toString();
|
||||
const MM = ('0' + (date.getMonth() + 1)).slice(-2);
|
||||
const DD = ('0' + date.getDate()).slice(-2);
|
||||
const hh = ('0' + date.getHours()).slice(-2);
|
||||
const mm = ('0' + date.getMinutes()).slice(-2);
|
||||
const ss = ('0' + date.getSeconds()).slice(-2);
|
||||
|
||||
return format
|
||||
.replace(/YYYY/, YYYY)
|
||||
.replace(/MM/, MM)
|
||||
.replace(/DD/, DD)
|
||||
.replace(/hh/, hh)
|
||||
.replace(/mm/, mm)
|
||||
.replace(/ss/, ss);
|
||||
}
|
||||
13
src/utils/http/error.ts
Normal file
13
src/utils/http/error.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import qs from "qs";
|
||||
|
||||
/**
|
||||
* 错误处理
|
||||
*/
|
||||
export function errorHand() {
|
||||
window.localStorage.removeItem("app-access")
|
||||
let location = window.location
|
||||
let query = qs.parse(location.href.split('?')[1])
|
||||
delete query.code
|
||||
let query2 = qs.stringify(query)
|
||||
window.location.href = location.origin + location.pathname + '?' + query2
|
||||
}
|
||||
55
src/utils/http/request.ts
Normal file
55
src/utils/http/request.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import axios, {type AxiosRequestHeaders, type AxiosResponse} from "axios"
|
||||
import {closeToast, showToast} from 'vant';
|
||||
import {useUserStore} from "@/stores";
|
||||
import {errorHand} from "@/utils/http/error";
|
||||
|
||||
const instance = axios.create({
|
||||
baseURL: import.meta.env.VITE_WEB_URL + "/api",
|
||||
timeout: 60000,
|
||||
})
|
||||
|
||||
//拦截器
|
||||
instance.interceptors.request.use((config) => {
|
||||
const store = useUserStore()
|
||||
if (store.token) {
|
||||
(config.headers as AxiosRequestHeaders).Authorization = `Bearer ${store.token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
instance.interceptors.response.use((response: AxiosResponse) => {
|
||||
const {code, data, message} = response.data
|
||||
|
||||
closeToast()
|
||||
if (code === 1) {
|
||||
return data
|
||||
} else if (code === 0) {
|
||||
showToast(message)
|
||||
return Promise.reject(new Error(message))
|
||||
|
||||
} else {
|
||||
if (code === 401 || code === 403) {
|
||||
errorHand()
|
||||
} else {
|
||||
showToast(message)
|
||||
}
|
||||
}
|
||||
}, error => {
|
||||
showToast(error.message)
|
||||
})
|
||||
|
||||
|
||||
//封装post,get
|
||||
function requestPost(url: string, data = {}) {
|
||||
return instance.post(url, data)
|
||||
}
|
||||
|
||||
function requestGet(url: string, params = {}) {
|
||||
return instance.get(url, {params})
|
||||
}
|
||||
|
||||
const request = {
|
||||
get: requestGet,
|
||||
post: requestPost
|
||||
}
|
||||
export default request
|
||||
Reference in New Issue
Block a user