Files
jnpf_app/components/Jnpf/OrganizeSelect/SelectPopup.vue

502 lines
13 KiB
Vue
Raw Normal View History

2026-01-04 11:09:06 +08:00
<template>
<u-popup class="jnpf-tree-select-popup" mode="right" v-model="showPopup" width="100%" @close="close">
<view class="jnpf-tree-select-body">
<view class="jnpf-tree-select-title">
<text class="icon-ym icon-ym-report-icon-preview-pagePre backIcon" @tap="close()"></text>
2026-01-19 17:34:15 +08:00
<view class="title">选择申请人单位</view>
2026-01-04 11:09:06 +08:00
</view>
<view class="jnpf-tree-select-search">
<u-search :placeholder="$t('app.apply.pleaseKeyword')" v-model="keyword" height="72"
:show-action="false" @change="handleSearch" bg-color="#f0f2f6" shape="square">
</u-search>
</view>
<view class="jnpf-tree-selected">
<view class="jnpf-tree-selected-head">
<view>{{$t('component.jnpf.common.selected')}}({{selectedList.length||0}})</view>
<view v-if="multiple" class="clear-btn" @click="setCheckAll">
{{$t('component.jnpf.common.clearAll')}}
</view>
</view>
<view class="jnpf-tree-selected-box">
<scroll-view scroll-y="true" class="select-list">
<u-tag closeable @close="delSelect(index)" v-for="(item,index) in selectedList" :key="index"
2026-01-19 17:34:15 +08:00
:text="item.deptName" class="u-selectTag" />
2026-01-04 11:09:06 +08:00
</scroll-view>
</view>
</view>
<view class="jnpf-tree-selected-line"></view>
2026-01-19 17:34:15 +08:00
<!-- <view class="jnpf-tree-selected-tabs">
<view class="tab-item" :class="{'tab-item-active':activeKey==='1'}" @click="toggloActive('1')">组织构架</view>
2026-01-04 11:09:06 +08:00
<view class="tab-item" :class="{'tab-item-active':activeKey==='2'}" @click="toggloActive('2')"
v-if="selectType === 'all'">
当前组织
</view>
2026-01-19 17:34:15 +08:00
</view> -->
2026-01-04 11:09:06 +08:00
<view class="jnpf-tree-select-tree">
2026-01-19 17:34:15 +08:00
<!-- 树形结构区域 -->
2026-01-04 11:09:06 +08:00
<scroll-view :scroll-y="true" style="height: 100%" v-if="activeKey==='1' && !hasPage">
<ly-tree v-if="selectType !== 'all'" :tree-data="options" :node-key="realProps.value"
default-expand-all :props="realProps" :filter-node-method="filterNode"
child-visible-for-filter-node show-node-icon @node-click="handleTreeNodeClick"
:show-checkbox="multiple" :show-radio="!multiple" :expandOnClickNode="false"
:checkOnClickNode="true" :expandOnCheckNode="false" checkStrictly ref="tree">
</ly-tree>
<ly-tree ref="tree" v-if="selectType === 'all'" :props="realProps" :node-key="realProps.value"
:load="loadNode" lazy :tree-data="lazyOptions" show-node-icon :defaultExpandAll='false'
@node-click="handleTreeNodeClick" :show-checkbox="multiple" :show-radio="!multiple"
:expandOnClickNode="false" :checkOnClickNode="true" :expandOnCheckNode="false" checkStrictly />
</scroll-view>
2026-01-19 17:34:15 +08:00
<!-- 列表/搜索结果区域 -->
2026-01-04 11:09:06 +08:00
<scroll-view :scroll-y="true" style="height: 100%" :refresher-enabled="false" :refresher-threshold="100"
:scroll-with-animation='true' :refresher-triggered="triggered" @scrolltolower="handleScrollToLower"
v-if="activeKey==='2'|| (activeKey==='1' && hasPage)">
<view class="jnpf-selcet-list" v-if="list.length">
2026-01-19 17:34:15 +08:00
<view class="jnpf-selcet-cell" v-for="item in list" :key="item.deptId"
2026-01-04 11:09:06 +08:00
@click.stop="handleNodeClick(item)">
<view class="jnpf-selcet-cell-action">
<lyCheckbox :type="multiple ? 'checkbox' : 'radio'"
2026-01-19 17:34:15 +08:00
:checked="selectedIds.includes(item.deptId)" />
2026-01-04 11:09:06 +08:00
</view>
<view class="jnpf-selcet-cell-name">
2026-01-19 17:34:15 +08:00
{{item.deptName}}
2026-01-04 11:09:06 +08:00
</view>
</view>
</view>
<Empty class="h-full" v-else />
</scroll-view>
</view>
<!-- 底部按钮 -->
<view class="jnpf-tree-select-actions">
<u-button class="buttom-btn" @click="close()">{{$t('common.cancelText')}}</u-button>
<u-button class="buttom-btn" type="primary"
@click.stop="handleConfirm()">{{$t('common.okText')}}</u-button>
</view>
</view>
</u-popup>
</template>
2026-01-19 17:34:15 +08:00
2026-01-04 11:09:06 +08:00
<script>
import {
getOrgByOrganizeCondition,
getOrganizeSelector
} from '@/api/common'
import lyCheckbox from '@/components/ly-tree/components/ly-checkbox.vue';
import Empty from '../Empty/index.vue'
2026-01-19 17:34:15 +08:00
// 适配接口返回的字段
2026-01-04 11:09:06 +08:00
const defaultProps = {
2026-01-19 17:34:15 +08:00
label: 'deptName', // 对应接口的deptName
value: 'deptId', // 对应接口的deptId
2026-01-04 11:09:06 +08:00
icon: 'icon',
2026-01-19 17:34:15 +08:00
children: 'children',// 子节点字段
isLeaf: 'isLeaf' // 叶子节点标识
2026-01-04 11:09:06 +08:00
}
2026-01-19 17:34:15 +08:00
2026-01-04 11:09:06 +08:00
export default {
props: {
selectedData: {
type: Array,
default: () => []
},
ableIds: {
type: Array,
default: () => []
},
selectType: {
type: String,
default: 'all'
},
2026-01-19 17:34:15 +08:00
type: {
type: String,
default: 'all'
},
2026-01-04 11:09:06 +08:00
modelValue: {
type: Boolean,
default: false
},
zIndex: {
type: [String, Number],
default: 0
},
props: {
type: Object,
2026-01-19 17:34:15 +08:00
default: () => ({})
2026-01-04 11:09:06 +08:00
},
multiple: {
type: Boolean,
default: false
}
},
components: {
lyCheckbox,
Empty
},
data() {
return {
moving: false,
selectedList: [],
selectedIds: [],
keyword: '',
showPopup: false,
options: [],
2026-01-19 17:34:15 +08:00
lazyOptions: [{}], // 懒加载根节点占位
2026-01-04 11:09:06 +08:00
activeKey: '1',
hasPage: false,
pagination: {
hasPage: 1,
currentPage: 1,
pageSize: 20
},
triggered: false,
finish: false,
list: []
};
},
watch: {
modelValue: {
handler(val) {
this.showPopup = val
if (!val) this.activeKey = ''
if (val) setTimeout(() => this.init(), 10);
},
immediate: true
},
selectedList: {
handler(val) {
2026-01-19 17:34:15 +08:00
// 适配deptId字段
this.selectedIds = val.map((o) => o.deptId);
2026-01-04 11:09:06 +08:00
this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
},
deep: true
},
},
computed: {
uZIndex() {
return this.zIndex ? this.zIndex : this.$u.zIndex.popup;
},
realProps() {
return {
...defaultProps,
...this.props
}
},
getCurrOrgList() {
const userInfo = uni.getStorageSync('userInfo') || {}
const list = (userInfo.organizeList || []).map((o) => ({
...o,
2026-01-19 17:34:15 +08:00
deptName: o.treeName || o.deptName,
deptId: o.id || o.deptId
2026-01-04 11:09:06 +08:00
}))
return list
}
},
methods: {
init() {
2026-01-19 17:34:15 +08:00
this.keyword = ""
this.hasPage = 0
this.activeKey = '1'
this.finish = false
this.pagination.currentPage = 1
this.$nextTick(() => {
this.triggered = false
})
// 深拷贝选中数据
this.selectedList = JSON.parse(JSON.stringify(this.selectedData)).map(item => ({
deptId: item.id || item.deptId,
deptName: item.orgNameTree || item.deptName || item.fullName,
...item
}))
// 如果是全量选择,预加载根节点
if (this.selectType === 'all') {
this.preloadRootNodes()
} else {
this.getConditionOptions()
}
},
preloadRootNodes() {
const data = {
type: this.type,
}
getOrganizeSelector(data).then(res => {
const list = res.data || []
// 构建根节点树
this.options = this.buildTree(list, 'deptId', 'deptPid', '0')
console.log('预加载的根节点:', this.options)
// 设置初始选中状态
this.$nextTick(() => {
if (this.$refs.tree && this.selectedIds.length > 0) {
this.$refs.tree.setCheckedKeys(this.selectedIds)
}
})
})
2026-01-04 11:09:06 +08:00
},
getConditionOptions() {
if (!this.ableIds.length) return
const query = {
ids: this.ableIds
}
getOrgByOrganizeCondition(query).then(res => {
2026-01-19 17:34:15 +08:00
// 适配接口返回的扁平数据为树形结构
this.options = this.buildTree(res.data.list || [], 'deptId', 'deptPid', '0')
2026-01-04 11:09:06 +08:00
this.$refs.tree && this.$refs.tree.setCheckedKeys(this.selectedIds)
})
},
2026-01-19 17:34:15 +08:00
// 扁平数组转树形结构
buildTree(data, idKey, pidKey, rootPid) {
const result = []
const map = {}
// 先构建ID映射
data.forEach(item => {
map[item[idKey]] = { ...item, children: [] }
})
// 组装父子关系
data.forEach(item => {
const parent = map[item[pidKey]]
if (item[pidKey] === rootPid) {
result.push(map[item[idKey]])
} else if (parent) {
parent.children.push(map[item[idKey]])
}
})
return result
},
// 树形节点过滤
2026-01-04 11:09:06 +08:00
filterNode(value, data) {
if (!value) return true;
2026-01-19 17:34:15 +08:00
return data.deptName && data.deptName.indexOf(value) !== -1;
2026-01-04 11:09:06 +08:00
},
2026-01-19 17:34:15 +08:00
// 懒加载节点
2026-01-04 11:09:06 +08:00
loadNode(node, resolve) {
2026-01-19 17:34:15 +08:00
const parentId = node.key || '0'
// 获取该节点的深度信息
const level = node.level || 0
console.log('加载节点:', { parentId, level, node })
// 如果是根节点level为0直接使用已经构建好的options
if (level === 0 && this.options.length > 0) {
resolve(this.options)
return
}
const data = {
type: this.type,
}
getOrganizeSelector(data).then(res => {
const list = res.data || []
// 将返回的扁平数据转换为树形节点
const treeData = this.buildTree(list, 'deptId', 'deptPid', parentId)
// 设置节点的isLeaf属性根据是否有子节点判断
treeData.forEach(item => {
// 判断该节点是否还有子节点(根据实际情况调整)
// 如果知道接口返回是否有子节点的字段,可以替换这个判断
item.isLeaf = !item.children || item.children.length === 0
})
console.log(`加载父节点 ${parentId} 的子节点:`, treeData)
resolve(treeData)
}).catch(() => {
resolve([]) // 异常时返回空数组,避免组件报错
2026-01-04 11:09:06 +08:00
})
},
2026-01-19 17:34:15 +08:00
// 滚动加载(仅搜索列表使用)
2026-01-04 11:09:06 +08:00
handleScrollToLower() {
2026-01-19 17:34:15 +08:00
if (this.finish || this.activeKey === '2' || !this.keyword) return
2026-01-04 11:09:06 +08:00
this.getTreeData()
},
2026-01-19 17:34:15 +08:00
// 获取搜索列表数据
2026-01-04 11:09:06 +08:00
getTreeData() {
2026-01-19 17:34:15 +08:00
const data = {
type: this.type,
// keyword: this.keyword,
// parentId: '0',
// ...this.pagination
2026-01-04 11:09:06 +08:00
}
getOrganizeSelector(data).then(res => {
2026-01-19 17:34:15 +08:00
const list = res.data || []
2026-01-04 11:09:06 +08:00
if (list.length < this.pagination.pageSize) this.finish = true;
this.list = this.list.concat(list);
this.pagination.currentPage++
})
},
2026-01-19 17:34:15 +08:00
// 树形节点点击
2026-01-04 11:09:06 +08:00
handleTreeNodeClick(item) {
2026-01-19 17:34:15 +08:00
const data = item.data || item
2026-01-04 11:09:06 +08:00
this.handleNodeClick(data)
},
2026-01-19 17:34:15 +08:00
// 列表节点点击
2026-01-04 11:09:06 +08:00
handleNodeClick(data) {
2026-01-19 17:34:15 +08:00
// 适配deptId字段
const index = this.selectedList.findIndex((o) => o.deptId === data.deptId);
if (index !== -1) {
this.selectedList.splice(index, 1);
} else {
this.multiple ? this.selectedList.push(data) : (this.selectedList = [data]);
}
2026-01-04 11:09:06 +08:00
},
2026-01-19 17:34:15 +08:00
// 删除选中项
2026-01-04 11:09:06 +08:00
delSelect(index) {
this.selectedList.splice(index, 1);
},
2026-01-19 17:34:15 +08:00
// 清空选中
2026-01-04 11:09:06 +08:00
setCheckAll() {
this.selectedIds = []
this.selectedList = []
},
2026-01-19 17:34:15 +08:00
// 确认选择
2026-01-04 11:09:06 +08:00
handleConfirm() {
2026-01-19 17:34:15 +08:00
// 转换回原始字段格式,兼容父组件
const resultList = this.selectedList.map(item => ({
id: item.deptId,
orgNameTree: item.deptName,
...item
}))
const resultIds = resultList.map(item => item.id)
console.log(resultIds,resultList,'数据')
this.$emit('confirm', resultList, resultIds);
2026-01-04 11:09:06 +08:00
this.close();
},
2026-01-19 17:34:15 +08:00
// 关闭弹窗
2026-01-04 11:09:06 +08:00
close() {
2026-01-19 17:34:15 +08:00
this.$emit('update:modelValue', false); // 双向绑定规范
2026-01-04 11:09:06 +08:00
this.$emit('close', false);
},
2026-01-19 17:34:15 +08:00
// 切换标签
2026-01-04 11:09:06 +08:00
toggloActive(key) {
if (this.activeKey === key) return
this.keyword = ''
this.$nextTick(() => {
this.activeKey = key
2026-01-19 17:34:15 +08:00
if (this.activeKey === '2') {
this.list = this.getCurrOrgList
} else {
this.list = []
}
2026-01-04 11:09:06 +08:00
})
},
2026-01-19 17:34:15 +08:00
// 搜索处理
2026-01-04 11:09:06 +08:00
handleSearch(val) {
2026-01-19 17:34:15 +08:00
this.keyword = val.trim()
// 非全量选择时,使用树形过滤
if (this.selectType !== 'all') {
this.$refs.tree && this.$refs.tree.filter(this.keyword)
return
}
// 有搜索关键词时,切换为列表展示
this.hasPage = !!this.keyword
2026-01-04 11:09:06 +08:00
this.list = []
2026-01-19 17:34:15 +08:00
this.finish = false
this.pagination.currentPage = 1
this.activeKey = '1'
if (this.keyword) {
this.$u.debounce(this.getTreeData, 300)() // 执行防抖函数
}
2026-01-04 11:09:06 +08:00
},
}
};
2026-01-19 17:34:15 +08:00
</script>
<style scoped>
/* 保留原有样式,如需补充可添加 */
.jnpf-tree-select-body {
height: 100vh;
display: flex;
flex-direction: column;
background: #fff;
}
.jnpf-tree-select-title {
display: flex;
align-items: center;
padding: 16rpx;
border-bottom: 1rpx solid #eee;
}
.backIcon {
font-size: 32rpx;
margin-right: 16rpx;
}
.title {
font-size: 32rpx;
font-weight: 500;
}
.jnpf-tree-select-search {
padding: 16rpx;
}
.jnpf-tree-selected {
padding: 0 16rpx;
}
.jnpf-tree-selected-head {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12rpx 0;
}
.clear-btn {
color: #007aff;
font-size: 28rpx;
}
.select-list {
display: flex;
flex-wrap: wrap;
gap: 12rpx;
padding: 8rpx 0;
max-height: 200rpx;
}
.u-selectTag {
margin-bottom: 8rpx;
}
.jnpf-tree-selected-line {
height: 1rpx;
background: #eee;
margin: 8rpx 0;
}
.jnpf-tree-selected-tabs {
display: flex;
}
.tab-item {
flex: 1;
text-align: center;
padding: 16rpx;
font-size: 28rpx;
}
.tab-item-active {
color: #007aff;
border-bottom: 2rpx solid #007aff;
}
.jnpf-tree-select-tree {
flex: 1;
padding: 16rpx;
}
.jnpf-selcet-cell {
display: flex;
align-items: center;
padding: 16rpx 0;
border-bottom: 1rpx solid #f5f5f5;
}
.jnpf-selcet-cell-action {
margin-right: 16rpx;
}
.jnpf-tree-select-actions {
display: flex;
padding: 16rpx;
gap: 16rpx;
}
.buttom-btn {
flex: 1;
height: 88rpx;
line-height: 88rpx;
}
.h-full {
height: 100%;
display: flex;
align-items: center;
justify-content: center;
}
</style>