Skip to content

Commit

Permalink
update: 登录注册逻辑完成
Browse files Browse the repository at this point in the history
  • Loading branch information
EsunR committed Feb 13, 2020
1 parent e4890bf commit 024ac0b
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 257 deletions.
1 change: 1 addition & 0 deletions clinet/.eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
build/*.js
src/assets
src/router/*.js
public
dist
6 changes: 3 additions & 3 deletions clinet/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"build:prod": "vue-cli-service build",
"build:stage": "vue-cli-service build --mode staging",
"preview": "node build/index.js --preview",
"lint": "eslint --ext .js,.vue src",
"lint": "npx eslint --ext .js,.vue src",
"test:unit": "jest --clearCache && vue-cli-service test:unit",
"test:ci": "npm run lint && npm run test:unit",
"svgo": "svgo -f src/icons/svg --config=src/icons/svgo.yml",
Expand Down Expand Up @@ -87,14 +87,14 @@
"connect": "3.6.6",
"eslint": "^5.16.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-prettier": "^3.1.1",
"eslint-plugin-prettier": "^3.1.2",
"eslint-plugin-vue": "^5.0.0",
"husky": "1.3.1",
"lint-staged": "8.1.5",
"mockjs": "1.0.1-beta3",
"node-sass": "^4.9.0",
"prettier": "^1.19.1",
"plop": "2.3.0",
"prettier": "^1.19.1",
"runjs": "^4.3.2",
"sass-loader": "^7.1.0",
"script-ext-html-webpack-plugin": "2.1.3",
Expand Down
14 changes: 3 additions & 11 deletions clinet/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,9 @@ export function login(data) {
})
}

export function getInfo(token) {
export function getInfo() {
return request({
url: '/vue-element-admin/user/info',
method: 'get',
params: { token }
})
}

export function logout() {
return request({
url: '/vue-element-admin/user/logout',
method: 'post'
url: '/user/info',
method: 'get'
})
}
4 changes: 2 additions & 2 deletions clinet/src/layout/components/AppMain.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default {
overflow: hidden;
}
.fixed-header+.app-main {
.fixed-header + .app-main {
padding-top: 50px;
}
Expand All @@ -41,7 +41,7 @@ export default {
min-height: calc(100vh - 84px);
}
.fixed-header+.app-main {
.fixed-header + .app-main {
padding-top: 84px;
}
}
Expand Down
52 changes: 20 additions & 32 deletions clinet/src/layout/components/Navbar.vue
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<template>
<div class="navbar">
<hamburger id="hamburger-container" :is-active="sidebar.opened" class="hamburger-container" @toggleClick="toggleSideBar" />
<hamburger
id="hamburger-container"
:is-active="sidebar.opened"
class="hamburger-container"
@toggleClick="toggleSideBar"
/>

<breadcrumb id="breadcrumb-container" class="breadcrumb-container" />

<div class="right-menu">
<template v-if="device!=='mobile'">
<search id="header-search" class="right-menu-item" />

<error-log class="errLog-container right-menu-item hover-effect" />

<template v-if="device !== 'mobile'">
<screenfull id="screenfull" class="right-menu-item hover-effect" />

<el-tooltip content="Global Size" effect="dark" placement="bottom">
<size-select id="size-select" class="right-menu-item hover-effect" />
</el-tooltip>

</template>

<el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
<el-dropdown
class="avatar-container right-menu-item hover-effect"
trigger="click"
>
<div class="avatar-wrapper">
<img :src="avatar+'?imageView2/1/w/80/h/80'" class="user-avatar">
<img :src="avatar + '?imageView2/1/w/80/h/80'" class="user-avatar" />
<i class="el-icon-caret-bottom" />
</div>
<el-dropdown-menu slot="dropdown">
Expand All @@ -30,12 +32,6 @@
<router-link to="/">
<el-dropdown-item>Dashboard</el-dropdown-item>
</router-link>
<a target="_blank" href="https://github.com/PanJiaChen/vue-element-admin/">
<el-dropdown-item>Github</el-dropdown-item>
</a>
<a target="_blank" href="https://panjiachen.github.io/vue-element-admin-site/#/">
<el-dropdown-item>Docs</el-dropdown-item>
</a>
<el-dropdown-item divided @click.native="logout">
<span style="display:block;">Log Out</span>
</el-dropdown-item>
Expand All @@ -49,26 +45,18 @@
import { mapGetters } from 'vuex'
import Breadcrumb from '@/components/Breadcrumb'
import Hamburger from '@/components/Hamburger'
import ErrorLog from '@/components/ErrorLog'
import Screenfull from '@/components/Screenfull'
import SizeSelect from '@/components/SizeSelect'
import Search from '@/components/HeaderSearch'
export default {
components: {
Breadcrumb,
Hamburger,
ErrorLog,
Screenfull,
SizeSelect,
Search
SizeSelect
},
computed: {
...mapGetters([
'sidebar',
'avatar',
'device'
])
...mapGetters(['sidebar', 'avatar', 'device'])
},
methods: {
toggleSideBar() {
Expand All @@ -88,18 +76,18 @@ export default {
overflow: hidden;
position: relative;
background: #fff;
box-shadow: 0 1px 4px rgba(0,21,41,.08);
box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
.hamburger-container {
line-height: 46px;
height: 100%;
float: left;
cursor: pointer;
transition: background .3s;
-webkit-tap-highlight-color:transparent;
transition: background 0.3s;
-webkit-tap-highlight-color: transparent;
&:hover {
background: rgba(0, 0, 0, .025)
background: rgba(0, 0, 0, 0.025);
}
}
Expand Down Expand Up @@ -131,10 +119,10 @@ export default {
&.hover-effect {
cursor: pointer;
transition: background .3s;
transition: background 0.3s;
&:hover {
background: rgba(0, 0, 0, .025)
background: rgba(0, 0, 0, 0.025);
}
}
}
Expand Down
78 changes: 41 additions & 37 deletions clinet/src/layout/index.vue
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<template>
<div :class="classObj" class="app-wrapper">
<div v-if="device==='mobile'&&sidebar.opened" class="drawer-bg" @click="handleClickOutside" />
<div
v-if="device === 'mobile' && sidebar.opened"
class="drawer-bg"
@click="handleClickOutside"
/>
<sidebar class="sidebar-container" />
<div :class="{hasTagsView:needTagsView}" class="main-container">
<div :class="{'fixed-header':fixedHeader}">
<div :class="{ hasTagsView: needTagsView }" class="main-container">
<div :class="{ 'fixed-header': fixedHeader }">
<navbar />
<tags-view v-if="needTagsView" />
</div>
Expand Down Expand Up @@ -58,45 +62,45 @@ export default {
</script>

<style lang="scss" scoped>
@import "~@/styles/mixin.scss";
@import "~@/styles/variables.scss";
@import '~@/styles/mixin.scss';
@import '~@/styles/variables.scss';
.app-wrapper {
@include clearfix;
position: relative;
height: 100%;
width: 100%;
.app-wrapper {
@include clearfix;
position: relative;
height: 100%;
width: 100%;
&.mobile.openSidebar {
position: fixed;
top: 0;
}
}
.drawer-bg {
background: #000;
opacity: 0.3;
width: 100%;
top: 0;
height: 100%;
position: absolute;
z-index: 999;
}
.fixed-header {
&.mobile.openSidebar {
position: fixed;
top: 0;
right: 0;
z-index: 9;
width: calc(100% - #{$sideBarWidth});
transition: width 0.28s;
}
}
.hideSidebar .fixed-header {
width: calc(100% - 54px)
}
.drawer-bg {
background: #000;
opacity: 0.3;
width: 100%;
top: 0;
height: 100%;
position: absolute;
z-index: 999;
}
.mobile .fixed-header {
width: 100%;
}
.fixed-header {
position: fixed;
top: 0;
right: 0;
z-index: 9;
width: calc(100% - #{$sideBarWidth});
transition: width 0.28s;
}
.hideSidebar .fixed-header {
width: calc(100% - 54px);
}
.mobile .fixed-header {
width: 100%;
}
</style>
8 changes: 4 additions & 4 deletions clinet/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ import * as filters from './filters' // global filters
* Currently MockJs will be used in the production environment,
* please remove it before going online ! ! !
*/
if (process.env.NODE_ENV === 'production') {
const { mockXHR } = require('../mock')
mockXHR()
}
// if (process.env.NODE_ENV === 'production') {
// const { mockXHR } = require('../mock')
// mockXHR()
// }

Vue.use(Element, {
size: Cookies.get('size') || 'medium' // set element-ui default size
Expand Down
34 changes: 18 additions & 16 deletions clinet/src/permission.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// 路由跳转前进行校验
import router from './router'
import store from './store'
import { Message } from 'element-ui'
Expand All @@ -10,58 +11,59 @@ NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/login', '/auth-redirect'] // no redirect whitelist

router.beforeEach(async(to, from, next) => {
router.beforeEach(async (to, from, next) => {
// start progress bar
NProgress.start()

// set page title
document.title = getPageTitle(to.meta.title)

// determine whether the user has logged in
// 判断是否存在 Token
const hasToken = getToken()

if (hasToken) {
// 如果存在 Token
if (to.path === '/login') {
// if is logged in, redirect to the home page
// 如果已登录,就跳转到系统主页
next({ path: '/' })
NProgress.done()
} else {
// determine whether the user has obtained his permission roles through getInfo
// 判断是否已经获取了用户角色,如果获取了就使用,否则向服务器请求用户数据
const hasRoles = store.getters.roles && store.getters.roles.length > 0
if (hasRoles) {
next()
} else {
try {
// get user info
// note: roles must be a object array! such as: ['admin'] or ,['developer','editor']
// 向 store 中存放用户信息,并获取用户角色
const { roles } = await store.dispatch('user/getInfo')
// 由用户角色生成路由表,并存放再 store 中
const accessRoutes = await store.dispatch(
'permission/generateRoutes',
roles
)

// generate accessible routes map based on roles
const accessRoutes = await store.dispatch('permission/generateRoutes', roles)

// dynamically add accessible routes
// 动态添加路由
router.addRoutes(accessRoutes)

// hack method to ensure that addRoutes is complete
// set the replace: true, so the navigation will not leave a history record
next({ ...to, replace: true })
} catch (error) {
// remove token and go to login page to re-login
// 如果出错,移除 token 并让用户重新登录
await store.dispatch('user/resetToken')
Message.error(error || 'Has Error')
Message.error(error || '用户身份信息获取失败,请重新登录')
next(`/login?redirect=${to.path}`)
NProgress.done()
}
}
}
} else {
/* has no token*/

// 本地没有 token
if (whiteList.indexOf(to.path) !== -1) {
// in the free login whitelist, go directly
// 如果是无需登录的页面就进行直接跳转
next()
} else {
// other pages that do not have permission to access are redirected to the login page.
// 跳转到登录页
next(`/login?redirect=${to.path}`)
NProgress.done()
}
Expand Down
Loading

0 comments on commit 024ac0b

Please sign in to comment.