添加用户登录验证及过期退出登录
This commit is contained in:
parent
f2a2827d02
commit
96e70c21ee
|
|
@ -1,7 +1,9 @@
|
|||
import axios from 'axios'
|
||||
import { MessageBox, Message } from 'element-ui'
|
||||
import store from '@/store'
|
||||
import { getToken } from '@/utils/auth'
|
||||
import {getToken, removeToken} from '@/utils/auth'
|
||||
import router from '@/router' // @表示src目录
|
||||
|
||||
|
||||
// create an axios instance
|
||||
const service = axios.create({
|
||||
|
|
@ -14,12 +16,11 @@ const service = axios.create({
|
|||
service.interceptors.request.use(
|
||||
config => {
|
||||
// do something before request is sent
|
||||
|
||||
if (store.getters.token) {
|
||||
// let each request carry token
|
||||
// ['X-Token'] is a custom headers key
|
||||
// please modify it according to the actual situation
|
||||
config.headers['X-Token'] = getToken()
|
||||
config.headers['Authorization'] = getToken()
|
||||
}
|
||||
return config
|
||||
},
|
||||
|
|
@ -43,19 +44,27 @@ service.interceptors.response.use(
|
|||
* You can also judge the status by HTTP Status Code
|
||||
*/
|
||||
response => {
|
||||
// return response => response
|
||||
|
||||
const res = response.data
|
||||
console.log(res)
|
||||
// if the custom code is not 20000, it is judged as an error.
|
||||
if (res.code == 401) {
|
||||
removeToken();
|
||||
MessageBox.confirm('登录已过期', '退出登录', {
|
||||
confirmButtonText: '重新登录',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
}).then(() => {
|
||||
router.push({ path: '/login' });
|
||||
})
|
||||
return ;
|
||||
}
|
||||
|
||||
if (res.code !== 200) {
|
||||
Message({
|
||||
message: res.message || 'Error',
|
||||
message: res.msg || 'Error',
|
||||
type: 'error',
|
||||
duration: 5 * 1000
|
||||
})
|
||||
|
||||
// 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
|
||||
if (res.code === 50008 || res.code === 50012 || res.code === 50014) {
|
||||
// to re-login
|
||||
MessageBox.confirm('You have been logged out, you can cancel to stay on this page, or log in again', 'Confirm logout', {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
<el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on" label-position="left">
|
||||
|
||||
<div class="title-container">
|
||||
<h3 class="title">登录</h3>
|
||||
<h3 class="title">智慧工地后台管理系统</h3>
|
||||
</div>
|
||||
|
||||
<el-form-item prop="username">
|
||||
|
|
@ -13,7 +13,7 @@
|
|||
<el-input
|
||||
ref="username"
|
||||
v-model="loginForm.username"
|
||||
placeholder="Username"
|
||||
placeholder="请输入用户名"
|
||||
name="username"
|
||||
type="text"
|
||||
tabindex="1"
|
||||
|
|
@ -31,7 +31,7 @@
|
|||
ref="password"
|
||||
v-model="loginForm.password"
|
||||
:type="passwordType"
|
||||
placeholder="Password"
|
||||
placeholder="请输入密码"
|
||||
name="password"
|
||||
tabindex="2"
|
||||
autocomplete="on"
|
||||
|
|
@ -45,22 +45,22 @@
|
|||
</el-form-item>
|
||||
</el-tooltip>
|
||||
|
||||
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">Login</el-button>
|
||||
<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin">登陆</el-button>
|
||||
|
||||
<div style="position:relative">
|
||||
<div class="tips">
|
||||
<span>Username : admin</span>
|
||||
<span>Password : any</span>
|
||||
</div>
|
||||
<div class="tips">
|
||||
<span style="margin-right:18px;">Username : editor</span>
|
||||
<span>Password : any</span>
|
||||
</div>
|
||||
<!-- <div style="position:relative">-->
|
||||
<!-- <div class="tips">-->
|
||||
<!-- <span>Username : admin</span>-->
|
||||
<!-- <span>Password : any</span>-->
|
||||
<!-- </div>-->
|
||||
<!-- <div class="tips">-->
|
||||
<!-- <span style="margin-right:18px;">Username : editor</span>-->
|
||||
<!-- <span>Password : any</span>-->
|
||||
<!-- </div>-->
|
||||
|
||||
<!-- <el-button class="thirdparty-button" type="primary" @click="showDialog=true">
|
||||
Or connect with
|
||||
</el-button> -->
|
||||
</div>
|
||||
<!-- </div>-->
|
||||
</el-form>
|
||||
|
||||
<el-dialog title="Or connect with" :visible.sync="showDialog">
|
||||
|
|
@ -83,26 +83,26 @@ export default {
|
|||
data() {
|
||||
const validateUsername = (rule, value, callback) => {
|
||||
if (!validUsername(value)) {
|
||||
callback(new Error('Please enter the correct user name'))
|
||||
callback(new Error('请输入正确的用户名'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
const validatePassword = (rule, value, callback) => {
|
||||
if (value.length < 6) {
|
||||
callback(new Error('The password can not be less than 6 digits'))
|
||||
callback(new Error('密码不能小于6位'))
|
||||
} else {
|
||||
callback()
|
||||
}
|
||||
}
|
||||
return {
|
||||
loginForm: {
|
||||
username: 'admin',
|
||||
password: '111111'
|
||||
username: 'guest',
|
||||
password: 'admin@123'
|
||||
},
|
||||
loginRules: {
|
||||
username: [{ required: true, trigger: 'blur', validator: validateUsername }],
|
||||
password: [{ required: true, trigger: 'blur', validator: validatePassword }]
|
||||
username: [{ required: true, trigger: 'blur', }],
|
||||
password: [{ required: true, trigger: 'blur',}]
|
||||
},
|
||||
passwordType: 'password',
|
||||
capsTooltip: false,
|
||||
|
|
|
|||
Loading…
Reference in New Issue