feat:优化 system/post 的实现代码

This commit is contained in:
YunaiV
2025-12-15 14:35:31 +08:00
parent 0461f29e97
commit dbcf7017d8
4 changed files with 74 additions and 61 deletions

View File

@@ -1,4 +1,13 @@
<template>
<!-- 搜索框入口 -->
<wd-search
:placeholder="searchPlaceholder"
:hide-cancel="true"
disabled
@click="visible = true"
/>
<!-- 搜索弹窗 -->
<wd-popup
v-model="visible"
position="top"
@@ -59,7 +68,7 @@
</template>
<script lang="ts" setup>
import { computed, reactive, watch } from 'vue'
import { computed, reactive, ref, watch } from 'vue'
/** 搜索表单数据 */
export interface SearchFormData {
@@ -69,19 +78,29 @@ export interface SearchFormData {
}
const props = defineProps<{
modelValue: boolean
searchParams?: Partial<SearchFormData> // 初始搜索参数
}>()
const emit = defineEmits<{
'update:modelValue': [value: boolean]
'search': [data: SearchFormData]
'reset': []
}>()
const visible = computed({
get: () => props.modelValue,
set: (val: boolean) => emit('update:modelValue', val),
const visible = ref(false)
/** 搜索条件 placeholder 拼接 */
const searchPlaceholder = computed(() => {
const conditions: string[] = []
if (props.searchParams?.name) {
conditions.push(`名称:${props.searchParams.name}`)
}
if (props.searchParams?.code) {
conditions.push(`编码:${props.searchParams.code}`)
}
if (props.searchParams?.status !== undefined && props.searchParams.status !== -1) {
conditions.push(`状态:${props.searchParams.status === 0 ? '启用' : '禁用'}`)
}
return conditions.length > 0 ? conditions.join(' | ') : '搜索岗位'
})
const formData = reactive<SearchFormData>({
@@ -91,7 +110,7 @@ const formData = reactive<SearchFormData>({
})
/** 监听弹窗打开,同步外部参数 */
watch(() => props.modelValue, (val) => {
watch(visible, (val) => {
if (val && props.searchParams) {
formData.name = props.searchParams.name
formData.code = props.searchParams.code

View File

@@ -8,8 +8,8 @@
/>
<!-- 详情内容 -->
<view class="p-24rpx pb-200rpx">
<wd-cell-group custom-class="cell-group" border>
<view>
<wd-cell-group border>
<wd-cell title="岗位名称" :value="formData?.name || '-'" />
<wd-cell title="岗位编码" :value="formData?.code || '-'" />
<wd-cell title="显示顺序" :value="String(formData?.sort ?? '-')" />
@@ -22,12 +22,18 @@
</view>
<!-- 底部操作按钮 -->
<view class="safe-area-inset-bottom fixed bottom-0 left-0 right-0 bg-white p-24rpx">
<view class="fixed bottom-0 left-0 right-0 bg-white p-24rpx">
<view class="w-full flex gap-24rpx">
<wd-button class="flex-1" type="warning" @click="handleEdit">
<wd-button
v-if="hasAccessByCodes(['system:post:update'])"
class="flex-1" type="warning" @click="handleEdit"
>
编辑
</wd-button>
<wd-button class="flex-1" type="error" :loading="deleting" @click="handleDelete">
<wd-button
v-if="hasAccessByCodes(['system:post:delete'])"
class="flex-1" type="error" :loading="deleting" @click="handleDelete"
>
删除
</wd-button>
</view>
@@ -40,11 +46,13 @@ import type { Post } from '@/api/system/post'
import { onMounted, ref } from 'vue'
import { useToast } from 'wot-design-uni'
import { deletePost, getPost } from '@/api/system/post'
import { useAccess } from '@/hooks/useAccess'
import { navigateBackPlus } from '@/utils'
import { DICT_TYPE } from '@/utils/constants'
import { formatDateTime } from '@/utils/date'
const props = defineProps<{
id: number
id?: number | any
}>()
definePage({
@@ -54,13 +62,14 @@ definePage({
},
})
const { hasAccessByCodes } = useAccess()
const toast = useToast()
const formData = ref<Post>() // 详情数据
const deleting = ref(false) // 删除中
/** 返回上一页 */
function handleBack() {
uni.navigateBack()
navigateBackPlus('/pages-system/post/index')
}
/** 加载岗位详情 */
@@ -68,7 +77,12 @@ async function getDetail() {
if (!props.id) {
return
}
formData.value = await getPost(props.id)
try {
toast.loading('加载中...')
formData.value = await getPost(props.id)
} finally {
toast.close()
}
}
/** 编辑岗位 */
@@ -111,13 +125,4 @@ onMounted(() => {
</script>
<style lang="scss" scoped>
:deep(.cell-group) {
border-radius: 12rpx;
overflow: hidden;
box-shadow: 0 3rpx 8rpx rgba(24, 144, 255, 0.06);
}
.safe-area-inset-bottom {
padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
}
</style>

View File

@@ -8,9 +8,9 @@
/>
<!-- 表单区域 -->
<view class="p-24rpx">
<view>
<wd-form ref="formRef" :model="formData" :rules="formRules">
<wd-cell-group custom-class="cell-group" border>
<wd-cell-group border>
<wd-input
v-model="formData.name"
label="岗位名称"
@@ -54,7 +54,7 @@
</view>
<!-- 底部保存按钮 -->
<view class="safe-area-inset-bottom fixed bottom-0 left-0 right-0 bg-white p-24rpx">
<view class="fixed bottom-0 left-0 right-0 bg-white p-24rpx">
<wd-button
type="primary"
block
@@ -72,10 +72,11 @@ import type { Post } from '@/api/system/post'
import { computed, onMounted, ref } from 'vue'
import { useToast } from 'wot-design-uni'
import { createPost, getPost, updatePost } from '@/api/system/post'
import { navigateBackPlus } from '@/utils'
import { CommonStatusEnum } from '@/utils/constants'
const props = defineProps<{
id?: number
id?: number | any
}>()
definePage({
@@ -106,7 +107,7 @@ const formRef = ref()
/** 返回上一页 */
function handleBack() {
uni.navigateBack()
navigateBackPlus('/pages-system/post/index')
}
/** 加载岗位详情 */
@@ -148,13 +149,4 @@ onMounted(() => {
</script>
<style lang="scss" scoped>
:deep(.cell-group) {
border-radius: 12rpx;
overflow: hidden;
box-shadow: 0 3rpx 8rpx rgba(24, 144, 255, 0.06);
}
.safe-area-inset-bottom {
padding-bottom: calc(24rpx + env(safe-area-inset-bottom));
}
</style>

View File

@@ -5,13 +5,14 @@
title="岗位管理"
left-arrow placeholder safe-area-inset-top fixed
@click-left="handleBack"
>
<template #right>
<view class="flex items-center" @click="searchVisible = !searchVisible">
<wd-icon name="search" size="20px" />
</view>
</template>
</wd-navbar>
/>
<!-- 搜索组件 -->
<SearchForm
:search-params="queryParams"
@search="handleQuery"
@reset="handleReset"
/>
<!-- 岗位列表 -->
<view class="p-24rpx">
@@ -50,30 +51,26 @@
/>
</view>
<!-- 搜索弹窗 -->
<SearchForm
v-model="searchVisible"
:search-params="queryParams"
@search="handleQuery"
@reset="handleReset"
/>
<!-- 新增按钮 -->
<view
class="fixed bottom-100rpx right-32rpx z-10 h-100rpx w-100rpx flex items-center justify-center rounded-full bg-[#1890ff] shadow-lg"
<wd-fab
v-if="hasAccessByCodes(['system:post:create'])"
position="right-bottom"
type="primary"
:expandable="false"
@click="handleAdd"
>
<wd-icon name="add" size="24px" color="#fff" />
</view>
/>
</view>
</template>
<script lang="ts" setup>
import type { SearchFormData } from './components/search-form.vue'
import type { Post } from '@/api/system/post'
import type { LoadMoreState } from '@/http/types'
import { onReachBottom } from '@dcloudio/uni-app'
import { onMounted, reactive, ref } from 'vue'
import { getPostPage } from '@/api/system/post'
import { useAccess } from '@/hooks/useAccess'
import { navigateBackPlus } from '@/utils'
import { DICT_TYPE } from '@/utils/constants'
import SearchForm from './components/search-form.vue'
@@ -84,10 +81,10 @@ definePage({
},
})
const { hasAccessByCodes } = useAccess()
const total = ref(0) // 列表的总页数
const list = ref<Post[]>([]) // 列表的数据
const loadMoreState = ref<LoadMoreState>('loading') // 加载更多状态
const searchVisible = ref(false) // 搜索弹窗
const queryParams = reactive({
pageNo: 1,
pageSize: 10,
@@ -98,7 +95,7 @@ const queryParams = reactive({
/** 返回上一页 */
function handleBack() {
uni.navigateBack()
navigateBackPlus()
}
/** 查询岗位列表 */
@@ -119,7 +116,7 @@ async function getList() {
}
/** 搜索按钮操作 */
function handleQuery(data?: { name?: string, code?: string, status?: number }) {
function handleQuery(data?: SearchFormData) {
queryParams.name = data?.name
queryParams.code = data?.code
queryParams.status = data?.status ?? -1