Commit 8dfc7750 by Lin

feat: 策略中心防抖处理/品牌id扁平化处理

1 parent 273e7282
......@@ -182,5 +182,53 @@ util.formatTime = function (time) {
}
}
/**
* 防抖函数
*/
util.debounce = function (func, wait = 300, immediate = false) {
let timeout = null
let result
const debounced = function (...args) {
const context = this
// 清除之前的定时器
if (timeout) {
clearTimeout(timeout)
}
// 如果设置了立即执行
if (immediate) {
// 如果还没有执行过,立即执行
const callNow = !timeout
timeout = setTimeout(() => {
timeout = null
}, wait)
if (callNow) {
result = func.apply(context, args)
}
} else {
// 延迟执行
timeout = setTimeout(() => {
func.apply(context, args)
timeout = null
}, wait)
}
return result
}
// 添加取消方法
debounced.cancel = function () {
if (timeout) {
clearTimeout(timeout)
timeout = null
}
}
return debounced
}
export default util
export const deepClone = util.deepClone
export const debounce = util.debounce
......@@ -6,7 +6,7 @@
<span
v-for="(item, index) in timeRangeOptions"
:key="index"
:class="['radio-item', { active: currentTimeRangeType === item.value }]"
:class="['radio-item', { active: currentTimeRangeType === item.label }]"
@click="handleTimeRangeChange(item.value)"
>
{{ item.label }}
......@@ -248,8 +248,11 @@ export default {
},
// 当前选中的时间范围类型
value: {
type: String,
default: '月'
type: Object,
default: () => ({
label: '周',
value: 1
})
},
// 日期范围对象 { startDate: Date, endDate: Date }
dateRange: {
......@@ -262,7 +265,7 @@ export default {
},
data() {
return {
currentTimeRangeType: this.value,
currentTimeRangeType: this.value?.label || this.value,
internalDateRange: {
startDate: this.dateRange.startDate ? new Date(this.dateRange.startDate) : null,
endDate: this.dateRange.endDate ? new Date(this.dateRange.endDate) : null
......@@ -304,8 +307,9 @@ export default {
// 监听外部传入的 value 变化
value: {
handler(newVal) {
if (newVal !== this.currentTimeRangeType) {
this.currentTimeRangeType = newVal;
const newLabel = newVal?.label || newVal;
if (newLabel !== this.currentTimeRangeType) {
this.currentTimeRangeType = newLabel;
this.initDateRange();
}
}
......@@ -332,13 +336,30 @@ export default {
methods: {
// 处理时间范围类型切换
handleTimeRangeChange(type) {
if (this.currentTimeRangeType === type) {
// 如果传入的是数字类型的 value,需要找到对应的 option
let selectedOption = null;
if (typeof type === 'number') {
selectedOption = this.timeRangeOptions.find(opt => opt.value === type);
} else if (typeof type === 'string') {
selectedOption = this.timeRangeOptions.find(opt => opt.label === type);
} else if (typeof type === 'object' && type !== null) {
selectedOption = type;
}
if (!selectedOption) {
console.warn('未找到匹配的时间范围选项:', type);
return;
}
const timeRangeLabel = selectedOption.label;
if (this.currentTimeRangeType === timeRangeLabel) {
return;
}
this.currentTimeRangeType = type;
this.currentTimeRangeType = timeRangeLabel;
this.initDateRange();
this.$emit('input', type);
this.$emit('time-range-change', type, { ...this.internalDateRange });
// emit 对象格式,与 value prop 类型一致
this.$emit('input', { label: selectedOption.label, value: selectedOption.value });
this.$emit('time-range-change', timeRangeLabel, { ...this.internalDateRange });
},
// 初始化日期范围
......
......@@ -92,6 +92,7 @@ import SelectCustom from './compoments/selectCustom.vue';
import CalendarCustom from './compoments/clendarCustom.vue';
import { getDistrictDetail,getCarBrandList,getMonitorTypeDictList,getMonitorList } from '@/api/earlyWarningManager';
import { merchantStaffByRoleList } from '@api/index'
import util from '@/libs/util'
export default {
name: 'DashboardExact',
components: {
......@@ -115,7 +116,7 @@ export default {
},
// 品牌级联选择器数据
brandOptions: [],
timeRangeType: '周', // 当前选中的时间范围类型
timeRangeType: { label: '周', value: 1 }, // 当前选中的时间范围类型
timeRangeOptions: [
{ label: '周', value: 1 },
{ label: '月', value: 2 },
......@@ -250,6 +251,15 @@ export default {
return this.isAllSelected ? '取消全选' : '全选';
},
},
created() {
// 创建防抖搜索函数
this.debouncedSearch = util.debounce((filters) => {
// 执行实际的查询逻辑
console.log('查询条件:', filters);
// 这里可以调用实际的搜索接口
// this.refreshTable();
}, 300);
},
mounted() {
// 动态添加滚动样式,只影响当前页面的父容器
this.enablePageScroll();
......@@ -267,6 +277,10 @@ export default {
beforeDestroy() {
// 组件销毁时移除样式,避免影响其他页面
this.disablePageScroll();
// 取消防抖函数的延迟执行
if (this.debouncedSearch && typeof this.debouncedSearch.cancel === 'function') {
this.debouncedSearch.cancel();
}
},
methods: {
async getCarBrandList () {
......@@ -291,10 +305,44 @@ export default {
console.log('Tab 切换:', tab);
},
handleSearch(filters) {
// 更新筛选条件
console.log('filters', JSON.stringify(filters.brand));
let brandData = filters.brand;
// 过滤品牌数据:分离一级品牌id和二级品牌id
let firstLevelBrandIds = []; // 一级品牌id数组
let secondLevelBrandIds = []; // 二级品牌id数组
if (Array.isArray(brandData) && brandData.length > 0) {
brandData.forEach(brandItem => {
if (Array.isArray(brandItem) && brandItem.length > 0) {
// 第一个元素是一级品牌id
const firstLevelId = brandItem[0];
if (firstLevelId !== undefined && firstLevelId !== null) {
firstLevelBrandIds.push(firstLevelId);
}
// 第二个元素(如果存在)是二级品牌id
if (brandItem.length > 1) {
const secondLevelId = brandItem[1];
if (secondLevelId !== undefined && secondLevelId !== null) {
secondLevelBrandIds.push(secondLevelId);
}
}
}
});
// 去重
firstLevelBrandIds = [...new Set(firstLevelBrandIds)];
secondLevelBrandIds = [...new Set(secondLevelBrandIds)];
}
console.log('一级品牌id:', firstLevelBrandIds);
console.log('二级品牌id:', secondLevelBrandIds);
// 更新筛选条件(立即更新,保证UI响应)
this.filters = { ...filters };
// 可以在这里处理查询逻辑
console.log('查询条件:', filters);
// 调用防抖搜索函数
this.debouncedSearch(filters);
},
handleReset(filters) {
// 重置筛选条件
......@@ -304,11 +352,22 @@ export default {
},
// 处理时间范围类型变化
handleTimeRangeChange(type, dateRange) {
this.timeRangeType = type;
// type 是字符串(label),需要转换为对象格式
if (typeof type === 'string') {
const option = this.timeRangeOptions.find(opt => opt.label === type);
if (option) {
this.timeRangeType = { label: option.label, value: option.value };
} else {
console.warn('未找到匹配的时间范围类型:', type);
}
} else if (typeof type === 'object' && type !== null) {
// 如果已经是对象,直接使用
this.timeRangeType = type;
}
if (dateRange) {
this.dateRange = { ...dateRange };
}
console.log('切换时间范围类型:', type, this.dateRange);
console.log('切换时间范围类型:', this.timeRangeType, this.dateRange);
},
// 处理日期范围变化
handleDateRangeChange(dateRange) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!