64 lines
1.6 KiB
JavaScript
64 lines
1.6 KiB
JavaScript
const config = {
|
|
mockApi: 'https://www.fastmock.site/mock/4b1d82b74b298c9bcdbf59c8793de4d2/api',
|
|
baseApi: `/`,
|
|
// baseApi: `http://192.168.11.46/`,
|
|
mock: false,
|
|
}
|
|
|
|
// 创建axios 实例对象,添加全局配置
|
|
const service = axios.create({
|
|
baseURL: config.mockApi,
|
|
timeout: 8000,
|
|
})
|
|
|
|
// 请求拦截
|
|
service.interceptors.request.use((req) => {
|
|
// todo
|
|
// const { token = "" } = storage.getItem('userInfo') || {};
|
|
const headers = req.headers
|
|
let token = ''
|
|
if (!headers.Authorization) headers.Authorization = 'Bearer ' + token
|
|
const getCookie = (name) => document.cookie.match(`[;\s+]?${name}=([^;]*)`)?.pop()
|
|
dncore = getCookie('dncore')
|
|
if (dncore == undefined)
|
|
window.location.href = window.location.origin + '/Accountsso/LogOn?ReturnUrl=/h5/mobile/index.html'
|
|
|
|
return req
|
|
})
|
|
|
|
// 响应拦截
|
|
service.interceptors.response.use((res) => {
|
|
// console.log(res)
|
|
const { data, status } = res
|
|
if (status == 200) {
|
|
console.log(data)
|
|
return data
|
|
}
|
|
})
|
|
|
|
function request(options) {
|
|
let isMock = config.mock
|
|
if (typeof options.mock != 'undefined') {
|
|
isMock = options.mock
|
|
}
|
|
options.method = options.method || 'get'
|
|
if (options.method.toLowerCase() === 'get') {
|
|
options.params = options.data
|
|
}
|
|
// console.log(isMock)
|
|
service.defaults.baseURL = isMock ? config.mockApi : config.baseApi
|
|
// console.log(service.defaults.baseURL)
|
|
return service(options)
|
|
}
|
|
|
|
;['get', 'post', 'put', 'delete', 'patch'].forEach((item) => {
|
|
request[item] = (url, data, options) => {
|
|
return request({
|
|
url,
|
|
data,
|
|
method: item,
|
|
...options,
|
|
})
|
|
}
|
|
})
|