From 440189d305f8387774edc776fba297a5f5be203c Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Mon, 9 May 2022 17:44:36 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E5=85=A8=E5=B1=80=E6=8E=A7?= =?UTF-8?q?=E5=88=B6=E7=99=BB=E5=BD=95=E5=BC=B9=E7=AA=97=EF=BC=8C=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=85=A8=E5=B1=80=E7=99=BB=E5=BD=95=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 13 +++++++++++++ layouts/default.vue | 14 +++++++++----- nuxt.config.js | 3 ++- pages/index/index.vue | 3 +++ plugins/vue-inject.js | 17 +++++++++++++++++ store/index.js | 8 ++++---- 6 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 plugins/vue-inject.js 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 }; From 00788dbd5c37fc69777c48aea2752af56034e0d7 Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Mon, 9 May 2022 18:49:41 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E9=A6=96=E9=A1=B5=E7=99=BB?= =?UTF-8?q?=E5=BD=95=E6=B3=A8=E9=94=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++-- components/BsLogin.vue | 6 +++- layouts/module/header/HeaderInfoBar.vue | 40 ++++++++++++++++++------- layouts/module/header/index.vue | 9 +++--- pages/index/index.vue | 3 -- 5 files changed, 49 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 60d30ad..a55ca49 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,15 @@ this.$store.commit('setToken') this.$store.commit('setLoginOut') // 获取登录的用户信息 this.$store.state.userInfo -// 是否需要登录拦截 -this.$isLoginValidate(); + +// 登录拦截 +// 示例:点击购买课程前需要判断当前用户是否登录 +function onPurchaseCourse() { + if (!this.$isLoginValidate()) { + return; + } + // 此处省略其他业务代码... +} + ``` diff --git a/components/BsLogin.vue b/components/BsLogin.vue index 18896fc..04f2469 100644 --- a/components/BsLogin.vue +++ b/components/BsLogin.vue @@ -147,7 +147,11 @@ export default { Message.error("请勾选同意《用户协议》和《隐私协议》"); return; } - const { result } = await ApiPostLogin({ ...this.form }); + const { result } = await ApiPostLogin({ + ...this.form, + clientId: 1, + systemId: 1, + }); if (result) { this.dialogTableVisible = false; this.$store.commit("setToken", result.token); diff --git a/layouts/module/header/HeaderInfoBar.vue b/layouts/module/header/HeaderInfoBar.vue index a62300b..aef98a2 100644 --- a/layouts/module/header/HeaderInfoBar.vue +++ b/layouts/module/header/HeaderInfoBar.vue @@ -6,33 +6,42 @@
diff --git a/layouts/module/header/index.vue b/layouts/module/header/index.vue index f4a15e8..fe91d01 100644 --- a/layouts/module/header/index.vue +++ b/layouts/module/header/index.vue @@ -81,7 +81,7 @@
@@ -178,7 +178,7 @@ export default { if (resultGoods && resultGoods.length > 0) { return { ...item, - list: resultGoods.data, + list: resultGoods, }; } }) @@ -291,6 +291,7 @@ export default { background: linear-gradient(270deg, #ffa25a 0%, #ff7f39 100%); border-radius: 0px 8px 8px 0px; z-index: 2; + cursor: pointer; img { width: 26px; height: 26px; @@ -373,13 +374,11 @@ export default { box-shadow: 7px 0px 10px 1px rgba(0, 0, 0, 0.10000000149011612); border: 1px solid #eeeeee; background: #ffffff; - .menu-right__item:last-child { - margin-bottom: 0; - } .menu-right__item:hover { color: #ff875b; } .menu-right__item { + display: inline-block; font-size: 12px; color: #999999; margin-right: 20px; diff --git a/pages/index/index.vue b/pages/index/index.vue index 2ddace0..6a4e2d6 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -17,8 +17,5 @@ import Banner from "./module/Banner.vue"; import Seckil from "./module/Seckill.vue"; export default { components: { Banner, Seckil }, - mounted() { - this.$isLoginValidate(); - }, }; From 45eb27529bdf08c4df9102268efde410a151ec64 Mon Sep 17 00:00:00 2001 From: xiaoguang Date: Mon, 9 May 2022 22:12:50 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=20=E7=83=AD=E9=97=A8=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=B7=BB=E5=8A=A0=E6=98=BE=E7=A4=BA=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0=E9=A1=B5=E9=9D=A2=E6=BB=9A=E5=8A=A8?= =?UTF-8?q?=E4=BA=8B=E4=BB=B6=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- layouts/default.vue | 42 +++++++- layouts/module/header/HeaderInfoBar.vue | 4 +- layouts/module/header/index.vue | 122 +++++++++++++++++------- 3 files changed, 129 insertions(+), 39 deletions(-) diff --git a/layouts/default.vue b/layouts/default.vue index f8f1e74..2d08f98 100644 --- a/layouts/default.vue +++ b/layouts/default.vue @@ -8,7 +8,11 @@