1234567891011121314151617181920212223242526272829303132333435363738394041 |
- import axios from 'axios'
- export default ({app}, inject) => {
- const service = axios.create({
- withCredentials: true,
- baseURL: '/',
- timeout: 5000
- })
- service.interceptors.request.use(config => {
- if (process.server) {
- config.url = process.env.baseUrl + config.url
- config.headers.cookie = app.store.state.option.cookies + ';' + app.store.state.option.sessionId
- config.headers['User-Agent'] = app.store.state.option.userAgent
- }
- return config
- }, error => {
- return Promise.reject(error)
- })
- service.interceptors.response.use(response => {
- const cookies = response.headers['set-cookie']
- if (cookies && cookies.length) {
- for (let i = 0; i < cookies.length; i++) {
- if (cookies[i].indexOf('JSESSIONID') > -1) {
- const sessionId = cookies[i]
- const first = sessionId.indexOf(';')
- const second = sessionId.lastIndexOf(';')
- const newSessionId = sessionId.replace(sessionId.substring(first, second), '')
- app.store.commit('option/SET_SESSION_ID', newSessionId)
- break
- }
- }
- }
- return response
- }, error => {
- return Promise.reject(error)
- })
- inject('http', service)
- }
|