文档服务地址:http://47.92.0.57:3000/ 周报索引地址:http://47.92.0.57:3000/s/NruNXRYmV

Commit 15c90823 by Wang Yuhang

使用vuex-presistedstate做数据持久化,刷新页面不会丢失数据,在main.js页面添加响应拦截器,统一处理通用的错误如未登录

parent 5c9a2220
......@@ -16,7 +16,8 @@
"node-sass": "^4.14.1",
"vue": "^2.5.2",
"vue-router": "^3.0.1",
"vuex": "^3.4.0"
"vuex": "^3.4.0",
"vuex-persistedstate": "^3.0.1"
},
"devDependencies": {
"autoprefixer": "^7.1.2",
......
......@@ -37,7 +37,7 @@ export default {
},
mounted () {
this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data
this.tasks = res.data
})
},
methods: {
......
......@@ -38,7 +38,7 @@ export default {
},
mounted () {
this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data
this.tasks = res.data
})
},
methods: {
......
......@@ -111,11 +111,11 @@ export default {
}
}).then(res => {
console.log(res)
if (res.data.code === 1) {
self.$store.commit('setUserInfo', res.data.data)
if (res.code === 1) {
self.$store.commit('setUserInfo', res.data)
self.$router.push({path: '/taskHall'})
} else {
self.tip(res.data.message)
self.tip(res.message)
}
})
},
......@@ -133,11 +133,11 @@ export default {
email: this.email
}
}).then(res => {
if (res.data.code === 1) {
self.$store.commit('setUserInfo', res.data.data)
if (res.code === 1) {
self.$store.commit('setUserInfo', res.data)
self.$router.push({path: '/taskHall'})
} else {
self.tip(res.data.message)
self.tip(res.message)
}
})
}
......
......@@ -59,22 +59,22 @@ export default {
this.axios.get('/getPieces', {
params: { id }
}).then(res => {
self.tableData = res.data.data
self.tableData = res.data
// 拉取每个分片下的文件
function getFile (pieceId) {
return self.axios.get('/getFiles', {
return self.axios.get('/file/getFiles', {
params: { pieceId }
})
}
var requestList = []
for (var piece of res.data.data) {
for (var piece of res.data) {
requestList.push(getFile(piece.id))
}
self.axios.all(requestList).then(self.axios.spread(function () {
for (var res of arguments) {
self.map[res.config.params.pieceId] = res.data.data
for (var arg of arguments) {
self.map[arg.id] = arg.data
}
}))
})
......
......@@ -41,7 +41,7 @@ export default {
},
mounted () {
this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data
this.tasks = res.data
})
},
methods: {
......
......@@ -58,7 +58,7 @@ export default {
},
mounted () {
this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data
this.tasks = res.data
})
},
methods: {
......
......@@ -46,9 +46,9 @@ export default {
userId: this.userInfo.userId
}
}).then(res => {
if (res.data.code === 1) {
self.$store.commit('setUserInfo', res.data.data)
} else if (res.data.code === 0) {
if (res.code === 1) {
self.$store.commit('setUserInfo', res.data)
} else if (res.code === 0) {
// this.$router.push({path: '/login'}),
// alert('未登录'),
// window.reload()
......
......@@ -37,7 +37,7 @@ export default {
},
mounted () {
this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data
this.tasks = res.data
})
},
methods: {
......
......@@ -11,6 +11,22 @@ import VueAxios from 'vue-axios'
axios.defaults.baseURL = 'http://localhost:9100/api'
axios.defaults.timeout = 8000
// 添加响应拦截器
axios.interceptors.response.use(function (response) {
let res = response.data
// 未登录
if (res.code === 10) {
window.location.href = '/#/login'
} else {
if (response.config.url === '/file/getFiles') {
res.id = response.config.params.pieceId
}
return res
}
}, function (error) {
// 对响应错误做点什么
return Promise.reject(error)
})
Vue.use(VueAxios, axios)
Vue.config.productionTip = false
......
......@@ -19,7 +19,7 @@ Mock.mock(/\/api\/getPieces\?id=[0-9]+/, {
}]
})
Mock.mock(/\/api\/getFiles\?pieceId=[0-9]+/, {
Mock.mock(/\/api\/file\/getFiles\?pieceId=[0-9]+/, {
'status': 0,
'data|1-5': [{
'id|1-65535': 1,
......
/* eslint-disable no-undef */
import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex)
......@@ -16,5 +17,6 @@ export default new Vuex.Store({
actions: {
},
modules: {
}
},
plugins: [createPersistedState()]
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment