From 685985ce59de95067de4e866f32001eeb44c1633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=9D=B0?= Date: Mon, 21 Jan 2019 17:20:07 +0800 Subject: [PATCH] =?UTF-8?q?v1.5=20=E6=AD=A3=E5=BC=8F=E7=89=88=E5=8F=91?= =?UTF-8?q?=E5=B8=83=20=EF=BC=8C=E8=AF=A6=E6=83=85=E6=9F=A5=E7=9C=8B?= =?UTF-8?q?=E5=8F=91=E8=A1=8C=E7=89=88=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/myTest.js | 24 ----------------- src/permission.js | 4 +-- src/store/modules/user.js | 7 ++--- src/utils/auth.js | 16 +++--------- src/utils/request.js | 4 +-- src/views/login/index.vue | 49 ++++++++++++++++++++++++++--------- src/views/monitor/log/msg.vue | 27 +++++++++++++++---- 7 files changed, 68 insertions(+), 63 deletions(-) delete mode 100644 src/api/myTest.js diff --git a/src/api/myTest.js b/src/api/myTest.js deleted file mode 100644 index faabc13b5..000000000 --- a/src/api/myTest.js +++ /dev/null @@ -1,24 +0,0 @@ -import request from '@/utils/request' - -export function add(data) { - return request({ - url: 'api/myTest', - method: 'post', - data - }) -} - -export function del(id) { - return request({ - url: 'api/myTest/' + id, - method: 'delete' - }) -} - -export function edit(data) { - return request({ - url: 'api/myTest', - method: 'put', - data - }) -} diff --git a/src/permission.js b/src/permission.js index 03e9ca9ea..a977ea577 100644 --- a/src/permission.js +++ b/src/permission.js @@ -2,7 +2,7 @@ import router from './router' import store from './store' import NProgress from 'nprogress' // progress bar import 'nprogress/nprogress.css'// progress bar style -import { getToken, getStorageToken } from '@/utils/auth' // getToken from cookie +import { getToken } from '@/utils/auth' // getToken from cookie import { buildMenus } from '@/api/menu' import { filterAsyncRouter } from './store/modules/permission' @@ -12,7 +12,7 @@ const whiteList = ['/login']// no redirect whitelist router.beforeEach((to, from, next) => { NProgress.start() // start progress bar - if (getToken() || getStorageToken()) { + if (getToken()) { // 已登录且要跳转的页面是登录页 if (to.path === '/login') { next({ path: '/' }) diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 4dc5d28f6..1d1696042 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,5 +1,5 @@ import { login, getInfo } from '@/api/login' -import { getToken, setToken, setStorageToken, removeToken } from '@/utils/auth' +import { getToken, setToken, removeToken } from '@/utils/auth' import { parseTime } from '@/utils/index' const user = { @@ -41,11 +41,8 @@ const user = { const rememberMe = userInfo.rememberMe return new Promise((resolve, reject) => { login(username, password).then(res => { - setToken(res.token) + setToken(res.token, rememberMe) commit('SET_TOKEN', res.token) - if (rememberMe) { - setStorageToken(res.token) - } resolve() }).catch(error => { reject(error) diff --git a/src/utils/auth.js b/src/utils/auth.js index 96fa7cce2..549c746e0 100644 --- a/src/utils/auth.js +++ b/src/utils/auth.js @@ -1,25 +1,17 @@ import Cookies from 'js-cookie' const TokenKey = 'Admin-Token' -const storageTokenKey = 'EL_ADMIN_COOKIE_TOKEN' export function getToken() { return Cookies.get(TokenKey) } -export function getStorageToken() { - return localStorage.getItem(storageTokenKey) -} - -export function setToken(token) { - return Cookies.set(TokenKey, token) -} - -export function setStorageToken(token) { - return localStorage.setItem(storageTokenKey, token) +export function setToken(token, rememberMe) { + if (rememberMe) { + return Cookies.set(TokenKey, token, { expires: 1 }) + } else return Cookies.set(TokenKey, token) } export function removeToken() { - localStorage.removeItem(storageTokenKey) return Cookies.remove(TokenKey) } diff --git a/src/utils/request.js b/src/utils/request.js index 3e07246b0..6011f4d33 100644 --- a/src/utils/request.js +++ b/src/utils/request.js @@ -2,7 +2,7 @@ import axios from 'axios' import router from '@/router' import { Notification, MessageBox } from 'element-ui' import store from '../store' -import { getToken, getStorageToken } from '@/utils/auth' +import { getToken } from '@/utils/auth' // 创建axios实例 const service = axios.create({ @@ -15,8 +15,6 @@ service.interceptors.request.use( config => { if (getToken()) { config.headers['Authorization'] = 'Bearer ' + getToken() // 让每个请求携带自定义token 请根据实际情况自行修改 - } else if (getStorageToken()) { - config.headers['Authorization'] = 'Bearer ' + getStorageToken() } config.headers['Content-Type'] = 'application/json' return config diff --git a/src/views/login/index.vue b/src/views/login/index.vue index c8ce8d428..26aa61897 100644 --- a/src/views/login/index.vue +++ b/src/views/login/index.vue @@ -12,27 +12,29 @@ - 记住密码 + 记住密码 登 录 登 录 中... +