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

142 lines
4.1 KiB
Vue
Raw Normal View History

2022-11-04 16:43:11 +08:00
<template>
<ContentWrap>
<!-- 列表 -->
2023-01-03 11:21:27 +08:00
<XTable @register="registerTable">
2022-11-04 16:43:11 +08:00
<template #toolbar_buttons>
2022-11-16 09:52:23 +08:00
<!-- 操作新增 -->
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']"
2023-01-17 15:13:23 +08:00
@click="openModel('create')"
2022-11-04 18:01:46 +08:00
/>
2022-11-16 09:52:23 +08:00
<!-- 操作导出 -->
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']"
2023-01-04 16:33:51 +08:00
@click="exportList('岗位列表.xls')"
2022-11-13 13:16:11 +08:00
/>
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 16:43:11 +08:00
v-hasPermi="['system:post:update']"
2023-01-17 15:13:23 +08:00
@click="openModel('update', 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-16 10:07:24 +08:00
v-hasPermi="['system:post:query']"
2023-01-17 15:13:23 +08:00
@click="openModel('detail', 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 16:43:11 +08:00
v-hasPermi="['system:post:delete']"
2023-01-04 16:33:51 +08:00
@click="deleteData(row.id)"
2022-11-04 18:01:46 +08:00
/>
2022-11-04 16:43:11 +08:00
</template>
2023-01-03 11:21:27 +08:00
</XTable>
2022-11-04 16:43:11 +08:00
</ContentWrap>
<!-- 弹窗 -->
2022-11-16 10:28:28 +08:00
<XModal id="postModel" :loading="modelLoading" v-model="modelVisible" :title="modelTitle">
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-15 17:42:04 +08:00
@click="submitForm()"
2022-11-04 16:43:11 +08:00
/>
<!-- 按钮关闭 -->
2022-11-16 10:28:28 +08:00
<XButton :loading="actionLoading" :title="t('dialog.close')" @click="modelVisible = 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-11-23 22:26:25 +08:00
<script setup lang="ts" name="Post">
2023-01-18 12:14:58 +08:00
import type { 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() // 消息弹窗
// 列表相关的变量
2023-01-03 11:21:27 +08:00
const [registerTable, { reload, deleteData, exportList }] = useXTable({
allSchemas: allSchemas,
getListApi: PostApi.getPostPageApi,
2022-11-16 10:28:28 +08:00
deleteApi: PostApi.deletePostApi,
2022-11-15 14:51:39 +08:00
exportListApi: PostApi.exportPostApi
})
// 弹窗相关的变量
2022-11-16 10:28:28 +08:00
const modelVisible = ref(false) // 是否显示弹出层
const modelTitle = ref('edit') // 弹出层标题
const modelLoading = ref(false) // 弹出层loading
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
2023-01-17 15:13:23 +08:00
const openModel = async (type: string, rowId?: number) => {
2022-11-16 10:28:28 +08:00
modelLoading.value = true
modelTitle.value = t('action.' + type)
2022-07-18 19:06:37 +08:00
actionType.value = type
2022-11-16 10:28:28 +08:00
modelVisible.value = true
2022-07-18 19:06:37 +08:00
// 设置数据
2023-01-17 15:13:23 +08:00
if (rowId) {
const res = await PostApi.getPostApi(rowId)
if (type === 'update') {
unref(formRef)?.setValues(res)
} else if (type === 'detail') {
detailData.value = res
}
}
2022-11-16 10:28:28 +08:00
modelLoading.value = false
2022-11-12 23:33:29 +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'))
}
2022-11-16 10:28:28 +08:00
modelVisible.value = false
2022-11-07 18:13:04 +08:00
} finally {
actionLoading.value = false
// 刷新列表
2023-01-17 15:13:23 +08:00
reload()
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>