generated from duanshuwen/webapp-vue-frontend
feat: 问卷功能开发
This commit is contained in:
@@ -1,32 +1,47 @@
|
||||
import axios from 'axios'
|
||||
import { tansParams } from '@utils/tansParams'
|
||||
import { Session } from '@utils/storage'
|
||||
|
||||
const request = async (method, url, data, config = {}) => {
|
||||
const options = Object.assign({}, config, {
|
||||
url,
|
||||
method
|
||||
})
|
||||
// 获取.env中的服务地址
|
||||
const { VITE_BASE_API, VITE_ENV } = import.meta.env
|
||||
console.log('🚀 ~ VITE_ENV:', VITE_ENV)
|
||||
console.log('🚀 ~ VITE_BASE_API:', VITE_BASE_API)
|
||||
|
||||
console.log('入参', data)
|
||||
axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
|
||||
|
||||
options.headers = Object.assign({}, options.headers, header)
|
||||
// 创建axios对象
|
||||
const instance = axios.create({
|
||||
baseURL: VITE_ENV === 'development' ? '/ingress/hotelBiz' : VITE_BASE_API,
|
||||
// 超时
|
||||
timeout: 10000
|
||||
})
|
||||
|
||||
axios
|
||||
.request(options)
|
||||
.then((res) => {
|
||||
console.log('返回值', res.data)
|
||||
resolve(res.data)
|
||||
})
|
||||
.catch((res) => {
|
||||
reject(res)
|
||||
})
|
||||
}
|
||||
// 添加拦截器
|
||||
instance.interceptors.request.use((config) => {
|
||||
const token = Session.getToken()
|
||||
config.headers['Authorization'] = `Bearer ${token}`
|
||||
|
||||
export const ajax = {
|
||||
request,
|
||||
get(url, config) {
|
||||
return request('get', url, null, config)
|
||||
},
|
||||
post(url, data, config) {
|
||||
return request('post', url, data, config)
|
||||
// get请求映射params参数
|
||||
if (config.method === 'get' && config.params) {
|
||||
let url = config.url + '?' + tansParams(config.params)
|
||||
console.log('🚀 ~ url:', url)
|
||||
|
||||
url = url.slice(0, -1)
|
||||
config.params = {}
|
||||
config.url = url
|
||||
}
|
||||
}
|
||||
|
||||
return config
|
||||
})
|
||||
|
||||
// 添加响应拦截器
|
||||
instance.interceptors.response.use(
|
||||
(res) => {
|
||||
return Promise.resolve(res.data.data)
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
export default instance
|
||||
|
||||
Reference in New Issue
Block a user