pixelqm
5 years ago
10 changed files with 298 additions and 72 deletions
-
12QMPlusVuePage/src/api/menu.js
-
16QMPlusVuePage/src/api/user.js
-
2QMPlusVuePage/src/main.js
-
58QMPlusVuePage/src/permission.js
-
9QMPlusVuePage/src/router/index.js
-
8QMPlusVuePage/src/store/index.js
-
36QMPlusVuePage/src/store/module/router.js
-
38QMPlusVuePage/src/store/module/user.js
-
82QMPlusVuePage/src/view/login/login.vue
-
97QMPlusVuePage/src/view/login/regist.vue
@ -0,0 +1,12 @@ |
|||
import service from '@/utils/request' |
|||
|
|||
// @Summary 用户登录
|
|||
// @Produce application/json
|
|||
// @Param 可以什么都不填 调一下即可
|
|||
// @Router /menu/getMenu [post]
|
|||
export const asyncMenu = () => { |
|||
return service({ |
|||
url: "/menu/getMenu", |
|||
method: 'post', |
|||
}) |
|||
} |
@ -1,26 +1,40 @@ |
|||
import router from './router' |
|||
import { asyncRouterHandle } from '@/utils/asyncRouter'; |
|||
import { store } from '@/store/index' |
|||
|
|||
router.beforeEach((to, from, next) => { |
|||
let asyncRouterFlag = 0 |
|||
|
|||
const whiteList = ['login', 'regist'] |
|||
router.beforeEach(async(to, from, next) => { |
|||
const token = store.getters['user/token'] |
|||
// 在白名单中的判断情况
|
|||
if (whiteList.indexOf(to.name) > -1) { |
|||
if (token) { |
|||
next({ name: 'dashbord' }) |
|||
} else { |
|||
next() |
|||
} |
|||
} else { |
|||
// 不在白名单中并且已经登陆的时候
|
|||
if (token) { |
|||
// 添加flag防止多次获取动态路由和栈溢出
|
|||
if (!asyncRouterFlag) { |
|||
asyncRouterFlag++ |
|||
await store.dispatch('router/SetAsyncRouter') |
|||
const asyncRouters = store.getters['router/asyncRouters'] |
|||
router.addRoutes(asyncRouters) |
|||
next({...to, replace: true }) |
|||
} else { |
|||
next() |
|||
// asyncRouterHandle(asyncRouter) // 等待动态使用 VUEX持久化 会将其放入 vuex并且动态生成左侧列表
|
|||
// router.addRoutes(asyncRouter)
|
|||
} |
|||
} |
|||
// 不在白名单中并且未登陆的时候
|
|||
if (!token) { |
|||
next({ |
|||
name: "login", |
|||
query: { |
|||
redirect: document.location.hash |
|||
} |
|||
}) |
|||
} |
|||
} |
|||
}) |
|||
|
|||
const asyncRouter = [{ |
|||
path: '/layout', |
|||
name: 'layout', |
|||
component: 'view/layout/index.vue', |
|||
meta: { |
|||
title: '首页', |
|||
}, |
|||
children: [{ |
|||
path: 'dashbord', |
|||
name: 'dashbord', |
|||
component: 'view/dashbord/index.vue' |
|||
}, { |
|||
path: "test", |
|||
name: "test", |
|||
component: "view/test/index.vue" |
|||
}] |
|||
}] |
@ -0,0 +1,36 @@ |
|||
import { asyncRouterHandle } from '@/utils/asyncRouter'; |
|||
|
|||
import { asyncMenu } from '@/api/menu' |
|||
|
|||
export const router = { |
|||
namespaced: true, |
|||
state: { |
|||
asyncRouters: [] |
|||
}, |
|||
mutations: { |
|||
// 设置动态路由
|
|||
setAsyncRouter(state, asyncRouters) { |
|||
state.asyncRouters = asyncRouters |
|||
} |
|||
}, |
|||
actions: { |
|||
// 从后台获取动态路由
|
|||
async SetAsyncRouter({ commit }) { |
|||
const asyncRouterRes = await asyncMenu() |
|||
const asyncRouter = asyncRouterRes.data.menus |
|||
asyncRouter.push({ |
|||
path: '*', |
|||
redirect: '/404' |
|||
|
|||
}) |
|||
asyncRouterHandle(asyncRouter) // 等待动态使用 VUEX持久化 会将其放入 vuex并且动态生成左侧列表
|
|||
commit('setAsyncRouter', asyncRouter) |
|||
} |
|||
}, |
|||
getters: { |
|||
// 获取动态路由
|
|||
asyncRouters(state) { |
|||
return state.asyncRouters |
|||
} |
|||
} |
|||
} |
@ -1,35 +1,81 @@ |
|||
<template> |
|||
<el-container class="login-box"> |
|||
<el-header>登录</el-header> |
|||
<el-main>输入框在这里 |
|||
<el-button @click="login"> |
|||
登录 |
|||
</el-button> |
|||
<el-main> |
|||
<el-form :model="loginForm" :rules="rules" label-width="100px" ref="loginForm" status-icon> |
|||
<el-form-item label="用户名" prop="username"> |
|||
<el-input v-model="loginForm.username"></el-input> |
|||
</el-form-item> |
|||
<el-form-item label="密码" prop="password"> |
|||
<el-input :type="lock==='lock'?'password':'text'" v-model="loginForm.password"> |
|||
<i :class="'el-input__icon el-icon-' + lock" @click="changeLock" slot="suffix"></i> |
|||
</el-input> |
|||
</el-form-item> |
|||
</el-form> |
|||
<el-button @click="submitForm">登录</el-button> |
|||
</el-main> |
|||
</el-container> |
|||
</template> |
|||
|
|||
<script> |
|||
import {mapState, mapGetters, mapActions , mapMutations} from 'vuex' |
|||
import {login} from '@/api/user' |
|||
import { mapActions } from 'vuex' |
|||
export default { |
|||
name:"Login", |
|||
computed:{ |
|||
...mapGetters("user",["userInfo"]), |
|||
...mapState("user",["token"]) |
|||
name: 'Login', |
|||
data() { |
|||
const checkUsername = (rule, value, callback) => { |
|||
if (value.length < 6 || value.length > 12) { |
|||
return callback(new Error('请输入正确的用户名')) |
|||
} else { |
|||
callback() |
|||
} |
|||
} |
|||
const checkPassword = (rule, value, callback) => { |
|||
if (value.length < 6 || value.length > 12) { |
|||
return callback(new Error('请输入正确的密码')) |
|||
} else { |
|||
callback() |
|||
} |
|||
} |
|||
|
|||
return { |
|||
lock: 'lock', |
|||
loginForm: { |
|||
username: '', |
|||
password: '' |
|||
}, |
|||
rules: { |
|||
username: [{ validator: checkUsername, trigger: 'blur' }], |
|||
password: [{ validator: checkPassword, trigger: 'blur' }] |
|||
} |
|||
} |
|||
}, |
|||
methods: { |
|||
...mapActions("user",["AsyncSetUserInfo"]), |
|||
...mapMutations("user",["setUserInfo"]), |
|||
login(){ |
|||
login({userName:"string1",passWord:"string"}) |
|||
...mapActions('user', ['LoginIn']), |
|||
async login() { |
|||
await this.LoginIn(this.loginForm) |
|||
}, |
|||
async submitForm() { |
|||
this.$refs.loginForm.validate(async v => { |
|||
if (v) { |
|||
await this.login() |
|||
} else { |
|||
this.$message({ |
|||
type: 'error', |
|||
message: '请正确填写登录信息', |
|||
showClose: true |
|||
}) |
|||
return false |
|||
} |
|||
}) |
|||
}, |
|||
changeLock() { |
|||
this.lock === 'lock' ? (this.lock = 'unlock') : (this.lock = 'lock') |
|||
} |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<style scoped lang="scss"> |
|||
.login-box{ |
|||
background: #409EFF |
|||
} |
|||
.login-box { |
|||
background: #409eff; |
|||
} |
|||
</style> |
@ -1,16 +1,101 @@ |
|||
<template> |
|||
<div> |
|||
注册 |
|||
</div> |
|||
<el-container class="login-box"> |
|||
<el-main> |
|||
<el-form :model="registForm" :rules="rules" label-width="100px" ref="registForm" status-icon> |
|||
<el-form-item label="用户名" prop="username"> |
|||
<el-input v-model="registForm.username"></el-input> |
|||
</el-form-item> |
|||
<el-form-item label="密码" prop="password"> |
|||
<el-input :type="lock==='lock'?'password':'text'" v-model="registForm.password"> |
|||
<i :class="'el-input__icon el-icon-' + lock" @click="changeLock" slot="suffix"></i> |
|||
</el-input> |
|||
</el-form-item> |
|||
<el-form-item label="重复密码" prop="rePassword"> |
|||
<el-input :type="lock==='lock'?'password':'text'" v-model="registForm.rePassword"> |
|||
<i :class="'el-input__icon el-icon-' + lock" @click="changeLock" slot="suffix"></i> |
|||
</el-input> |
|||
</el-form-item> |
|||
</el-form> |
|||
<el-button @click="submitForm">注册</el-button> |
|||
</el-main> |
|||
</el-container> |
|||
</template> |
|||
|
|||
<script> |
|||
import { mapActions } from 'vuex' |
|||
import { regist } from '@/api/user' |
|||
export default { |
|||
name:"Regist" |
|||
name: 'Regist', |
|||
data() { |
|||
const ratioPassword = (rule, value, callback) => { |
|||
if (value != this.registForm.password) { |
|||
return callback(new Error('两次密码不同')) |
|||
} else { |
|||
callback() |
|||
} |
|||
} |
|||
const checkUsername = (rule, value, callback) => { |
|||
if (value.length < 6 || value.length > 12) { |
|||
return callback(new Error('请输入正确的用户名')) |
|||
} else { |
|||
callback() |
|||
} |
|||
} |
|||
const checkPassword = (rule, value, callback) => { |
|||
if (value.length < 6 || value.length > 12) { |
|||
return callback(new Error('请输入正确的密码')) |
|||
} else { |
|||
callback() |
|||
} |
|||
} |
|||
|
|||
return { |
|||
lock: 'lock', |
|||
registForm: { |
|||
username: '', |
|||
password: '', |
|||
rePassword: '' |
|||
}, |
|||
rules: { |
|||
username: [{ validator: checkUsername, trigger: 'blur' }], |
|||
password: [{ validator: checkPassword, trigger: 'blur' }], |
|||
rePassword: [{ validator: ratioPassword, trigger: 'blur' }] |
|||
} |
|||
} |
|||
}, |
|||
methods: { |
|||
...mapActions('user', ['LoginIn']), |
|||
async submitForm() { |
|||
this.$refs.registForm.validate(async v => { |
|||
if (v) { |
|||
const res = await regist(this.registForm) |
|||
if (res.success) { |
|||
this.$message({ |
|||
type: 'success', |
|||
message: '注册成功', |
|||
showClose: true |
|||
}) |
|||
await this.LoginIn(this.registForm) |
|||
} |
|||
} else { |
|||
this.$message({ |
|||
type: 'error', |
|||
message: '请正确填写注册信息', |
|||
showClose: true |
|||
}) |
|||
return false |
|||
} |
|||
}) |
|||
}, |
|||
changeLock() { |
|||
this.lock === 'lock' ? (this.lock = 'unlock') : (this.lock = 'lock') |
|||
} |
|||
} |
|||
} |
|||
</script> |
|||
|
|||
<style scoped> |
|||
|
|||
<style scoped lang="scss"> |
|||
.login-box { |
|||
background: #409eff; |
|||
} |
|||
</style> |
Write
Preview
Loading…
Cancel
Save
Reference in new issue