广丰卷烟厂数采质量分析系统
zhuguifei
2026-03-04 64c2870483568cdeb0206661249a19c5b06de8fe
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
/* eslint-disable */
/* prettier-ignore */
// Generated by elegant-router
// Read more: https://github.com/soybeanjs/elegant-router
 
import type { RouteRecordRaw, RouteComponent } from 'vue-router';
import type { ElegantConstRoute } from '@elegant-router/vue';
import type { RouteMap, RouteKey, RoutePath } from '@elegant-router/types';
 
/**
 * transform elegant const routes to vue routes
 * @param routes elegant const routes
 * @param layouts layout components
 * @param views view components
 */
export function transformElegantRoutesToVueRoutes(
  routes: ElegantConstRoute[],
  layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
) {
  return routes.flatMap(route => transformElegantRouteToVueRoute(route, layouts, views));
}
 
/**
 * transform elegant route to vue route
 * @param route elegant const route
 * @param layouts layout components
 * @param views view components
 */
function transformElegantRouteToVueRoute(
  route: ElegantConstRoute,
  layouts: Record<string, RouteComponent | (() => Promise<RouteComponent>)>,
  views: Record<string, RouteComponent | (() => Promise<RouteComponent>)>
) {
  const LAYOUT_PREFIX = 'layout.';
  const VIEW_PREFIX = 'view.';
  const ROUTE_DEGREE_SPLITTER = '_';
  const FIRST_LEVEL_ROUTE_COMPONENT_SPLIT = '$';
 
  function isLayout(component: string) {
    return component.startsWith(LAYOUT_PREFIX);
  }
 
  function getLayoutName(component: string) {
    const layout = component.replace(LAYOUT_PREFIX, '');
 
    if(!layouts[layout]) {
      throw new Error(`Layout component "${layout}" not found`);
    }
 
    return layout;
  }
 
  function isView(component: string) {
    return component.startsWith(VIEW_PREFIX);
  }
 
  function getViewName(component: string) {
    const view = component.replace(VIEW_PREFIX, '');
 
    if(!views[view]) {
      throw new Error(`View component "${view}" not found`);
    }
 
    return view;
  }
 
  function isFirstLevelRoute(item: ElegantConstRoute) {
    return !item.name.includes(ROUTE_DEGREE_SPLITTER);
  }
 
  function isSingleLevelRoute(item: ElegantConstRoute) {
    return isFirstLevelRoute(item) && !item.children?.length;
  }
 
  function getSingleLevelRouteComponent(component: string) {
    const [layout, view] = component.split(FIRST_LEVEL_ROUTE_COMPONENT_SPLIT);
 
    return {
      layout: getLayoutName(layout),
      view: getViewName(view)
    };
  }
 
  const vueRoutes: RouteRecordRaw[] = [];
 
  // add props: true to route
  if (route.path.includes(':') && !route.props) {
    route.props = true;
  }
 
  const { name, path, component, children, ...rest } = route;
 
  const vueRoute = { name, path, ...rest } as RouteRecordRaw;
 
  try {
    if (component) {
      if (isSingleLevelRoute(route)) {
        const { layout, view } = getSingleLevelRouteComponent(component);
 
        const singleLevelRoute: RouteRecordRaw = {
          path,
          component: layouts[layout],
          meta: {
            title: route.meta?.title || ''
          },
          children: [
            {
              name,
              path: '',
              component: views[view],
              ...rest
            } as RouteRecordRaw
          ]
        };
 
        return [singleLevelRoute];
      }
 
      if (isLayout(component)) {
        const layoutName = getLayoutName(component);
 
        vueRoute.component = layouts[layoutName];
      }
 
      if (isView(component)) {
        const viewName = getViewName(component);
 
        vueRoute.component = views[viewName];
      }
 
    }
  } catch (error: any) {
    console.error(`Error transforming route "${route.name}": ${error.toString()}`);
    return [];
  }
 
  // add redirect to child
  if (children?.length && !vueRoute.redirect) {
    vueRoute.redirect = {
      name: children[0].name
    };
  }
 
  if (children?.length) {
    const childRoutes = children.flatMap(child => transformElegantRouteToVueRoute(child, layouts, views));
 
    if(isFirstLevelRoute(route)) {
      vueRoute.children = childRoutes;
    } else {
      vueRoutes.push(...childRoutes);
    }
  }
 
  vueRoutes.unshift(vueRoute);
 
  return vueRoutes;
}
 
/**
 * map of route name and route path
 */
const routeMap: RouteMap = {
  "root": "/",
  "not-found": "/:pathMatch(.*)*",
  "exception": "/exception",
  "exception_403": "/exception/403",
  "exception_404": "/exception/404",
  "exception_500": "/exception/500",
  "403": "/403",
  "404": "/404",
  "500": "/500",
  "about": "/about",
  "analy": "/analy",
  "analy_feed-match": "/analy/feed-match",
  "analy_hoister": "/analy/hoister",
  "analy_output-analy": "/analy/output-analy",
  "analy_packer": "/analy/packer",
  "analy_roller": "/analy/roller",
  "analy_store-silk": "/analy/store-silk",
  "demo": "/demo",
  "demo_demo": "/demo/demo",
  "demo_tree": "/demo/tree",
  "home": "/home",
  "iframe-page": "/iframe-page/:url",
  "login": "/login/:module(pwd-login|code-login|register|reset-pwd|bind-wechat)?",
  "md": "/md",
  "md_shift": "/md/shift",
  "monitor": "/monitor",
  "monitor_cache": "/monitor/cache",
  "monitor_logininfor": "/monitor/logininfor",
  "monitor_online": "/monitor/online",
  "monitor_operlog": "/monitor/operlog",
  "qm": "/qm",
  "qm_batch": "/qm/batch",
  "social-callback": "/social-callback",
  "system": "/system",
  "system_client": "/system/client",
  "system_config": "/system/config",
  "system_dept": "/system/dept",
  "system_dict": "/system/dict",
  "system_menu": "/system/menu",
  "system_notice": "/system/notice",
  "system_oss": "/system/oss",
  "system_oss-config": "/system/oss-config",
  "system_post": "/system/post",
  "system_role": "/system/role",
  "system_tenant": "/system/tenant",
  "system_tenant-package": "/system/tenant-package",
  "system_user": "/system/user",
  "tool": "/tool",
  "tool_gen": "/tool/gen",
  "user-center": "/user-center"
};
 
/**
 * get route path by route name
 * @param name route name
 */
export function getRoutePath<T extends RouteKey>(name: T) {
  return routeMap[name];
}
 
/**
 * get route name by route path
 * @param path route path
 */
export function getRouteName(path: RoutePath) {
  const routeEntries = Object.entries(routeMap) as [RouteKey, RoutePath][];
 
  const routeName: RouteKey | null = routeEntries.find(([, routePath]) => routePath === path)?.[0] || null;
 
  return routeName;
}