后台用户管理使用ProMax组件

后台用户管理使用ProMax组件
This commit is contained in:
TimSpan 2024-09-09 15:01:20 +08:00
parent be3eea4a2a
commit 98a0438393
5 changed files with 253 additions and 365 deletions

BIN
.DS_Store vendored

Binary file not shown.

View File

@ -1,4 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="VcsDirectoryMappings" defaultProject="true" /> <component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project> </project>

View File

@ -1,15 +1,16 @@
<template> <template>
<div class="table-pro-content"> <div class="table-pro-content">
<div class="card padding" v-if="props.searchFormOptions"> <div class="card padding" v-if="props.searchFormOptions">
<FormProMax ref="searchFormRef" :form-item-options="props.searchFormOptions" v-model:value="searchParams" v-bind="props.searchFormProps"> <FormProMax ref="searchFormRef" :form-item-options="props.searchFormOptions" v-model:value="searchParams"
v-bind="props.searchFormProps">
<template v-slot:formOperation> <template v-slot:formOperation>
<a-space class="margin-right flex-end"> <a-space class="margin-right flex-end">
<a-button type="primary" @click="search"> <a-button type="primary" @click="search">
<search-outlined /> <search-outlined/>
搜索 搜索
</a-button> </a-button>
<a-button danger @click="resetFormAndTable"> <a-button danger @click="resetFormAndTable">
<rollback-outlined /> <rollback-outlined/>
重置 重置
</a-button> </a-button>
</a-space> </a-space>
@ -26,7 +27,7 @@
<a-tooltip> <a-tooltip>
<template #title>刷新数据</template> <template #title>刷新数据</template>
<a-button shape="circle" @click="requestGetTableData"> <a-button shape="circle" @click="requestGetTableData">
<ReloadOutlined /> <ReloadOutlined/>
</a-button> </a-button>
</a-tooltip> </a-tooltip>
</template> </template>
@ -34,34 +35,34 @@
<a-tooltip> <a-tooltip>
<template #title>打印数据</template> <template #title>打印数据</template>
<a-button shape="circle"> <a-button shape="circle">
<PrinterOutlined /> <PrinterOutlined/>
</a-button> </a-button>
</a-tooltip> </a-tooltip>
</template> </template>
</a-space> </a-space>
</div> </div>
<a-table <a-table
class="margin-top" class="margin-top"
v-bind="props" v-bind="props"
:columns="tableColumns" :columns="tableColumns"
:row-selection="props.isSelection ? (props.selectionProps ? props.selectionProps : defaultSelectProps) : null" :row-selection="props.isSelection ? (props.selectionProps ? props.selectionProps : defaultSelectProps) : null"
:data-source="dataSource" :data-source="dataSource"
:loading="loading" :loading="loading"
:pagination="false" :pagination="false"
> >
<template v-for="(_, key) in slots" v-slot:[key]="scope"> <template v-for="(_, key) in slots" v-slot:[key]="scope">
<slot v-if="!includes(['tableHeader', 'tableHeaderRight'], String(key))" :name="key" v-bind="scope"></slot> <slot v-if="!includes(['tableHeader', 'tableHeaderRight'], String(key))" :name="key" v-bind="scope"></slot>
</template> </template>
</a-table> </a-table>
<a-pagination <a-pagination
v-if="props.isPagination" v-if="props.isPagination"
class="flex-end margin-top margin-right" class="flex-end margin-top margin-right"
v-model:current="pageParams.current" v-model:current="pageParams.current"
v-model:page-size="pageParams.size" v-model:page-size="pageParams.size"
:total="pageParams.total" :total="pageParams.total"
v-bind="props.paginationProps" v-bind="props.paginationProps"
@change="handleCurrentChange" @change="handleCurrentChange"
@showSizeChange="handleSizeChange" @showSizeChange="handleSizeChange"
/> />
</div> </div>
</div> </div>
@ -69,12 +70,17 @@
<script setup lang="ts" generic="T extends BaseTableRowRecord = {},P extends { [key: string]: any } ={}"> <script setup lang="ts" generic="T extends BaseTableRowRecord = {},P extends { [key: string]: any } ={}">
import FormProMax from '@/components/form/FormProMax.vue' import FormProMax from '@/components/form/FormProMax.vue'
import { PrinterOutlined, ReloadOutlined, RollbackOutlined, SearchOutlined } from '@ant-design/icons-vue' import {PrinterOutlined, ReloadOutlined, RollbackOutlined, SearchOutlined} from '@ant-design/icons-vue'
import { computed, onMounted, Ref, ref } from 'vue' import {computed, onMounted, Ref, ref} from 'vue'
import { FormInstance } from 'ant-design-vue' import {FormInstance} from 'ant-design-vue'
import useTableProMax from '@/hooks/useTableProMax.ts' import useTableProMax from '@/hooks/useTableProMax.ts'
import { includes, isEmpty } from 'lodash-es' import {includes, isEmpty} from 'lodash-es'
import { BaseTableRowRecord, TableProMaxProps, TableProMaxRowSelect, TableProMaxSlots } from '@/types/components/table/index.ts' import {
BaseTableRowRecord,
TableProMaxProps,
TableProMaxRowSelect,
TableProMaxSlots
} from '@/types/components/table/index.ts'
// //
const selectKeys = ref<string[]>([]) const selectKeys = ref<string[]>([])
@ -150,7 +156,7 @@ const tableColumns = computed(() => {
dataIndex: 'index', dataIndex: 'index',
width: 60, width: 60,
title: '序号', title: '序号',
customRender: ({ index }) => index + 1, customRender: ({index}) => index + 1,
}) })
} }
} }
@ -166,14 +172,24 @@ const searchFormRef = ref<FormInstance>() as Ref<FormInstance>
*/ */
const searchParams = ref<P | Record<string, any>>(props.defaultSearchParams || {}) as Ref<P> const searchParams = ref<P | Record<string, any>>(props.defaultSearchParams || {}) as Ref<P>
const { loading, dataSource, pageParams, search, requestGetTableData, handleSizeChange, handleCurrentChange, resetState } = useTableProMax( const {
props.requestApi, loading,
searchFormRef, dataSource,
searchParams, pageParams,
props.isPagination, search,
props.dataCallback, requestGetTableData,
props.requestError handleSizeChange,
handleCurrentChange,
resetState
} = useTableProMax(
props.requestApi,
searchFormRef,
searchParams,
props.isPagination,
props.dataCallback,
props.requestError
) )
console.log('pageParams', pageParams)
onMounted(() => props.requestAuto && requestGetTableData(true)) onMounted(() => props.requestAuto && requestGetTableData(true))

View File

@ -2,373 +2,243 @@
<!-- 后台用户 --> <!-- 后台用户 -->
<!-- template 内部必须有一个根节点 div否则<Transition>将会失效所以这个<a-modal></a-modal> div --> <!-- template 内部必须有一个根节点 div否则<Transition>将会失效所以这个<a-modal></a-modal> div -->
<div> <div>
<div class="h-16 w-full bg-white rounded shadow-md py-5 px-10 flex items-center"> <TableProMax
<div class="mr-5">名称</div> ref="tableRef"
<a-input class="w-40 mr-5" v-model:value="searchUser" autocomplete="off" placeholder="请输入名称搜索" /> :request-api="reqApi"
<div class="mr-5 ml-5">是否启用</div> :columns="columns"
<a-space> :searchFormOptions="searchFormOptions"
<a-select ref="select" v-model:value="isEnableValue" style="width: 120px" @focus="focus" @change="handleChange"> :scroll="{x}"
<a-select-option v-for="(item, index) in enumsIsEnable" :key="index" :value="item.value">{{ item.label }}</a-select-option> >
</a-select> <template #tableHeader>
</a-space> <a-space>
<a-button type="primary" @click="addUserManagement">新增用户</a-button>
<div class="mr-5 ml-5">性别</div> </a-space>
<a-space> </template>
<a-select ref="select" v-model:value="sexValue" style="width: 120px" @focus="focus" @change="sexChange"> </TableProMax>
<a-select-option v-for="(item, index) in enumsSex" :key="index" :value="item.value">{{ item.label }}</a-select-option>
</a-select>
</a-space>
<div class="mr-5 ml-5">手机号</div>
<a-input class="w-40 mr-5" v-model:value="searchTelePhone" autocomplete="off" placeholder="请输入手机号搜索" />
<a-button @click="search" class="ml-5 flex items-center" type="primary"> <SearchOutlined style="font-size: 16px" />搜索 </a-button>
<a-button @click="resetSearch" class="ml-5 flex items-center"><SyncOutlined style="font-size: 16px" />重置</a-button>
</div>
<div class="w-full h-full bg-white mt-5 rounded">
<div class="w-full h-16 py-5 px-10 flex items-center">
<a-button class="flex items-center" type="primary" @click="addUser"><PlusOutlined style="font-size: 16px" />新增</a-button>
<!-- <a-button :disabled="!hasSelected" class="ml-5 flex items-center" type="primary" danger @click="search"><DeleteOutlined style="font-size: 16px" />删除</a-button> -->
</div>
<div class="px-10">
<!-- :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }" -->
<a-table :loading="loading" :pagination="pagination" :columns="columns" :data-source="tableData" />
</div>
</div>
<!-- template 内部必须有一个根节点 div否则<Transition>将会失效所以这个<a-modal></a-modal> div --> <!-- template 内部必须有一个根节点 div否则<Transition>将会失效所以这个<a-modal></a-modal> div -->
<a-modal v-model:open="visible" :title="title" @ok="submit" @cancel="closeModal"> <a-modal v-model:open="visible" :title="title" @ok="submit" @cancel="closeModal">
<a-form ref="formRef" name="custom-validation" :model="formState" :rules="rules" v-bind="layout" @finish="handleFinish" @validate="handleValidate" @finishFailed="handleFinishFailed"> <FormProMax ref="formRef" v-model:value="formParams" :form-item-options="formItemOptions"/>
<!-- 用户根据单位代码去查询 审核状态 -->
<a-form-item has-feedback label="姓名" name="name">
<a-input v-model:value="formState.name" autocomplete="off" />
</a-form-item>
<a-form-item has-feedback label="性别" name="sex">
<a-radio-group v-model:value="formState.sex" name="radioGroup">
<a-radio v-for="(item, index) in enumsSex" :key="index" :value="item.value">{{ item.label }}</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item has-feedback label="手机号" name="telephone">
<a-input v-model:value="formState.telephone" autocomplete="off" />
</a-form-item>
<a-form-item has-feedback label="启用状态" name="isEnable">
<a-radio-group v-model:value="formState.isEnable" name="radioGroup">
<a-radio v-for="(item, index) in enumsIsEnable" :key="index" :value="item.value">{{ item.label }}</a-radio>
</a-radio-group>
</a-form-item>
</a-form>
</a-modal> </a-modal>
</div> </div>
</template> </template>
<script setup lang="tsx"> <script setup lang="tsx">
/** import TableProMax from "@/components/table/TableProMax.vue";
* @current 当前页 import {TableProMaxProps} from "@/types/components/table";
* @pages 总页数 import {message} from 'ant-design-vue'
* @size 每页多少条 import {dictSelectNodes} from '@/config/dict.ts'
* @total 总条数 // const enumsSex = ref<any[]>(dictSelectNodes('Sex'))
*/ // const enumsIsEnable = ref<any[]>(dictSelectNodes('IsEnable'))
import { message } from 'ant-design-vue'
import { dictSelectNodes } from '@/config/dict.ts'
const enumsSex = ref<any[]>(dictSelectNodes('Sex'))
const enumsIsEnable = ref<any[]>(dictSelectNodes('IsEnable'))
import api from '@/axios/index.ts' import api from '@/axios/index.ts'
import { SearchOutlined, SyncOutlined, PlusOutlined } from '@ant-design/icons-vue' import {ref, reactive, onMounted} from 'vue'
import { ref, reactive, onMounted } from 'vue' import FormProMax from "@/components/form/FormProMax.vue";
const loading = ref(false) import {FormProMaxItemOptions} from "@/types/components/form";
const focus = () => { import {FormExpose} from "ant-design-vue/es/form/Form";
console.log('focus') import {publicUnitPagerQueryParams, FromItem} from "@/types/views/publicUnit.ts";
}
const handlePageChange = (page: any, pageSize: any) => { const tableRef = ref<ComponentExposed<typeof TableProMax>>(null!)
console.log('🚀 ~ handlePageChange ~ page, pageSize:', page, pageSize) const formRef = ref<FormExpose>(null)
type TableProps = TableProMaxProps<publicUnitPagerQueryParams>
const reqApi: TableProps['requestApi'] = (params) => api.post('/managementPoliceUnitUser/pager', params) //
const formParams = ref<{
snowFlakeId?: string,
name: string,
sex: number,
telephone: string,
isEnable: any,
pagination.current = page }>({
pagination.pageSize = pageSize name: '',
getUserList() sex: 0,
} telephone: '',
isEnable: 0,
const pagination = reactive({
pageSize: 5, //
showSizeChanger: true, //
pageSizeOptions: ['5', '10'], //
showQuickJumper: true, //
showTotal: (total: string | number) => `${total}`, //
current: 1, //
total: null as number | string | null, //
onChange: handlePageChange, //
}) })
const formItemOptions = ref<FormProMaxItemOptions<FromItem>>({
const getUserList = async function (params?: any) { name: {
loading.value = true type: 'input',
let obj = { page: { size: pagination.pageSize, current: pagination.current }, params } label: '姓名',
const res = await api.post<any>('/managementPoliceUnitUser/pager', obj) required: true,
const { total, records } = res.data rules: [
tableData.value = records {required: true, message: '请输入姓名'}
pagination.total = Number(total) ],
loading.value = false },
} sex: {
type: 'radioGroup',
onMounted(() => { label: '性别',
getUserList() options: dictSelectNodes('Sex'),
required: true,
},
telephone: {
type: 'input',
label: '手机号',
required: true,
rules: [
{required: true, message: '请输入手机号'},
{
validator: (rule, value) => {
const phoneRegex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
if (!value) {
return Promise.reject('手机号不能为空');
} else if (!phoneRegex.test(value)) {
return Promise.reject('手机号格式不正确');
} else {
return Promise.resolve();
}
},
trigger: 'blur',
},
],
},
isEnable: {
type: 'radioGroup',
label: '启用状态',
options: dictSelectNodes('IsEnable'),
required: true,
},
}) })
const columns: TableProps['columns'] = [
type Key = string | number
interface DataType {
key: Key
name: string
age: number
address: string
}
interface RecordItem {
account: string
createTime: string
createUserName: string | null
isAdmin: LabelValue //
isEnable: EnableStatus //
name: string
sex: LabelValue //
snowFlakeId: string // Snowflake ID
telephone: string
}
interface LabelValue {
value: number
label: string
}
interface EnableStatus {
value: number
label: string
extData: {
color: string
}
}
const columns = [
// {
// title: '',
// customRender: (text, record, index, column) => {
// console.log(index)
// },
// },
{ {
title: '账号',
dataIndex: 'account', dataIndex: 'account',
title: '账号',
width: 100,
ellipsis: true
}, },
{ {
title: '名称',
dataIndex: 'name', dataIndex: 'name',
title: '名称',
width: 200,
ellipsis: true
}, },
{ {
dataIndex: 'sex', dataIndex: 'sex',
title: '性别', title: '性别',
customRender: ({ record }: { record: RecordItem }) => { customRender: ({text}) => <a-tag>{text?.label}</a-tag>,
return <a-tag>{record.sex.label}</a-tag> width: 150
}, },
{
dataIndex: 'telephone',
title: '手机号码',
width: 150,
ellipsis: true
},
{
dataIndex: 'createTime',
title: '创建时间',
width: 200,
ellipsis: true,
}, },
{ {
title: '是否启用',
dataIndex: 'isEnable', dataIndex: 'isEnable',
customRender: ({ record }: { record: RecordItem }) => { title: '是否启用',
return record.isEnable.extData?.color === 'success' ? <a-tag color='green'>{record.isEnable.label}</a-tag> : <a-tag color='#f50'>{record.isEnable.label}</a-tag> customRender: ({text}) => <a-tag color={text?.extData?.color}>{text?.label}</a-tag>,
}, width: 150
},
{
title: '创建时间',
dataIndex: 'createTime',
}, },
{ {
dataIndex: 'opt', dataIndex: 'opt',
title: '操作', title: '操作',
fixed: 'right', fixed: "right",
customRender({ record }: { record: RecordItem }) { customRender({record}) {
return record.isAdmin.value === 1 ? ( return (
<a-space> record.isAdmin.value === 1 ?
<a-button <a-space>
type='primary' <a-popconfirm
onClick={async () => { style="width:100%"
console.log(record) title="确认删除账号吗?"
{ onConfirm={async () => {
visible.value = true const resp = await api.delete('/managementPoliceUnitUser/deleteById', {
title.value = '编辑用户' managementPoliceUnitUserId: record.snowFlakeId,
formState.name = record.name })
formState.telephone = record.telephone message.success(resp.message)
formState.sex = record.sex.value await tableRef.value?.requestGetTableData()
formState.isEnable = record.isEnable.value }}>
formState.snowFlakeId = record.snowFlakeId <a-button type="primary" danger>删除</a-button>
} </a-popconfirm>
}} <a-button type="primary" onClick={async () => {
> visible.value = true
编辑 title.value = "编辑用户"
</a-button> formParams.value.snowFlakeId = record.snowFlakeId
<a-button type='primary' danger> formParams.value.name = record.name,
<a-popconfirm formParams.value.sex = record.sex.value,
title='确认删除账号吗?' formParams.value.telephone = record.telephone,
onConfirm={async () => { formParams.value.isEnable = record.isEnable?.value
{
const resp = await api.delete('/managementPoliceUnitUser/deleteById', { }}>
managementPoliceUnitUserId: record.snowFlakeId, 编辑
}) </a-button>
message.success(resp.message) </a-space>
getUserList() :
} <div>超级管理员不能编辑</div>
}}
>
删除
</a-popconfirm>
</a-button>
</a-space>
) : (
<i></i>
) )
}, }
}, },
] ]
const x: number = columns.reduce((a, b) => a + (b.width as number), 0)
const saveOrUpdate = async () => { const searchFormOptions: TableProps["searchFormOptions"] = {
const res = await api.post<any>('/managementPoliceUnitUser/saveOrUpdate', formState) name: {
message.success(res.message) type: 'input',
visible.value = false label: '名称'
getUserList() // }, sex: {
resetForm() // type: 'select',
resetFormState() label: '性别',
if (formState.hasOwnProperty('snowFlakeId')) { options: [
delete formState.snowFlakeId {
value: null,
label: '全部'
}, ...dictSelectNodes('Sex')
]
},
telephone: {
type: 'input',
label: '手机号'
},
isEnable: {
type: 'select',
label: '是否启用',
options: [
{
value: null,
label: '全部'
}, ...dictSelectNodes('IsEnable')
]
} }
if (title.value === '新增用户') {
}
// console.log('saveOrUpdate', res)
} }
const visible = ref(false) const visible = ref(false)
const title = ref('') const title = ref('新增用户')
const submit = async () => { const addUserManagement = () => {
await formRef.value?.validate() //
console.log('表单校验完成')
saveOrUpdate()
}
const closeModal = () => {
resetForm()
resetFormState()
}
const addUser = () => {
visible.value = true visible.value = true
}
const submit = async () => {
await formRef.value.validate()
const snowFlakeId = ref('')
if (title.value === '新增用户') {
snowFlakeId.value = ''
} else {
snowFlakeId.value = formParams.value.snowFlakeId
}
const managementSecurityUnitUserSaveOrUpdateParams = {
snowFlakeId: snowFlakeId.value,
name: formParams.value.name,
sex: formParams.value.sex,
telephone: formParams.value.telephone,
isEnable: formParams.value.isEnable,
}
const resp = await api.post('/managementPoliceUnitUser/saveOrUpdate', managementSecurityUnitUserSaveOrUpdateParams)
message.success(resp.message)
tableRef.value?.requestGetTableData()
closeModal()
}
const closeModal = () => {
formParams.value = {
name: '',
sex: 0,
telephone: '',
isEnable: 0
}
visible.value = false
title.value = '新增用户' title.value = '新增用户'
} }
const tableData = ref<DataType[]>([])
const layout = {
labelCol: { span: 4 },
wrapperCol: { span: 14 },
}
import type { FormInstance } from 'ant-design-vue'
import type { Rule } from 'ant-design-vue/es/form'
interface FormState {
name: string
sex: string | number
telephone: string
isEnable: string | number
snowFlakeId?: string | number
[key: string]: any //
}
const formRef = ref<FormInstance>()
const formState = reactive<FormState>({
name: '',
sex: '',
telephone: '',
isEnable: '',
})
const checkName = async (_rule: Rule, value: string) => {
if (value === '') {
return Promise.reject('请输入姓名')
} else {
return Promise.resolve()
}
}
const checkSex = async (_rule: Rule, value: string) => {
if (value === '') {
return Promise.reject('请选择姓名')
} else {
return Promise.resolve()
}
}
var reg_tel = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/
const checkTelephone = async (_rule: Rule, value: string) => {
if (reg_tel.test(value.trim())) {
return Promise.resolve()
} else {
return Promise.reject('手机号格式不正确')
}
}
const checkIsEnable = async (_rule: Rule, value: string) => {
if (value === '') {
return Promise.reject('请选择启用状态')
} else {
return Promise.resolve()
}
}
const rules: Record<string, Rule[]> = {
name: [{ required: true, validator: checkName, trigger: 'change' }],
sex: [{ required: true, validator: checkSex, trigger: 'change' }],
telephone: [{ required: true, validator: checkTelephone, trigger: 'change' }],
isEnable: [{ required: true, validator: checkIsEnable, trigger: 'change' }],
}
const handleFinish = (values: FormState) => {
console.log('自定义校验成功')
console.log(values)
}
const handleFinishFailed = (errors: any) => {
console.log(errors)
}
const resetForm = () => {
formRef.value?.resetFields()
}
const resetFormState = () => {
// Object.keys(formState).forEach((key) => (formState[key] = ''))
for (const key in formState) {
if (Object.prototype.hasOwnProperty.call(formState, key)) {
formState[key] = '' // nullundefined
}
}
}
const handleValidate = (...args: any[]) => {
console.log(args)
}
//
const searchParams = reactive({
name: '',
telephone: '',
sex: null,
isEnable: null,
})
const searchUser = ref('')
const isEnableValue = ref(null)
const sexValue = ref(null)
const searchTelePhone = ref('')
const handleChange = (value: string) => {
console.log(`selected ${value}`)
}
const sexChange = (value: string) => {
console.log(`selected ${value}`)
}
const search = async () => {
searchParams.name = searchUser.value
searchParams.telephone = searchTelePhone.value
searchParams.sex = sexValue.value
searchParams.isEnable = isEnableValue.value
getUserList(searchParams)
}
const resetSearch = () => {
searchUser.value = ''
isEnableValue.value = null
sexValue.value = null
searchTelePhone.value = ''
}
</script> </script>

BIN
policeSecurityServer/.DS_Store vendored Normal file

Binary file not shown.