setMenuMark.js 3.36 KB
import { getColumnLayoutList } from '@/api/portal.js'
import { validArray, nest2tile } from '@/utils/array.js'
export default {
  data() {
    return {
      newRoutes: [],
      indexLayoutConfig: {},
    }
  },
  computed: {
    indexRoutesIndex() {
      return this.routes.findIndex(
        (item) =>
          item.path === '/' &&
          item.name === '首页' &&
          item.meta.title === '首页'
      )
    },
    indexRoutesChildren() {
      let indexChildren = []
      if (
        this.routes[this.indexRoutesIndex] &&
        this.routes[this.indexRoutesIndex].children
      ) {
        indexChildren = this.routes[this.indexRoutesIndex].children
      }
      return indexChildren
    },
  },
  mounted() {
    this.newRoutes = this.routes
    this.getLayoutListByAlias()
    this.$baseEventBus.$on('get-layout-config', () => {
      this.getLayoutListByAlias()
    })
  },
  methods: {
    getLayoutListByAlias() {
      if (this.routes && this.routes.length) {
        const routeList = nest2tile(this.routes)
        const columnLayoutRoute = routeList.filter((item) => {
          const itemRouteExtend =
            item.routeExtend && JSON.parse(item.routeExtend)
          return (
            (item.meta && item.meta.index) ||
            (itemRouteExtend &&
              itemRouteExtend.component == '@/views/home/index') ||
            item.alias === 'Index'
          )
        })
        const columnAlias = columnLayoutRoute.map((item) => {
          const itemRouteExtend =
            item.routeExtend && JSON.parse(item.routeExtend)
          return (
            item.meta.alias ||
            ((itemRouteExtend &&
              itemRouteExtend.component == '@/views/home/index') ||
            item.alias === 'Index'
              ? 'default_front'
              : '')
          )
        })
        getColumnLayoutList(
          validArray(columnAlias).join(',') || 'default_front'
        ).then((res) => {
          if (res && res.length > 0) {
            this.indexLayoutConfig = res.reduce((pre, cur) => {
              pre[cur.alias] = cur.identifyChange === 2
              return pre
            }, {})
            this.getRoutes()
          }
        })
      }
    },
    getRoutes() {
      let routeList = _.cloneDeep(this.routes)
      routeList.forEach((item) => {
        if (item.children && item.children.length) {
          item.children.forEach((child) => {
            const itemRouteExtend =
              child.routeExtend && JSON.parse(child.routeExtend)
            if (
              (child.meta && child.meta.index) ||
              child.alias === 'Index' ||
              (itemRouteExtend &&
                itemRouteExtend.component == '@/views/home/index')
            ) {
              child.meta.isNew =
                (itemRouteExtend &&
                  itemRouteExtend.component == '@/views/home/index') ||
                child.alias === 'Index'
                  ? this.indexLayoutConfig['default_front']
                  : this.indexLayoutConfig[child.meta && child.meta.alias]
            }
          })
          if (item.type && item.type === 'catalog' && item.meta) {
            item.meta.isNew = this.hasLeastOneNew(item.children)
          }
        }
      })
      this.newRoutes = routeList
    },
    hasLeastOneNew(data) {
      return (
        data && data.length && data.some((item) => item.meta && item.meta.isNew)
      )
    },
  },
}