SelectCar.vue 14.3 KB
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 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
<template>
  <GeneralPopup ref="carPopup" popupClass="carPopup" :list="list" :title="title" @confirm="confirm" :loading="loading">
    <template v-slot:content>
      <StepTab
        :steps="
          steps.map(step => ({
            label: step.selectedText || step.label,
            number: step.number
          }))
        "
        :currentStep="currentStep"
        :clickable="true"
        @step-change="onStepChange"
        :isShowStepNumber="false"
      ></StepTab>
      <view v-if="carTips" class="carTips">{{ carTips }}</view>
      <view class="list-component">
        <!-- 修复activeValue绑定,统一使用currentSelectedId -->
        <SortByNameList :items="currentStepData" :activeValue="currentSelectedId" :activeField="'id'" :itemNameField="'name'" :groupKeyField="'letter'" :itemsPerRow="3" :itemSpacing="32" @item-click="handleItemClick" :enableFastScroll="false" />
      </view>
    </template>
  </GeneralPopup>
</template>
<script>
import GeneralPopup from './GeneralPopup.vue'
import SortByNameList from './SortByNameList.vue'
import StepTab from './StepTab.vue'
export default {
  name: 'SelectCar',
  components: {
    GeneralPopup,
    SortByNameList,
    StepTab
  },
  props: {
    title: {
      type: String,
      default: '选择车型'
    },
    list: {
      type: Array,
      default: () => []
    },
    carBrandData: {
      type: Array,
      default: () => []
    },
    onLoadYearData: {
      type: Function,
      default: () => Promise.resolve([])
    },
    carTips: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      currentStep: 0,
      stepSelectedIds: [null, null, null, null],
      steps: [
        { label: '主品牌', number: 1, selectedText: '' },
        { label: '子品牌', number: 2, selectedText: '' },
        { label: '车系', number: 3, selectedText: '' },
        { label: '车型', number: 4, selectedText: '' }
      ],
      selectedBrand: null,
      selectedSeries: null,
      selectedModel: null,
      selectedYear: null,
      loadingYearData: false,
      selectionResult: null,
      lastSelectedResult: null,
      // 标记当前是否为编辑模式
      isEditMode: false,
      loading: false,
      selectedFlag: false
    }
  },
  computed: {
    currentStepData() {
      let data = []

      if (this.loading && !this.isEditMode) {
        return []
      }

      switch (this.currentStep) {
        case 0:
          data = this.carBrandData || []
          break
        case 1:
          data = this.selectedBrand?.childs || []
          break
        case 2:
          data = this.selectedSeries?.childs || []
          break
        case 3:
          data = this.selectedModel?.childs || []
          break
        default:
          data = []
      }
      if (!Array.isArray(data)) {
        data = []
      }

      return data
    },
    currentSelectedId() {
      console.log('currentStep', this.currentStep)
      console.log('currentSelectedId', this.stepSelectedIds[this.currentStep])
      return this.stepSelectedIds[this.currentStep]
    }
  },
  methods: {
    open(carInfo = null) {
      // 重置状态
      this.selectedBrand = null
      this.selectedSeries = null
      this.selectedModel = null
      this.selectedYear = null
      this.stepSelectedIds = [null, null, null, null]
      this.steps.forEach(step => (step.selectedText = ''))
      this.currentStep = 0
      this.isEditMode = false
      this.loading = true
      this.selectedFlag = false

      setTimeout(() => {
        if (this.$refs.carPopup) {
          this.$refs.carPopup.open()
        }
      }, 50)
      this._processOpenData(carInfo)
    },

    async _processOpenData(carInfo) {
      // 确保数据加载完成
      if (this.carBrandData.length === 0) {
        // 显示加载中状态
        this.loading = true
        await this._waitForDataLoad()
      }

      // 编辑模式 - 传入了完整的车型信息对象
      if (carInfo && typeof carInfo === 'object' && carInfo.brand) {
        this.isEditMode = true
        this._handleEditMode(carInfo)
      }
      // 识别模式 - 传入了品牌名称字符串
      else if (carInfo && typeof carInfo === 'string') {
        this._handleIdentifyMode(carInfo)
      }
      //重复打开/新增模式 - 无参数或使用上次选择结果
      else if (!carInfo && this.lastSelectedResult) {
        this._handleRepeatOpenMode()
      }

      // 数据处理完成后关闭loading
      this.loading = false
    },

    // 等待数据加载完成的辅助方法
    _waitForDataLoad() {
      return new Promise(resolve => {
        if (this.carBrandData.length > 0) {
          resolve()
          return
        }

        // 添加计数器,显示加载尝试次数
        let attemptCount = 0
        const maxAttempts = 30 // 3秒,每100ms检查一次

        const checkInterval = setInterval(() => {
          attemptCount++

          if (this.carBrandData.length > 0) {
            clearInterval(checkInterval)
            resolve()
          } else if (attemptCount >= maxAttempts) {
            clearInterval(checkInterval)
            console.warn('数据加载超时,显示空列表')
            resolve()
          }
        }, 100)
      })
    },

    async _handleEditMode(carInfo) {
      try {
        // 设置品牌信息
        if (carInfo.brand && Array.isArray(this.carBrandData)) {
          const fullBrand = this.carBrandData.find(item => item.id === carInfo.brand.id || item.name === carInfo.brand.name)
          if (fullBrand) {
            this.selectedBrand = fullBrand
            this.stepSelectedIds[0] = fullBrand.id
            this.steps[0].selectedText = fullBrand.name
          } else {
            this.selectedBrand = { ...carInfo.brand, childs: [] }
            this.stepSelectedIds[0] = carInfo.brand.id
            this.steps[0].selectedText = carInfo.brand.name
          }
        }

        // 设置系列信息
        if (carInfo.series && this.selectedBrand) {
          if (!this.selectedBrand.childs) {
            this.selectedBrand.childs = []
          }
          const fullSeries = this.selectedBrand.childs.find(item => item.id === carInfo.series.id || item.name === carInfo.series.name)
          if (fullSeries) {
            this.selectedSeries = fullSeries
            this.stepSelectedIds[1] = fullSeries.id
            this.steps[1].selectedText = fullSeries.name
            this.currentStep = 1
          } else {
            this.selectedSeries = { ...carInfo.series, childs: [] }
            this.stepSelectedIds[1] = carInfo.series.id
            this.steps[1].selectedText = carInfo.series.name
            this.currentStep = 1
          }
        }

        // 设置车型信息
        if (carInfo.model && this.selectedSeries) {
          if (!this.selectedSeries.childs) {
            this.selectedSeries.childs = []
          }
          const fullModel = this.selectedSeries.childs.find(item => item.id === carInfo.model.id || item.name === carInfo.model.name)
          if (fullModel) {
            this.selectedModel = fullModel
            this.stepSelectedIds[2] = fullModel.id
            this.steps[2].selectedText = fullModel.name
            this.currentStep = 2

            // 加载年份数据
            if (!this.selectedModel.childs || this.selectedModel.childs.length === 0) {
              await this.loadYearDataForModel(this.selectedModel)
            }
          } else {
            this.selectedModel = { ...carInfo.model, childs: [] }
            this.stepSelectedIds[2] = carInfo.model.id
            this.steps[2].selectedText = carInfo.model.name
            this.currentStep = 2
          }
        }

        // 设置年份信息
        if (carInfo.year && this.selectedModel) {
          if (!this.selectedModel.childs) {
            this.selectedModel.childs = []
          }
          const fullYear = this.selectedModel.childs.find(item => item.id === carInfo.year.id || item.name === carInfo.year.name)
          if (fullYear) {
            this.selectedYear = fullYear
            this.stepSelectedIds[3] = fullYear.id
            this.steps[3].selectedText = fullYear.name
            this.currentStep = 3
          } else {
            this.selectedYear = { ...carInfo.year }
            this.stepSelectedIds[3] = carInfo.year.id
            this.steps[3].selectedText = carInfo.year.name
            this.currentStep = 3
          }
        }
        // 强制更新UI以确保选中状态正确显示
        this.$nextTick(() => {
          this.$forceUpdate()
        })
      } catch (error) {
        console.error('处理编辑模式数据时出错:', error)
        // 出错时确保loading状态正确
        this.loading = false
      }
    },

    // 处理识别模式
    _handleIdentifyMode(brandName) {
      const matchedBrand = this.carBrandData.find(item => (item.name && brandName && item.name.includes(brandName)) || item.name === brandName)

      if (matchedBrand) {
        this.selectedBrand = matchedBrand
        this.stepSelectedIds[0] = matchedBrand.id
        this.steps[0].selectedText = matchedBrand.name
        this.selectedSeries = null
        this.selectedModel = null
        this.selectedYear = null
        this.stepSelectedIds[1] = this.stepSelectedIds[2] = this.stepSelectedIds[3] = null
        this.steps[1].selectedText = this.steps[2].selectedText = this.steps[3].selectedText = ''
      }
    },

    // 处理重复打开模式
    _handleRepeatOpenMode() {
      this._handleEditMode(this.lastSelectedResult)
    },

    // 通过props调用父组件方法加载年份数据
    async loadYearDataForModel(model) {
      if (model.childs && model.childs.length > 0) {
        return model.childs
      }

      this.loadingYearData = true
      try {
        const yearData = await this.onLoadYearData(model.id)
        model.childs = yearData || []
        return model.childs
      } catch (error) {
        console.error('加载年份数据失败:', error)
        model.childs = []
        return []
      } finally {
        this.loadingYearData = false
      }
    },

    confirm() {
      this.lastSelectedResult = this.selectionResult
      this.$emit('car-selected', this.selectionResult)
      if (this.$refs.carPopup) {
        this.$refs.carPopup.close()
      }
    },

    onStepChange(index) {
      // 限制只能前进到已选择的下一级
      if (index === 0) {
        this.currentStep = 0
      } else if (index === 1 && this.selectedBrand) {
        this.currentStep = 1
        // 确保selectedBrand有childs属性
        if (!this.selectedBrand.childs) {
          this.selectedBrand.childs = []
        }
      } else if (index === 2 && this.selectedSeries) {
        this.currentStep = 2
        // 确保selectedSeries有childs属性
        if (!this.selectedSeries.childs) {
          this.selectedSeries.childs = []
        }
      } else if (index === 3 && this.selectedModel) {
        // 如果还没有年份数据,则加载
        if (!this.selectedModel.childs || this.selectedModel.childs.length === 0) {
          this.loadingYearData = true
          this.loadYearDataForModel(this.selectedModel).then(() => {
            this.loadingYearData = false
            this.currentStep = 3
            // 强制更新UI以确保年份列表正确显示
            this.$nextTick(() => {
              this.$forceUpdate()
            })
          })
        } else {
          this.currentStep = 3
        }
      }
      // 强制更新UI以确保步骤切换正确显示
      this.$nextTick(() => {
        this.$forceUpdate()
      })
    },

    // 处理项目点击事件
    handleItemClick(item) {
      const isSameItem = this.stepSelectedIds[this.currentStep] === item.id
      this.$set(this.stepSelectedIds, this.currentStep, item.id)

      if (!isSameItem) {
        if (this.currentStep === 0) {
          this.selectedBrand = item
          this.selectedSeries = null
          this.selectedModel = null
          this.selectedYear = null
          this.steps[1].selectedText = ''
          this.steps[2].selectedText = ''
          this.steps[3].selectedText = ''
        } else if (this.currentStep === 1) {
          this.selectedSeries = item
          this.selectedModel = null
          this.selectedYear = null
          this.steps[2].selectedText = ''
          this.steps[3].selectedText = ''
        } else if (this.currentStep === 2) {
          this.selectedModel = item
          this.selectedYear = null
          this.steps[3].selectedText = ''
        } else if (this.currentStep === 3) {
          // 修复第四步选择逻辑,加强UI更新
          this.selectedYear = item
          this.selectionResult = {
            brand: this.selectedBrand,
            series: this.selectedSeries,
            model: this.selectedModel,
            year: this.selectedYear
          }
          // 使用一个标记变量切换来强制响应式更新
          this.selectedFlag = !this.selectedFlag
          // 双重保险,确保UI更新
          this.$nextTick(() => {
            this.$forceUpdate()
          })
        }
      }

      this.steps[this.currentStep].selectedText = item.name

      if (this.currentStep < 3) {
        // 如果是第三步,需要加载年份数据并等待完成后再进入第四步
        if (this.currentStep === 2) {
          this.loadingYearData = true
          this.loadYearDataForModel(this.selectedModel).then(() => {
            this.loadingYearData = false
            this.currentStep = 3
            // 强制更新UI以确保年份列表正确显示
            this.$nextTick(() => {
              this.$forceUpdate()
            })
          })
        } else {
          this.currentStep++
        }
      }
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .carPopup {
  .content {
    width: 100%;
    height: toRpx(764);
    background-color: #fff;
    box-sizing: border-box;
    overflow: hidden;
    .list-component {
      width: 100%;
      height: calc(100% - #{toRpx(100)});
    }
  }
}

// 增强选中状态的样式
::v-deep .carPopup .content .list-component {
  .sort-by-name-list {
    .item-active {
      background-color: #e6f7ff !important;
      color: #1890ff !important;
      border-color: #1890ff !important;
    }
  }
}
.carTips {
  border-radius: toRpx(16);
  background: #fef4e8;
  padding: toRpx(16) 0;
  color: #f59619;
  font-size: toRpx(26);
  font-weight: 400;
  text-align: center;
  margin: 0 toRpx(48) toRpx(16);
}
</style>