Skip to content

Commit

Permalink
添加cookie
Browse files Browse the repository at this point in the history
  • Loading branch information
gpj committed Oct 15, 2019
1 parent 7f4d25b commit 1091621
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dependencies": {
"axios": "^0.19.0",
"js-md5": "^0.7.3",
"vue-cookie": "^1.1.4",
"vuelidate": "^0.7.4"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions quasar.conf.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Configuration for your app

module.exports = function (ctx) {
module.exports = function(ctx) {
return {
plugins: [
'axios',
Expand Down Expand Up @@ -32,7 +32,7 @@ module.exports = function (ctx) {
},
devServer: {
open: true,
host: '192.168.1.254',
host: 'localhost',
port: ctx.mode.spa ? 9000 : (ctx.mode.pwa ? 9010 : 9020),
proxy: {
// 将所有以/api开头的请求代理到jsonplaceholder
Expand Down
52 changes: 49 additions & 3 deletions src/plugins/axios.js
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
}

0 comments on commit 1091621

Please sign in to comment.