You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.4 KiB
60 lines
1.4 KiB
import axios from 'axios'
|
|
import { useStore } from '@/store/root'
|
|
|
|
axios.defaults.timeout = 16000
|
|
|
|
axios.interceptors.request.use(
|
|
config => {
|
|
//添加时间戳 防止访问json文件出现缓存
|
|
if (/get/i.test(config.method) && /\.json$/i.test(config.url)) {
|
|
config.params = config.params || {}
|
|
config.params.t = Date.parse(new Date()) / 1000
|
|
}
|
|
|
|
//请求类型不是json文件
|
|
if (!/\.json$/i.test(config.url)) {
|
|
const store = useStore()
|
|
const hasCode = store.currentFloor?.projectCode
|
|
//处理拼接符号 一开始有 '?' 说明已有query params 需要把符号改成'&'
|
|
const code = `${config.url.includes('?') ? '&' : '?'}projectCode=${store.currentFloor.projectCode}`
|
|
Object.assign(config, {
|
|
url: hasCode ? `${config.url}${code}` : config.url
|
|
})
|
|
}
|
|
|
|
return config
|
|
},
|
|
error => {
|
|
return Promise.reject(error)
|
|
}
|
|
)
|
|
|
|
//封装post方法
|
|
export function post(url, params) {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.post(url, params)
|
|
.then(res => {
|
|
resolve(res.data)
|
|
})
|
|
.catch(err => {
|
|
reject(err)
|
|
})
|
|
})
|
|
}
|
|
|
|
//封装get方法
|
|
export function get(url, params) {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.get(url, {
|
|
params
|
|
})
|
|
.then(res => {
|
|
resolve(res.data)
|
|
})
|
|
.catch(err => {
|
|
reject(err)
|
|
})
|
|
})
|
|
}
|
|
|