FlowPacs/yudao-ui-admin-vue3/src/views/system/post/index.vue

167 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-09 16:31:37 +08:00
<XTextButton
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
/>
2022-11-09 16:31:37 +08:00
<XTextButton
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
/>
2022-11-09 16:31:37 +08:00
<XTextButton
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>
<!-- 对话框(添加 / 修改) -->
2022-11-07 18:13:04 +08:00
<Form
2022-11-04 16:43:11 +08:00
v-if="['create', 'update'].includes(actionType)"
2022-11-07 18:13:04 +08:00
:schema="allSchemas.formSchema"
2022-11-04 16:43:11 +08:00
:rules="rules"
2022-11-07 18:13:04 +08:00
ref="formRef"
2022-11-04 16:43:11 +08:00
/>
<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-07 14:12:16 +08:00
type="primary"
2022-11-10 14:38:16 +08:00
:title="t('action.save')"
:loading="actionLoading"
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-07 18:13:04 +08:00
import { ref, unref } from 'vue'
2022-07-18 19:06:37 +08:00
import dayjs from 'dayjs'
2022-11-03 14:33:30 +08:00
import { DICT_TYPE } from '@/utils/dict'
2022-11-07 14:58:53 +08:00
import * as PostApi from '@/api/system/post'
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-07 14:58:53 +08:00
import { useI18n } from '@/hooks/web/useI18n'
2022-11-03 14:33:30 +08:00
import { useMessage } from '@/hooks/web/useMessage'
import { useVxeGrid } from '@/hooks/web/useVxeGrid'
2022-11-10 17:40:42 +08:00
import { VxeGridInstance } from 'vxe-table'
2022-11-07 18:13:04 +08:00
import { FormExpose } from '@/components/Form'
2022-07-18 19:06:37 +08:00
2022-11-01 14:36:18 +08:00
const { t } = useI18n() // 国际化
2022-11-07 14:58:53 +08:00
const message = useMessage() // 消息弹窗
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('') // 操作按钮的类型
2022-11-07 14:58:53 +08:00
const actionLoading = ref(false) // 按钮Loading
2022-11-10 17:40:42 +08:00
const xGrid = ref<VxeGridInstance>() // grid Ref
const formRef = ref<FormExpose>() // 表单 Ref
const detailRef = ref() // 详情 Ref
2022-07-18 19:06:37 +08:00
2022-11-07 18:13:04 +08:00
const { gridOptions } = useVxeGrid<PostVO>({
allSchemas: allSchemas,
getListApi: PostApi.getPostPageApi
})
2022-11-10 17:40:42 +08:00
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-10 17:40:42 +08:00
2022-07-18 19:06:37 +08:00
// 新增操作
const handleCreate = () => {
setDialogTile('create')
2022-11-07 18:13:04 +08:00
// 重置表单
unref(formRef)?.getElFormRef()?.resetFields()
2022-07-18 19:06:37 +08:00
}
2022-11-10 17:40:42 +08:00
// 详情操作
const handleDetail = (row: PostVO) => {
setDialogTile('detail')
detailRef.value = row
}
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)
2022-11-07 18:13:04 +08:00
unref(formRef)?.setValues(res)
2022-07-18 19:06:37 +08:00
}
2022-11-10 17:40:42 +08:00
2022-11-01 14:36:18 +08:00
// 删除操作
2022-11-07 18:13:04 +08:00
const handleDelete = async (rowId: number) => {
2022-11-03 14:33:30 +08:00
message
2022-11-07 14:58:53 +08:00
.delConfirm()
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-10 17:40:42 +08:00
2022-11-01 14:36:18 +08:00
// 提交按钮
2022-11-10 17:40:42 +08:00
const submitForm = async () => {
2022-11-07 18:13:04 +08:00
const elForm = unref(formRef)?.getElFormRef()
if (!elForm) return
elForm.validate(async (valid) => {
if (valid) {
actionLoading.value = true
// 提交请求
try {
const data = unref(formRef)?.formModel as PostVO
if (actionType.value === 'create') {
await PostApi.createPostApi(data)
message.success(t('common.createSuccess'))
} else {
await PostApi.updatePostApi(data)
message.success(t('common.updateSuccess'))
}
dialogVisible.value = false
} finally {
actionLoading.value = false
xGrid.value?.commitProxy('query')
}
2022-11-01 14:36:18 +08:00
}
2022-11-07 18:13:04 +08:00
})
2022-07-18 19:06:37 +08:00
}
</script>