refactor: role

This commit is contained in:
xingyu 2022-11-15 20:12:22 +08:00
parent fb5b71ca9c
commit a123c47042
4 changed files with 124 additions and 159 deletions

View File

@ -1,8 +1,24 @@
import request from '@/config/axios'
import type { RoleVO } from './types'
export type RoleVO = {
id: number
name: string
code: string
sort: number
status: number
type: number
createTime: string
}
export interface RolePageReqVO extends PageParam {
name?: string
code?: string
status?: number
createTime?: string[]
}
// 查询角色列表
export const getRolePageApi = async (params) => {
export const getRolePageApi = async (params: RolePageReqVO) => {
return await request.get({ url: '/system/role/page', params })
}

View File

@ -1,9 +0,0 @@
export type RoleVO = {
id: number
name: string
code: string
sort: number
status: number
type: number
createTime: string
}

View File

@ -1,42 +1,18 @@
<template>
<!-- 搜索工作区 -->
<ContentWrap>
<Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
</ContentWrap>
<ContentWrap>
<!-- 操作工具栏 -->
<div class="mb-10px">
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:role:create']"
@click="handleCreate()"
/>
</div>
<!-- 列表 -->
<Table
:columns="allSchemas.tableColumns"
:selection="false"
:data="tableObject.tableList"
:loading="tableObject.loading"
:pagination="{
total: tableObject.total
}"
v-model:pageSize="tableObject.pageSize"
v-model:currentPage="tableObject.currentPage"
@register="register"
>
<template #type="{ row }">
<DictTag :type="DICT_TYPE.SYSTEM_ROLE_TYPE" :value="row.type" />
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<!-- 操作新增 -->
<template #toolbar_buttons>
<XButton
type="primary"
preIcon="ep:zoom-in"
:title="t('action.add')"
v-hasPermi="['system:role:create']"
@click="handleCreate()"
/>
</template>
<template #status="{ row }">
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
</template>
<template #createTime="{ row }">
<span>{{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}</span>
</template>
<template #action="{ row }">
<template #actionbtns_default="{ row }">
<XTextButton
preIcon="ep:edit"
:title="t('action.edit')"
@ -47,7 +23,7 @@
preIcon="ep:view"
:title="t('action.detail')"
v-hasPermi="['system:role:update']"
@click="handleDetail(row)"
@click="handleDetail(row.id)"
/>
<XTextButton
preIcon="ep:basketball"
@ -65,10 +41,10 @@
preIcon="ep:delete"
:title="t('action.del')"
v-hasPermi="['system:role:delete']"
@click="delList(row.id, false)"
@click="handleDelete(row.id)"
/>
</template>
</Table>
</vxe-grid>
</ContentWrap>
<XModal v-model="dialogVisible" :title="dialogTitle">
@ -91,10 +67,10 @@
v-if="['create', 'update'].includes(actionType)"
type="primary"
:title="t('action.save')"
:loading="loading"
:loading="actionLoading"
@click="submitForm()"
/>
<XButton :loading="loading" :title="t('dialog.close')" @click="dialogVisible = false" />
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
</template>
</XModal>
<XModal v-model="dialogScopeVisible" :title="dialogScopeTitle">
@ -151,56 +127,66 @@
</el-form>
<!-- 操作按钮 -->
<template #footer>
<XButton type="primary" :title="t('action.save')" :loading="loading" @click="submitScope()" />
<XButton :loading="loading" :title="t('dialog.close')" @click="dialogScopeVisible = false" />
<XButton
type="primary"
:title="t('action.save')"
:loading="actionLoading"
@click="submitScope()"
/>
<XButton
:loading="actionLoading"
:title="t('dialog.close')"
@click="dialogScopeVisible = false"
/>
</template>
</XModal>
</template>
<script setup lang="ts">
import { onMounted, reactive, ref, unref } from 'vue'
import dayjs from 'dayjs'
import {
ElForm,
ElFormItem,
ElInput,
ElSelect,
ElOption,
ElMessage,
ElTree,
ElCard,
ElSwitch
} from 'element-plus'
import { DICT_TYPE, getDictOptions } from '@/utils/dict'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { useMessage } from '@/hooks/web/useMessage'
import { FormExpose } from '@/components/Form'
import { rules, allSchemas } from './role.data'
import type { RoleVO } from '@/api/system/role/types'
import { handleTree } from '@/utils/tree'
import { SystemDataScopeEnum } from '@/utils/constants'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
import { VxeGridInstance } from 'vxe-table'
import * as RoleApi from '@/api/system/role'
import { listSimpleMenusApi } from '@/api/system/menu'
import { listSimpleDeptApi } from '@/api/system/dept'
import * as PermissionApi from '@/api/system/permission'
import type {
PermissionAssignRoleDataScopeReqVO,
PermissionAssignRoleMenuReqVO
} from '@/api/system/permission/types'
import * as RoleApi from '@/api/system/role'
import * as PermissionApi from '@/api/system/permission'
import { listSimpleMenusApi } from '@/api/system/menu'
import { listSimpleDeptApi } from '@/api/system/dept'
import { handleTree } from '@/utils/tree'
import { SystemDataScopeEnum } from '@/utils/constants'
const { t } = useI18n() //
// ========== ==========
const { register, tableObject, methods } = useTable<RoleVO>({
const message = useMessage() //
//
const xGrid = ref<VxeGridInstance>() // Grid Ref
const { gridOptions, reloadList, delList } = useVxeGrid<RoleApi.RoleVO>({
allSchemas: allSchemas,
getListApi: RoleApi.getRolePageApi,
delListApi: RoleApi.deleteRoleApi
})
const { getList, setSearchParams, delList } = methods
// ========== CRUD ==========
const loading = ref(false) //
const actionLoading = ref(false) //
const actionType = ref('') //
const dialogVisible = ref(false) //
const dialogTitle = ref('edit') //
const formRef = ref<FormExpose>() // Ref
const detailRef = ref() // Ref
//
const setDialogTile = (type: string) => {
@ -222,41 +208,44 @@ const handleUpdate = async (rowId: number) => {
unref(formRef)?.setValues(res)
}
//
const handleDelete = async (rowId: number) => {
await delList(xGrid, rowId)
}
//
const submitForm = async () => {
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
loading.value = true
actionLoading.value = true
//
try {
const data = unref(formRef)?.formModel as RoleVO
const data = unref(formRef)?.formModel as RoleApi.RoleVO
if (actionType.value === 'create') {
await RoleApi.createRoleApi(data)
ElMessage.success(t('common.createSuccess'))
message.success(t('common.createSuccess'))
} else {
await RoleApi.updateRoleApi(data)
ElMessage.success(t('common.updateSuccess'))
message.success(t('common.updateSuccess'))
}
//
dialogVisible.value = false
await getList()
} finally {
loading.value = false
actionLoading.value = false
//
reloadList(xGrid)
}
}
})
}
// ========== ==========
const detailRef = ref() // Ref
//
const handleDetail = async (row: RoleVO) => {
//
detailRef.value = row
const handleDetail = async (rowId: number) => {
setDialogTile('detail')
//
const res = await RoleApi.getRoleApi(rowId)
detailRef.value = res
}
// ========== ==========
@ -287,7 +276,7 @@ const handleCheckedTreeNodeAll = () => {
treeRef.value!.setCheckedNodes(treeNodeAll.value ? treeOptions.value : [])
}
//
const handleScope = async (type: string, row: RoleVO) => {
const handleScope = async (type: string, row: RoleApi.RoleVO) => {
dataScopeForm.id = row.id
dataScopeForm.name = row.name
dataScopeForm.code = row.code
@ -329,7 +318,7 @@ const submitScope = async () => {
})
await PermissionApi.assignRoleMenuApi(data.value)
}
ElMessage.success(t('common.updateSuccess'))
message.success(t('common.updateSuccess'))
dialogScopeVisible.value = false
}
const init = () => {
@ -337,7 +326,6 @@ const init = () => {
}
// ========== ==========
onMounted(() => {
getList()
init()
})
</script>

View File

@ -2,7 +2,7 @@ import { reactive } from 'vue'
import { useI18n } from '@/hooks/web/useI18n'
import { required } from '@/utils/formRules'
import { DICT_TYPE } from '@/utils/dict'
import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { VxeCrudSchema, useVxeCrudSchemas } from '@/hooks/web/useVxeCrudSchemas'
// 国际化
const { t } = useI18n()
// 表单校验
@ -12,82 +12,52 @@ export const rules = reactive({
sort: [required]
})
// CrudSchema
const crudSchemas = reactive<CrudSchema[]>([
{
label: t('common.index'),
field: 'id',
type: 'index',
form: {
show: false
const crudSchemas = reactive<VxeCrudSchema>({
primaryKey: 'id',
primaryType: 'seq',
action: true,
actionWidth: '400px',
columns: [
{
title: '角色名称',
field: 'name',
isSearch: true
},
detail: {
show: false
}
},
{
label: '角色名称',
field: 'name',
search: {
show: true
}
},
{
label: '角色类型',
field: 'type',
dictType: DICT_TYPE.SYSTEM_ROLE_TYPE
},
{
label: '角色标识',
field: 'code',
search: {
show: true
}
},
{
label: '显示顺序',
field: 'sort',
form: {
component: 'InputNumber',
value: 0
}
},
{
label: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
search: {
show: true
}
},
{
label: t('common.createTime'),
field: 'createTime',
form: {
show: false
{
title: '角色类型',
field: 'type',
dictType: DICT_TYPE.SYSTEM_ROLE_TYPE
},
detail: {
show: false
{
title: '角色标识',
field: 'code',
isSearch: true
},
search: {
show: true,
component: 'DatePicker',
componentProps: {
type: 'daterange',
valueFormat: 'YYYY-MM-DD HH:mm:ss',
defaultTime: [new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)]
{
title: '显示顺序',
field: 'sort',
form: {
component: 'InputNumber'
}
},
{
title: t('common.status'),
field: 'status',
dictType: DICT_TYPE.COMMON_STATUS,
isSearch: true
},
{
title: t('common.createTime'),
field: 'createTime',
formatter: 'formatDate',
isForm: false,
search: {
show: true,
itemRender: {
name: 'XDataTimePicker'
}
}
}
},
{
field: 'action',
width: '450px',
label: t('table.action'),
form: {
show: false
},
detail: {
show: false
}
}
])
export const { allSchemas } = useCrudSchemas(crudSchemas)
]
})
export const { allSchemas } = useVxeCrudSchemas(crudSchemas)