登录页面
login.vue
// login/login.vue
<template>
<div class="login-container">
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
<div class="title-container">
<h3 class="title">Login Form</h3>
</div>
<el-form-item prop="username">
<span class="svg-container">
<svg-icon icon-class="user" />
</span>
<el-input
ref="username"
v-model="loginForm.username"
placeholder="Username"
name="username"
type="text"
tabindex="1"
auto-complete="on"
/>
</el-form-item>
<el-form-item prop="password">
<span class="svg-container">
<svg-icon icon-class="password" />
</span>
<el-input
:key="passwordType"
ref="password"
v-model="loginForm.password"
:type="passwordType"
placeholder="Password"
name="password"
tabindex="2"
auto-complete="on"
@keyup.enter.native="handleLogin"
/>
<span class="show-pwd" @click="showPwd">
<svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
</span>
</el-form-item>
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
<div class="tips">
<span style="margin-right:20px;">username: admin</span>
<span> password: any</span>
</div>
</el-form>
</div>
</template>
<script>
import { validUsername } from '@/utils/validate'
export default {
name: 'Login',
data() {
const validateUsername = (rule, value, callback) => {
if (!validUsername(value)) {
callback(new Error('Please enter the correct user name'))
} else {
callback()
}
}
const validatePassword = (rule, value, callback) => {
if (value.length < 6) {
callback(new Error('The password can not be less than 6 digits'))
} else {
callback()
}
}
return {
loginForm: {
username: 'admin',
password: '111111'
},
loginRules: {
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
},
loading: false,
passwordType: 'password',
redirect: undefined
}
},
watch: {
// 侦听路由对象的变化(页面跳转到这里, 就会触发下面handler函数)
$route: {
handler: function(route) {
// 看看上一个页面是否传递了redirect过来 (登陆返回未遂地址)
this.redirect = route.query && route.query.redirect
},
immediate: true
}
},
methods: {
showPwd() {
if (this.passwordType === 'password') {
this.passwordType = ''
} else {
this.passwordType = 'password'
}
this.$nextTick(() => {
this.$refs.password.focus()
})
},
handleLogin() {
this.$refs.loginForm.validate(valid => {
// console.log(valid)
if (valid) {
this.loading = true
//第二种:使用mapActions辅助函数
//import { mapActions } from 'vuex'
//...mapActions('user', ['loginActions']),
//const res = await this.loginActions(this.loginForm)
this.$store.dispatch('user/loginActions', this.loginForm).then(() => {
this.$router.push({ path: this.redirect || '/' })
this.loading = false
}).catch(() => {
this.loading = false
})
} else {
console.log('error submit!!')
return false
}
})
}
}
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */
$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
.login-container .el-input input {
color: $cursor;
}
}
/* reset element-ui css */
.login-container {
.el-input {
display: inline-block;
height: 47px;
width: 85%;
input {
background: transparent;
border: 0px;
-webkit-appearance: none;
border-radius: 0px;
padding: 12px 5px 12px 15px;
color: $light_gray;
height: 47px;
caret-color: $cursor;
&:-webkit-autofill {
box-shadow: 0 0 0px 1000px $bg inset !important;
-webkit-text-fill-color: $cursor !important;
}
}
}
.el-form-item {
border: 1px solid rgba(255, 255, 255, 0.1);
background: rgba(0, 0, 0, 0.1);
border-radius: 5px;
color: #454545;
}
}
</style>
<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;
.login-container {
min-height: 100%;
width: 100%;
background-color: $bg;
overflow: hidden;
.login-form {
position: relative;
width: 520px;
max-width: 100%;
padding: 160px 35px 0;
margin: 0 auto;
overflow: hidden;
}
.tips {
font-size: 14px;
color: #fff;
margin-bottom: 10px;
span {
&:first-of-type {
margin-right: 16px;
}
}
}
.svg-container {
padding: 6px 5px 6px 15px;
color: $dark_gray;
vertical-align: middle;
width: 30px;
display: inline-block;
}
.title-container {
position: relative;
.title {
font-size: 26px;
color: $light_gray;
margin: 0px auto 40px auto;
text-align: center;
font-weight: bold;
}
}
.show-pwd {
position: absolute;
right: 10px;
top: 7px;
font-size: 16px;
color: $dark_gray;
cursor: pointer;
user-select: none;
}
}
</style>
store
token处理
//store/modules/user.js
import { getToken, setToken, removeToken } from '@/utils/auth'
import { loginAPI, getUserProfileAPI, getUserPhotoAPI } from '@/api'
const getDefaultState = () => {
return {
token: getToken(), // 用户 Token,默认为 ''
userInfo: {}
}
}
const state = getDefaultState()
//const state = () => ({
// token: '' // 用户 Token,默认为 ''
//})
//const state = {
// token: getToken() // 设置token初始状态
//}
const mutations = {
RESET_STATE: (state) => {
Object.assign(state, getDefaultState())
},
// 设置token
SET_TOKEN(state, newToken) {
state.token = newToken
setToken(newToken)
},
// 删除token
REMOVE_TOKEN(state) {
state.token = ''
removeToken()
},
SET_USER(state, value) {
state.userInfo = value
},
// 删除用户信息
REMOVE_USER(state) {
state.userInfo = {}
}
}
const actions = {
// 登录逻辑-封装
async loginActions({ commit }, value) {
try {
const res = await loginAPI(value)
// 我们只需要token, 保存到上面的vuex中
commit('SET_TOKEN', res.data)
// 逻辑页面还在接收数组, 外面写成功/失败的逻辑, 所以这里要把数组返回出去
// return到actions调用的地方(login/index.vue)
return res
} catch (err) {
return Promise.reject(err)
}
},
// 获取用户-信息
async getUserInfoActions({ commit }) {
const { data: userObj } = await getUserProfileAPI() // 获取用户基本资料对象
const { data: photoObj } = await getUserPhotoAPI(userObj.userId) // 获取用户头像等
const newObj = { ...userObj, ...photoObj } // 合并一个信息非常全的对象
commit('SET_USER', newObj) // 保存到vuex的userInfo对象上 -> 一会儿用调试工具查看
},
// 退出登录
async logOutActions({ commit }) {
commit('REMOVE_TOKEN')
commit('RESET_STATE')
}
}
export default {
namespaced: true,
state,
mutations,
actions
}
API
import request from '@/utils/request'
/**
* 用户 - 登录
* @param {*} 手机号+密码的对象
* @returns
*/
export function loginAPI(data) {
return request({
url: '/sys/login',
method: 'post',
//headers:{}//axios默认会携带请求头Content-Type:'application/json',请求体里的参数名和值会变成JSON字符串格式给后台
data
})
}
axios
//requset.js
import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
import router from '@/router'
const service = axios.create({
baseURL: process.env.VUE_APP_BASE_API,
timeout: 5000
})
service.interceptors.request.use(
config => {
// 知识点: js文件中能否使用this.$store?
// 不能, 因为这个this关键字不是Vue组件对象, 无法查找原型链上$store
// 但是this.$store为了拿到的是store/index.js导出store对象
// 解决: 我们直接把store对象导入过来使用, 是同一个store对象
const token = store.getters.token
if (token) {
config.headers.Authorization = `Bearer ${token}`
}
return config
},
error => {
return Promise.reject(error)
}
)
service.interceptors.response.use(
response => {
const { success, message } = response.data
if (success) {
return response.data
} else {
Message.error(message) // http状态码2xx, 但是逻辑错误
return Promise.reject(new Error(message)) // 返回Promise错误的对象, 等同reject() -> 自己根据success字段判断逻辑错误(账号密码错误)
}
},
error => {
Message.error(error.response.data.message)
if (error.response && error.response.data && error.response.data.code === 10002) {
store.commit('user/REMOVE_TOKEN')
store.commit('user/RESET_STATE')
router.replace(`/login?redirect=${encodeURIComponent(router.currentRoute.fullPath)}`)
}
return Promise.reject(error)
}
)
export default service
token持久化
//utils/auth.js
import Cookies from 'js-cookie'
const TokenKey = 'hrsaas-ihrm-token' // 设定一个独一无二的key
export function getToken() {
return Cookies.get(TokenKey)
}
export function setToken(token) {
return Cookies.set(TokenKey, token)
}
export function removeToken() {
return Cookies.remove(TokenKey)
}