You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
9.5 KiB

<template>
<div class="app-container">
<!--工具栏-->
<div class="head-container">
<div v-if="crud.props.searchToggle">
<!-- 搜索 -->
<el-input
v-model="query.blurry"
size="mini"
clearable
:placeholder="$t('Role.msg.m1')"
style="width: 200px;"
class="filter-item"
@keyup.enter.native="crud.toQuery"
/>
<rrOperation />
</div>
<crudOperation :permission="permission" />
</div>
<!-- 表单渲染 -->
<el-dialog
append-to-body
:close-on-click-modal="false"
:before-close="crud.cancelCU"
:visible.sync="crud.status.cu > 0"
:title="crud.status.title"
:width="computedLabelWidth"
>
<el-form ref="form" :inline="true" :model="form" :rules="rules" size="mini" :label-width="computedFormLabelWidth">
<el-form-item :label="$t('Role.dialog.name')" prop="name">
<el-input v-model="form.name" style="width: 240px;" />
</el-form-item>
<el-form-item :label="$t('Role.dialog.description')" prop="description">
<el-input v-model="form.remark" style="width: 240px;" rows="2" type="textarea" />
</el-form-item>
</el-form>
<div slot="footer" class="dialog-footer">
<el-button type="text" @click="crud.cancelCU">{{ $t('common.Cancel') }}</el-button>
<el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">{{ $t('common.Confirm') }}</el-button>
</div>
</el-dialog>
<el-row :gutter="15">
<!--角色管理-->
<el-col :span="12" style="margin-bottom: 10px">
<el-card class="box-card" shadow="never">
<div slot="header" class="clearfix">
<span class="role-span">{{ $t('Role.title_left') }}</span>
</div>
<el-table
ref="table"
v-loading="crud.loading"
highlight-current-row
style="width: 100%;"
:data="crud.data"
@selection-change="crud.selectionChangeHandler"
@current-change="handleCurrentChange"
>
<el-table-column type="selection" width="55" />
<el-table-column prop="name" :label="$t('Role.table.name')" min-width="100" show-overflow-tooltip />
<el-table-column show-overflow-tooltip prop="remark" :label="$t('Role.table.description')" />
<el-table-column show-overflow-tooltip width="135px" prop="create_time" :label="$t('Role.table.create_time')">
<template slot-scope="scope">
<span>{{ parseTime(scope.row.create_time) }}</span>
</template>
</el-table-column>
<el-table-column
v-permission="['admin','roles:edit','roles:del']"
:label="$t('common.Operate')"
width="130px"
align="center"
fixed="right"
>
<template slot-scope="scope">
<udOperation
:data="scope.row"
:permission="permission"
/>
</template>
</el-table-column>
</el-table>
<!--分页组件-->
<pagination />
</el-card>
</el-col>
<!-- 菜单授权 -->
<el-col :span="12">
<el-card class="box-card" shadow="never">
<div slot="header" class="clearfix">
<el-tooltip class="item" effect="dark" :content="$t('Role.msg.m2')" placement="top">
<span class="role-span">{{ $t('Role.title_right') }}</span>
</el-tooltip>
<el-button
v-permission="['admin','roles:edit']"
:disabled="!showButton"
:loading="menuLoading"
icon="el-icon-check"
size="mini"
style="float: right; padding: 6px 10px"
type="primary"
@click="saveMenu"
>{{ $t('Role.save') }}
</el-button>
</div>
<el-tree
ref="menu"
lazy
:data="menus"
:default-checked-keys="menuIds"
:load="getMenuDatas"
:props="defaultProps"
check-strictly
accordion
show-checkbox
node-key="menu_id"
@check="menuChange"
/>
</el-card>
</el-col>
</el-row>
</div>
</template>
<script>
import crudRoles from './role'
import crudMenu from '@/views/system/menu/menu'
import { getChild, getMenusTree } from '@/views/system/menu/menu'
import CRUD, { crud, form, header, presenter } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import i18n from '@/i18n'
const defaultForm = { role_id: null, name: null, remark: null }
export default {
name: 'Role',
components: { pagination, crudOperation, rrOperation, udOperation, crudMenu },
cruds() {
return CRUD({ idField: 'role_id', title: i18n.t('Role.title'), url: 'api/sysRole', crudMethod: { ...crudRoles }})
},
mixins: [presenter(), header(), form(defaultForm), crud()],
data() {
return {
defaultProps: { children: 'children', label: 'title', isLeaf: 'leaf' },
currentId: 0, menuLoading: false, showButton: false,
menus: [], menuIds: [], // 多选时使用
permission: {
add: ['admin', 'roles:add'],
edit: ['admin', 'roles:edit'],
del: ['admin', 'roles:del']
},
rules: {
name: [
{ required: true, message: i18n.t('Role.msg.m3'), trigger: 'blur' }
],
permission: [
{ required: true, message: i18n.t('Role.msg.m4'), trigger: 'blur' }
]
}
}
},
computed: {
computedLabelWidth() {
const item = localStorage.getItem('lang')
if (item === 'zh') {
return `400px`
}
return `420px`
},
computedFormLabelWidth() {
const item = localStorage.getItem('lang')
if (item === 'zh') {
return `80px`
}
return `120px`
}
},
created() {
},
methods: {
getMenuDatas(node, resolve) {
setTimeout(() => {
getMenusTree(node.data.menu_id ? node.data.menu_id : 0).then(res => {
resolve(res)
})
}, 100)
},
getMenusByRole(val) {
if (this.currentId && val[0] && val[1]) {
const param = {
role_id: this.currentId,
system_type: val[0],
category: val[1]
}
const _this = this
crudMenu.getMenusByRole(param).then(res => {
_this.menus = res
// 初始化默认选中的key
_this.menuIds = []
_this.menus.forEach(function(data) {
_this.menuIds.push(data)
})
})
}
},
[CRUD.HOOK.afterRefresh]() {
this.$refs.menu.setCheckedKeys([])
},
// 提交前做的操作
[CRUD.HOOK.afterValidateCU](crud) {
return true
},
// 触发单选
handleCurrentChange(val) {
if (val) {
const _this = this
// 清空菜单的选中
this.$refs.menu.setCheckedKeys([])
// 保存当前的角色id
this.currentId = val.role_id
// 初始化默认选中的key
this.menuIds = []
val.menus.forEach(function(data) {
_this.menuIds.push(data)
})
this.showButton = true
}
},
menuChange(menu) {
// 获取该节点的所有子节点,id 包含自身
getChild(menu.menu_id).then(childIds => {
// 判断是否在 menuIds 中,如果存在则删除,否则添加
if (this.menuIds.indexOf(menu.menu_id) !== -1) {
for (let i = 0; i < childIds.length; i++) {
const index = this.menuIds.indexOf(childIds[i])
if (index !== -1) {
this.menuIds.splice(index, 1)
}
}
} else {
for (let i = 0; i < childIds.length; i++) {
const index = this.menuIds.indexOf(childIds[i])
if (index === -1) {
this.menuIds.push(childIds[i])
}
}
}
this.$refs.menu.setCheckedKeys(this.menuIds)
})
},
// 保存菜单
saveMenu() {
this.menuLoading = true
const role = { role_id: this.currentId, menus: [] }
// 得到已选中的 key 值
this.menuIds.forEach(function(menuId) {
const menu = { menuId: menuId }
role.menus.push(menu)
})
crudRoles.editMenu(role).then(() => {
this.crud.notify(i18n.t('Role.msg.m5'), CRUD.NOTIFICATION_TYPE.SUCCESS)
this.menuLoading = false
this.crud.toQuery()
// this.update()
}).catch(err => {
this.menuLoading = false
console.log(err.response.data.message)
})
},
// 改变数据
update() {
// 无刷新更新 表格数据
crudRoles.get(this.currentId).then(res => {
for (let i = 0; i < this.crud.data.length; i++) {
if (res.menu_id === this.crud.data[i].menu_id) {
this.crud.data[i] = res
break
}
}
})
}
}
}
</script>
<style rel="stylesheet/scss" lang="scss">
.role-span {
font-weight: bold;
color: #303133;
font-size: 15px;
}
</style>
<style rel="stylesheet/scss" lang="scss" scoped>
::v-deep .el-input-number .el-input__inner {
text-align: left;
}
::v-deep .el-card__header {
padding: 5px 0 5px 10px;
background-color: #f8f8f9;
}
::v-deep .el-card__body {
padding: 10px 0 10px 0;
}
</style>