ref: 用户管理modal抽离

This commit is contained in:
zy7y 2022-09-18 17:39:32 +08:00
parent aefb93b399
commit b6d8c034ad
10 changed files with 327 additions and 5945 deletions

BIN
backend/mini.db-shm Normal file

Binary file not shown.

0
backend/mini.db-wal Normal file
View File

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -0,0 +1,50 @@
<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,45 @@
<script setup>
import { ref, reactive } from 'vue'
const emits = defineEmits(['queryClick', 'resetClick'])
const formRef = ref()
const queryForm = reactive({
name: ''
})
const queryEvent = () => {
emits('queryClick', queryForm)
}
const resetEvent = () => {
formRef.value.resetFields()
emits('resetClick')
}
</script>
<template>
<div class="search">
<a-form ref="formRef" layout="inline" :model="queryForm">
<a-form-item label="名称" name="name">
<a-input placeholder="角色名称" v-model:value="queryForm.name"> </a-input>
</a-form-item>
<a-form-item v-per="'role:query'">
<a-button type="primary" @click="queryEvent">查询</a-button>
<a-button style="margin-left: 10px" @click="resetEvent">重置</a-button>
</a-form-item>
</a-form>
</div>
</template>
<style scoped>
.search {
display: flex;
/* justify-content: center; */
align-content: center;
margin-bottom: 16px;
padding: 24px;
background: #fff;
}
</style>

View File

@ -7,13 +7,9 @@ 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 RoleSearch from './role-search.vue'
/**查询表单响应式数据 */
const queryFormRef = ref()
const queryForm = reactive({
name: ''
})
//
const isQuery = ref(false)
@ -41,7 +37,7 @@ onMounted(() => {
})
//
const getPageData = () => {
const getPageData = (form = null) => {
let offset = pagination.current
let limit = pagination.pageSize
if (!isQuery.value) {
@ -50,7 +46,7 @@ const getPageData = () => {
pagination.total = res.data.total
})
} else {
queryRole({ offset, limit, name: queryForm.name }).then((res) => {
queryRole({ offset, limit, name: form?.name }).then((res) => {
dataSource.value = res.data.items
pagination.total = res.data.total
})
@ -58,14 +54,13 @@ const getPageData = () => {
}
//
const clickQuery = () => {
const clickQuery = (form) => {
isQuery.value = true
getPageData()
getPageData(form)
}
//
const resetQueryForm = () => {
queryFormRef.value.resetFields()
isQuery.value = false
getPageData()
}
@ -209,17 +204,8 @@ const treeData = ref()
<template>
<div class="role">
<!-- 查询 -->
<div class="search">
<a-form ref="queryFormRef" layout="inline" :model="queryForm">
<a-form-item label="名称" name="name">
<a-input placeholder="角色名称" v-model:value="queryForm.name"> </a-input>
</a-form-item>
<a-form-item v-per="'role:query'">
<a-button type="primary" @click="clickQuery">查询</a-button>
<a-button style="margin-left: 10px" @click="resetQueryForm">重置</a-button>
</a-form-item>
</a-form>
</div>
<RoleSearch @query-click="clickQuery" @reset-click="resetQueryForm"></RoleSearch>
<Table
:columns="columns"

View File

@ -0,0 +1,152 @@
<script setup>
import { reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import { addUserRules, putUserRules } from './conf'
import { addUser, putUser } from '@/service/user'
import { userStore } from '@/stores/user'
const props = defineProps({
modalTitle: {
// modal title
type: String
},
modalType: {
// create or update
type: String,
default: 'create'
},
roleOptions: {
//
type: Array,
default: () => []
},
userId: {
// 使
type: Number,
require: false
}
})
const emits = defineEmits(['modalCancel'])
const sotre = userStore()
/** 页面数据 */
const formRef = ref()
// create form
const newUserForm = reactive({
username: '',
nickname: '',
password: '',
roles: []
})
// update form
const putUserForm = reactive({
nickname: '',
password: '',
roles: []
})
const onOk = () => {
formRef.value.validateFields().then(async () => {
let res
if (props.modalType === 'create') {
newUserForm.roles = newUserForm.roles.map((e, i) => ({ rid: e, status: i === 0 ? 5 : 1 }))
res = await addUser(newUserForm)
} 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 })
}
formRef.value.resetFields()
showModal.value = !showModal.value
})
}
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
}
})
})
}
</script>
<template>
<div class="modal">
<a-modal
v-model:visible="showModal"
:title="modalTitle"
ok-text="确认"
cancel-text="取消"
@ok="onOk"
@cancel="onCancel"
>
<!-- 新建 -->
<template v-if="modalType === 'create'">
<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>
</template>
<!-- 编辑 -->
<template v-else>
<a-form ref="formRef" :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>
</template>
</a-modal>
</div>
</template>
<style scoped></style>

View File

@ -0,0 +1,49 @@
<script setup>
import { ref, reactive } from 'vue'
const emits = defineEmits(['queryClick', 'resetClick'])
const formRef = ref()
const queryForm = reactive({
username: '',
nickname: ''
})
const queryEvent = () => {
emits('queryClick', queryForm)
}
const resetEvent = () => {
formRef.value.resetFields()
emits('resetClick')
}
</script>
<template>
<div class="search">
<a-form ref="formRef" layout="inline" :model="queryForm">
<a-form-item label="用户名" name="username">
<a-input placeholder="用户名" v-model:value="queryForm.username"> </a-input>
</a-form-item>
<a-form-item label="昵称" name="nickname">
<a-input placeholder="昵称" v-model:value="queryForm.nickname"> </a-input>
</a-form-item>
<a-form-item v-per="'user:query'">
<a-button type="primary" @click="queryEvent">查询</a-button>
<a-button style="margin-left: 10px" @click="resetEvent">重置</a-button>
</a-form-item>
</a-form>
</div>
</template>
<style scoped>
.search {
display: flex;
/* justify-content: center; */
align-content: center;
margin-bottom: 16px;
padding: 24px;
background: #fff;
}
</style>

View File

@ -8,17 +8,10 @@ import { getRoles } from '@/service/role'
import { columns, addUserRules, putUserRules } from './conf'
import { message } from 'ant-design-vue'
import { userStore } from '@/stores/user'
import UserSearch from './user-search.vue'
const store = userStore()
/**查询表单响应式数据 */
const queryFormRef = ref()
const queryForm = reactive({
username: '',
nickname: ''
})
//
const isQuery = ref(false)
@ -45,7 +38,7 @@ onMounted(() => {
})
//
const getPageData = () => {
const getPageData = (form = null) => {
let offset = pagination.current
let limit = pagination.pageSize
if (!isQuery.value) {
@ -54,24 +47,21 @@ const getPageData = () => {
pagination.total = res.data.total
})
} else {
queryUser({ offset, limit, username: queryForm.username, nickname: queryForm.nickname }).then(
(res) => {
queryUser({ offset, limit, username: form?.username, nickname: form?.nickname }).then((res) => {
dataSource.value = res.data.items
pagination.total = res.data.total
}
)
})
}
}
//
const clickQuery = () => {
const clickQuery = (form) => {
isQuery.value = true
getPageData()
getPageData(form)
}
//
const resetQueryForm = () => {
queryFormRef.value.resetFields()
isQuery.value = false
getPageData()
}
@ -114,8 +104,6 @@ const onOk = () => {
addVisible.value = !addVisible.value
// 2.
formRef.value.resetFields()
// 3.
getPageData()
}
})
})
@ -194,20 +182,7 @@ const onCancelPut = () => {
<template>
<div class="user">
<!-- 查询 -->
<div class="search">
<a-form ref="queryFormRef" layout="inline" :model="queryForm">
<a-form-item label="用户名" name="username">
<a-input placeholder="用户名" v-model:value="queryForm.username"> </a-input>
</a-form-item>
<a-form-item label="昵称" name="nickname">
<a-input placeholder="昵称" v-model:value="queryForm.nickname"> </a-input>
</a-form-item>
<a-form-item v-per="'user:query'">
<a-button type="primary" @click="clickQuery">查询</a-button>
<a-button style="margin-left: 10px" @click="resetQueryForm">重置</a-button>
</a-form-item>
</a-form>
</div>
<UserSearch @query-click="clickQuery" @reset-click="resetQueryForm" />
<Table
:columns="columns"