在线商城项目16-头部前端逻辑修改
2018-04-25 本文已影响47人
love丁酥酥
简介
头部右侧之前有三个按键如下:
这是不合理的,我们应该根据登录态来做一个区别显示。未登录情况下显示login和购物车图标。已登录情况下显示用户名,logout,购物车图标。
本节有如下主要任务:
- 将用户信息作为全局状态进行管理
- 处理登入时的头部前端逻辑
- 处理登出头时的部前端逻辑
1. 将用户信息作为全局状态进行管理
头部是公共组件,我们要求在所有引入头部组件页面中,都能根据用户态区别显示。那么我们需要将用户信息作为全局状态进行管理.
step1 引入vue的全局状态管理器vuex。
npm install vuex --save
step2 新建store目录如下:
user.js
const user = {
state: {
isLogin: false,
userName: ''
},
mutations: {
LOG_IN: (state, payload) => {
state.isLogin = true
state.userName = payload.userName
},
LOG_OUT: (state, payload) => {
state.isLogin = false
state.userName = ''
}
}
}
export default user
index.js
import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
Vue.use(Vuex)
const store = new Vuex.Store({
modules: {
user
}
})
export default store
step3 在main.js中引入store
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import VueLazyLoad from 'vue-lazyload'
import './assets/css/base.css'
import './assets/css/login.css'
import './assets/css/product.css'
Vue.config.productionTip = false
Vue.use(VueLazyLoad, {
loading: '../static/loading/loading-bars.svg',
error: '../static/ok-2.png'
})
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
store,
components: { App },
template: '<App/>'
})
2. 处理登入时的头部前端逻辑
step1 修改PageHeader.vue逻辑如下
<template>
<header class="header">
<div class="navbar">
<div class="navbar-left-container">
<a href="/">
<img class="navbar-brand-logo" src="../assets/logo.png"></a>
</div>
<div class="navbar-right-container" style="display: flex;">
<div class="navbar-menu-container">
<!--<a href="/" class="navbar-link">我的账户</a>-->
<span class="navbar-link">{{user.userName}}</span>
<a href="javascript:void(0)" class="navbar-link" v-if="!user.isLogin" @click="toPageLogin">Login</a>
<a href="javascript:void(0)" class="navbar-link" v-else>Logout</a>
<div class="navbar-cart-container">
<span class="navbar-cart-count"></span>
<a class="navbar-link navbar-cart-link" href="/#/cart">
<svg class="navbar-cart-logo">
<symbol id="icon-cart" viewBox="0 0 38 32">
<title>cart</title>
<path class="path1"
d="M37.759 0h-4.133c-0.733 0.004-1.337 0.549-1.434 1.255l-0.546 4.342c-0.081 0.484-0.496 0.849-0.997 0.849-0.005 0-0.009-0-0.014-0h-27.604c-0.003 0-0.007-0-0.011-0-1.674 0-3.031 1.357-3.031 3.031 0 0.34 0.056 0.666 0.159 0.971l2.52 8.062c0.385 1.194 1.486 2.043 2.785 2.043 0.126 0 0.25-0.008 0.372-0.023l22.983 0.002c0.515 0.131 0.626 0.768 0.626 1.283 0.005 0.044 0.009 0.095 0.009 0.146 0 0.501-0.294 0.933-0.718 1.134l-22.439 0.003c-0.354 0-0.642 0.287-0.642 0.642s0.287 0.642 0.642 0.642h22.745l0.131-0.071c0.919-0.392 1.551-1.287 1.551-2.33 0-0.058-0.002-0.116-0.006-0.173 0.021-0.108 0.033-0.24 0.033-0.376 0-1.072-0.732-1.973-1.724-2.23l-23.357-0.004c-0.063 0.008-0.135 0.013-0.209 0.013-0.719 0-1.332-0.455-1.566-1.093l-2.53-8.095c-0.048-0.154-0.076-0.332-0.076-0.515 0-0.973 0.782-1.764 1.752-1.778h27.657c1.159-0.004 2.112-0.883 2.232-2.011l0.547-4.345c0.010-0.083 0.078-0.147 0.161-0.152l4.133-0c0.354 0 0.642-0.287 0.642-0.642s-0.287-0.642-0.642-0.642z"></path>
<path class="path2"
d="M31.323 9.69c-0.022-0.003-0.048-0.004-0.074-0.004-0.328 0-0.598 0.248-0.633 0.567l-0.809 7.268c-0.003 0.022-0.004 0.048-0.004 0.074 0 0.328 0.248 0.598 0.567 0.633l0.074 0c0.001 0 0.003 0 0.004 0 0.327 0 0.596-0.246 0.632-0.563l0.809-7.268c0.003-0.022 0.004-0.048 0.004-0.074 0-0.328-0.248-0.598-0.567-0.633z"></path>
<path class="path3"
d="M27.514 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM27.514 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
<path class="path4"
d="M9.599 25.594c-1.769 0-3.203 1.434-3.203 3.203s1.434 3.203 3.203 3.203c1.769 0 3.203-1.434 3.203-3.203s-1.434-3.203-3.203-3.203zM9.599 30.717c-1.060 0-1.92-0.86-1.92-1.92s0.86-1.92 1.92-1.92c1.060 0 1.92 0.86 1.92 1.92s-0.86 1.92-1.92 1.92z"></path>
</symbol>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-cart"></use>
</svg>
</a>
</div>
</div>
</div>
</div>
</header>
</template>
<script>
import { mapState } from 'vuex'
export default {
computed: {
...mapState([
'user'
])
},
methods: {
// 跳转登录页
toPageLogin () {
this.$router.push({ path: '/login', query: { redirect: this.$route.fullPath } })
}
}
}
</script>
step3 修改Login.vue
改动如下逻辑:
axios.post('/api/users/login', queryObj).then(res => {
let data = (res && res.data) || {}
if (data.code === '000') {
let result = data.result || {}
this.$store.commit('LOG_IN', {userName: result.userName})
if (this.$route.query.redirect) {
this.$router.push(this.$route.query.redirect)
} else {
this.$router.push('/')
}
} else {
alert(`err:${data.msg || '系统错误'}`)
}
})
此时尝试运行,能正常跳转到登录页,但是输入第一遍用户名和密码时,并没有跳转,第二遍才跳转。因为这里button在form内,默认会执行submit。可以简单地在该button中添加'type= button',也可以将button挪到form外,不过此时userName和userPwd为空不再有提示,需要我们补充提示逻辑。重新修改Login.vue如下:
<template>
<div class="login">
<div class="logo">
<h1>weleome to six-tao</h1>
</div>
<form action="">
<div class="item">
<label for="userName">账号:</label>
<input type="text" placeholder="Username" v-model="userName" id="userName"/>
</div>
<div class="item">
<label for="userPwd">密码:</label>
<input type="userPwd" placeholder="userPwd" v-model="userPwd" id="userPwd"/>
</div>
<div class="item err-tip" v-show="errTip">
<label>提示:</label>
<span>{{errTip}}</span>
</div>
</form>
<div class="btn-wraper">
<button id="loginButton" @click="login">登录</button>
</div>
</div>
</template>
<script>
import axios from 'axios'
export default {
data () {
return {
userName: '',
userPwd: '',
errTip: ''
}
},
methods: {
login () {
if (!this.userName || !this.userPwd) {
this.errTip = '用户名和密码不能为空'
return
}
this.errTip = ''
let queryObj = {
userName: this.userName,
userPwd: this.userPwd
}
axios.post('/api/users/login', queryObj).then(res => {
let data = (res && res.data) || {}
if (data.code === '000') {
let result = data.result || {}
this.$store.commit('LOG_IN', {userName: result.userName})
if (this.$route.query.redirect) {
this.$router.push(this.$route.query.redirect)
} else {
this.$router.push('/')
}
} else {
alert(`err:${data.msg || '系统错误'}`)
}
})
}
}
}
</script>
<style scoped>
.login {
font-size: 12px;
position: fixed;
width: 300px;
height: 240px;
background-color: lightcyan;
border-radius: 10px;
padding: 10px 20px 10px 10px;
margin-top: -120px;
margin-left: -150px;
top: 50%;
left: 50%;
}
.item {
display: flex;
align-items: center;
margin-top: 20px
}
.item label {
width: 60px;
font-size: 1.2em;
}
.item input {
flex: 1;
height: 40px;
line-height: 40px;
}
.btn-wraper {
display: flex;
justify-content: center;
}
.err-tip {
margin-top: 5px;
color: red;
}
#loginButton {
width: 100px;
color: #85592e;
margin-left: auto;
margin-right: auto;
margin-top: 30px;
background: yellow;
border-radius: 5px;
border: 1px solid gray;
font-size: 1.2em;
}
.logo {
text-align: center;
}
</style>
此时,我们再来运行一遍:
3. 处理登出时的头部前端逻辑
修改PageHeader.vue以下两处:
<a href="javascript:void(0)" class="navbar-link" v-else @click="logout">Logout</a>
logout () {
this.$store.commit('LOG_OUT')
}
总结
这一节我们从前端角度完成了登入登出的头部前端逻辑。但是仍然还有一些问题,下一节我们继续来完善。
提交一下代码:
six-tao
git status
git diff
git add .
git status
git commit -am 'head logic changes to fit login'
git push