diff --git a/README.md b/README.md index 7f58f4e..60d30ad 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,19 @@ export { @include adj(transform, translate3d(-50%, 0, 0)); } +``` +## 登录相关 +```javascript +// 访问token +this.$store.state.token +// 设置token +this.$store.commit('setToken') +// 退出登录 +this.$store.commit('setLoginOut') +// 获取登录的用户信息 +this.$store.state.userInfo +// 是否需要登录拦截 +this.$isLoginValidate(); ``` diff --git a/layouts/default.vue b/layouts/default.vue index 2ca99f1..f8f1e74 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -15,17 +15,21 @@ diff --git a/nuxt.config.js b/nuxt.config.js index d33ba6d..5ea82b3 100644 --- a/nuxt.config.js +++ b/nuxt.config.js @@ -42,7 +42,8 @@ export default { plugins: [ '@/plugins/element-ui', '@/plugins/axios', - '@plugins/axiosTk.js' + '@plugins/axiosTk.js', + '@plugins/vue-inject.js' ], // Auto import components: https://go.nuxtjs.dev/config-components diff --git a/pages/index/index.vue b/pages/index/index.vue index 6a4e2d6..2ddace0 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -17,5 +17,8 @@ import Banner from "./module/Banner.vue"; import Seckil from "./module/Seckill.vue"; export default { components: { Banner, Seckil }, + mounted() { + this.$isLoginValidate(); + }, }; diff --git a/plugins/vue-inject.js b/plugins/vue-inject.js new file mode 100644 index 0000000..7d8acb5 --- /dev/null +++ b/plugins/vue-inject.js @@ -0,0 +1,17 @@ +import Vue from "vue"; +import { TOKEN_KEY } from "@/constants"; + +const injectOptions = { + // 是否需要登录拦截 + $isLoginValidate() { + if (this.$cookies.get(TOKEN_KEY)) { + return true; + } + this.$store.commit("setLoginVisible", true); + return false; + }, +}; + +for (let key in injectOptions) { + Vue.prototype[key] = injectOptions[key]; +} diff --git a/store/index.js b/store/index.js index f39eb52..18f385f 100644 --- a/store/index.js +++ b/store/index.js @@ -12,6 +12,7 @@ const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000; const state = () => ({ token: "", userInfo: {}, + loginVisible: false, // 是否展示登录弹窗 }); const mutations = { setUserInfo(state, info) { @@ -29,6 +30,9 @@ const mutations = { state.userInfo = {}; this.$cookies.remove(TOKEN_KEY); }, + setLoginVisible(state, visible) { + state.loginVisible = visible; + }, }; const actions = { async nuxtServerInit({ state, commit, dispatch }) { @@ -44,10 +48,6 @@ const actions = { commit("setUserInfo", result); } }, - loginOut({ commit }) { - commit("setLoginOut"); - // 此处请求接口 - }, }; export { state, mutations, actions };