文档服务地址: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 @@ ...@@ -16,7 +16,8 @@
"node-sass": "^4.14.1", "node-sass": "^4.14.1",
"vue": "^2.5.2", "vue": "^2.5.2",
"vue-router": "^3.0.1", "vue-router": "^3.0.1",
"vuex": "^3.4.0" "vuex": "^3.4.0",
"vuex-persistedstate": "^3.0.1"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^7.1.2", "autoprefixer": "^7.1.2",
......
...@@ -37,7 +37,7 @@ export default { ...@@ -37,7 +37,7 @@ export default {
}, },
mounted () { mounted () {
this.axios.get('/getTasks').then(res => { this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data this.tasks = res.data
}) })
}, },
methods: { methods: {
......
...@@ -38,7 +38,7 @@ export default { ...@@ -38,7 +38,7 @@ export default {
}, },
mounted () { mounted () {
this.axios.get('/getTasks').then(res => { this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data this.tasks = res.data
}) })
}, },
methods: { methods: {
......
...@@ -111,11 +111,11 @@ export default { ...@@ -111,11 +111,11 @@ export default {
} }
}).then(res => { }).then(res => {
console.log(res) console.log(res)
if (res.data.code === 1) { if (res.code === 1) {
self.$store.commit('setUserInfo', res.data.data) self.$store.commit('setUserInfo', res.data)
self.$router.push({path: '/taskHall'}) self.$router.push({path: '/taskHall'})
} else { } else {
self.tip(res.data.message) self.tip(res.message)
} }
}) })
}, },
...@@ -133,11 +133,11 @@ export default { ...@@ -133,11 +133,11 @@ export default {
email: this.email email: this.email
} }
}).then(res => { }).then(res => {
if (res.data.code === 1) { if (res.code === 1) {
self.$store.commit('setUserInfo', res.data.data) self.$store.commit('setUserInfo', res.data)
self.$router.push({path: '/taskHall'}) self.$router.push({path: '/taskHall'})
} else { } else {
self.tip(res.data.message) self.tip(res.message)
} }
}) })
} }
......
...@@ -59,22 +59,22 @@ export default { ...@@ -59,22 +59,22 @@ export default {
this.axios.get('/getPieces', { this.axios.get('/getPieces', {
params: { id } params: { id }
}).then(res => { }).then(res => {
self.tableData = res.data.data self.tableData = res.data
// 拉取每个分片下的文件 // 拉取每个分片下的文件
function getFile (pieceId) { function getFile (pieceId) {
return self.axios.get('/getFiles', { return self.axios.get('/file/getFiles', {
params: { pieceId } params: { pieceId }
}) })
} }
var requestList = [] var requestList = []
for (var piece of res.data.data) { for (var piece of res.data) {
requestList.push(getFile(piece.id)) requestList.push(getFile(piece.id))
} }
self.axios.all(requestList).then(self.axios.spread(function () { self.axios.all(requestList).then(self.axios.spread(function () {
for (var res of arguments) { for (var arg of arguments) {
self.map[res.config.params.pieceId] = res.data.data self.map[arg.id] = arg.data
} }
})) }))
}) })
......
...@@ -41,7 +41,7 @@ export default { ...@@ -41,7 +41,7 @@ export default {
}, },
mounted () { mounted () {
this.axios.get('/getTasks').then(res => { this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data this.tasks = res.data
}) })
}, },
methods: { methods: {
......
...@@ -58,7 +58,7 @@ export default { ...@@ -58,7 +58,7 @@ export default {
}, },
mounted () { mounted () {
this.axios.get('/getTasks').then(res => { this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data this.tasks = res.data
}) })
}, },
methods: { methods: {
......
...@@ -46,9 +46,9 @@ export default { ...@@ -46,9 +46,9 @@ export default {
userId: this.userInfo.userId userId: this.userInfo.userId
} }
}).then(res => { }).then(res => {
if (res.data.code === 1) { if (res.code === 1) {
self.$store.commit('setUserInfo', res.data.data) self.$store.commit('setUserInfo', res.data)
} else if (res.data.code === 0) { } else if (res.code === 0) {
// this.$router.push({path: '/login'}), // this.$router.push({path: '/login'}),
// alert('未登录'), // alert('未登录'),
// window.reload() // window.reload()
......
...@@ -37,7 +37,7 @@ export default { ...@@ -37,7 +37,7 @@ export default {
}, },
mounted () { mounted () {
this.axios.get('/getTasks').then(res => { this.axios.get('/getTasks').then(res => {
this.tasks = res.data.data this.tasks = res.data
}) })
}, },
methods: { methods: {
......
...@@ -11,6 +11,22 @@ import VueAxios from 'vue-axios' ...@@ -11,6 +11,22 @@ import VueAxios from 'vue-axios'
axios.defaults.baseURL = 'http://localhost:9100/api' axios.defaults.baseURL = 'http://localhost:9100/api'
axios.defaults.timeout = 8000 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.use(VueAxios, axios)
Vue.config.productionTip = false Vue.config.productionTip = false
......
...@@ -19,7 +19,7 @@ Mock.mock(/\/api\/getPieces\?id=[0-9]+/, { ...@@ -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, 'status': 0,
'data|1-5': [{ 'data|1-5': [{
'id|1-65535': 1, 'id|1-65535': 1,
......
/* eslint-disable no-undef */ /* eslint-disable no-undef */
import Vue from 'vue' import Vue from 'vue'
import Vuex from 'vuex' import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'
Vue.use(Vuex) Vue.use(Vuex)
...@@ -16,5 +17,6 @@ export default new Vuex.Store({ ...@@ -16,5 +17,6 @@ export default new Vuex.Store({
actions: { actions: {
}, },
modules: { 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