feat: 全局控制登录弹窗,添加全局登录拦截方法

merge-requests/6/head
xiaoguang 3 years ago
parent dfce24e466
commit 440189d305

@ -101,6 +101,19 @@ export {
@include adj(transform, translate3d(-50%, 0, 0)); @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();
``` ```

@ -15,17 +15,21 @@
</template> </template>
<script> <script>
import BsLogin from "@/components/BsLogin.vue"; import BsLogin from "@/components/BsLogin.vue";
import { TOKEN_KEY } from "@/constants";
import Header from "./module/header/index.vue"; import Header from "./module/header/index.vue";
import Footer from "./module/footer/index.vue"; import Footer from "./module/footer/index.vue";
export default { export default {
name: "Layout", name: "Layout",
components: { Header, Footer, BsLogin }, components: { Header, Footer, BsLogin },
data() { computed: {
return { loginVisible: {
loginVisible: true, get() {
}; return this.$store.state.loginVisible;
},
set(val) {
this.$store.commit("setLoginVisible", val);
},
},
}, },
}; };
</script> </script>

@ -42,7 +42,8 @@ export default {
plugins: [ plugins: [
'@/plugins/element-ui', '@/plugins/element-ui',
'@/plugins/axios', '@/plugins/axios',
'@plugins/axiosTk.js' '@plugins/axiosTk.js',
'@plugins/vue-inject.js'
], ],
// Auto import components: https://go.nuxtjs.dev/config-components // Auto import components: https://go.nuxtjs.dev/config-components

@ -17,5 +17,8 @@ import Banner from "./module/Banner.vue";
import Seckil from "./module/Seckill.vue"; import Seckil from "./module/Seckill.vue";
export default { export default {
components: { Banner, Seckil }, components: { Banner, Seckil },
mounted() {
this.$isLoginValidate();
},
}; };
</script> </script>

@ -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];
}

@ -12,6 +12,7 @@ const ONE_DAY = 86400000; // 一天的毫秒数 24 * 60 * 60 * 1000;
const state = () => ({ const state = () => ({
token: "", token: "",
userInfo: {}, userInfo: {},
loginVisible: false, // 是否展示登录弹窗
}); });
const mutations = { const mutations = {
setUserInfo(state, info) { setUserInfo(state, info) {
@ -29,6 +30,9 @@ const mutations = {
state.userInfo = {}; state.userInfo = {};
this.$cookies.remove(TOKEN_KEY); this.$cookies.remove(TOKEN_KEY);
}, },
setLoginVisible(state, visible) {
state.loginVisible = visible;
},
}; };
const actions = { const actions = {
async nuxtServerInit({ state, commit, dispatch }) { async nuxtServerInit({ state, commit, dispatch }) {
@ -44,10 +48,6 @@ const actions = {
commit("setUserInfo", result); commit("setUserInfo", result);
} }
}, },
loginOut({ commit }) {
commit("setLoginOut");
// 此处请求接口
},
}; };
export { state, mutations, actions }; export { state, mutations, actions };

Loading…
Cancel
Save