注销用户

pull/1/head
pnoker 4 years ago
parent a3ee3e2c4f
commit 23612c7592

@ -22,3 +22,11 @@ export const checkTokenValid = (user, token) => request({
token token
} }
}); });
export const cancelToken = (name) => request({
url: '/user_api/auth/token/cancel',
method: 'get',
params: {
username: name
}
});

@ -3,7 +3,7 @@ import router from '@/router/router'
import store from '@/store/store'; import store from '@/store/store';
import NProgress from 'nprogress' import NProgress from 'nprogress'
import 'nprogress/nprogress.css' import 'nprogress/nprogress.css'
import {getToken} from '@/util/auth' import {getTokenCookies, getUserCookies} from '@/util/auth'
import {showError} from '@/util/util' import {showError} from '@/util/util'
//返回其他状态码 //返回其他状态码
@ -22,7 +22,8 @@ NProgress.configure({
axios.interceptors.request.use(config => { axios.interceptors.request.use(config => {
NProgress.start(); NProgress.start();
config.headers['Content-Type'] = 'application/json'; config.headers['Content-Type'] = 'application/json';
config.headers['Auth-Token'] = getToken(); config.headers['Auth-User'] = getUserCookies();
config.headers['Auth-Token'] = getTokenCookies();
return config; return config;
}, error => { }, error => {
return Promise.reject(error) return Promise.reject(error)

@ -2,7 +2,8 @@ export default [
{ {
path: '*', path: '*',
redirect: '/404' redirect: '/404'
}, { },
{
name: 'login', name: 'login',
path: '/login', path: '/login',
meta: { meta: {
@ -10,14 +11,16 @@ export default [
title: 'DC3 Web UI Login' title: 'DC3 Web UI Login'
}, },
component: () => import('@/views/login/Login') component: () => import('@/views/login/Login')
}, { },
{
name: 'index', name: 'index',
path: '/', path: '/',
redirect: '/home', redirect: '/home',
meta: { meta: {
isAuth: true isAuth: true
} }
}, { },
{
name: '404', name: '404',
path: '/404', path: '/404',
meta: { meta: {
@ -26,7 +29,8 @@ export default [
}, },
component: () => import('@/components/error/404') component: () => import('@/components/error/404')
}, { },
{
name: '403', name: '403',
path: '/403', path: '/403',
meta: { meta: {
@ -34,7 +38,8 @@ export default [
title: '403' title: '403'
}, },
component: () => import('@/components/error/403') component: () => import('@/components/error/403')
}, { },
{
name: '500', name: '500',
path: '/500', path: '/500',
meta: { meta: {

@ -3,7 +3,7 @@ import VueRouter from 'vue-router'
import CommonRouter from './common/index' import CommonRouter from './common/index'
import ViewsRouter from './views/index' import ViewsRouter from './views/index'
import NProgress from "nprogress"; import NProgress from "nprogress";
import {getToken, getUser} from "@/util/auth"; import {getTokenCookies, getUserCookies} from "@/util/auth";
import store from "@/store/store"; import store from "@/store/store";
import {checkTokenValid} from "@/api/user"; import {checkTokenValid} from "@/api/user";
@ -35,13 +35,13 @@ router.beforeEach((to, from, next) => {
if (meta.isAuth !== true) { if (meta.isAuth !== true) {
next(); next();
} else { } else {
const token = getToken(), user = getUser(); const token = getTokenCookies(), user = getUserCookies();
if ('' === token || undefined === token || '' === user || undefined === user) { if ('' === token || undefined === token || '' === user || undefined === user) {
next({path: '/login'}); next({path: '/login'});
return; return;
} }
checkTokenValid(user, token).then(res => { checkTokenValid(user, token).then(res => {
if (res.ok && store.getters.token.length > 0) { if (res.ok) {
next(); next();
} else { } else {
throw new Error(res.message); throw new Error(res.message);

@ -14,7 +14,8 @@ export default [
title: '首页' title: '首页'
}, },
component: () => import('@/views/home/Home') component: () => import('@/views/home/Home')
}, { },
{
name: 'driver', name: 'driver',
path: '/driver', path: '/driver',
meta: { meta: {
@ -22,7 +23,8 @@ export default [
title: '驱动' title: '驱动'
}, },
component: () => import('@/views/driver/Driver') component: () => import('@/views/driver/Driver')
}, { },
{
name: 'driverAttribute', name: 'driverAttribute',
path: '/driverAttribute', path: '/driverAttribute',
meta: { meta: {
@ -30,7 +32,8 @@ export default [
title: '驱动属性' title: '驱动属性'
}, },
component: () => import('@/views/driver/DriverAttribute') component: () => import('@/views/driver/DriverAttribute')
}, { },
{
name: 'pointAttribute', name: 'pointAttribute',
path: '/pointAttribute', path: '/pointAttribute',
meta: { meta: {
@ -38,7 +41,8 @@ export default [
title: '位号属性' title: '位号属性'
}, },
component: () => import('@/views/point/PointAttribute') component: () => import('@/views/point/PointAttribute')
}, { },
{
name: 'profile', name: 'profile',
path: '/profile', path: '/profile',
meta: { meta: {
@ -46,7 +50,8 @@ export default [
title: '模版' title: '模版'
}, },
component: () => import('@/views/profile/Profile') component: () => import('@/views/profile/Profile')
}, { },
{
name: 'driverInfo', name: 'driverInfo',
path: '/driverInfo', path: '/driverInfo',
meta: { meta: {
@ -54,7 +59,8 @@ export default [
title: '驱动配置' title: '驱动配置'
}, },
component: () => import('@/views/driver/DriverInfo') component: () => import('@/views/driver/DriverInfo')
}, { },
{
name: 'point', name: 'point',
path: '/point', path: '/point',
meta: { meta: {
@ -62,7 +68,8 @@ export default [
title: '位号' title: '位号'
}, },
component: () => import('@/views/point/Point') component: () => import('@/views/point/Point')
}, { },
{
name: 'group', name: 'group',
path: '/group', path: '/group',
meta: { meta: {
@ -70,7 +77,8 @@ export default [
title: '分组' title: '分组'
}, },
component: () => import('@/views/group/Group') component: () => import('@/views/group/Group')
}, { },
{
name: 'device', name: 'device',
path: '/device', path: '/device',
meta: { meta: {
@ -78,7 +86,8 @@ export default [
title: '设备' title: '设备'
}, },
component: () => import('@/views/device/Device') component: () => import('@/views/device/Device')
}, { },
{
name: 'pointInfo', name: 'pointInfo',
path: '/pointInfo', path: '/pointInfo',
meta: { meta: {
@ -86,7 +95,8 @@ export default [
title: '位号配置' title: '位号配置'
}, },
component: () => import('@/views/point/PointInfo') component: () => import('@/views/point/PointInfo')
}, { },
{
name: 'pointValue', name: 'pointValue',
path: '/pointValue', path: '/pointValue',
meta: { meta: {

@ -1,5 +1,6 @@
const getters = { const getters = {
token: state => state.user.token, token: state => state.user.token,
user: state => state.user.user,
}; };
export default getters export default getters

@ -1,12 +1,12 @@
import md5 from 'js-md5'; import md5 from 'js-md5';
import {getStore, removeStore, setStore} from '@/util/store' import {getStore, removeStore, setStore} from '@/util/store'
import {removeToken, removeUser, setToken, setUser} from '@/util/auth' import {removeTokenCookies, removeUserCookies, setTokenCookies, setUserCookies} from '@/util/auth'
import {generateSalt, generateToken} from '@/api/user' import {cancelToken, generateSalt, generateToken} from '@/api/user'
const user = { const user = {
state: { state: {
user: getStore({name: 'user'}) || [],
token: getStore({name: 'token'}) || '', token: getStore({name: 'token'}) || '',
userInfo: getStore({name: 'userInfo'}) || [],
}, },
actions: { actions: {
GenerateSalt(_, name) { GenerateSalt(_, name) {
@ -19,43 +19,53 @@ const user = {
}) })
}, },
GenerateToken({commit}, form) { GenerateToken({commit}, form) {
form.user.password = md5(md5(form.user.password) + form.salt); let user = {
name: form.user.name,
password: md5(md5(form.user.password) + form.salt)
};
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
generateToken(form.user).then(res => { generateToken(user).then(res => {
const data = res.data; const data = res.data;
commit('SET_USER', form.user); commit('SET_USER', user.name);
commit('SET_TOKEN', data); commit('SET_TOKEN', data);
resolve(); resolve();
}).catch(error => { }).catch(error => {
reject(error); reject(error);
}) });
}) })
}, },
ClearToken({commit}) { ClearToken({commit}, name) {
commit('REMOVE_TOKEN'); return new Promise((resolve, reject) => {
commit('REMOVE_USER'); cancelToken(name).then((res) => {
commit('REMOVE_TOKEN');
commit('REMOVE_USER');
resolve(res);
}).catch(error => {
reject(error);
});
})
} }
}, },
mutations: { mutations: {
SET_USER: (state, user) => {
setUserCookies(user);
state.user = user;
setStore({name: 'user', content: state.user, type: 'session'})
},
REMOVE_USER: (state) => {
removeUserCookies();
state.user = '';
removeStore({name: 'user', type: 'session'});
},
SET_TOKEN: (state, token) => { SET_TOKEN: (state, token) => {
setToken(token); setTokenCookies(token);
state.token = token; state.token = token;
setStore({name: 'token', content: state.token, type: 'session'}) setStore({name: 'token', content: state.token, type: 'session'})
}, },
SET_USER: (state, userInfo) => {
setUser(userInfo.name);
state.userInfo = userInfo;
setStore({name: 'userInfo', content: state.userInfo})
},
REMOVE_TOKEN: (state) => { REMOVE_TOKEN: (state) => {
removeToken(); removeTokenCookies();
state.token = ''; state.token = '';
removeStore({name: 'token', type: 'session'}); removeStore({name: 'token', type: 'session'});
},
REMOVE_USER: (state) => {
removeUser();
state.userInfo = '';
removeStore({name: 'userInfo'});
} }
} }
}; };

@ -6,26 +6,26 @@ const TokenKey = 'x-access-token';
// 120分钟120 * 60 * 1000 // 120分钟120 * 60 * 1000
const inFifteenMinutes = new Date(new Date().getTime() + 120 * 60 * 1000); const inFifteenMinutes = new Date(new Date().getTime() + 120 * 60 * 1000);
export function getUser() { export function getUserCookies() {
return Cookies.get(UserKey); return Cookies.get(UserKey);
} }
export function setUser(user) { export function setUserCookies(user) {
return Cookies.set(UserKey, user, {expires: inFifteenMinutes}); return Cookies.set(UserKey, user, {expires: inFifteenMinutes});
} }
export function removeUser() { export function removeUserCookies() {
return Cookies.remove(UserKey); return Cookies.remove(UserKey);
} }
export function getToken() { export function getTokenCookies() {
return Cookies.get(TokenKey); return Cookies.get(TokenKey);
} }
export function setToken(token) { export function setTokenCookies(token) {
return Cookies.set(TokenKey, token, {expires: inFifteenMinutes}); return Cookies.set(TokenKey, token, {expires: inFifteenMinutes});
} }
export function removeToken() { export function removeTokenCookies() {
return Cookies.remove(TokenKey); return Cookies.remove(TokenKey);
} }

@ -58,6 +58,7 @@
</template> </template>
<script> <script>
export default { export default {
data() { data() {
return {}; return {};
@ -70,8 +71,11 @@
}, },
handleCommand(command) { handleCommand(command) {
if (command === 'logout') { if (command === 'logout') {
this.$store.dispatch('ClearToken').then(() => { let name = this.$store.getters.user;
this.$router.push('/login'); this.$store.dispatch('ClearToken', name).then((res) => {
if (res.ok) {
this.$router.push('/login');
}
}); });
} else { } else {
this.$message('click on item ' + command); this.$message('click on item ' + command);

@ -84,20 +84,19 @@
handleLogin() { handleLogin() {
this.$refs.loginForm.validate(valid => { this.$refs.loginForm.validate(valid => {
if (valid) { if (valid) {
const loading = this.$loading({ let loading = this.$loading({
lock: true, lock: true,
text: '登录中,请稍后。。。', text: '登录中,请稍后。。。',
spinner: "el-icon-loading" spinner: "el-icon-loading"
}); });
this.$store.dispatch("GenerateSalt", this.loginForm.name).then((salt) => { this.$store.dispatch("GenerateSalt", this.loginForm.name).then((salt) => {
this.$store.dispatch("GenerateToken", {salt, user:this.loginForm}).then(() => { this.$store.dispatch("GenerateToken", {salt, user: this.loginForm}).then(() => {
this.$router.push({path: '/'}); this.$router.push({path: '/'}).then(() => loading.close());
setTimeout(() => loading.close(), 500);
}).catch(() => { }).catch(() => {
loading.close() loading.close();
}); });
}).catch(() => { }).catch(() => {
loading.close() loading.close();
}); });
} }
}); });

Loading…
Cancel
Save