/**
 * 生成路由
 * @param {Array} routerlist 格式化路由
 * @returns
 */
export function addRouter(routerlist) {
  const router = []
  try {
    routerlist.forEach(e => {
      if (e.path && e.menuName && e.component) {
        let e_new = {
          path: e.path,
          name: e.menuName,
          component: resolve => e.component === 'Layout' ? require([`@/layout`], resolve) : require([`@/views/${e.component}/index`], resolve)
        }
        if (e.children) {
          const children = addRouter(e.children)
          // 保存权限
          e_new = { ...e_new, children: children }
        }
        if (e.redirect) {
          e_new = { ...e_new, redirect: e.redirect }
        }
        // if (e.generatemenu === 0) {
        //   e_new = { ...e_new, hidden: true }
        // }
        if (e.icon !== '' && e.menuLabel !== '') {
          e_new = { ...e_new, meta: { title: e.menuLabel, icon: e.icon } }
        } else if (e.menuLabel !== '' && e.icon === '') {
          e_new = { ...e_new, meta: { title: e.menuLabel } }
        }
        router.push(e_new)
      }

    })
  } catch (error) {
    console.error(error)
    return []
  }
  return router
}