-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gpj
committed
Oct 15, 2019
1 parent
7f4d25b
commit 1091621
Showing
4 changed files
with
65 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,53 @@ | ||
// import something here | ||
import axios from 'axios' | ||
// leave the export, even if you don't use it | ||
export default ({ app, router, Vue }) => { | ||
// something to do | ||
import cookie from 'vue-cookie' | ||
|
||
// axios默认配置 | ||
axios.defaults.timeout = 10000 // 超时时间 | ||
// axios.defaults.baseURL = 'http://192.168.2.251' // 默认地址 | ||
|
||
// 整理数据 | ||
axios.defaults.transformRequest = function (data) { | ||
data = JSON.stringify(data) | ||
return data; | ||
} | ||
|
||
// http request 拦截器 | ||
axios.interceptors.request.use( | ||
config => { | ||
config.data = JSON.stringify(config.data) | ||
config.headers['Content-Type'] = 'application/json;charset=UTF-8'; | ||
//判断是否存在ticket,如果存在的话,则每个http header都加上ticket | ||
if (cookie.get("token")) { | ||
//用户每次操作,都将cookie设置成2小时 | ||
cookie.set("token",cookie.get("token") ,1/12) | ||
cookie.set("name",cookie.get("name") ,1/12) | ||
config.headers.token = cookie.get("token") | ||
config.headers.name= cookie.get("name") | ||
} | ||
return config | ||
}, | ||
error => { | ||
return Promise.reject(error.response); | ||
}) | ||
|
||
// http response 拦截器 | ||
axios.interceptors.response.use( | ||
response => { | ||
if (response.data.resultCode ==="404") { | ||
console.log("response.data.resultCode是404") | ||
// 返回 错误代码-1 清除ticket信息并跳转到登录页面 | ||
// cookie.del("ticket") | ||
// window.location.href='http://login.com' | ||
return | ||
}else{ | ||
return response; | ||
} | ||
}, | ||
error => { | ||
return Promise.reject(error.response) // 返回接口返回的错误信息 | ||
}) | ||
|
||
export default ({ Vue }) => { | ||
Vue.prototype.$axios = axios | ||
} |