163 lines
4.8 KiB
Vue
Raw Normal View History

2022-11-04 16:43:11 +08:00
<template>
<ContentWrap>
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<template #toolbar_buttons>
2022-11-04 18:01:46 +08:00
<XButton
2022-11-04 18:19:11 +08:00
type="primary"
2022-11-04 18:01:46 +08:00
preIcon="ep:zoom-in"
2022-11-04 18:19:11 +08:00
:title="t('action.add')"
2022-11-04 18:01:46 +08:00
v-hasPermi="['system:post:create']"
@click="handleCreate()"
/>
2022-11-04 16:43:11 +08:00
</template>
<template #status_default="{ row }">
<DictTag :type="DICT_TYPE.COMMON_STATUS" :value="row.status" />
</template>
<template #action_default="{ row }">
2022-11-04 18:01:46 +08:00
<XButton
2022-11-04 16:43:11 +08:00
link
2022-11-04 18:19:11 +08:00
type="primary"
2022-11-04 18:01:46 +08:00
preIcon="ep:edit"
2022-11-04 18:19:11 +08:00
:title="t('action.edit')"
2022-11-04 16:43:11 +08:00
v-hasPermi="['system:post:update']"
@click="handleUpdate(row.id)"
2022-11-04 18:01:46 +08:00
/>
<XButton
2022-11-04 16:43:11 +08:00
link
2022-11-04 18:19:11 +08:00
type="primary"
2022-11-04 18:01:46 +08:00
preIcon="ep:view"
2022-11-04 18:19:11 +08:00
:title="t('action.detail')"
2022-11-04 16:43:11 +08:00
v-hasPermi="['system:post:update']"
@click="handleDetail(row)"
2022-11-04 18:01:46 +08:00
/>
<XButton
2022-11-04 16:43:11 +08:00
link
2022-11-04 18:19:11 +08:00
type="primary"
2022-11-04 18:01:46 +08:00
preIcon="ep:delete"
2022-11-04 18:19:11 +08:00
:title="t('action.del')"
2022-11-04 16:43:11 +08:00
v-hasPermi="['system:post:delete']"
@click="handleDelete(row.id)"
2022-11-04 18:01:46 +08:00
/>
2022-11-04 16:43:11 +08:00
</template>
</vxe-grid>
</ContentWrap>
<XModal id="postModel" v-model="dialogVisible" :title="dialogTitle">
<template #default>
<!-- 对话框(添加 / 修改) -->
<vxe-form
ref="xForm"
v-if="['create', 'update'].includes(actionType)"
:data="formData"
:items="formItems"
:rules="rules"
/>
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
:data="detailRef"
>
<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>
</Descriptions>
</template>
<template #footer>
2022-11-04 18:01:46 +08:00
<XButton
2022-11-04 16:43:11 +08:00
v-if="['create', 'update'].includes(actionType)"
2022-11-04 18:01:46 +08:00
:loading="actionLoading"
2022-11-04 18:19:11 +08:00
:title="t('action.save')"
2022-11-07 14:12:16 +08:00
type="primary"
2022-11-04 16:43:11 +08:00
@click="submitForm"
/>
2022-11-07 14:12:16 +08:00
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="dialogVisible = false" />
2022-11-04 16:43:11 +08:00
</template>
</XModal>
</template>
2022-07-18 19:06:37 +08:00
<script setup lang="ts">
2022-11-03 14:33:30 +08:00
import { ref } from 'vue'
2022-07-18 19:06:37 +08:00
import dayjs from 'dayjs'
import { useI18n } from '@/hooks/web/useI18n'
2022-11-03 14:33:30 +08:00
import { VxeFormEvents, VxeFormInstance, VxeFormItemProps, VxeGridInstance } from 'vxe-table'
2022-07-18 19:06:37 +08:00
import * as PostApi from '@/api/system/post'
2022-11-03 14:33:30 +08:00
import { DICT_TYPE } from '@/utils/dict'
2022-11-01 14:36:18 +08:00
import { ContentWrap } from '@/components/ContentWrap'
2022-11-03 14:33:30 +08:00
import { PostVO } from '@/api/system/post/types'
2022-11-01 14:36:18 +08:00
import { rules, allSchemas } from './post.data'
2022-11-03 14:33:30 +08:00
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
2022-07-18 19:06:37 +08:00
2022-11-01 14:36:18 +08:00
const { t } = useI18n() // 国际化
2022-11-03 14:33:30 +08:00
const message = useMessage()
2022-11-01 14:36:18 +08:00
const xGrid = ref<VxeGridInstance>()
2022-11-01 17:12:26 +08:00
const xForm = ref<VxeFormInstance>()
2022-07-18 19:06:37 +08:00
const dialogVisible = ref(false) // 是否显示弹出层
const dialogTitle = ref('edit') // 弹出层标题
2022-11-01 14:36:18 +08:00
const actionType = ref('') // 操作按钮的类型
const actionLoading = ref(false) // 遮罩层
2022-07-18 19:06:37 +08:00
2022-11-03 14:33:30 +08:00
const gridOptions = useVxeGrid(allSchemas, PostApi.getPostPageApi)
2022-11-01 17:35:36 +08:00
const formData = ref<PostVO>()
2022-11-03 14:33:30 +08:00
const formItems = ref<VxeFormItemProps[]>(allSchemas.formSchema)
2022-07-18 19:06:37 +08:00
// 设置标题
const setDialogTile = (type: string) => {
dialogTitle.value = t('action.' + type)
actionType.value = type
dialogVisible.value = true
}
2022-11-01 14:36:18 +08:00
// ========== 详情相关 ==========
const detailRef = ref() // 详情 Ref
// 详情操作
const handleDetail = (row: PostVO) => {
setDialogTile('detail')
detailRef.value = row
}
2022-07-18 19:06:37 +08:00
// 新增操作
const handleCreate = () => {
setDialogTile('create')
2022-11-07 09:39:44 +08:00
formData.value = undefined
2022-07-18 19:06:37 +08:00
}
// 修改操作
2022-11-01 14:36:18 +08:00
const handleUpdate = async (rowId: number) => {
2022-07-18 19:06:37 +08:00
setDialogTile('update')
// 设置数据
2022-11-01 14:36:18 +08:00
const res = await PostApi.getPostApi(rowId)
formData.value = res
2022-07-18 19:06:37 +08:00
}
2022-11-01 14:36:18 +08:00
// 删除操作
const handleDelete = (rowId: number) => {
2022-11-03 14:33:30 +08:00
message
.confirm(t('common.delMessage'), t('common.confirmTitle'))
2022-11-01 14:36:18 +08:00
.then(async () => {
await PostApi.deletePostApi(rowId)
2022-11-03 14:33:30 +08:00
message.success(t('common.delSuccess'))
2022-11-01 14:36:18 +08:00
})
.finally(() => {
2022-11-01 17:35:36 +08:00
xGrid.value?.commitProxy('query')
2022-11-01 14:36:18 +08:00
})
2022-07-18 19:06:37 +08:00
}
2022-11-01 14:36:18 +08:00
// 提交按钮
const submitForm: VxeFormEvents.Submit = async () => {
actionLoading.value = true
// 提交请求
try {
const data = formData.value as PostVO
if (actionType.value === 'create') {
await PostApi.createPostApi(data)
2022-11-03 14:33:30 +08:00
message.success(t('common.createSuccess'))
2022-11-01 14:36:18 +08:00
} else {
await PostApi.updatePostApi(data)
2022-11-03 14:33:30 +08:00
message.success(t('common.updateSuccess'))
2022-11-01 14:36:18 +08:00
}
// 操作成功,重新加载列表
dialogVisible.value = false
} finally {
actionLoading.value = false
2022-11-01 17:35:36 +08:00
xGrid.value?.commitProxy('query')
2022-11-01 14:36:18 +08:00
}
2022-07-18 19:06:37 +08:00
}
</script>