gin-vue-admin 2.0代码重构

This commit is contained in:
QM303176530
2020-04-04 21:39:07 +08:00
parent 5ed41ddd8c
commit 1b9adea284
202 changed files with 1240 additions and 1299 deletions
+286
View File
@@ -0,0 +1,286 @@
<template>
<div>
<div class="search-term">
<el-form :inline="true" :model="searchInfo" class="demo-form-inline">
<el-form-item label="路径">
<el-input placeholder="路径" v-model="searchInfo.path"></el-input>
</el-form-item>
<el-form-item label="描述">
<el-input placeholder="描述" v-model="searchInfo.description"></el-input>
</el-form-item>
<el-form-item label="请求">
<el-select placeholder="请选择" clearable v-model="searchInfo.method">
<el-option
:key="item.value"
:label="`${item.label}(${item.value})`"
:value="item.value"
v-for="item in methodOptions"
></el-option>
</el-select>
</el-form-item>
<el-form-item>
<el-button @click="onSubmit" type="primary">查询</el-button>
</el-form-item>
<el-form-item>
<el-button @click="openDialog('addApi')" type="primary">新增api</el-button>
</el-form-item>
</el-form>
</div>
<el-table :data="tableData" border stripe>
<el-table-column label="id" min-width="60" prop="ID"></el-table-column>
<el-table-column label="api路径" min-width="150" prop="path"></el-table-column>
<el-table-column label="api分组" min-width="150" prop="apiGroup"></el-table-column>
<el-table-column label="api简介" min-width="150" prop="description"></el-table-column>
<el-table-column label="请求" min-width="150" prop="method">
<template slot-scope="scope">
<div>
{{scope.row.method}}
<el-tag
:key="scope.row.methodFiletr"
:type="scope.row.method|tagTypeFiletr"
effect="dark"
size="mini"
>{{scope.row.method|methodFiletr}}</el-tag>
<!-- {{scope.row.method|methodFiletr}} -->
</div>
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" width="200">
<template slot-scope="scope">
<el-button @click="editApi(scope.row)" size="small" type="text">编辑</el-button>
<el-button @click="deleteApi(scope.row)" size="small" type="text">删除</el-button>
</template>
</el-table-column>
</el-table>
<el-pagination
:current-page="page"
:page-size="pageSize"
:page-sizes="[10, 30, 50, 100]"
:style="{float:'right',padding:'20px'}"
:total="total"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
layout="total, sizes, prev, pager, next, jumper"
></el-pagination>
<el-dialog :before-close="closeDialog" :visible.sync="dialogFormVisible" title="新增Api">
<el-form :inline="true" :model="form" :rules="rules" label-width="80px" ref="apiForm">
<el-form-item label="路径" prop="path">
<el-input autocomplete="off" v-model="form.path"></el-input>
</el-form-item>
<el-form-item label="请求" prop="method">
<el-select placeholder="请选择" v-model="form.method">
<el-option
:key="item.value"
:label="`${item.label}(${item.value})`"
:value="item.value"
v-for="item in methodOptions"
></el-option>
</el-select>
</el-form-item>
<el-form-item label="api分组" prop="apiGroup">
<el-input autocomplete="off" v-model="form.apiGroup"></el-input>
</el-form-item>
<el-form-item label="api简介" prop="description">
<el-input autocomplete="off" v-model="form.description"></el-input>
</el-form-item>
</el-form>
<div class="warning">新增Api需要在角色管理内配置权限才可使用</div>
<div class="dialog-footer" slot="footer">
<el-button @click="closeDialog">取 消</el-button>
<el-button @click="enterDialog" type="primary">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成 条件搜索时候 请把条件安好后台定制的结构体字段 放到 this.searchInfo 中即可实现条件搜索
import {
getApiById,
getApiList,
createApi,
updateApi,
deleteApi
} from '@/api/api'
import infoList from '@/components/mixins/infoList'
const methodOptions = [
{
value: 'POST',
label: '创建',
type: 'success'
},
{
value: 'GET',
label: '查看',
type: ''
},
{
value: 'PUT',
label: '更新',
type: 'warning'
},
{
value: 'DELETE',
label: '删除',
type: 'danger'
}
]
export default {
name: 'Api',
mixins: [infoList],
data() {
return {
listApi: getApiList,
listKey: 'list',
dialogFormVisible: false,
form: {
path: '',
apiGroup: '',
method: '',
description: ''
},
methodOptions: methodOptions,
type: '',
rules: {
path: [{ required: true, message: '请输入api路径', trigger: 'blur' }],
apiGroup: [{ required: true, message: '请输入组名称', trigger: 'blur' }],
method: [
{ required: true, message: '请选择请求方式', trigger: 'blur' }
],
description: [
{ required: true, message: '请输入api介绍', trigger: 'blur' }
]
}
}
},
methods: {
//条件搜索前端看此方法
onSubmit() {
this.page = 1
this.pageSize = 10
this.getTableData()
},
initForm() {
this.form = {
path: '',
apiGroup: '',
description: '',
method: ''
}
},
closeDialog() {
this.initForm()
this.dialogFormVisible = false
},
openDialog(type) {
this.type = type
this.dialogFormVisible = true
},
async editApi(row) {
const res = await getApiById({ id: row.ID })
this.form = res.data.api
this.openDialog('edit')
},
async deleteApi(row) {
this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async () => {
const res = await deleteApi(row)
if (res.success) {
this.$message({
type: 'success',
message: '删除成功!'
})
this.getTableData()
}
})
.catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
})
})
},
async enterDialog() {
this.$refs.apiForm.validate(async valid => {
if (valid) {
switch (this.type) {
case 'addApi':
{
const res = await createApi(this.form)
if (res.success) {
this.$message({
type: 'success',
message: '添加成功',
showClose: true
})
}
this.getTableData()
this.closeDialog()
}
break
case 'edit':
{
const res = await updateApi(this.form)
if (res.success) {
this.$message({
type: 'success',
message: '添加成功',
showClose: true
})
}
this.getTableData()
this.closeDialog()
}
break
default:
{
this.$message({
type: 'error',
message: '未知操作',
showClose: true
})
}
break
}
}
})
}
},
filters: {
methodFiletr(value) {
const target = methodOptions.filter(item => item.value === value)[0]
// return target && `${target.label}(${target.value})`
return target && `${target.label}`
},
tagTypeFiletr(value) {
const target = methodOptions.filter(item => item.value === value)[0]
return target && `${target.type}`
}
}
}
</script>
<style scoped lang="scss">
.button-box {
padding: 10px 20px;
.el-button {
float: right;
}
}
.el-tag--mini {
margin-left: 5px;
}
.warning {
color: #dc143c;
}
</style>
@@ -0,0 +1,201 @@
<template>
<div class="authority">
<div class="button-box clearflex">
<el-button @click="addAuthority('0')" type="primary">新增角色</el-button>
</div>
<el-table
:data="tableData"
:tree-props="{children: 'children', hasChildren: 'hasChildren'}"
border
row-key="authorityId"
stripe
style="width: 100%"
>
<el-table-column label="角色id" min-width="180" prop="authorityId"></el-table-column>
<el-table-column label="角色名称" min-width="180" prop="authorityName"></el-table-column>
<el-table-column fixed="right" label="操作" min-width="300">
<template slot-scope="scope">
<el-button @click="opdendrawer(scope.row)" size="small" type="text">设置权限</el-button>
<el-button @click="deleteAuth(scope.row)" size="small" type="text">删除角色</el-button>
<el-button @click="addAuthority(scope.row.authorityId)" size="small" type="text">新增子角色</el-button>
</template>
</el-table-column>
</el-table>
<!-- 新增角色弹窗 -->
<el-dialog :visible.sync="dialogFormVisible" title="新增角色">
<el-form :model="form" :rules="rules" ref="authorityForm">
<el-form-item label="父级角色ID" prop="parentId">
<el-input autocomplete="off" disabled v-model="form.parentId"></el-input>
</el-form-item>
<el-form-item label="角色ID" prop="authorityId">
<el-input autocomplete="off" v-model="form.authorityId"></el-input>
</el-form-item>
<el-form-item label="角色姓名" prop="authorityName">
<el-input autocomplete="off" v-model="form.authorityName"></el-input>
</el-form-item>
</el-form>
<div class="dialog-footer" slot="footer">
<el-button @click="closeDialog">取 消</el-button>
<el-button @click="enterDialog" type="primary">确 定</el-button>
</div>
</el-dialog>
<el-drawer :visible.sync="drawer" :with-header="false" size="40%" title="角色配置" v-if="drawer">
<el-tabs class="role-box" type="border-card">
<el-tab-pane label="角色菜单">
<Menus :row="activeRow" />
</el-tab-pane>
<el-tab-pane label="角色api">
<apis :row="activeRow" />
</el-tab-pane>
<el-tab-pane label="资源权限">
<Datas :authority="tableData" :row="activeRow" />
</el-tab-pane>
</el-tabs>
</el-drawer>
</div>
</template>
<script>
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
import {
getAuthorityList,
deleteAuthority,
createAuthority
} from '@/api/authority'
import Menus from '@/view/superAdmin/authority/components/menus'
import Apis from '@/view/superAdmin/authority/components/apis'
import Datas from '@/view/superAdmin/authority/components/datas'
import infoList from '@/components/mixins/infoList'
export default {
name: 'Authority',
mixins: [infoList],
data() {
return {
listApi: getAuthorityList,
listKey: 'list',
drawer: false,
activeRow: {},
activeUserId: 0,
dialogFormVisible: false,
apiDialogFlag: false,
form: {
authorityId: '',
authorityName: '',
parentId: '0'
},
rules: {
authorityId: [
{ required: true, message: '请输入角色ID', trigger: 'blur' }
],
authorityName: [
{ required: true, message: '请输入角色名', trigger: 'blur' }
],
parentId: [
{ required: true, message: '请选择请求方式', trigger: 'blur' }
]
}
}
},
components: {
Menus,
Apis,
Datas
},
methods: {
opdendrawer(row) {
this.drawer = true
this.activeRow = row
},
// 删除角色
deleteAuth(row) {
this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async () => {
const res = await deleteAuthority({ authorityId: row.authorityId })
if (res.success) {
this.$message({
type: 'success',
message: '删除成功!'
})
this.getTableData()
}
})
.catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
})
})
},
// 初始化表单
initForm() {
for (const key in this.form) {
this.form[key] = ''
}
},
// 关闭窗口
closeDialog() {
this.initForm()
this.dialogFormVisible = false
this.apiDialogFlag = false
},
// 确定弹窗
async enterDialog() {
if (this.form.authorityId == '0') {
this.$message({
type: 'error',
message: '角色id不能为0'
})
return false
}
this.$refs.authorityForm.validate(async valid => {
if (valid) {
const res = await createAuthority(this.form)
if (res.success) {
this.$message({
type: 'success',
message: '添加成功!'
})
this.getTableData()
this.closeDialog()
}
this.initForm()
this.dialogFormVisible = false
}
})
},
// 增加角色
addAuthority(parentId) {
this.form.parentId = parentId
this.dialogFormVisible = true
}
},
created() {
this.pageSize = 999
}
}
</script>
<style lang="scss">
.authority {
.button-box {
padding: 10px 20px;
.el-button {
float: right;
}
}
}
.role-box {
.el-tabs__content {
height: calc(100vh - 150px);
overflow: auto;
}
}
</style>
@@ -0,0 +1,99 @@
<template>
<div>
<div class="clearflex">
<el-button @click="authApiEnter" class="fl-right" size="small" type="primary">确 定</el-button>
</div>
<el-tree
:data="apiTreeData"
:default-checked-keys="apiTreeIds"
:props="apiDefaultProps"
default-expand-all
highlight-current
node-key="path"
ref="apiTree"
show-checkbox
></el-tree>
</div>
</template>
<script>
import { getAllApis } from '@/api/api'
import { casbinPUpdate, getPolicyPathByAuthorityId } from '@/api/casbin'
export default {
name: 'Apis',
props: {
row: {
default: function() {
return {}
},
type: Object
}
},
data() {
return {
apiTreeData: [],
apiTreeIds: [],
apiDefaultProps: {
children: 'children',
label: 'description'
}
}
},
methods: {
// 创建api树方法
buildApiTree(apis) {
const apiObj = new Object()
apis &&
apis.map(item => {
if (apiObj.hasOwnProperty(item.group)) {
apiObj[item.group].push(item)
} else {
Object.assign(apiObj, { [item.group]: [item] })
}
})
const apiTree = []
for (const key in apiObj) {
const treeNode = {
ID: key,
description: key + '组',
children: apiObj[key]
}
apiTree.push(treeNode)
}
return apiTree
},
// 关联关系确定
async authApiEnter() {
const checkArr = this.$refs.apiTree.getCheckedNodes(true)
var casbinInfos = []
checkArr&&checkArr.map(item=>{
var casbinInfo = {
path:item.path,
method:item.method
}
casbinInfos.push(casbinInfo)
})
const res = await casbinPUpdate({
authorityId: this.activeUserId,
casbinInfos
})
if (res.success) {
this.$message({ type: 'success', message: res.msg })
}
}
},
async created() {
// 获取api并整理成树结构
const res2 = await getAllApis()
const apis = res2.data.apis
this.apiTreeData = this.buildApiTree(apis)
const res = await getPolicyPathByAuthorityId({
authorityId: this.row.authorityId
})
this.activeUserId = this.row.authorityId
this.apiTreeIds = res.data.paths || []
}
}
</script>
<style lang="scss">
</style>
@@ -0,0 +1,96 @@
<template>
<div>
<div class="clearflex" style="margin:18px">
<el-button @click="authDataEnter" class="fl-right" size="small" type="primary">确 定</el-button>
<el-button @click="all" class="fl-left" size="small" type="primary">全选</el-button>
<el-button @click="self" class="fl-left" size="small" type="primary">本部门</el-button>
<el-button @click="selfAndChildren" class="fl-left" size="small" type="primary">部门及以下</el-button>
</div>
<el-checkbox-group v-model="dataAuthorityId" @change="selectAuthority">
<el-checkbox v-for="(item,key) in authoritys" :label="item" :key="key">{{item.authorityName}}</el-checkbox>
</el-checkbox-group>
</div>
</template>
<script>
import {setDataAuthority} from '@/api/authority'
export default {
name: 'Datas',
data() {
return {
authoritys:[],
dataAuthorityId:[]
}
},
props: {
row: {
default: function() {
return {}
},
type: Object
},
authority: {
default: function() {
return {}
},
type: Array
}
},
methods:{
all(){
this.dataAuthorityId = [...this.authoritys]
this.row.dataAuthorityId = this.dataAuthorityId
},
self(){
this.dataAuthorityId = this.authoritys.filter(item=>item.authorityId===this.row.authorityId)
this.row.dataAuthorityId = this.dataAuthorityId
},
selfAndChildren(){
const arrBox = []
this.getChildrenId(this.row,arrBox)
this.dataAuthorityId = this.authoritys.filter(item=>arrBox.indexOf(item.authorityId)>-1)
this.row.dataAuthorityId = this.dataAuthorityId
},
getChildrenId(row,arrBox){
arrBox.push(row.authorityId)
row.children&&row.children.map(item=>{
this.getChildrenId(item,arrBox)
})
},
// 提交
async authDataEnter(){
const res = await setDataAuthority(this.row)
if(res.success){
this.$message({ type: 'success', message: res.msg })
}
},
// 平铺角色
roundAuthority(authoritys){
authoritys&&authoritys.map(item=>{
const obj = {}
obj.authorityId = item.authorityId
obj.authorityName = item.authorityName
this.authoritys.push(obj)
if(item.children.length){
this.roundAuthority(item.children)
}
})
},
// 选择
selectAuthority(){
this.row.dataAuthorityId = this.dataAuthorityId
}
},
created() {
this.authoritys = []
this.dataAuthorityId = []
this.roundAuthority(this.authority)
this.row.dataAuthorityId&&this.row.dataAuthorityId.map(item=>{
const obj = this.authoritys&&this.authoritys.filter(au=>au.authorityId === item.authorityId)&&this.authoritys.filter(au=>au.authorityId === item.authorityId)[0]
this.dataAuthorityId.push(obj)
})
}
}
</script>
<style lang="less">
</style>
@@ -0,0 +1,76 @@
<template>
<div>
<div class="clearflex">
<el-button @click="relation" class="fl-right" size="small" type="primary">确 定</el-button>
</div>
<el-tree
:data="menuTreeData"
:default-checked-keys="menuTreeIds"
:props="menuDefaultProps"
default-expand-all
highlight-current
node-key="ID"
ref="menuTree"
show-checkbox
></el-tree>
</div>
</template>
<script>
import { getBaseMenuTree, getMenuAuthority, addMenuAuthority } from '@/api/menu'
export default {
name: 'Menus',
props: {
row: {
default: function() {
return {}
},
type: Object
}
},
data() {
return {
menuTreeData: [],
menuTreeIds: [],
menuDefaultProps: {
children: 'children',
label: 'nickName'
}
}
},
methods: {
// 关联树 确认方法
async relation() {
const checkArr = this.$refs.menuTree.getCheckedNodes(false, true)
const res = await addMenuAuthority({
menus: checkArr,
authorityId: this.row.authorityId
})
if (res.success) {
this.$message({
type: 'success',
message: '添加成功!'
})
}
}
},
async created() {
// 获取所有菜单树
const res = await getBaseMenuTree()
this.menuTreeData = res.data.menus
const res1 = await getMenuAuthority({ authorityId: this.row.authorityId })
const menus = res1.data.menus
const arr = []
menus.map(item => {
// 防止直接选中父级造成全选
if (!menus.some(same => same.parentId === item.menuId)) {
arr.push(Number(item.menuId))
}
})
this.menuTreeIds = arr
}
}
</script>
<style lang="scss">
</style>
+12
View File
@@ -0,0 +1,12 @@
<template>
<router-view></router-view>
</template>
<script>
export default {
name:"SuperAdmin",
}
</script>
<style lang="scss">
</style>
+239
View File
@@ -0,0 +1,239 @@
<template>
<div>
<div class="button-box clearflex">
<el-button @click="addMenu('0')" type="primary">新增根菜单</el-button>
</div>
<!-- 由于此处菜单跟左侧列表一一对应所以不需要分页 pageSize默认999 -->
<el-table :data="tableData" border row-key="ID" stripe>
<el-table-column label="ID" min-width="100" prop="ID"></el-table-column>
<el-table-column label="路由Name" min-width="160" prop="name"></el-table-column>
<el-table-column label="是否隐藏" min-width="100" prop="hidden">
<template slot-scope="scope">
<span>{{scope.row.hidden?"隐藏":"显示"}}</span>
</template>
</el-table-column>
<el-table-column label="父节点" min-width="90" prop="parentId"></el-table-column>
<el-table-column label="排序" min-width="70" prop="sort"></el-table-column>
<el-table-column label="文件路径" min-width="360" prop="component"></el-table-column>
<el-table-column label="展示名称" min-width="120" prop="authorityName">
<template slot-scope="scope">
<span>{{scope.row.meta.title}}</span>
</template>
</el-table-column>
<el-table-column label="图标" min-width="140" prop="authorityName">
<template slot-scope="scope">
<span>{{scope.row.meta.icon}}</span>
</template>
</el-table-column>
<el-table-column fixed="right" label="操作" width="300">
<template slot-scope="scope">
<el-button @click="deleteMenu(scope.row.ID)" size="small" type="text">删除菜单</el-button>
<el-button @click="editMenu(scope.row.ID)" size="small" type="text">编辑菜单</el-button>
<el-button @click="addMenu(scope.row.ID)" size="small" type="text">添加子菜单</el-button>
</template>
</el-table-column>
</el-table>
<el-dialog :before-close="handleClose" :visible.sync="dialogFormVisible" title="新增菜单">
<el-form :inline="true" :model="form" :rules="rules" label-width="85px" ref="menuForm">
<el-form-item label="路由name" prop="path">
<el-input autocomplete="off" placeholder="唯一英文字符串" v-model="form.path"></el-input>
</el-form-item>
<el-form-item label="是否隐藏">
<el-select placeholder="是否在列表隐藏" v-model="form.hidden">
<el-option :value="false" label="否"></el-option>
<el-option :value="true" label="是"></el-option>
</el-select>
</el-form-item>
<el-form-item label="父节点Id">
<el-input autocomplete="off" disabled v-model="form.parentId"></el-input>
</el-form-item>
<el-form-item label="文件路径" prop="component">
<el-input autocomplete="off" v-model="form.component"></el-input>
</el-form-item>
<el-form-item label="展示名称" prop="meta.title">
<el-input autocomplete="off" v-model="form.meta.title"></el-input>
</el-form-item>
<el-form-item label="图标">
<el-input autocomplete="off" v-model="form.meta.icon"></el-input>
</el-form-item>
<el-form-item label="排序标记">
<el-input autocomplete="off" v-model="form.sort"></el-input>
</el-form-item>
</el-form>
<div class="warning">新增菜单需要在角色管理内配置权限才可使用</div>
<div class="dialog-footer" slot="footer">
<el-button @click="closeDialog">取 消</el-button>
<el-button @click="enterDialog" type="primary">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
import {
updateBaseMenu,
getMenuList,
addBaseMenu,
deleteBaseMenu,
getBaseMenuById
} from '@/api/menu'
import infoList from '@/components/mixins/infoList'
export default {
name: 'Menus',
mixins: [infoList],
data() {
return {
listApi: getMenuList,
listKey: 'list',
dialogFormVisible: false,
form: {
ID: 0,
path: '',
name: '',
hidden: '',
parentId: '',
component: '',
meta: {
title: '',
icon: ''
}
},
rules: {
path: [{ required: true, message: '请输入菜单name', trigger: 'blur' }],
component: [
{ required: true, message: '请输入文件路径', trigger: 'blur' }
],
'meta.title': [
{ required: true, message: '请输入菜单展示名称', trigger: 'blur' }
]
},
isEdit: false
}
},
methods: {
handleClose(done) {
this.initForm()
done()
},
// 懒加载子菜单
load(tree, treeNode, resolve) {
resolve([
{
id: 31,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
},
{
id: 32,
date: '2016-05-01',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄'
}
])
},
// 删除菜单
deleteMenu(ID) {
this.$confirm('此操作将永久删除所有角色下该菜单, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async () => {
const res = await deleteBaseMenu({ ID })
if (res.success) {
this.$message({
type: 'success',
message: '删除成功!'
})
this.getTableData()
}
})
.catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
})
})
},
// 初始化弹窗内表格方法
initForm() {
this.form = {
path: '',
name: '',
hidden: '',
parentId: '',
component: '',
meta: {
title: '',
icon: ''
}
}
},
// 关闭弹窗
closeDialog() {
this.initForm()
this.dialogFormVisible = false
},
// 添加menu
async enterDialog() {
this.$refs.menuForm.validate(async valid => {
if (valid) {
let res
this.form.name = this.form.path
if (this.isEdit) {
res = await updateBaseMenu(this.form)
} else {
res = await addBaseMenu(this.form)
}
if (res.success) {
this.$message({
type: 'success',
message: '添加成功!'
})
this.getTableData()
} else {
this.$message({
type: 'error',
message: '添加失败!'
})
}
this.initForm()
this.dialogFormVisible = false
}
})
},
// 添加菜单方法,id为 0则为添加根菜单
addMenu(id) {
this.form.parentId = String(id)
this.isEdit = false
this.dialogFormVisible = true
},
// 修改菜单方法
async editMenu(id) {
const res = await getBaseMenuById({ id })
this.form = res.data.menu
this.dialogFormVisible = true
this.isEdit = true
}
},
created() {
this.pageSize = 999
}
}
</script>
<style scoped lang="scss">
.button-box {
padding: 10px 20px;
.el-button {
float: right;
}
}
.warning {
color: #dc143c;
}
</style>
+203
View File
@@ -0,0 +1,203 @@
<template>
<div>
<div class="button-box clearflex">
<el-button @click="addUser" type="primary">新增用户</el-button>
</div>
<el-table :data="tableData" border stripe>
<el-table-column label="头像" min-width="50">
<template slot-scope="scope">
<div :style="{'textAlign':'center'}">
<img :src="scope.row.headerImg" height="50" width="50" />
</div>
</template>
</el-table-column>
<el-table-column label="uuid" min-width="250" prop="uuid"></el-table-column>
<el-table-column label="用户名" min-width="150" prop="userName"></el-table-column>
<el-table-column label="昵称" min-width="150" prop="nickName"></el-table-column>
<el-table-column label="用户角色" min-width="150">
<template slot-scope="scope">
<el-select
@change="changeAuthority(scope.row)"
placeholder="请选择"
v-model="scope.row.authority.authorityId"
>
<el-option
:key="item.authorityId"
:label="item.authorityName"
:value="item.authorityId"
v-for="item in authOptions"
></el-option>
</el-select>
</template>
</el-table-column>
</el-table>
<el-pagination
:current-page="page"
:page-size="pageSize"
:page-sizes="[10, 30, 50, 100]"
:style="{float:'right',padding:'20px'}"
:total="total"
@current-change="handleCurrentChange"
@size-change="handleSizeChange"
layout="total, sizes, prev, pager, next, jumper"
></el-pagination>
<el-dialog :visible.sync="addUserDialog" custom-class="user-dialog" title="新增用户">
<el-form :rules="rules" ref="userForm" :model="userInfo" >
<el-form-item label="用户名" label-width="80px" prop="username">
<el-input v-model="userInfo.username"></el-input>
</el-form-item>
<el-form-item label="密码" label-width="80px" prop="password">
<el-input v-model="userInfo.password"></el-input>
</el-form-item>
<el-form-item label="别名" label-width="80px" prop="nickName">
<el-input v-model="userInfo.nickName"></el-input>
</el-form-item>
<el-form-item label="头像" label-width="80px">
<el-upload
:headers="{'x-token':token}"
:on-success="handleAvatarSuccess"
:show-file-list="false"
:action="`${path}/fileUploadAndDownload/upload?noSave=1`"
class="avatar-uploader"
name="file"
>
<img :src="userInfo.headerImg" class="avatar" v-if="userInfo.headerImg" />
<i class="el-icon-plus avatar-uploader-icon" v-else></i>
</el-upload>
</el-form-item>
<el-form-item label="用户角色" label-width="80px" prop="authorityId">
<el-select placeholder="请选择" v-model="userInfo.authorityId">
<el-option
:key="item.authorityId"
:label="item.authorityName"
:value="item.authorityId"
v-for="item in authOptions"
></el-option>
</el-select>
</el-form-item>
</el-form>
<div class="dialog-footer" slot="footer">
<el-button @click="closeAddUserDialog">取 消</el-button>
<el-button @click="enterAddUserDialog" type="primary">确 定</el-button>
</div>
</el-dialog>
</div>
</template>
<script>
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
const path = process.env.VUE_APP_BASE_API
import { getUserList, setUserAuthority, register } from '@/api/user'
import { getAuthorityList } from '@/api/authority'
import infoList from '@/components/mixins/infoList'
import { mapGetters } from 'vuex'
export default {
name: 'Api',
mixins: [infoList],
data() {
return {
listApi: getUserList,
listKey: 'userList',
path:path,
authOptions: [],
addUserDialog: false,
userInfo: {
username: '',
password: '',
nickName: '',
headerImg: '',
authorityId: ''
},
rules: {
username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
password: [
{ required: true, message: '请输入用户密码', trigger: 'blur' }
],
nickName: [
{ required: true, message: '请输入用户昵称', trigger: 'blur' }
],
authorityId: [
{ required: true, message: '请选择用户角色', trigger: 'blur' }
]
},
}
},
computed: {
...mapGetters('user', ['token'])
},
methods: {
async enterAddUserDialog() {
this.$refs.userForm.validate(async valid => {
if (valid) {
const res = await register(this.userInfo)
if (res.success) {
this.$message({ type: 'success', message: '创建成功' })
}
await this.getTableData()
this.closeAddUserDialog()
}
})
},
closeAddUserDialog() {
this.userInfo = {
username: '',
password: '',
nickName: '',
headerImg: '',
authorityId: ''
}
this.addUserDialog = false
},
handleAvatarSuccess(res) {
this.userInfo.headerImg = res.data.file.url
},
addUser() {
this.addUserDialog = true
},
async changeAuthority(row) {
const res = await setUserAuthority({
uuid: row.uuid,
authorityId: row.authority.authorityId
})
if (res.success) {
this.$message({ type: 'success', message: '角色设置成功' })
}
}
},
async created() {
const res = await getAuthorityList({ page: 1, pageSize: 999 })
this.authOptions = res.data.list
}
}
</script>
<style scoped lang="scss">
.button-box {
padding: 10px 20px;
.el-button {
float: right;
}
}
.user-dialog {
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
border: 1px dashed #d9d9d9 !important;
border-radius: 6px;
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
}
</style>