170 lines
4.8 KiB
Vue
Raw Normal View History

2022-11-04 16:43:11 +08:00
<template>
<ContentWrap>
<!-- 列表 -->
2022-11-04 16:43:11 +08:00
<vxe-grid ref="xGrid" v-bind="gridOptions" class="xtable-scrollbar">
<!-- 操作新增 -->
2022-11-04 16:43:11 +08:00
<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-13 13:16:11 +08:00
<XButton
2022-11-13 15:13:38 +08:00
type="warning"
2022-11-13 13:16:11 +08:00
preIcon="ep:download"
:title="t('action.export')"
v-hasPermi="['system:post:export']"
@click="handleExport()"
/>
2022-11-04 16:43:11 +08:00
</template>
2022-11-12 15:52:43 +08:00
<template #actionbtns_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']"
2022-11-13 13:16:11 +08:00
@click="handleDetail(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: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>
<!-- 弹窗 -->
2022-11-15 12:25:19 +08:00
<XModal id="postModel" v-model="dialogVisible" :title="dialogTitle">
2022-11-14 09:15:11 +08:00
<!-- 表单添加/修改 -->
<Form
ref="formRef"
v-if="['create', 'update'].includes(actionType)"
:schema="allSchemas.formSchema"
:rules="rules"
/>
<!-- 表单详情 -->
<Descriptions
v-if="actionType === 'detail'"
:schema="allSchemas.detailSchema"
2022-11-15 10:52:27 +08:00
:data="detailData"
2022-11-14 09:15:11 +08:00
/>
2022-11-04 16:43:11 +08:00
<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>
2022-11-15 12:25:19 +08:00
</XModal>
2022-11-04 16:43:11 +08:00
</template>
2022-07-18 19:06:37 +08:00
<script setup lang="ts">
// 全局相关的 import
2022-11-07 18:13:04 +08:00
import { ref, unref } from 'vue'
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'
// 业务相关的 import
import * as PostApi from '@/api/system/post'
import { rules, allSchemas } from './post.data'
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() // 消息弹窗
// 列表相关的变量
const xGrid = ref<VxeGridInstance>() // 列表 Grid Ref
2022-11-15 14:51:39 +08:00
const { gridOptions, reloadList, delList, exportList } = useVxeGrid<PostApi.PostVO>({
allSchemas: allSchemas,
getListApi: PostApi.getPostPageApi,
2022-11-15 14:51:39 +08:00
delListApi: PostApi.deletePostApi,
exportListApi: PostApi.exportPostApi
})
// 弹窗相关的变量
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) // 按钮 Loading
2022-11-10 17:40:42 +08:00
const formRef = ref<FormExpose>() // 表单 Ref
2022-11-15 10:52:27 +08:00
const detailData = ref() // 详情 Ref
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-13 13:16:11 +08:00
// 导出操作
const handleExport = async () => {
2022-11-15 14:51:39 +08:00
await exportList(xGrid, '岗位列表.xls')
2022-11-13 13:16:11 +08:00
}
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-12 23:33:29 +08:00
// 详情操作
2022-11-13 13:16:11 +08:00
const handleDetail = async (rowId: number) => {
2022-11-12 23:33:29 +08:00
setDialogTile('detail')
2022-11-13 13:16:11 +08:00
const res = await PostApi.getPostApi(rowId)
2022-11-15 10:52:27 +08:00
detailData.value = res
2022-11-12 23:33:29 +08:00
}
2022-11-01 14:36:18 +08:00
// 删除操作
2022-11-07 18:13:04 +08:00
const handleDelete = async (rowId: number) => {
delList(xGrid, rowId)
2022-07-18 19:06:37 +08:00
}
2022-11-10 17:40:42 +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 {
2022-11-13 13:16:11 +08:00
const data = unref(formRef)?.formModel as PostApi.PostVO
2022-11-07 18:13:04 +08:00
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
// 刷新列表
2022-11-15 14:39:39 +08:00
reloadList(xGrid)
2022-11-07 18:13:04 +08:00
}
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>