From e074e1079d421d031061358e81f42e1b8b1a1425 Mon Sep 17 00:00:00 2001 From: ronger Date: Fri, 3 Jul 2020 14:43:14 +0800 Subject: [PATCH] first commit --- components/layouts/pc/header.vue | 6 +--- nuxt.config.js | 1 + pages/article/_article_id.vue | 4 +-- pages/login.vue | 16 +++++----- pages/register.vue | 6 ++-- plugins/axios.js | 53 ++++++++++++++++++++++++++++++-- store/article.js | 4 +-- 7 files changed, 68 insertions(+), 22 deletions(-) diff --git a/components/layouts/pc/header.vue b/components/layouts/pc/header.vue index 8af9635..5ae8690 100644 --- a/components/layouts/pc/header.vue +++ b/components/layouts/pc/header.vue @@ -196,11 +196,7 @@ getUnreadNotifications() { let _ts = this; if (_ts.isLogin) { - _ts.$axios.$get('/api/v1/notification/unread', { - headers: { - Authorization: _ts.$store.state.oauth?.accessToken - } - }).then(function (res) { + _ts.$axios.$get('/api/v1/notification/unread').then(function (res) { if (res) { _ts.$set(_ts, 'notifications', res.data.notifications); _ts.$set(_ts, 'notificationNumbers', res.data.notifications.length == 0 ? "" : res.data.notifications.length); diff --git a/nuxt.config.js b/nuxt.config.js index cb99cb7..6359d49 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -52,6 +52,7 @@ export default { ** https://nuxtjs.org/guide/plugins */ plugins: [ + {src: '@/plugins/axios'}, {src: '@/plugins/element-ui'}, {src: '@/plugins/vditor', ssr: false} ], diff --git a/pages/article/_article_id.vue b/pages/article/_article_id.vue index 93a4c86..7406297 100644 --- a/pages/article/_article_id.vue +++ b/pages/article/_article_id.vue @@ -226,7 +226,7 @@ } }) } else { - _ts.axios.get('/article/' + _ts.article.idArticle + '/share').then(function (res) { + _ts.$axios.$get('/api/v1/article/' + _ts.article.idArticle + '/share').then(function (res) { if (res) { _ts.$set(_ts, 'shareData', res); _ts.$set(_ts, 'isShare', true); @@ -241,8 +241,8 @@ } }, mounted() { + this.$store.commit('setActiveMenu', 'articleDetail') Vue.nextTick(() => { - this.$store.commit('setActiveMenu', 'articleDetail') const previewElement = document.getElementById("articleContent"); // //const outLineElement = document.getElementById("articleToC"); // VditorPreview.setContentTheme('light'); diff --git a/pages/login.vue b/pages/login.vue index 7a07dac..4f64034 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -87,16 +87,16 @@ } _ts.$axios.$post('/api/v1/console/login', data).then(function (res) { - if (res.data) { - if (res.data.message) { - _ts.$message(res.data.message); + if (res) { + if (res.message) { + _ts.$message(res.message); return false; } let auth = { - accessToken: res.data.user.token, - nickname: res.data.user.nickname, - avatarURL: res.data.user.avatarUrl, - role: res.data.user.weights + accessToken: res.user.token, + nickname: res.user.nickname, + avatarURL: res.user.avatarUrl, + role: res.user.weights } _ts.$store.commit('setAuth', auth) // mutating to store for client rendering Cookie.set('auth', auth) @@ -133,7 +133,7 @@ let data = { email: email }; - _ts.axios.post('/console/get-forget-password-email', _ts.qs.stringify(data)).then(function (res) { + _ts.axios.post('/console/get-forget-password-email', data).then(function (res) { _ts.loading = false; _ts.forget = false; if (res) { diff --git a/pages/register.vue b/pages/register.vue index 1c39fdb..789bea1 100644 --- a/pages/register.vue +++ b/pages/register.vue @@ -83,9 +83,9 @@ code: _ts.user.code } _ts.$axios.$post('/api/v1/console/register', data).then(function (res) { - if (res.data) { - _ts.$message(res.data.message); - if (res.data.flag && res.data.flag === 1) { + if (res) { + _ts.$message(res.message); + if (res.flag && res.flag === 1) { setTimeout(function () { _ts.$router.push( { diff --git a/plugins/axios.js b/plugins/axios.js index b2ddde1..9aa6172 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -1,2 +1,51 @@ -import apiConfig from '~/config/api.config' - +export default function ({app, $axios, store, redirect}) { + $axios.onRequest(config => { + if (!(config.url.indexOf('console') > -1)) { + config.headers['Authorization'] = store.state.oauth?.accessToken + } + }) + $axios.onResponse(response => { + return new Promise((resolve, reject) => { + //返回数据逻辑处理 比如:error_code错误处理 + let message; + if (typeof (response.data.data) !== 'undefined') { + message = response.data.data.message + } else if (typeof (response.data) !== 'undefined') { + message = response.data.message + } + if (response.data.success) { + resolve(response.data); + } else { + if (response.data.code === 0) { + app.$message(message); + } else if (response.data.code === 401) { + store.commit('logout'); + app.$router.push({ + name: 'login' + }) + } else if (response.data.code === 402) { + store.commit('logout'); + app.$router.push({ + name: 'login', + query: { + historyUrl: window.location.href + } + }) + } else if (response.data.code === 404) { + app.$message('操作失败,请稍后再试......') + } else if (response.data.code === 500) { + app.$message('服务器正在开小差,请稍后再试......') + } + } + reject(response); + }) + }); + $axios.onError(error => { + const code = parseInt(error.response && error.response.status) + if (code === 400) { + redirect('/400') + } else { + console.log('网络异常') + } + }) +} diff --git a/store/article.js b/store/article.js index dd4d5b7..0841faa 100644 --- a/store/article.js +++ b/store/article.js @@ -66,7 +66,7 @@ export const actions = { .$get(`${ARTICLE_API_PATH}/articles`, {params}) .then(response => { commit('updateListFetching', false) - commit('updateListData', response.data) + commit('updateListData', response) if (isBrowser) { Vue.nextTick(() => { window.scrollTo(0,0); @@ -92,7 +92,7 @@ export const actions = { .$get(`${ARTICLE_API_PATH}/article/${params.article_id}`) .then(response => { return new Promise(resolve => { - commit('updateDetailData', response.data) + commit('updateDetailData', response) commit('updateDetailFetching', false) resolve(response) // delay(() => {