res: 角色管理modal抽取,优化用户管理modal实现

This commit is contained in:
zy7y 2022-09-19 00:08:10 +08:00
parent b6d8c034ad
commit 9b16af74ea
11 changed files with 6162 additions and 456 deletions

File diff suppressed because it is too large Load Diff

View File

@ -26,7 +26,7 @@ defineProps({
</template>
<style scoped>
.v-md-editor-preview /deep/ .github-markdown-body {
:deep(.github-markdown-body) {
padding: 5px 20px !important;
margin: 0px !important;
}

View File

@ -38,17 +38,14 @@ const emits = defineEmits(['createClick', 'updateClick', 'deleteClick'])
//
const createEvent = () => {
console.log('点击了 table组件 新增 -> 向父组件传递')
emits('createClick')
}
const updateEvent = (record) => {
console.log('点击了 table组件 更新 -> 向父组件传递', record)
emits('updateClick', record)
}
const deleteEvent = (record) => {
console.log('点击了 table组件 删除-> 向父组件传递', record)
emits('deleteClick', record)
}
@ -68,7 +65,7 @@ const expand = tableTree()
</template>
<a-table
:columns="columns"
:scroll="{ y: 'calc(100vh - 300px)' }"
:scroll="{ y: 'calc(100vh - 420px)' }"
:data-source="dataSource"
:pagination="pagination"
:row-key="(record) => record.id"

View File

@ -16,13 +16,7 @@ const routes = [
name: 'main',
path: '/main',
meta: { title: '主页' },
component: () => import('@/views/main/main.vue'),
children: [
{
path: '/demo',
component: () => import('@/views/main/demo.vue')
}
]
component: () => import('@/views/main/main.vue')
},
{
path: '/:pathMatch(.*)*',

View File

@ -18,6 +18,8 @@ export const userStore = defineStore(
const isLoading = ref(false)
const isPush = ref(false)
// getter
const accessToken = computed(() => 'Bearer ' + token.value)
@ -83,6 +85,7 @@ export const userStore = defineStore(
}
return {
isPush,
token,
accessToken,
userInfo,

View File

@ -1,50 +0,0 @@
<script setup>
import { ref } from 'vue'
import UserModal from './system/user/user-modal.vue'
import { getRoles } from '@/service/role'
const isShow = ref(false)
const title = ref()
const type = ref()
const roleOptions = ref([])
getRoles({ limit: 100 }).then((res) => {
roleOptions.value = res.data.items.map((e) => ({ label: e.name, value: e.id }))
})
const add = () => {
isShow.value = !isShow.value
title.value = '新增用户'
}
const put = () => {
console.log('put')
}
const ok = (formRef) => {
formRef.value.validateFields().then(() => {})
}
const cancel = () => {
console.log('cancel')
isShow.value = !isShow.value
}
</script>
<template>
<div>
demo
<a-button @click="add">新增</a-button>
<a-button @click="put">更新</a-button>
<UserModal
:modal-title="title"
:modal-type="type"
:show-modal="isShow"
:role-options="roleOptions"
@modal-ok="ok"
@modal-cancel="cancel"
></UserModal>
</div>
</template>
<style scoped></style>

View File

@ -0,0 +1,151 @@
<script setup>
import { ref, reactive, watch } from 'vue'
import { message } from 'ant-design-vue'
import { rules, treeFieldNames } from './conf'
import { addRole, putRole } from '@/service/role'
import { getMenus as getRoleMenu } from '@/service/user'
import { getMenus } from '@/service/menu'
import { userStore } from '@/stores/user'
const props = defineProps({
modalTitle: {
// modal title
type: String
},
modalType: {
// create or update
type: String,
default: 'create'
}
})
const showModal = ref(false)
const formRef = ref()
const roleForm = reactive({
name: '',
remark: '',
menus: []
})
// ID
const userId = ref()
// menu
const treeData = ref()
const treeRef = ref()
watch(showModal, async (newValue) => {
if (newValue) {
const res = await getMenus()
treeData.value = res.data
}
})
//
const checkedKeys = ref([])
//
const expandedKeys = ref([])
//
const check = (checkedKeys, e) => {
roleForm.menus = [...e.halfCheckedKeys, ...checkedKeys]
}
// menus treemenus
const getCurrentMenu = (record) => {
let allMenus = []
let checkMenus = []
getRoleMenu(record.id).then((res) => {
function _mids(menus) {
for (const menu of menus) {
allMenus.push(menu.id)
if (menu.children) {
_mids(menu.children)
} else {
checkMenus.push(menu.id)
}
}
}
_mids(res.data)
})
return { allMenus, checkMenus }
}
//
const openModal = (record) => {
showModal.value = true
const { allMenus, checkMenus } = getCurrentMenu(record)
userId.value = record.id
roleForm.name = record.name
roleForm.remark = record.remark
checkedKeys.value = checkMenus
roleForm.menus = allMenus
}
// modal
const resetData = () => {
formRef.value.resetFields()
checkedKeys.value = []
expandedKeys.value = []
}
const onOk = () => {
formRef.value.validateFields().then(async () => {
//
let res
if (props.modalType === 'create') {
res = await addRole(roleForm)
} else {
res = await putRole(userId.value, roleForm)
}
message.success(res.msg)
resetData()
showModal.value = false
userStore().isPush = true
})
}
const onCancel = () => {
resetData()
}
defineExpose({ showModal, openModal })
</script>
<template>
<div class="modal">
<a-modal
v-model:visible="showModal"
:title="modalTitle"
ok-text="确认"
cancel-text="取消"
@ok="onOk"
@cancel="onCancel"
>
<a-form ref="formRef" :model="roleForm" :rules="rules">
<a-form-item name="name" label="名称">
<a-input v-model:value="roleForm.name" />
</a-form-item>
<a-form-item name="remark" label="描述">
<a-input v-model:value="roleForm.remark" />
</a-form-item>
<a-form-item name="menus" label="菜单">
<a-tree
ref="treeRef"
checkable
:tree-data="treeData"
:fieldNames="treeFieldNames"
@check="check"
v-model:checkedKeys="checkedKeys"
v-model:expandedKeys="expandedKeys"
>
</a-tree>
</a-form-item>
</a-form>
</a-modal>
</div>
</template>
<style scoped></style>

View File

@ -1,13 +1,22 @@
<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ref, reactive, onMounted, toRefs } from 'vue'
import Table from '@/components/table/table.vue'
import { getRoles, queryRole, delRole, putRole, addRole } from '@/service/role'
import { getMenus } from '@/service/menu'
import { columns, rules, treeFieldNames } from './conf'
import { message } from 'ant-design-vue'
import { getMenus as getRoleMenu } from '@/service/user'
import { getRoles, queryRole, delRole } from '@/service/role'
import { columns } from './conf'
import RoleSearch from './role-search.vue'
import RoleModal from './role-modal.vue'
import { userStore } from '@/stores/user'
const store = userStore()
store.$subscribe((mutation, state) => {
if (state.isPush) {
getPageData()
state.isPush = false
}
})
/**查询表单响应式数据 */
@ -71,134 +80,27 @@ const delClick = (record) => {
getPageData()
}
/**新增角色 */
const addVisible = ref(false)
const formRef = ref(null)
const newRoleForm = reactive({
name: '',
remark: '',
menus: []
//
const modalRef = ref()
const modalConf = reactive({
title: '',
type: ''
})
//
const checkedKeys = ref([])
//
const expandedKeys = ref([])
//
const check = (checkedKeys, e) => {
newRoleForm.menus = [...e.halfCheckedKeys, ...checkedKeys]
}
const addClick = () => {
addVisible.value = !addVisible.value
// modal
getMenus().then((res) => (treeData.value = res.data))
modalConf.title = '新增角色'
modalConf.type = 'create'
modalRef.value.showModal = true
}
// modal
const onOk = () => {
formRef.value.validateFields().then(() => {
//
addRole(newRoleForm).then((res) => {
if (res.msg === '请求成功') {
message.success('新增成功')
// 1. modal
addVisible.value = !addVisible.value
// 2.
formRef.value.resetFields()
checkedKeys.value = []
expandedKeys.value = []
// 3.
getPageData()
}
})
})
}
// modal
const onCancel = () => {
// 2.
formRef.value.resetFields()
checkedKeys.value = []
expandedKeys.value = []
}
/**更新 */
/**
* 编辑时获取数据 回显到put modal
* @param {} record 行数据
*/
// modal
const putRoleForm = reactive({
name: '',
remark: '',
menus: []
})
// modal
const putClick = (record) => {
putVisible.value = !putVisible.value
console.log(record, '父组件')
getPutModalData(record)
modalConf.title = '编辑角色'
modalConf.type = 'update'
modalRef.value.openModal(record)
}
const getPutModalData = (record) => {
// modal
getMenus().then((res) => (treeData.value = res.data))
// ,
getRoleMenu(record.id).then((res) => {
function _mids(menus) {
for (const menu of menus) {
if (menu.children) {
_mids(menu.children)
} else {
putRoleForm.menus.push(menu.id)
}
}
}
_mids(res.data)
})
putId.value = record.id
putRoleForm.name = record.name
putRoleForm.remark = record.remark
}
// modal
const putVisible = ref(false)
// modal
const putRoleFormRef = ref()
// id 便
const putId = ref()
//modal
const onOkPut = () => {
//
putRoleFormRef.value.validateFields().then(() => {
//
putRole(putId.value, putRoleForm).then((res) => {
if (res.msg === '请求成功') {
message.success('修改成功')
// 1. modal
putVisible.value = !putVisible.value
// 2.
putRoleFormRef.value.resetFields()
// 3.
getPageData()
}
})
})
}
const onCancelPut = () => {
putRoleFormRef.value.resetFields()
}
// tree
const treeData = ref()
const { title, type } = toRefs(modalConf)
</script>
<template>
@ -218,62 +120,8 @@ const treeData = ref()
@delete-click="delClick"
>
</Table>
<!-- 新增 -->
<a-modal
v-model:visible="addVisible"
title="新建角色"
ok-text="创建"
cancel-text="取消"
@ok="onOk"
@cancel="onCancel"
>
<a-form ref="formRef" :model="newRoleForm" :rules="rules">
<a-form-item name="name" label="名称">
<a-input v-model:value="newRoleForm.name" />
</a-form-item>
<a-form-item name="remark" label="描述">
<a-input v-model:value="newRoleForm.remark" />
</a-form-item>
<a-form-item name="menus" label="菜单">
<a-tree
checkable
:tree-data="treeData"
:fieldNames="treeFieldNames"
@check="check"
v-model:checkedKeys="checkedKeys"
v-model:expandedKeys="expandedKeys"
>
</a-tree>
</a-form-item>
</a-form>
</a-modal>
<!-- 修改 -->
<a-modal
v-model:visible="putVisible"
title="编辑角色"
ok-text="确认"
cancel-text="取消"
@ok="onOkPut"
@cancel="onCancelPut"
>
<a-form ref="putRoleFormRef" :model="putRoleForm" :rules="rules">
<a-form-item name="name" label="名称">
<a-input v-model:value="putRoleForm.name" />
</a-form-item>
<a-form-item name="remark" label="描述">
<a-input v-model:value="putRoleForm.remark" />
</a-form-item>
<a-form-item name="menus" label="菜单">
<a-tree
checkable
:tree-data="treeData"
:fieldNames="treeFieldNames"
v-model:checkedKeys="putRoleForm.menus"
></a-tree>
</a-form-item>
</a-form>
</a-modal>
<RoleModal ref="modalRef" :modal-title="title" :modal-type="type" />
</div>
</template>

View File

@ -1,10 +1,11 @@
<script setup>
import { reactive, ref } from 'vue'
import { reactive, ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { addUserRules, putUserRules } from './conf'
import { addUser, putUser } from '@/service/user'
import { addUser, putUser, getUserInfo } from '@/service/user'
import { userStore } from '@/stores/user'
import { getRoles } from '@/service/role'
const props = defineProps({
modalTitle: {
@ -15,22 +16,10 @@ const props = defineProps({
// create or update
type: String,
default: 'create'
},
roleOptions: {
//
type: Array,
default: () => []
},
userId: {
// 使
type: Number,
require: false
}
})
const emits = defineEmits(['modalCancel'])
const sotre = userStore()
const store = userStore()
/** 页面数据 */
const formRef = ref()
@ -50,6 +39,34 @@ const putUserForm = reactive({
roles: []
})
// modal
const showModal = ref(false)
// id
const userId = ref()
// modal
const roleOptions = ref([])
watch(showModal, async () => {
if (showModal.value) {
const res = await getRoles({ limit: 100 })
roleOptions.value = res.data.items.map((e) => ({ label: e.name, value: e.id }))
}
})
// modal -
const openModal = async (record) => {
// modal
showModal.value = !showModal.value
userId.value = record.id
// id
const res = await getUserInfo(record.id)
putUserForm.roles = res.data.roles.map((e) => e.id)
//
putUserForm.nickname = res.data.nickname
putUserForm.password = '加密之后的密码'
}
// modal
const onOk = () => {
formRef.value.validateFields().then(async () => {
let res
@ -59,36 +76,31 @@ const onOk = () => {
} else {
const { nickname, password, roles } = putUserForm
let rids = roles.map((e, i) => ({ rid: e, status: i === 0 ? 5 : 1 }))
res = await putUser(props.userId, { nickname, password, roles: rids })
res = await putUser(userId.value, {
nickname,
password,
roles: rids
})
if (userId.value === store.userInfo.id) {
//
if (rids[0]['rid'] !== store.userInfo.roles[0]['id']) {
store.getUserData(userId.value)
}
}
}
message.success(res.msg)
formRef.value.resetFields()
showModal.value = !showModal.value
store.isPush = true
})
}
// modal
const onCancel = () => {
formRef.value.resetFields()
emits('modalCancel')
}
const showModal = ref(false)
//
const addUserAction = () => {
formRef.value.validateFields().then(() => {
//
newUserForm.roles = newUserForm.roles.map((e, i) => ({ rid: e, status: i === 0 ? 5 : 1 }))
addUser(newUserForm).then((res) => {
if (res.msg === '请求成功') {
message.success('新增成功')
// 2.
formRef.value.resetFields()
// 1. modal
showModal.value = !showModal.value
}
})
})
}
defineExpose({ showModal, openModal })
</script>
<template>

View File

@ -22,7 +22,7 @@ const resetEvent = () => {
<template>
<div class="search">
<a-form ref="formRef" layout="inline" :model="queryForm">
<a-form ref="formRef" layout="inline" :model="queryForm" name="search">
<a-form-item label="用户名" name="username">
<a-input placeholder="用户名" v-model:value="queryForm.username"> </a-input>
</a-form-item>

View File

@ -3,21 +3,37 @@ import { ref, reactive, onMounted } from 'vue'
import Table from '@/components/table/table.vue'
import { getUsers, queryUser, delUser, addUser, getUserInfo, putUser } from '@/service/user'
import { getRoles } from '@/service/role'
import { columns, addUserRules, putUserRules } from './conf'
import { message } from 'ant-design-vue'
import { userStore } from '@/stores/user'
import { getUsers, queryUser, delUser } from '@/service/user'
import { columns } from './conf'
import UserSearch from './user-search.vue'
import UserModal from './user-modal.vue'
import { userStore } from '@/stores/user'
import { message } from 'ant-design-vue'
const store = userStore()
// sotre isPush
store.$subscribe((mutation, state) => {
if (state.isPush) {
getPageData()
state.isPush = false
}
})
//
const isQuery = ref(false)
//
const dataSource = ref([])
const modalRef = ref()
// modal
const modalConf = reactive({
title: '',
type: ''
})
//
const pagination = reactive({
current: 1, //
@ -63,119 +79,29 @@ const clickQuery = (form) => {
//
const resetQueryForm = () => {
isQuery.value = false
getPageData()
store.isPush = true
}
//
const delClick = (record) => {
delUser(record.id)
const delClick = async (record) => {
const res = await delUser(record.id)
message.success(res.msg)
getPageData()
}
/**新增用户 */
const addVisible = ref(false)
const formRef = ref(null)
const newUserForm = reactive({
username: '',
nickname: '',
password: '',
roles: []
})
//
const roleOptions = ref([])
/**新增 */
const addClick = () => {
addVisible.value = !addVisible.value
//
getRoles({ limit: 100 }).then((res) => {
roleOptions.value = res.data.items.map((e) => ({ label: e.name, value: e.id }))
})
const addClick = async () => {
modalConf.title = '新增用户'
modalConf.type = 'create'
modalRef.value.showModal = !modalRef.value.showModal
}
// modal
const onOk = () => {
formRef.value.validateFields().then(() => {
//
newUserForm.roles = newUserForm.roles.map((e, i) => ({ rid: e, status: i === 0 ? 5 : 1 }))
addUser(newUserForm).then((res) => {
if (res.msg === '请求成功') {
message.success('新增成功')
// 1. modal
addVisible.value = !addVisible.value
// 2.
formRef.value.resetFields()
}
})
})
}
// modal
const onCancel = () => {
// 2.
formRef.value.resetFields()
}
/**更新用户 */
const putClick = (record) => {
// modal
putVisible.value = !putVisible.value
//
getRoles({ limit: 100 }).then((res) => {
roleOptions.value = res.data.items.map((e) => ({ label: e.name, value: e.id }))
})
putId.value = record.id
// id
getUserInfo(record.id).then((res) => {
//
putUserForm.roles = res.data.roles.map((e) => e.id)
//
putUserForm.nickname = res.data.nickname
putUserForm.password = '加密之后的密码'
})
}
const putVisible = ref(false)
const putUserFormRef = ref()
const putUserForm = reactive({
nickname: '',
password: '',
roles: []
})
const putId = ref()
//modal
const onOkPut = async () => {
//
putUserFormRef.value.validateFields().then(async () => {
//
const { nickname, password, roles } = putUserForm
let rids = roles.map((e, i) => ({ rid: e, status: i === 0 ? 5 : 1 }))
const res = await putUser(putId.value, { nickname, password, roles: rids })
if (res.msg === '请求成功') {
message.success('修改成功')
// 1. modal
putVisible.value = !putVisible.value
// 2.
putUserFormRef.value.resetFields()
//
if (putId.value === store.userInfo.id) {
//
if (rids[0]['rid'] !== store.userInfo.roles[0]['id']) {
store.getUserData(putId.value)
// todo
}
} else {
// 3.
getPageData()
}
}
})
}
const onCancelPut = () => {
putUserFormRef.value.resetFields()
/**编辑 */
const putClick = async (record) => {
modalConf.title = '编辑用户'
modalConf.type = 'update'
modalRef.value.openModal(record)
}
</script>
@ -184,6 +110,7 @@ const onCancelPut = () => {
<!-- 查询 -->
<UserSearch @query-click="clickQuery" @reset-click="resetQueryForm" />
<!-- 列表数据 -->
<Table
:columns="columns"
:data-source="dataSource"
@ -193,67 +120,10 @@ const onCancelPut = () => {
@create-click="addClick"
@update-click="putClick"
@delete-click="delClick"
>
</Table>
/>
<!-- 新增 用户-->
<a-modal
v-model:visible="addVisible"
title="新建用户"
ok-text="创建"
cancel-text="取消"
@ok="onOk"
@cancel="onCancel"
>
<a-form ref="formRef" :model="newUserForm" :rules="addUserRules">
<a-form-item name="username" label="账号">
<a-input v-model:value="newUserForm.username" placeholder="用于登录" />
</a-form-item>
<a-form-item name="nickname" label="昵称">
<a-input v-model:value="newUserForm.nickname" />
</a-form-item>
<a-form-item name="password" label="密码">
<a-input-password v-model:value="newUserForm.password" autocomplete="on" />
</a-form-item>
<a-form-item name="roles" label="角色">
<a-select
v-model:value="newUserForm.roles"
mode="multiple"
style="width: 100%"
placeholder="默认激活第一个角色"
:options="roleOptions"
></a-select>
</a-form-item>
</a-form>
</a-modal>
<!-- 修改用户 -->
<a-modal
v-model:visible="putVisible"
title="编辑用户"
ok-text="确认"
cancel-text="取消"
@ok="onOkPut"
@cancel="onCancelPut"
>
<a-form ref="putUserFormRef" :model="putUserForm" :rules="putUserRules">
<a-form-item name="nickname" label="昵称">
<a-input v-model:value="putUserForm.nickname" />
</a-form-item>
<a-form-item name="password" label="密码">
<a-input-password v-model:value="putUserForm.password" autocomplete="on" />
</a-form-item>
<a-form-item name="roles" label="角色">
<a-select
v-model:value="putUserForm.roles"
mode="multiple"
style="width: 100%"
placeholder="默认激活第一个角色"
:options="roleOptions"
></a-select>
</a-form-item>
</a-form>
</a-modal>
<!-- 新增&编辑 -->
<UserModal ref="modalRef" :modal-title="modalConf.title" :modal-type="modalConf.type" />
</div>
</template>