吉安感知网项目-前端
shuishen
2026-02-03 89380e6260a75d1d3b94de687ebcc2f50d50659d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<template>
    <basic-container>
        <el-form ref="queryParamsRef" :model="searchParams" class="command-page-history-search">
            <el-form-item label="部门名称" prop="deptName">
                <el-input
                    class="command-input"
                    v-model="searchParams.deptName"
                    placeholder="请输入"
                    clearable
                    @clear="handleSearch"
                />
            </el-form-item>
 
            <el-form-item label="部门全称" prop="fullName">
                <el-input
                    class="command-input"
                    v-model="searchParams.fullName"
                    placeholder="请输入"
                    clearable
                    @clear="handleSearch"
                />
            </el-form-item>
 
            <el-form-item class="history-search-actions">
                <el-button :icon="RefreshRight" @click="resetForm"></el-button>
                <el-button class="search-btn" :icon="Search" @click="handleSearch"></el-button>
            </el-form-item>
        </el-form>
 
        <div class="command-table-toolbar">
            <el-button :icon="Plus" color="#284FE3" type="primary" @click="handleAdd">新增</el-button>
            <el-button :icon="Delete" color="#1A2652" type="primary" :disabled="!selectedIds.length" @click="handleDelete()">
                删除
            </el-button>
        </div>
 
        <div class="command-table-container" v-loading="loading" element-loading-background="rgba(5, 5, 15, 0.6)">
            <div class="command-table-content command-table-content-bg">
                <el-table
                    class="command-table"
                    :data="list"
                    row-key="id"
                    lazy
                    :load="loadChildren"
                    :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="46" />
                    <el-table-column type="index" show-overflow-tooltip width="64" label="序号" />
                    <el-table-column prop="deptName" show-overflow-tooltip label="部门名称" />
                    <el-table-column prop="fullName" show-overflow-tooltip label="部门全称" />
                    <el-table-column prop="deptCategory" show-overflow-tooltip label="部门类型">
                        <template v-slot="{ row }">
                            {{ getDictLabel(row.deptCategory, dictObj.org_category) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="deptNature" show-overflow-tooltip label="部门性质">
                        <template v-slot="{ row }">
                            {{ getDictLabel(row.deptNature, dictObj.org_nature) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="sort" show-overflow-tooltip width="80" label="排序" />
                    <el-table-column prop="bingId" show-overflow-tooltip width="80" label="组织id" />
                    <el-table-column prop="areaName" show-overflow-tooltip label="行政区划" />
 
                    <el-table-column label="操作" class-name="operation-btns">
                        <template v-slot="{ row }">
                            <el-link @click="handleView(row)">查看</el-link>
                            <el-link @click="handleEdit(row)">编辑</el-link>
                            <el-link @click="handleAddChild(row)">新增子项</el-link>
                            <el-link type="danger" @click="handleDelete(row)">删除</el-link>
                            <el-link type="danger" @click="handleShare(row)" v-if="hasSharing">共享</el-link>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
 
            <div class="command-table-pagination">
                <el-pagination
                    popper-class="command-select-popper"
                    v-model:current-page="searchParams.current"
                    v-model:page-size="searchParams.size"
                    layout="total, prev, pager, next, sizes"
                    :total="total"
                    @change="getList"
                />
            </div>
        </div>
 
        <FormDiaLog ref="dialogRef" @success="handleFormSuccess" v-model="visible" v-if="visible" />
        <ShareDiaLog ref="shareDialogRef" @success="getList" v-model="shareVisible" v-if="shareVisible" />
    </basic-container>
</template>
 
<script setup>
import { Delete, Plus, RefreshRight, Search } from '@element-plus/icons-vue'
import { nextTick, onMounted, provide, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getDictionary } from '@/api/system/dict'
import { getChildLazyTree, getDeptTree, getLazyList, remove } from '@/api/system/dept'
import { getDictLabel } from '@ztzf/utils'
import FormDiaLog from './FormDiaLog.vue'
import ShareDiaLog from './ShareDiaLog.vue'
import { saveOperationLog } from '@ztzf/apis'
import { useRoute } from 'vue-router'
import { useStore } from 'vuex'
 
const store = useStore()
const permission = computed(() => store.state.user.permission)
const ancestors = computed(() => store.state.user.userInfo?.detail?.ancestors)
// 市级部门并且有权限才能看到共享按钮
const hasSharing = computed(() => permission.value.department_share && ancestors.value.split(',')?.length === 2)
const initSearchParams = () => ({
    deptName: '',
    fullName: '',
    current: 1,
    size: 10,
})
 
const searchParams = ref(initSearchParams())
const loading = ref(true)
const visible = ref(false)
const list = ref([])
const total = ref(0)
const selectedIds = ref([])
const selectedRows = ref([])
const lastActionParentRow = ref(null)
const queryParamsRef = ref(null)
const dialogRef = ref(null)
const shareDialogRef = ref(null)
const shareVisible = ref(false)
const dictObj = ref({
    org_category: [],
    org_nature: [],
})
const deptTree = ref([])
const route = useRoute()
const treeResolveMap = new Map()
 
provide('dictObj', dictObj)
provide('deptTree', deptTree)
 
async function getList() {
    loading.value = true
    try {
        const res = await getLazyList(0, searchParams.value)
        list.value = res?.data?.data ?? []
        total.value = list.value.length
    } finally {
        loading.value = false
    }
}
 
function handleSearch() {
    searchParams.value.current = 1
    getList()
}
 
function resetForm() {
    queryParamsRef.value?.resetFields()
    searchParams.value.current = 1
    getList()
}
 
function handleAdd() {
    visible.value = true
    nextTick(() => {
        dialogRef.value?.open({ mode: 'add' })
    })
}
 
function handleAddChild(row) {
    lastActionParentRow.value = row
    visible.value = true
    nextTick(() => {
        dialogRef.value?.open({ mode: 'add', parent: row })
    })
}
 
function handleView(row) {
    visible.value = true
    nextTick(() => {
        dialogRef.value?.open({ mode: 'view', row: { ...row } })
    })
}
 
function handleEdit(row) {
    visible.value = true
    nextTick(() => {
        dialogRef.value?.open({ mode: 'edit', row: { ...row } })
    })
}
 
function handleShare(row) {
    shareVisible.value = true
    nextTick(() => {
        shareDialogRef.value?.open({ row })
    })
}
 
async function handleDelete(row) {
    const tips = row ? '该条' : '选中的项'
    await ElMessageBox.confirm(`确认删除${tips}吗?`, '提示', {
        type: 'warning',
        customClass: 'command-page-view-message-box',
        confirmButtonClass: 'command-message-box-confirm',
        cancelButtonClass: 'command-message-box-cancel',
    })
    const ids = row ? row.id : selectedIds.value.join(',')
    const rows = row ? [row] : selectedRows.value
    await remove(ids)
    saveOperationLog({
        requestUri: route.path,
        title: `${route.name || '部门管理'}-删除`,
        type: 1,
    })
    ElMessage.success('删除成功')
    selectedIds.value = []
    selectedRows.value = []
    const parentIds = Array.from(new Set(rows.map(item => item.parentId).filter(Boolean)))
    const hasRoot = rows.some(item => !item.parentId)
    if (hasRoot) {
        getList()
    }
    parentIds.forEach(parentId => refreshChildNodes(parentId))
}
 
function handleSelectionChange(rows) {
    selectedIds.value = rows.map(item => item.id)
    selectedRows.value = rows
}
 
function getDictList() {
    Promise.all([getDictionary({ code: 'org_category' }), getDictionary({ code: 'org_nature' })]).then(
        ([categoryRes, natureRes]) => {
            dictObj.value = {
                org_category: categoryRes?.data?.data ?? [],
                org_nature: natureRes?.data?.data ?? [],
            }
        }
    )
}
 
function getDeptTreeList() {
    getDeptTree().then(res => {
        deptTree.value = res.data.data
    })
}
 
function loadChildren(row, treeNode, resolve) {
    treeResolveMap.set(row.id, { resolve, row })
    getChildLazyTree({ parentId: row.id })
        .then(res => {
            const children = res?.data?.data ?? []
            row.hasChildren = children.length > 0
            resolve(children)
        })
        .catch(() => {
            row.hasChildren = false
            resolve([])
        })
}
 
function refreshChildNodes(parentId) {
    const cache = treeResolveMap.get(parentId)
    if (!cache?.resolve) return
    getChildLazyTree({ parentId })
        .then(res => {
            const children = res?.data?.data ?? []
            if (cache.row) {
                cache.row.hasChildren = children.length > 0
            }
            cache.resolve(children)
        })
        .catch(() => {
            if (cache.row) {
                cache.row.hasChildren = false
            }
            cache.resolve([])
        })
}
 
function handleFormSuccess(payload = {}) {
    if (payload?.parentId) {
        refreshChildNodes(payload.parentId)
        if (payload.isAddChild && lastActionParentRow.value?.id === payload.parentId) {
            lastActionParentRow.value.hasChildren = true
        }
        lastActionParentRow.value = null
        return
    }
    lastActionParentRow.value = null
    getList()
}
 
onMounted(() => {
    getList()
    getDictList()
    getDeptTreeList()
})
</script>
 
<style scoped lang="scss"></style>