登录逻辑调整

pull/1/head
pnoker 4 years ago
parent f84de5f290
commit a3ee3e2c4f

@ -0,0 +1,3 @@
node_modules
public
src

2
.gitignore vendored

@ -8,6 +8,8 @@ node_modules
# local env files # local env files
.env.local .env.local
.env.*.local .env.*.local
package-lock.json
yarn.lock
# Log files # Log files
npm-debug.log* npm-debug.log*

@ -20,6 +20,7 @@
"insert-css": "^2.0.0", "insert-css": "^2.0.0",
"js-base64": "latest", "js-base64": "latest",
"js-cookie": "latest", "js-cookie": "latest",
"js-md5": "^0.7.3",
"nprogress": "latest", "nprogress": "latest",
"vue": "^2.6.11", "vue": "^2.6.11",
"vue-axios": "^2.1.5", "vue-axios": "^2.1.5",

@ -1,12 +1,24 @@
import request from '@/config/axios' import request from '@/config/axios'
export const generateSalt = (name) => request({
url: '/user_api/auth/token/salt',
method: 'get',
params: {
username: name
}
});
export const generateToken = (user) => request({ export const generateToken = (user) => request({
url: '/user_api/auth/token', url: '/user_api/auth/token/generate',
method: 'post', method: 'post',
data: user data: user
}); });
export const checkTokenValid = (token) => request({ export const checkTokenValid = (user, token) => request({
url: '/user_api/auth/token/' + token, url: '/user_api/auth/token/check',
method: 'post' method: 'get',
params: {
username: user,
token
}
}); });

@ -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} from "@/util/auth"; import {getToken, getUser} from "@/util/auth";
import store from "@/store/store"; import store from "@/store/store";
import {checkTokenValid} from "@/api/user"; import {checkTokenValid} from "@/api/user";
@ -35,12 +35,12 @@ router.beforeEach((to, from, next) => {
if (meta.isAuth !== true) { if (meta.isAuth !== true) {
next(); next();
} else { } else {
const token = getToken(); const token = getToken(), user = getUser();
if ('' === token || undefined === token) { if ('' === token || undefined === token || '' === user || undefined === user) {
next({path: '/login'}); next({path: '/login'});
return; return;
} }
checkTokenValid(token).then(res => { checkTokenValid(user, token).then(res => {
if (res.ok && store.getters.token.length > 0) { if (res.ok && store.getters.token.length > 0) {
next(); next();
} else { } else {

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

@ -1,8 +1,23 @@
import Cookies from 'js-cookie' import Cookies from 'js-cookie'
const UserKey = 'x-access-user';
const TokenKey = 'x-access-token'; const TokenKey = 'x-access-token';
// 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() {
return Cookies.get(UserKey);
}
export function setUser(user) {
return Cookies.set(UserKey, user, {expires: inFifteenMinutes});
}
export function removeUser() {
return Cookies.remove(UserKey);
}
export function getToken() { export function getToken() {
return Cookies.get(TokenKey); return Cookies.get(TokenKey);
} }

@ -89,9 +89,13 @@
text: '登录中,请稍后。。。', text: '登录中,请稍后。。。',
spinner: "el-icon-loading" spinner: "el-icon-loading"
}); });
this.$store.dispatch("GenerateToken", this.loginForm).then(() => { this.$store.dispatch("GenerateSalt", this.loginForm.name).then((salt) => {
this.$router.push({path: '/'}); this.$store.dispatch("GenerateToken", {salt, user:this.loginForm}).then(() => {
setTimeout(() => loading.close(), 500); this.$router.push({path: '/'});
setTimeout(() => loading.close(), 500);
}).catch(() => {
loading.close()
});
}).catch(() => { }).catch(() => {
loading.close() loading.close()
}); });

Loading…
Cancel
Save