From 5a13c949fa8b5d1a815aa3ae66ae8d9231beb3ce Mon Sep 17 00:00:00 2001 From: ronger Date: Fri, 3 Jul 2020 11:04:25 +0800 Subject: [PATCH] first commit --- components/layouts/mobile/footer.vue | 68 +- components/layouts/mobile/header.vue | 438 ++++---- components/layouts/mobile/main.vue | 45 +- components/layouts/pc/header.vue | 30 +- config/api.config.js | 2 +- config/app.config.js | 7 +- middleware/authenticated.js | 6 + middleware/notAuthenticated.js | 6 + nuxt.config.js | 4 +- package-lock.json | 1542 +++++++++++++------------- package.json | 6 +- pages/article/_article_id.vue | 125 ++- pages/index.vue | 4 + pages/login.vue | 18 +- pages/register.vue | 7 +- plugins/axios.js | 26 - store/auth.js | 83 -- store/index.js | 58 +- 18 files changed, 1261 insertions(+), 1214 deletions(-) create mode 100644 middleware/authenticated.js create mode 100644 middleware/notAuthenticated.js delete mode 100644 store/auth.js diff --git a/components/layouts/mobile/footer.vue b/components/layouts/mobile/footer.vue index 6f463a8..5570247 100644 --- a/components/layouts/mobile/footer.vue +++ b/components/layouts/mobile/footer.vue @@ -1,42 +1,46 @@ diff --git a/components/layouts/mobile/header.vue b/components/layouts/mobile/header.vue index c884aed..e5749bd 100644 --- a/components/layouts/mobile/header.vue +++ b/components/layouts/mobile/header.vue @@ -1,224 +1,242 @@ diff --git a/components/layouts/mobile/main.vue b/components/layouts/mobile/main.vue index 8b81654..f9e4ab1 100644 --- a/components/layouts/mobile/main.vue +++ b/components/layouts/mobile/main.vue @@ -1,27 +1,58 @@ diff --git a/components/layouts/pc/header.vue b/components/layouts/pc/header.vue index 138eab7..8af9635 100644 --- a/components/layouts/pc/header.vue +++ b/components/layouts/pc/header.vue @@ -84,6 +84,7 @@ diff --git a/pages/login.vue b/pages/login.vue index 3db1cfb..7a07dac 100644 --- a/pages/login.vue +++ b/pages/login.vue @@ -52,9 +52,12 @@ diff --git a/pages/register.vue b/pages/register.vue index 2df10ee..1c39fdb 100644 --- a/pages/register.vue +++ b/pages/register.vue @@ -69,7 +69,9 @@ if (response.data) { _ts.$message(response.data.message) } - }).catch(error=>{ console.log(error) }) + }).catch(error => { + console.log(error) + }) }, register() { let _ts = this; @@ -107,6 +109,9 @@ } ) } + }, + mounted() { + this.$store.commit('setActiveMenu', 'register') } } diff --git a/plugins/axios.js b/plugins/axios.js index 150eda6..b2ddde1 100644 --- a/plugins/axios.js +++ b/plugins/axios.js @@ -1,28 +1,2 @@ import apiConfig from '~/config/api.config' -export default function ({ app: { $axios, $cookies } }) { - $axios.defaults.baseURL = apiConfig.BASE - $axios.defaults.timeout = 30000 - $axios.interceptors.request.use(config => { - config.headers['X-Token'] = $cookies.get('token') || '' - config.headers['X-Device-Id'] = $cookies.get('clientId') || '' - config.headers['X-Uid'] = $cookies.get('userId') || '' - return config - }) - $axios.interceptors.response.use(response => { - if (/^[4|5]/.test(response.status)) { - return Promise.reject(response.statusText) - } - let message; - if (typeof(response.data.data) !== 'undefined') { - message = response.data.data.message - } else if (typeof(response.data) !== 'undefined') { - message = response.data.message - } - console.log(message); - if (response.data.success) { - return response.data.data - } - return response.data - }) -} diff --git a/store/auth.js b/store/auth.js deleted file mode 100644 index 85d41c6..0000000 --- a/store/auth.js +++ /dev/null @@ -1,83 +0,0 @@ -export const state = () => { - return { - isLogin: false, - token: '', - nickname: '', - idUser: '', - avatarURL: '', - role: 0, // 0-no login, 1-admin, 2-blog admin, 3-blog author, 4-blog user, 5-visitor - login: false - } -} - -export const mutations = { - updateUserInfo(state, data) { - state.isLogin = true; - state.avatarURL = data.avatarUrl; - state.nickname = data.nickname; - state.token = data.token; - state.account = data.account; - state.role = data.weights; - state.idUser = data.idUser; - } - -} -export const actions = { - setLogin(state, data){ - state.login = data - }, - setUserInfo(state, data) { - state.avatarURL = data.avatarUrl; - state.nickname = data.nickname; - }, - initLogin({commit}, data = {}){ - commit('updateUserInfo', data) - }, - logout(state){ - state.isLogin = false; - state.avatarURL = ''; - state.nickname = ''; - state.token = ''; - state.account = ''; - state.role = ''; - state.idUser = ''; - } -} - -export const getters = { - isLogin(state){ - return state.isLogin - }, - hasPermissions:(state)=>(scenes)=>{ - let hasPermissions = false; - if (state.role) { - switch (scenes) { - case 'user': - hasPermissions = state.role < 5; - break; - case 'role': - hasPermissions = state.role < 2; - break; - case 'topic': - hasPermissions = state.role < 3; - break; - case 'tag': - hasPermissions = state.role < 3; - break; - case 'admin': - hasPermissions = state.role < 2; - break; - case 'blog_admin': - hasPermissions = state.role < 3; - break; - default: - hasPermissions = false; - this.commit('logout'); - } - } - return hasPermissions; - }, - isAuthor: (state)=>(scenes)=> { - return state.nickname === scenes ? true : false; - } -} diff --git a/store/index.js b/store/index.js index 459d60d..fce8c3c 100644 --- a/store/index.js +++ b/store/index.js @@ -1,11 +1,34 @@ +const cookieparser = process.server ? require('cookieparser') : undefined + export const state = () => { return { - activeMenu: 'index' + activeMenu: 'index', + oauth: null } } + +export const mutations = { + setAuth (state, auth) { + state.oauth = auth + }, + setActiveMenu (state, activeMenu) { + state.activeMenu = activeMenu + } +} + export const actions = { nuxtServerInit(store, {req}) { // 初始化时的全局任务 + let auth = null + if (req.headers.cookie) { + const parsed = cookieparser.parse(req.headers.cookie) + try { + auth = JSON.parse(parsed.auth) + } catch (err) { + // No valid cookie found + } + store.commit('setAuth', auth) + } const initFetchAppData = [ // 内容数据 store.dispatch('article/fetchList') @@ -13,3 +36,36 @@ export const actions = { return Promise.all(initFetchAppData) } } + +export const getters = { + hasPermissions: (state) => (scenes) => { + let hasPermissions = false; + const role = state.oauth?.role + if (role) { + switch (scenes) { + case 'user': + hasPermissions = role < 5; + break; + case 'role': + hasPermissions = role < 2; + break; + case 'topic': + hasPermissions = role < 3; + break; + case 'tag': + hasPermissions = role < 3; + break; + case 'admin': + hasPermissions = role < 2; + break; + case 'blog_admin': + hasPermissions = role < 3; + break; + default: + hasPermissions = false; + this.commit('logout'); + } + } + return hasPermissions; + } +}