orderList.vue 11 KB
<template>
  <div class="order-list-wrap">
    <div class="header" style="background: #e7eaec">
      <el-tabs v-model="activeCategory" @tab-click="onCategoryTabClick">
        <el-tab-pane label="轻改类" name="light" />
        <el-tab-pane label="权益类" name="rights" />
        <el-tab-pane label="续保积分" name="renewal" />
      </el-tabs>
      <div v-loading="scenesLoading" class="sub-nav">
        <el-radio-group
          v-model="activeLeaf"
          size="small"
          @change="onSubLeafChange"
        >
          <el-radio-button
            v-for="s in currentSubList"
            :key="s.id"
            :label="s.id"
          >{{ s.label }}</el-radio-button>
        </el-radio-group>
      </div>
      <component
        :is="tabName"
        :key="leafComponentKey"
        v-bind="childExtraProps"
      />
    </div>
  </div>
</template>
<script>
import Order from "./order.vue"
import CrowdFunding from "./crowdfunding.vue"
import carDamage from "./carDamage.vue"
import configUpgradation from "./configUpgradation.vue"
import integralShopping from "./integralShopping.vue"
import { sceneListWithOrder } from "@/api/order"
import {
  buildSceneLeafItem,
  normalizeSceneResponse
} from "./orderListSceneMap.js"
import { shouldReapplyRouteParams } from "./utils/orderListRouteGuard.js"
import { computeRouteParamPatch } from "./utils/orderListRouteParams.js"

/** 接口失败或无数据时的二级兜底(与原先静态一致) */
const FALLBACK_LIGHT = [
  {
    id: "l-1-1",
    label: "1.1 轻改精品升级",
    tabName: "Order",
    baseSceneCode: "",
    sceneBusinessType: undefined
  },
  {
    id: "l-1-2",
    label: "1.2 轻改次新车升配",
    tabName: "configUpgradation",
    baseSceneCode: "",
    sceneBusinessType: undefined
  },
  {
    id: "l-1-3",
    label: "1.3 轻改售后众筹",
    tabName: "CrowdFunding",
    baseSceneCode: "",
    sceneBusinessType: undefined
  }
]

const FALLBACK_RIGHTS = [
  {
    id: "l-2-1",
    label: "2.1 会员权益升级",
    tabName: "carDamage",
    baseSceneCode: "",
    sceneBusinessType: 1
  },
  {
    id: "l-2-3",
    label: "2.3 延保",
    tabName: "carDamage",
    baseSceneCode: "",
    sceneBusinessType: 2
  }
]

const RENEWAL_SUB = [
  {
    id: "l-3-1",
    label: "积分商城",
    tabName: "integralShopping",
    baseSceneCode: "",
    sceneBusinessType: undefined
  }
]

/** 旧路由 leaf -> 匹配子项用的规则 */
const LEGACY_LEAF_MATCH = {
  "l-1-1": { tabName: "Order" },
  "l-1-2": { tabName: "configUpgradation" },
  "l-1-3": { tabName: "CrowdFunding" },
  "l-2-1": { tabName: "carDamage", sceneBusinessType: 1 },
  "l-2-3": { tabName: "carDamage", sceneBusinessType: 2 },
  "l-3-1": { tabName: "integralShopping" }
}

export default {
  // 需与路由 name 一致,d2admin keep-alive include 按路由名匹配
  name: "orderList",
  components: {
    Order,
    CrowdFunding,
    carDamage,
    configUpgradation,
    integralShopping
  },
  data() {
    return {
      activeCategory: "light",
      activeLeaf: "",
      rightsFromLegacyTab4: false,
      isApplyingRoute: false,
      scenesLoading: false,
      /** 权益类场景由子页 d2-search 下拉里选择;此处只拉取列表数据传入 carDamage */
      rightsScenesLoading: false,
      rightsScenesForEquity: [],
      rightsScenesForWarranty: [],
      sceneSubs: {
        light: []
      },
      /** 路由带入:匹配完成后清空 */
      pendingLegacyLeaf: null
    }
  },
  computed: {
    /** 权益类二级固定为 2.1 / 2.3,不用接口行替换 */
    currentSubList() {
      if (this.activeCategory === "renewal") {
        return RENEWAL_SUB
      }
      if (this.activeCategory === "rights") {
        return FALLBACK_RIGHTS
      }
      const list = this.sceneSubs[this.activeCategory]
      return Array.isArray(list) ? list : []
    },
    /** 传给 carDamage 搜索区「场景」下拉的 options(2.1=权益 2.3=延保) */
    rightsSceneListForSearch() {
      let raw = []
      if (this.activeLeaf === "l-2-1") {
        raw = this.rightsScenesForEquity
      } else if (this.activeLeaf === "l-2-3") {
        raw = this.rightsScenesForWarranty
      }
      return (raw || []).filter(
        (o) => o && String(o.baseSceneCode || "").trim() !== ""
      )
    },
    tabName() {
      const item = this.currentSubList.find(s => s.id === this.activeLeaf)
      return item?.tabName || "Order"
    },
    /** 切换场景时强制子组件重建(除仅改 query 外) */
    leafComponentKey() {
      const item = this.currentSubList.find(s => s.id === this.activeLeaf)
      // 权益类:场景在子页 search 中切换,不纳入 key,避免改下拉就整表重建
      const scenePart =
        this.activeCategory === "rights" ? "" : (item?.baseSceneCode || "")
      const parts = [
        this.activeLeaf,
        scenePart,
        item?.sceneBusinessType != null ? String(item.sceneBusinessType) : ""
      ]
      return parts.join("|")
    },
    carDamageRightsNavPreset() {
      if (this.tabName !== "carDamage") return null
      const item = this.currentSubList.find(s => s.id === this.activeLeaf)
      if (item?.sceneBusinessType !== undefined && item.sceneBusinessType !== "") {
        const bt = Number(item.sceneBusinessType)
        if (bt === 2) return "extended"
        if (bt === 1) {
          return this.rightsFromLegacyTab4 ? "all" : "equity"
        }
      }
      if (this.activeLeaf === "l-2-3") return "extended"
      if (this.activeLeaf === "l-2-1") {
        return this.rightsFromLegacyTab4 ? "all" : "equity"
      }
      return "all"
    },
    childExtraProps() {
      const item = this.currentSubList.find(s => s.id === this.activeLeaf)
      const base = {}
      if (this.activeCategory === "rights" && this.tabName === "carDamage") {
        base.rightsSceneListForSearch = this.rightsSceneListForSearch
        base.rightsScenesSearchLoading = this.rightsScenesLoading
        if (item?.sceneBusinessType !== undefined && item.sceneBusinessType !== "") {
          base.sceneBusinessType = Number(item.sceneBusinessType)
        }
      } else if (item?.baseSceneCode) {
        base.baseSceneCode = item.baseSceneCode
        if (item?.sceneBusinessType !== undefined && item.sceneBusinessType !== "") {
          base.sceneBusinessType = Number(item.sceneBusinessType)
        }
      }
      if (this.tabName === "carDamage" && this.carDamageRightsNavPreset) {
        base.rightsNavPreset = this.carDamageRightsNavPreset
      }
      const { customerName, customerPhone } = this.$route.query
      if (customerName || customerPhone) {
        base.customerFilter = {
          customerName: customerName || "",
          customerPhone: customerPhone || "",
        }
      }
      return base
    }
  },
  watch: {
    $route: {
      async handler(to, from) {
        if (!shouldReapplyRouteParams(to, from)) {
          return
        }
        this.applyRouteParams(to.params)
        await this.loadScenesForCategory(this.activeCategory)
        this.pickLeafAfterLoad()
      }
    }
  },
  async mounted() {
    this.applyRouteParams(this.$route.params)
    await this.loadScenesForCategory(this.activeCategory)
    this.pickLeafAfterLoad()
  },
  methods: {
    matchLegacyLeafId(leafKey) {
      const rule = LEGACY_LEAF_MATCH[leafKey]
      if (!rule) return null
      const subs = this.currentSubList
      const found = subs.find((s) => {
        if (s.tabName !== rule.tabName) return false
        if (rule.sceneBusinessType != null) {
          return Number(s.sceneBusinessType) === Number(rule.sceneBusinessType)
        }
        return true
      })
      return found?.id || null
    },
    pickLeafAfterLoad() {
      const subs = this.currentSubList
      if (!subs.length) return
      if (this.pendingLegacyLeaf) {
        const id =
          this.matchLegacyLeafId(this.pendingLegacyLeaf) || subs[0].id
        this.activeLeaf = id
        this.pendingLegacyLeaf = null
        return
      }
      if (!subs.some(s => s.id === this.activeLeaf)) {
        this.activeLeaf = subs[0].id
      }
      if (!this.activeLeaf) {
        this.activeLeaf = subs[0].id
      }
    },
    async loadScenesForCategory(category) {
      if (category === "renewal") {
        return
      }
      this.scenesLoading = category === "light"
      this.rightsScenesLoading = category === "rights"
      try {
        if (category === "light") {
          const res = await sceneListWithOrder({ bizTypeCode: "1" })
          const arr = normalizeSceneResponse(res)
          const mapped = arr.map(s => buildSceneLeafItem(s, "light"))
          this.sceneSubs.light = mapped.length ? mapped : [...FALLBACK_LIGHT]
        } else if (category === "rights") {
          const [res1, res2] = await Promise.all([
            sceneListWithOrder({ bizTypeCode: "2", businessType: 1 }),
            sceneListWithOrder({ bizTypeCode: "2", businessType: 2 })
          ])
          this.rightsScenesForEquity = normalizeSceneResponse(res1)
          this.rightsScenesForWarranty = normalizeSceneResponse(res2)
        }
      } catch (e) {
        if (category === "light") {
          this.sceneSubs.light = [...FALLBACK_LIGHT]
        } else {
          this.rightsScenesForEquity = []
          this.rightsScenesForWarranty = []
        }
      } finally {
        this.scenesLoading = false
        this.rightsScenesLoading = false
      }
    },
    ensureLeafInCategory() {
      const subs = this.currentSubList
      if (subs.length && !subs.some(s => s.id === this.activeLeaf)) {
        this.activeLeaf = subs[0].id
      }
    },
    async onCategoryTabClick() {
      await this.loadScenesForCategory(this.activeCategory)
      this.$nextTick(() => {
        this.ensureLeafInCategory()
        if (!this.activeLeaf && this.currentSubList.length) {
          this.activeLeaf = this.currentSubList[0].id
        }
      })
    },
    onSubLeafChange() {
      if (this.isApplyingRoute) return
      this.rightsFromLegacyTab4 = false
    },
    applyRouteParams(params) {
      const patch = computeRouteParamPatch(params)
      if (!patch) {
        return
      }
      this.activeCategory = patch.activeCategory
      this.pendingLegacyLeaf = patch.pendingLegacyLeaf
      this.rightsFromLegacyTab4 = patch.rightsFromLegacyTab4
      this.isApplyingRoute = patch.isApplyingRoute
      this.$nextTick(() => {
        this.isApplyingRoute = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.order-list-wrap {
  padding: 0px 24px 0 20px;

  .header {
    ::v-deep .el-tabs__header {
      margin: 0;
      padding: 0 20px;
    }

    /* 只保留一级 tab 的头部,不展示空内容区;列表下移由各子页 m-t-94 与原先 m-t-40 同逻辑 */
    ::v-deep .el-tabs__content {
      display: none;
    }
  }

  .sub-nav {
    margin: 0;
    padding: 12px 20px 16px;
    border-top: none;
    background: #fff;

    ::v-deep .el-radio-button {
      margin-right: 8px;
    }

    ::v-deep .el-radio-button__inner {
      border-radius: 4px;
    }

    ::v-deep .el-radio-button:not(:first-child):not(.is-active) .el-radio-button__inner {
      border-left: 1px solid #dcdfe6;
    }
  }
}
</style>