1.关闭mock
在vue.config.js中注释如下内容
在main.js中注释如下
2.更改连接源
在config下的setting.config中将
原本为mock改为自己的后台地址就好了
3.后台传递
将mock文件下的controller中的user按照相同格式从后台往前台传输
前台mock传递数据
const accessTokens = {
admin: 'admin-accessToken',
editor: 'editor-accessToken',
test: 'test-accessToken',
sample: 'sample-accessToken',
division: 'division-accessToken',
applicant: 'applicant-accessToken',
}
module.exports = [
{
url: '/publicKey',
type: 'post',
response() {
return {
code: 200,
msg: 'success',
data: {
mockServer: true,
publicKey:
'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBT2vr+dhZElF73FJ6xiP181txKWUSNLPQQlid6DUJhGAOZblluafIdLmnUyKE8mMHhT3R+Ib3ssZcJku6Hn72yHYj/qPkCGFv0eFo7G+GJfDIUeDyalBN0QsuiE/XzPHJBuJDfRArOiWvH0BXOv5kpeXSXM8yTt5Na1jAYSiQ/wIDAQAB',
},
}
},
},
{
url: '/login',
type: 'post',
response(config) {
const { username } = config.body
const accessToken = accessTokens[username]
if (!accessToken) {
return {
code: 500,
msg: '帐户或密码不正确。',
}
}
return {
code: 200,
msg: 'success',
data: { accessToken },
}
},
},
{
url: '/register',
type: 'post',
response() {
return {
code: 200,
msg: '模拟注册成功',
}
},
},
{
url: '/userInfo',
type: 'post',
response(config) {
const { accessToken } = config.body
let permissions = ['admin']
let username = 'admin'
if ('admin-accessToken' === accessToken) {
permissions = ['admin']
username = 'admin'
}
if ('editor-accessToken' === accessToken) {
permissions = ['editor']
username = 'editor'
}
if ('test-accessToken' === accessToken) {
permissions = ['test']
username = 'test'
}
if ('sample-accessToken' === accessToken) {
permissions = ['sample']
username = 'sample'
}
if ('division-accessToken' === accessToken) {
permissions = ['division']
username = 'division'
}
if ('applicant-accessToken' === accessToken) {
permissions = ['applicant']
username = 'applicant'
}
return {
code: 200,
msg: 'success',
data: {
permissions,
username,
'avatar|1': [
'https://i.gtimg.cn/club/item/face/img/2/15922_100.gif',
'https://i.gtimg.cn/club/item/face/img/8/15918_100.gif',
],
},
}
},
},
{
url: '/logout',
type: 'post',
response() {
return {
code: 200,
msg: 'success',
}
},
},
]
后台Java传递数据
**
* <p>
* 前端控制器
* </p>
*
*
* @author 又菜又爱玩
* @since 2021-12-21
*/
@Api(tags = "用户信息类")
@RestController
public class MmLoginuserController {
@Autowired
private MmLoginuserService mmLoginuserService;
@ApiOperation("用户登录")
@PostMapping(value = "/login")
public CommonResponse Login(){
return CommonResponse.ok().data("data","admin-accessToken");
}
@ApiOperation("用户信息")
@PostMapping("/userInfo")
public CommonResponse info(){
ArrayList list = new ArrayList();
list.add("admin");
return CommonResponse.ok()
.data("permissions",list)
.data("username","admin")
.data("avatar","https://img0.baidu.com/it/u=3623309039,2407011632&fm=26&fmt=auto");
}
@ApiOperation("用户退出")
@GetMapping("/logout")
public CommonResponse logout(){
return CommonResponse.ok().data("token", "admin");
}
@ApiOperation("密钥")
@PostMapping("/publicKey")
public CommonResponse Key(){
return CommonResponse.ok().data("mockServer", false)
.data("publicKey", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDBT2vr+dhZElF73FJ6xiP181txKWUSNLPQQlid6DUJhGAOZblluafIdLmnUyKE8mMHhT3R+Ib3ssZcJku6Hn72yHYj/qPkCGFv0eFo7G+GJfDIUeDyalBN0QsuiE/XzPHJBuJDfRArOiWvH0BXOv5kpeXSXM8yTt5Na1jAYSiQ/wIDAQAB");
}
}
4.修改store/modules下的user.js
在这个地方,如果传递格式不匹配data就是空值,将自己后台传递的数据拉出来,像我传过来的数据都在data的集合而不是data,这里就需要修改。![在这里插入图片描述](https://img-blog.csdnimg.cn/1a6146d020bd49a88ef559335552ca82.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y-I6I-c5Y-I54ix546p5ZGc5ZGc5ZGcfg==,size_13,color_FFFFFF,t_70,g_se,x_16)
这里发现一个数组检验,也就是permissions是必须是数组的,我的相应后台
退出可以直接将清除token的方法拉上来就可以了
这时你的登录,登出代码都在后端了。
5、权限管理
vue-admin-templete在config文件夹下的permission中权限所需要的代码已经写好了我们只需要在路由中进行权限配置就好了。![在这里插入图片描述](https://img-blog.csdnimg.cn/a9814ba57bb643f8b07d4733ca854e24.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5Y-I6I-c5Y-I54ix546p5ZGc5ZGc5ZGcfg==,size_15,color_FFFFFF,t_70,g_se,x_16)
我的路由代码如下
/**
* @copyright chuzhixin 1204505056@qq.com
* @description router全局配置,如有必要可分文件抽离
*/
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layouts'
import EmptyLayout from '@/layouts/EmptyLayout'
import { publicPath, routerMode } from '@/config'
Vue.use(VueRouter)
export const constantRoutes = [
{
path: '/login',
component: () => import('@/views/login/index'),
hidden: true,
},
{
path: '/register',
component: () => import('@/views/register/index'),
hidden: true,
},
{
path: '/401',
name: '401',
component: () => import('@/views/401'),
hidden: true,
},
{
path: '/404',
name: '404',
component: () => import('@/views/404'),
hidden: true,
},
]
/*当settings.js里authentication配置的是intelligence时,views引入交给前端配置*/
export const asyncRoutes = [
{
path: '/',
component: Layout,
redirect: '/index',
children: [
{
path: '/index',
name: 'Index',
component: () => import('@/views/index/index'),
meta: {
title: '首页',
icon: 'home',
affix: true,
noKeepAlive: true,
},
},
],
},
{
path: '/choseProgram',
component: Layout,
children: [
{
path: '/choseProgram',
name: 'choseProgram',
component: () => import('@/views/choseProgram'),
meta: {
title: '选择项目',
icon: 'home',
permissions: ['admin', 'applicant'],
// affix: true,
// noKeepAlive: true,
},
},
],
},
{
path: '/approver',
component: Layout,
name: 'Vab',
alwaysShow: true,
meta: {
title: '审批',
icon: 'box-open',
permissions: ['admin', 'division', 'sample', 'test', 'editor'],
},
children: [
{
path: '/approver',
name: 'approver',
component: () => import('@/views/approver'),
meta: {
title: '审批',
icon: 'home',
permissions: ['admin'],
// affix: true,
// noKeepAlive: true,
},
},
{
path: '/manager',
component: () => import('@/views/approver/manager/index'),
redirect: 'noRedirect',
alwaysShow: true,
meta: {
title: '主管审批',
icon: 'box-open',
permissions: ['admin', 'division'],
},
children: [
{
path: 'y_approval',
name: 'y_approval',
component: () =>
import('@/views/approver/manager/y_approval/index'),
meta: {
title: '已审批',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'n_approval',
name: 'n_approval',
component: () =>
import('@/views/approver/manager/n_approval/index'),
meta: {
title: '未审批',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'turn_down',
name: 'turn_down',
component: () => import('@/views/approver/manager/turn_down/index'),
meta: {
title: '已驳回',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'm_info',
name: 'm_info',
component: () => import('@/views/approver/manager/m_info/index'),
meta: {
title: '详情',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
],
},
{
path: '/checkout',
component: () => import('@/views/approver//checkout/index'),
redirect: 'noRedirect',
alwaysShow: true,
meta: {
title: '校验员审批',
icon: 'box-open',
permissions: ['admin', 'editor'],
},
children: [
{
path: 'y_pass',
name: 'y_pass',
component: () => import('@/views/approver/checkout/y_pass/index'),
meta: {
title: '已通过',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'n_check',
name: 'n_check',
component: () => import('@/views/approver/checkout/n_check/index'),
meta: {
title: '未审批',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'turn_down',
name: 'turn_down',
component: () => import('@/views/approver/checkout/turn_down'),
meta: {
title: '已驳回',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'c_info',
name: 'c_info',
component: () => import('@/views/approver/checkout/c_info/index'),
meta: {
title: '详情',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
],
},
{
path: '/receiver',
component: () => import('@/views/approver/receiver/index'),
redirect: 'noRedirect',
alwaysShow: true,
meta: {
title: '接样人审批',
icon: 'box-open',
permissions: ['admin', 'sample'],
},
children: [
{
path: 'y_pass1',
name: 'y_pass1',
component: () => import('@/views/approver/receiver/y_pass1/index'),
meta: {
title: '已通过',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'n_check1',
name: 'n_check1',
component: () => import('@/views/approver/receiver/n_check1/index'),
meta: {
title: '未审批',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'turn_down1',
name: 'turn_down1',
component: () => import('@/views/approver/receiver/turn_down1'),
meta: {
title: '已驳回',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'c_info1',
name: 'c_info1',
component: () => import('@/views/approver/receiver/c_info1/index'),
meta: {
title: '详情',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
],
},
{
path: '/monitor',
component: () => import('@/views/approver//monitor/index'),
redirect: 'noRedirect',
alwaysShow: true,
meta: {
title: '检测员审批',
icon: 'box-open',
permissions: ['admin', 'test'],
},
children: [
{
path: 'indata',
name: 'indata',
component: () => import('@/views/approver/monitor/indata/index'),
meta: {
title: '录入数据',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'y_pass',
name: 'y_pass',
component: () => import('@/views/approver/checkout/y_pass/index'),
meta: {
title: '已通过',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'n_check',
name: 'n_check',
component: () => import('@/views/approver/checkout/n_check/index'),
meta: {
title: '未审批',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'turn_down',
name: 'turn_down',
component: () => import('@/views/approver/checkout/turn_down'),
meta: {
title: '已驳回',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'c_info',
name: 'c_info',
component: () => import('@/views/approver/checkout/c_info/index'),
meta: {
title: '详情',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
{
path: 'detail',
name: 'detail',
component: () => import('@/views/approver/monitor/detail/index'),
meta: {
title: '详情',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
],
},
],
},
{
path: '/distribution',
component: Layout,
children: [
{
path: '/distribution',
name: 'distribution',
component: () => import('@/views/distribution'),
meta: {
title: '技术分配',
icon: 'home',
permissions: ['admin', 'sample'],
// affix: true,
// noKeepAlive: true,
},
},
],
},
{
path: '/management',
name: 'management',
component: Layout,
alwaysShow: true,
meta: { title: '管理', icon: 'box-open', permissions: ['admin'] },
children: [
{
path: 'M_user',
name: 'M_user',
component: () => import('@/views/approver/management/M_user/index'),
meta: {
title: '用户管理',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'serve',
name: 'serve',
component: () => import('@/views/approver/management/serve/index'),
meta: {
title: '添加用户',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
{
path: 'N_user',
name: 'N_user',
component: () => import('@/views/approver/management/N_user/index'),
meta: {
title: '添加用户',
//affix: false,
//noKeepAlive: false,
},
hidden: true,
},
{
path: 'pass',
name: 'pass',
component: () => import('@/views/approver/management/T_user/index'),
meta: {
title: '条目管理',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: 'remember',
name: 'remember',
component: () => import('@/views/approver/management/B_user/index'),
meta: {
title: '部门管理',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
{
path: '/result/detail',
name: 'detail',
hidden: true,
component: () => import('@/views/result/detail'),
meta: {
title: '详情',
icon: 'home',
hidden: true,
// affix: true,
// noKeepAlive: true,
},
},
],
},
{
path: '/result',
component: Layout,
children: [
{
path: '/result',
name: 'result',
component: () => import('@/views/result/index'),
meta: {
title: '结果',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
],
},
{
path: '/print',
component: Layout,
children: [
{
path: '/print',
name: 'print',
component: () => import('@/views/print'),
meta: {
title: '打印',
icon: 'home',
// affix: true,
// noKeepAlive: true,
},
},
],
},
{
path: '*',
redirect: '/404',
hidden: true,
},
]
const router = new VueRouter({
base: routerMode === 'history' ? publicPath : '',
mode: routerMode,
scrollBehavior: () => ({
y: 0,
}),
routes: constantRoutes,
})
//注释的地方是允许路由重复点击,如果你觉得框架路由跳转规范太过严格可选择放开
/* const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
if (onResolve || onReject)
return originalPush.call(this, location, onResolve, onReject);
return originalPush.call(this, location).catch((err) => err);
}; */
export function resetRouter() {
router.matcher = new VueRouter({
base: routerMode === 'history' ? publicPath : '',
mode: routerMode,
scrollBehavior: () => ({
y: 0,
}),
routes: constantRoutes,
}).matcher
}
export default router
当我使用smaple权限进入时
这些都是应该从用户表中读权限的,为了演示,就先这样写咯