import { Message } from 'element-ui'
import axios from 'axios'
import { get } from 'lodash'
import storage from './storage'
import router from '@/router'
import { baseURL } from '@/utils/baseURL'
function createService () {
const service = axios.create()
service.interceptors.request.use(
config => config,
error => Promise.reject(error)
)
service.interceptors.response.use(
response => {
if (response.status === 200) {
return response.data
} else {
Message.error('数据获取失败')
}
},
error => {
const status = get(error, 'response.status')
const data = get(error, 'response.data')
if (status === 401) {
router.replace('/Login')
return Message.error('登录失效,请重新登录')
}
Message.error(data.message ? data.message : error.message)
}
)
return service
}
function createRequest (service) {
return function (config) {
const configDefault = {
headers: {
Qtoken: storage.get('token'),
'Content-Type': get(config, 'headers.Content-Type', 'application/json'),
_hotel: storage.get('hotelCode') || ''
},
timeout: 5000,
baseURL: baseURL,
data: {}
}
const option = Object.assign(configDefault, config)
return service(option)
}
}
export const service = createService()
export const request = createRequest(service)
import {request} from '../utils/service';
export default {
login (data) {
return request({
url: '/api/Auth',
method: 'post',
data,
})
},
}