角色管理setup改造完成

This commit is contained in:
piexlmax
2021-12-29 23:49:01 +08:00
parent f0957c615e
commit d2eaf0b38c
4 changed files with 508 additions and 461 deletions
+243 -230
View File
@@ -31,7 +31,7 @@
icon="copy-document"
size="mini"
type="text"
@click="copyAuthority(scope.row)"
@click="copyAuthorityFunc(scope.row)"
>拷贝</el-button>
<el-button
icon="edit"
@@ -94,9 +94,7 @@
</div>
</template>
<script>
// 获取列表内容封装在mixins内部 getTableData方法 初始化已封装完成
<script setup>
import {
getAuthorityList,
deleteAuthority,
@@ -110,262 +108,277 @@ import Apis from '@/view/superAdmin/authority/components/apis.vue'
import Datas from '@/view/superAdmin/authority/components/datas.vue'
import warningBar from '@/components/warningBar/warningBar.vue'
import infoList from '@/mixins/infoList'
export default {
name: 'Authority',
components: {
Menus,
Apis,
Datas,
warningBar
},
mixins: [infoList],
data() {
var mustUint = (rule, value, callback) => {
if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
return callback(new Error('请输入正整数'))
}
return callback()
}
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
return {
AuthorityOption: [
{
authorityId: '0',
authorityName: '根角色'
}
],
listApi: getAuthorityList,
drawer: false,
dialogType: 'add',
activeRow: {},
activeUserId: 0,
dialogTitle: '新增角色',
dialogFormVisible: false,
apiDialogFlag: false,
copyForm: {},
form: {
authorityId: '',
authorityName: '',
parentId: '0'
},
rules: {
authorityId: [
{ required: true, message: '请输入角色ID', trigger: 'blur' },
{ validator: mustUint, trigger: 'blur' }
],
authorityName: [
{ required: true, message: '请输入角色名', trigger: 'blur' }
],
parentId: [
{ required: true, message: '请选择请求方式', trigger: 'blur' }
]
}
const mustUint = (rule, value, callback) => {
if (!/^[0-9]*[1-9][0-9]*$/.test(value)) {
return callback(new Error('请输入正整数'))
}
return callback()
}
const AuthorityOption = ref([
{
authorityId: '0',
authorityName: '根角色'
}
])
const drawer = ref(false)
const dialogType = ref('add')
const activeRow = ref({})
const dialogTitle = ref('新增角色')
const dialogFormVisible = ref(false)
const apiDialogFlag = ref(false)
const copyForm = ref({})
const form = ref({
authorityId: '',
authorityName: '',
parentId: '0'
})
const rules = ref({
authorityId: [
{ required: true, message: '请输入角色ID', trigger: 'blur' },
{ validator: mustUint, trigger: 'blur' }
],
authorityName: [
{ required: true, message: '请输入角色名', trigger: 'blur' }
],
parentId: [
{ required: true, message: '请选择请求方式', trigger: 'blur' }
]
})
const page = ref(1)
const total = ref(0)
const pageSize = ref(999)
const tableData = ref([])
const searchInfo = ref({})
// 查询
const getTableData = async() => {
const table = await getAuthorityList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
if (table.code === 0) {
tableData.value = table.data.list
total.value = table.data.total
page.value = table.data.page
pageSize.value = table.data.pageSize
}
}
getTableData()
const changeRow = (key, value) => {
activeRow.value[key] = value
}
const menus = ref(null)
const apis = ref(null)
const datas = ref(null)
const autoEnter = (activeName, oldActiveName) => {
const paneArr = [menus, apis, datas]
if (oldActiveName) {
console.log(paneArr[oldActiveName].value.needConfirm)
if (paneArr[oldActiveName].value.needConfirm) {
paneArr[oldActiveName].value.enterAndNext()
paneArr[oldActiveName].value.needConfirm = false
}
},
async created() {
this.pageSize = 999
await this.getTableData()
},
methods: {
changeRow(key, value) {
this.activeRow[key] = value
},
autoEnter(activeName, oldActiveName) {
const paneArr = ['menus', 'apis', 'datas']
if (oldActiveName) {
if (this.$refs[paneArr[oldActiveName]].needConfirm) {
this.$refs[paneArr[oldActiveName]].enterAndNext()
this.$refs[paneArr[oldActiveName]].needConfirm = false
}
}
// 拷贝角色
const copyAuthorityFunc = (row) => {
setOptions()
dialogTitle.value = '拷贝角色'
dialogType.value = 'copy'
for (const k in form.value) {
form.value[k] = row[k]
}
copyForm.value = row
dialogFormVisible.value = true
}
const opdendrawer = (row) => {
drawer.value = true
activeRow.value = row
}
// 删除角色
const deleteAuth = (row) => {
ElMessageBox.confirm('此操作将永久删除该角色, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.then(async() => {
const res = await deleteAuthority({ authorityId: row.authorityId })
if (res.code === 0) {
ElMessage({
type: 'success',
message: '删除成功!'
})
if (tableData.value.length === 1 && page.value > 1) {
page.value--
}
getTableData()
}
},
// 拷贝角色
copyAuthority(row) {
this.setOptions()
this.dialogTitle = '拷贝角色'
this.dialogType = 'copy'
for (const k in this.form) {
this.form[k] = row[k]
}
this.copyForm = row
this.dialogFormVisible = true
},
opdendrawer(row) {
this.drawer = true
this.activeRow = row
},
// 删除角色
deleteAuth(row) {
this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
confirmButtonText: '确定',
cancelButtonText: '取消',
type: 'warning'
})
.catch(() => {
ElMessage({
type: 'info',
message: '已取消删除'
})
.then(async() => {
const res = await deleteAuthority({ authorityId: row.authorityId })
})
}
// 初始化表单
const authorityForm = ref(null)
const initForm = () => {
if (authorityForm.value) {
authorityForm.value.resetFields()
}
form.value = {
authorityId: '',
authorityName: '',
parentId: '0'
}
}
// 关闭窗口
const closeDialog = () => {
initForm()
dialogFormVisible.value = false
apiDialogFlag.value = false
}
// 确定弹窗
const enterDialog = () => {
if (form.value.authorityId === '0') {
ElMessage({
type: 'error',
message: '角色id不能为0'
})
return false
}
authorityForm.value.validate(async valid => {
if (valid) {
switch (dialogType.value) {
case 'add':
{
const res = await createAuthority(form.value)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '添加成功!'
})
getTableData()
closeDialog()
}
}
break
case 'edit':
{
const res = await updateAuthority(form.value)
if (res.code === 0) {
ElMessage({
type: 'success',
message: '添加成功!'
})
getTableData()
closeDialog()
}
}
break
case 'copy': {
const data = {
authority: {
authorityId: 'string',
authorityName: 'string',
datauthorityId: [],
parentId: 'string'
},
oldAuthorityId: 0
}
data.authority.authorityId = form.value.authorityId
data.authority.authorityName = form.value.authorityName
data.authority.parentId = form.value.parentId
data.authority.dataAuthorityId = copyForm.value.dataAuthorityId
data.oldAuthorityId = copyForm.value.authorityId
const res = await copyAuthority(data)
if (res.code === 0) {
this.$message({
ElMessage({
type: 'success',
message: '删除成功!'
message: '复制成功!'
})
if (this.tableData.length === 1 && this.page > 1) {
this.page--
}
this.getTableData()
getTableData()
}
})
.catch(() => {
this.$message({
type: 'info',
message: '已取消删除'
})
})
},
// 初始化表单
initForm() {
if (this.$refs.authorityForm) {
this.$refs.authorityForm.resetFields()
}
this.form = {
authorityId: '',
authorityName: '',
parentId: '0'
}
},
// 关闭窗口
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) {
switch (this.dialogType) {
case 'add':
{
const res = await createAuthority(this.form)
if (res.code === 0) {
this.$message({
type: 'success',
message: '添加成功!'
})
this.getTableData()
this.closeDialog()
}
}
break
case 'edit':
{
const res = await updateAuthority(this.form)
if (res.code === 0) {
this.$message({
type: 'success',
message: '添加成功!'
})
this.getTableData()
this.closeDialog()
}
}
break
case 'copy': {
const data = {
authority: {
authorityId: 'string',
authorityName: 'string',
datauthorityId: [],
parentId: 'string'
},
oldAuthorityId: 0
}
data.authority.authorityId = this.form.authorityId
data.authority.authorityName = this.form.authorityName
data.authority.parentId = this.form.parentId
data.authority.dataAuthorityId = this.copyForm.dataAuthorityId
data.oldAuthorityId = this.copyForm.authorityId
const res = await copyAuthority(data)
if (res.code === 0) {
this.$message({
type: 'success',
message: '复制成功!'
})
this.getTableData()
}
}
}
this.initForm()
this.dialogFormVisible = false
}
})
},
setOptions() {
this.AuthorityOption = [
{
authorityId: '0',
authorityName: '根角色'
}
]
this.setAuthorityOptions(this.tableData, this.AuthorityOption, false)
},
setAuthorityOptions(AuthorityData, optionsData, disabled) {
this.form.authorityId = String(this.form.authorityId)
AuthorityData &&
}
initForm()
dialogFormVisible.value = false
}
})
}
const setOptions = () => {
AuthorityOption.value = [
{
authorityId: '0',
authorityName: '根角色'
}
]
setAuthorityOptions(tableData.value, AuthorityOption.value, false)
}
const setAuthorityOptions = (AuthorityData, optionsData, disabled) => {
form.value.authorityId = String(form.value.authorityId)
AuthorityData &&
AuthorityData.forEach(item => {
if (item.children && item.children.length) {
const option = {
authorityId: item.authorityId,
authorityName: item.authorityName,
disabled: disabled || item.authorityId === this.form.authorityId,
disabled: disabled || item.authorityId === form.value.authorityId,
children: []
}
this.setAuthorityOptions(
setAuthorityOptions(
item.children,
option.children,
disabled || item.authorityId === this.form.authorityId
disabled || item.authorityId === form.value.authorityId
)
optionsData.push(option)
} else {
const option = {
authorityId: item.authorityId,
authorityName: item.authorityName,
disabled: disabled || item.authorityId === this.form.authorityId
disabled: disabled || item.authorityId === form.value.authorityId
}
optionsData.push(option)
}
})
},
// 增加角色
addAuthority(parentId) {
this.initForm()
this.dialogTitle = '新增角色'
this.dialogType = 'add'
this.form.parentId = parentId
this.setOptions()
this.dialogFormVisible = true
},
// 编辑角色
editAuthority(row) {
this.setOptions()
this.dialogTitle = '编辑角色'
this.dialogType = 'edit'
for (const key in this.form) {
this.form[key] = row[key]
}
this.setOptions()
this.dialogFormVisible = true
}
}
// 增加角色
const addAuthority = (parentId) => {
initForm()
dialogTitle.value = '新增角色'
dialogType.value = 'add'
form.value.parentId = parentId
setOptions()
dialogFormVisible.value = true
}
// 编辑角色
const editAuthority = (row) => {
setOptions()
dialogTitle.value = '编辑角色'
dialogType.value = 'edit'
for (const key in form.value) {
form.value[key] = row[key]
}
setOptions()
dialogFormVisible.value = true
}
</script>
<script>
export default {
name: 'Authority'
}
</script>