Files
ks-app-employment-service/components/expected-station/expected-station.vue

391 lines
15 KiB
Vue
Raw Normal View History

2025-03-28 15:19:42 +08:00
<template>
<view class="expected-station">
<view class="sex-search" v-if="search">
<uni-icons class="iconsearch" type="search" size="20"></uni-icons>
<input class="uni-input searchinput" confirm-type="search" />
</view>
<view class="sex-content">
2025-11-13 19:28:00 +08:00
<scroll-view ref="leftScroll" :show-scrollbar="false" :scroll-y="true" class="sex-content-left">
2025-03-28 15:19:42 +08:00
<view
v-for="item in copyTree"
:key="item.id"
class="left-list-btn"
:class="{ 'left-list-btned': item.id === leftValue.id }"
@click="changeStationLog(item)"
>
{{ item.label }}
2025-05-13 11:10:38 +08:00
<!-- <view class="positionNum" v-show="item.checkednumber">
2025-03-28 15:19:42 +08:00
{{ item.checkednumber }}
2025-05-13 11:10:38 +08:00
</view> -->
2025-03-28 15:19:42 +08:00
</view>
</scroll-view>
2025-05-13 11:10:38 +08:00
<scroll-view
:show-scrollbar="false"
:scroll-top="scrollTop"
@scroll="scrollTopBack"
:scroll-y="true"
class="sex-content-right"
>
2025-03-28 15:19:42 +08:00
<view v-for="item in rightValue" :key="item.id">
<view class="secondary-title">{{ item.label }}</view>
<view class="grid-sex">
<view
v-for="item in item.children"
:key="item.id"
:class="{ 'sex-right-btned': item.checked }"
class="sex-right-btn"
@click="addItem(item)"
>
{{ item.label }}
</view>
<!-- <view class="sex-right-btn sex-right-btned" @click="addItem()">客户经理</view>
<view class="sex-right-btn" @click="addItem()">客户经理</view> -->
</view>
</view>
</scroll-view>
</view>
</view>
</template>
<script>
export default {
name: 'expected-station',
data() {
return {
leftValue: {},
rightValue: [],
stationCateLog: 0,
copyTree: [],
2025-05-13 11:10:38 +08:00
scrollTop: 0,
2025-11-19 13:02:40 +08:00
scrollTimer: null,
lastScrollTime: 0,
2025-03-28 15:19:42 +08:00
};
},
props: {
station: {
type: Array,
default: [],
},
search: {
type: Boolean,
default: true,
},
max: {
type: Number,
default: 5,
},
},
created() {
this.copyTree = this.station;
if (this.copyTree.length) {
this.leftValue = this.copyTree[0];
this.rightValue = this.copyTree[0].children;
}
},
watch: {
station(newVal) {
this.copyTree = this.station;
if (this.copyTree.length) {
this.leftValue = this.copyTree[0];
this.rightValue = this.copyTree[0].children;
}
},
},
2025-11-19 13:02:40 +08:00
beforeDestroy() {
// 组件销毁前清除定时器
if (this.scrollTimer) {
clearTimeout(this.scrollTimer);
this.scrollTimer = null;
}
},
2025-03-28 15:19:42 +08:00
methods: {
changeStationLog(item) {
this.leftValue = item;
this.rightValue = item.children;
2025-05-13 11:10:38 +08:00
this.scrollTop = 0;
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 使用更激进的防抖策略,避免频繁的左侧滚动
if (this.scrollTimer) {
clearTimeout(this.scrollTimer);
}
this.scrollTimer = setTimeout(() => {
// 确保左侧列表滚动到正确位置,让当前选中的标签可见
2025-11-13 19:28:00 +08:00
const index = this.copyTree.findIndex(i => i.id === item.id);
if (index !== -1 && this.$refs.leftScroll) {
2025-11-19 13:02:40 +08:00
const itemHeight = 160; // 每个选项高度
const visibleHeight = 4 * itemHeight; // 可见区域高度
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 计算目标位置
const targetTop = index * itemHeight;
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 获取当前左侧列表的滚动位置
const currentLeftScrollTop = this.$refs.leftScroll.scrollTop || 0;
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 只有当目标位置不在当前可见区域内时才滚动
if (targetTop < currentLeftScrollTop || targetTop > currentLeftScrollTop + visibleHeight - itemHeight) {
let targetScrollTop = targetTop;
// 如果选中的标签在底部,确保它不会滚动出可见区域
if (targetTop > this.$refs.leftScroll.scrollHeight - visibleHeight) {
targetScrollTop = Math.max(0, this.$refs.leftScroll.scrollHeight - visibleHeight);
}
this.$refs.leftScroll.scrollTo({
top: targetScrollTop,
duration: 100 // 进一步减少滚动动画时间
});
2025-11-13 19:28:00 +08:00
}
}
2025-11-19 13:02:40 +08:00
}, 100);
2025-05-13 11:10:38 +08:00
},
scrollTopBack(e) {
2025-11-19 13:02:40 +08:00
const currentTime = Date.now();
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 更严格的防抖处理:如果距离上次滚动时间太短,则跳过处理
if (currentTime - this.lastScrollTime < 80) {
return;
}
this.lastScrollTime = currentTime;
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 清除之前的定时器
if (this.scrollTimer) {
clearTimeout(this.scrollTimer);
}
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
// 设置新的定时器,延迟处理滚动事件
this.scrollTimer = setTimeout(() => {
this.scrollTop = e.detail.scrollTop;
// 滚动时自动选中对应的左侧标签
const scrollTop = e.detail.scrollTop;
let currentSection = null;
let minDistance = Infinity;
// 遍历所有右侧的二级标题,找到当前最接近顶部的那个
this.rightValue.forEach((section, index) => {
// 更精确地估算每个section的位置考虑标题高度和内容高度
const sectionTop = index * 280; // 增加估算高度,避免过于敏感
const distance = Math.abs(sectionTop - scrollTop);
2025-11-13 19:28:00 +08:00
2025-11-19 13:02:40 +08:00
if (distance < minDistance) {
minDistance = distance;
currentSection = section;
}
});
// 只有当距离足够近时才切换左侧标签,避免过于敏感
if (currentSection && minDistance < 100 && this.leftValue.id !== currentSection.parentId) {
const parentItem = this.copyTree.find(item => item.id === currentSection.parentId);
if (parentItem) {
this.leftValue = parentItem;
// 使用更激进的防抖策略,避免频繁的左侧滚动
if (this.scrollTimer) {
clearTimeout(this.scrollTimer);
2025-11-13 19:28:00 +08:00
}
2025-11-19 13:02:40 +08:00
this.scrollTimer = setTimeout(() => {
// 只在必要时滚动左侧列表,避免频繁滚动导致的抖动
const index = this.copyTree.findIndex(i => i.id === parentItem.id);
if (index !== -1 && this.$refs.leftScroll) {
// 获取当前左侧列表的滚动位置
const currentLeftScrollTop = this.$refs.leftScroll.scrollTop || 0;
const itemHeight = 160; // 每个选项高度
const visibleHeight = 4 * itemHeight; // 可见区域高度
// 计算目标位置
const targetTop = index * itemHeight;
// 只有当目标位置不在当前可见区域内时才滚动
if (targetTop < currentLeftScrollTop || targetTop > currentLeftScrollTop + visibleHeight - itemHeight) {
let targetScrollTop = targetTop;
// 如果选中的标签在底部,确保它不会滚动出可见区域
if (targetTop > this.$refs.leftScroll.scrollHeight - visibleHeight) {
targetScrollTop = Math.max(0, this.$refs.leftScroll.scrollHeight - visibleHeight);
}
this.$refs.leftScroll.scrollTo({
top: targetScrollTop,
duration: 100 // 进一步减少滚动动画时间
});
}
}
}, 150); // 增加延迟,避免滚动冲突
}
2025-11-13 19:28:00 +08:00
}
2025-11-19 13:02:40 +08:00
}, 80); // 增加防抖延迟到80ms
2025-03-28 15:19:42 +08:00
},
addItem(item) {
let titiles = [];
2025-05-13 11:10:38 +08:00
let labels = [];
2025-03-28 15:19:42 +08:00
let count = 0;
// 先统计已选中的职位数量
for (const firstLayer of this.copyTree) {
for (const secondLayer of firstLayer.children) {
for (const thirdLayer of secondLayer.children) {
if (thirdLayer.checked) {
count++;
}
}
}
}
for (const firstLayer of this.copyTree) {
firstLayer.checkednumber = 0; // 初始化当前层级的 checked 计数
for (const secondLayer of firstLayer.children) {
for (const thirdLayer of secondLayer.children) {
// **如果是当前点击的职位**
if (thirdLayer.id === item.id) {
if (!thirdLayer.checked && count >= 5) {
// 如果已经选了 5 个,并且点击的是未选中的职位,则禁止选择
uni.showToast({
title: `最多选择5个职位`,
icon: 'none',
});
continue; // 跳过后续逻辑,继续循环
}
// 切换选中状态
thirdLayer.checked = !thirdLayer.checked;
}
// 统计被选中的第三层节点
if (thirdLayer.checked) {
titiles.push(`${thirdLayer.id}`);
2025-05-13 11:10:38 +08:00
labels.push(`${thirdLayer.label}`);
2025-03-28 15:19:42 +08:00
firstLayer.checkednumber++; // 累加计数器
}
}
}
}
titiles = titiles.join(',');
2025-05-13 11:10:38 +08:00
labels = labels.join(',');
this.$emit('onChange', {
ids: titiles,
labels,
});
2025-03-28 15:19:42 +08:00
},
},
};
</script>
<style lang="stylus" scoped>
.secondary-title{
2025-05-13 11:10:38 +08:00
color: #333333
font-size: 28rpx
2025-03-28 15:19:42 +08:00
padding: 40rpx 0 10rpx 30rpx;
}
.expected-station{
width: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
2025-05-13 11:10:38 +08:00
height: 100%
2025-03-28 15:19:42 +08:00
}
.sex-search
width: calc(100% - 28rpx - 28rpx);
padding: 10rpx 28rpx;
display: grid;
// grid-template-columns: 50rpx auto;
position: relative;
.iconsearch
position: absolute;
left: 40rpx;
top: 20rpx;
.searchinput
border-radius: 10rpx;
background: #FFFFFF;
padding: 10rpx 0 10rpx 58rpx;
.sex-content
background: #FFFFFF;
2025-05-13 11:10:38 +08:00
// border-radius: 20rpx;
2025-03-28 15:19:42 +08:00
width: 100%;
margin-top: 20rpx;
display: flex;
border-bottom: 2px solid #D9D9D9;
overflow: hidden;
2025-05-13 11:10:38 +08:00
height: 100%;
2025-03-28 15:19:42 +08:00
.sex-content-left
2025-05-13 11:10:38 +08:00
width: 198rpx;
padding: 20rpx 0 0 0;
2025-11-19 13:02:40 +08:00
/* 添加硬件加速优化 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
will-change: transform;
2025-03-28 15:19:42 +08:00
.left-list-btn
padding: 0 40rpx 0 24rpx;
display: grid;
place-items: center;
height: 100rpx;
text-align: center;
color: #606060;
font-size: 28rpx;
position: relative
2025-05-13 11:10:38 +08:00
margin-top: 60rpx
2025-11-19 13:02:40 +08:00
/* 优化渲染性能 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
2025-05-13 11:10:38 +08:00
.left-list-btn:first-child
margin-top: 0
// .positionNum
// position: absolute
// right: 0
// top: 50%;
// transform: translate(0, -50%)
// color: #FFFFFF
// background: #4778EC
// border-radius: 50%
// width: 36rpx;
// height: 36rpx;
2025-03-28 15:19:42 +08:00
.left-list-btned
color: #4778EC;
position: relative;
2025-05-13 11:10:38 +08:00
// .left-list-btned::after
// position: absolute;
// left: 20rpx;
// content: '';
// width: 7rpx;
// height: 38rpx;
// background: #4778EC;
// border-radius: 0rpx 0rpx 0rpx 0rpx;
2025-03-28 15:19:42 +08:00
.sex-content-right
2025-05-13 11:10:38 +08:00
// border-left: 2px solid #D9D9D9;
background: #F6F6F6;
2025-03-28 15:19:42 +08:00
flex: 1;
2025-11-19 13:02:40 +08:00
/* 添加硬件加速优化 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
will-change: transform;
2025-03-28 15:19:42 +08:00
.grid-sex
display: grid;
grid-template-columns: 50% 50%;
place-items: center;
2025-05-13 11:10:38 +08:00
padding: 0 20rpx 40rpx 20rpx;
2025-03-28 15:19:42 +08:00
.sex-right-btn
2025-05-13 11:10:38 +08:00
width: 228rpx;
height: 80rpx;
font-size: 28rpx;
2025-03-28 15:19:42 +08:00
line-height: 41rpx;
text-align: center;
display: grid;
place-items: center;
2025-05-13 11:10:38 +08:00
border-radius: 12rpx;
margin-top: 30rpx;
background: #E8EAEE;
2025-03-28 15:19:42 +08:00
color: #606060;
2025-11-19 13:02:40 +08:00
/* 优化渲染性能 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
2025-03-28 15:19:42 +08:00
.sex-right-btned
2025-05-13 11:10:38 +08:00
font-weight: 500
width: 224rpx;
height: 76rpx;
background: rgba(37,107,250,0.06);
border: 2rpx solid #256BFA;
color: #256BFA
2025-11-19 13:02:40 +08:00
/* 优化渲染性能 */
transform: translateZ(0);
-webkit-transform: translateZ(0);
2025-03-28 15:19:42 +08:00
</style>