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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
| import auth, { AuthParams } from '@/utils/authentication';
| import { useEffect, useMemo, useState } from 'react';
| export type IRoute = AuthParams & {
| name: string;
| key: string;
| // 当前页是否展示面包屑
| breadcrumb?: boolean;
| children?: IRoute[];
| // 当前路由是否渲染菜单项,为 true 的话不会在菜单中显示,但可通过路由地址访问。
| ignore?: boolean;
| buttons?: {
| name: string;
| title: string;
| }[];
| };
|
| export const routes: IRoute[] = [
| {
| name: '首页',
| key: 'dashboard',
| requiredPermissions: [],
| },
| {
| name: '用户管理',
| key: 'member',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '用户信息',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'member/index',
| buttons: [
| {
| name: '用户列表',
| title: 'member/index',
| },
| {
| name: '启用/禁用',
| title: 'member:updateStatus',
| },
| ],
| },
| {
| name: '用户详情',
| requiredPermissions: [],
| oneOfPerm: false,
| ignore: true,
| key: 'member/detail',
| buttons: [
| {
| name: '用户详情',
| title: 'member/detail',
| },
| {
| name: '添加订单',
| title: 'member:addOrder',
| },
| {
| name: '保存',
| title: 'member:update',
| },
| {
| name: '查看优惠券',
| title: 'member:coupon:view',
| },
| ],
| },
| ],
| },
| {
| name: '销售管理',
| key: 'sales',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '销售订单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/order/index',
| buttons: [
| {
| name: '销售订单列表',
| title: 'sales/order/index',
| },
| {
| name: '订单售后',
| title: 'order:refundOrder:create',
| },
| ],
| },
| {
| name: '添加手工单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/order/update',
| ignore: true,
| },
| {
| name: '销售订单详情',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/order/detail',
| ignore: true,
| },
| {
| name: '验光单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/optometryOrder/index',
| buttons: [
| {
| name: '验光单列表',
| title: 'sales/optometryOrder/index',
| },
| {
| name: '添加验光单',
| title: 'optometryOrder:create',
| },
| ],
| },
| {
| name: '验光单详情',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/optometryOrder/detail',
| ignore: true,
| },
| {
| name: '排号管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'lineUp/index',
| buttons: [
| {
| name: '排号管理列表',
| title: 'lineUp/index',
| },
| {
| name: '取消排队',
| title: 'lineUp:cancel',
| },
| ],
| },
| {
| name: '售后管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/refundOrder/index',
| },
| {
| name: '售后订单详情',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'sales/refundOrder/detail',
| ignore: true,
| },
| ],
| },
| {
| name: '门店管理',
| key: 'stores',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '店铺信息',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stores/index',
| buttons: [
| {
| name: '店铺列表',
| title: 'stores/index',
| },
| {
| name: '启用/禁用',
| title: 'store:updateStatus',
| },
| ],
| },
| {
| name: '店铺编辑',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stores/update',
| ignore: true,
| buttons: [
| {
| name: '店铺详情查看/编辑页',
| title: 'stores/update',
| },
| {
| name: '新增/编辑',
| title: 'store:update',
| },
| ],
| },
| {
| name: '验光师信息',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'optometrist/index',
| buttons: [
| {
| name: '验光师列表',
| title: 'optometrist/index',
| },
| {
| name: '启用/禁用',
| title: 'optometrist:updateStatus',
| },
| ],
| },
| {
| name: '验光师编辑',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'optometrist/update',
| ignore: true,
| buttons: [
| {
| name: '验光师详情查看',
| title: 'optometrist/update',
| },
| {
| name: '新增/编辑',
| title: 'optometrist:update',
| },
| ],
| },
| ],
| },
| {
| name: '库存管理',
| key: 'stocks',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '镜架库存',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrame/index',
| buttons: [
| {
| name: '镜架库存列表',
| title: 'stocks/spectaclesFrame/index',
| },
| {
| name: '镜架品牌管理',
| title: 'spectaclesFrame:brand',
| },
| {
| name: '启用/禁用',
| title: 'spectaclesFrame:updateStatus',
| },
| ],
| },
| {
| name: '镜架型号管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameModel/index',
| ignore: true,
| },
| {
| name: '镜架入库',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameOperation/store/index',
| ignore: true,
| },
| {
| name: '镜架出库',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameOperation/outbound/index',
| ignore: true,
| },
| {
| name: '镜架报废',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameOperation/abandon/index',
| ignore: true,
| },
| {
| name: '镜架退货',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameOperation/rollback/index',
| ignore: true,
| },
| {
| name: '镜架出入库记录',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/spectaclesFrameStoreOrder/index',
| ignore: true,
| },
| {
| name: '镜片库存',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lens/index',
| buttons: [
| {
| name: '镜片库存列表',
| title: 'stocks/lens/index',
| },
| {
| name: '镜片品牌管理',
| title: 'lens:brand',
| },
| {
| name: '镜片品牌管理',
| title: 'lens:updateStatus',
| },
| ],
| },
| {
| name: '镜片系列管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensSeries/index',
| ignore: true,
| },
| {
| name: '镜片入库',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensOperation/store/index',
| ignore: true,
| },
| {
| name: '镜片出库',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensOperation/outbound/index',
| ignore: true,
| },
| {
| name: '镜片报废',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensOperation/abandon/index',
| ignore: true,
| },
| {
| name: '镜片退货',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensOperation/rollback/index',
| ignore: true,
| },
| {
| name: '镜片库存生成',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensOperation/generate/index',
| ignore: true,
| },
| {
| name: '镜片出入库记录',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lensStoreOrder/index',
| ignore: true,
| },
| {
| name: '镜片库存详情',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/lens/detail',
| ignore: true,
| },
| {
| name: '供应商管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/supplier/index',
| buttons: [
| {
| name: '供应商列表',
| title: 'stocks/supplier/index',
| },
| {
| name: '添加供应商',
| title: 'supplier:create',
| },
| {
| name: '编辑供应商',
| title: 'supplier:update',
| },
| {
| name: '启用/禁用',
| title: 'supplier:updateStatus',
| },
| ],
| },
| {
| name: '材质管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/material/index',
| buttons: [
| {
| name: '材质管理列表',
| title: 'stocks/material/index',
| },
| {
| name: '添加',
| title: 'material:create',
| },
| {
| name: '编辑',
| title: 'material:update',
| },
| {
| name: '启用/禁用',
| title: 'material:updateStatus',
| },
| ],
| },
| {
| name: '盘点管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/stocktaking',
| },
| {
| name: '添加盘点',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'stocks/stockTakingOperation/index',
| ignore: true,
| },
| ],
| },
| {
| name: '营销管理',
| key: 'markets',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '优惠券',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'markets/coupon/index',
| buttons: [
| {
| name: '优惠券列表',
| title: 'markets/coupon/index',
| },
| {
| name: '暂停发放',
| title: 'markets:coupon:updateStatus',
| },
| ],
| },
| {
| name: '新增优惠券',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'markets/coupon/update',
| ignore: true,
| },
| {
| name: '优惠券查看',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'markets/coupon/detail',
| ignore: true,
| },
| ],
| },
| {
| name: '财务管理',
| key: 'finance',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '订单核算',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'finance/accounting/index',
| },
| {
| name: '订单核算详情',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'finance/accounting/detail',
| ignore: true,
| },
| // {
| // name: '财务统计',
| // requiredPermissions: [],
| // oneOfPerm: false,
| // key: 'finance/charts/index',
| // },
| ],
| },
| {
| name: '系统管理',
| key: 'setting',
| requiredPermissions: [],
| oneOfPerm: true,
| children: [
| {
| name: '员工管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'user/staff/index',
| buttons: [
| {
| name: '员工列表',
| title: 'user/staff/index',
| },
| {
| name: '新增/编辑',
| title: 'staff:update',
| },
| {
| name: '启用/禁用',
| title: 'staff:updateStatus',
| },
| ],
| },
| {
| name: '员工编辑',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'user/staff/update',
| ignore: true,
| },
| {
| name: '角色管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'role/index',
| buttons: [
| {
| name: '角色管理列表',
| title: 'role/index',
| },
| {
| name: '添加',
| title: 'role:create',
| },
| {
| name: '编辑',
| title: 'role:update',
| },
| {
| name: '删除',
| title: 'role:delete',
| },
| ],
| },
| {
| name: '后台设置',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'setting/setting/index',
| },
| {
| name: '小票机管理',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'setting/ticketMachine/index',
| buttons: [
| {
| name: '小票机管理列表',
| title: 'setting/ticketMachine/index',
| },
| {
| name: '新增/编辑',
| title: 'ticketMachine:update',
| },
| {
| name: '启用/禁用',
| title: 'ticketMachine:updateStatus',
| },
| ],
| },
| {
| name: '小票机编辑',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'setting/ticketMachine/update',
| ignore: true,
| },
| ],
| },
| {
| name: '打印镜片出入库单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'print/lens',
| ignore: true,
| },
| {
| name: '打印镜架出入库单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'print/frame',
| ignore: true,
| },
| {
| name: '打印盘点清单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'print/stocktaking',
| ignore: true,
| },
| {
| name: '打印销售订单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'print/order',
| ignore: true,
| },
| {
| name: '打印售后单',
| requiredPermissions: [],
| oneOfPerm: false,
| key: 'print/refundOrder',
| ignore: true,
| },
| ];
|
| export const getName = (path: string, routes) => {
| return routes.find((item) => {
| const itemPath = `/${item.key}`;
| if (path === itemPath) {
| return item.name;
| } else if (item.children) {
| return getName(path, item.children);
| }
| });
| };
|
| const useRoute = (userPermission): [IRoute[], string] => {
| const filterRoute = (routes: IRoute[], arr = []): IRoute[] => {
| if (!routes.length) {
| return [];
| }
| for (const route of routes) {
| let { requiredPermissions, oneOfPerm } = route;
| if ((route.children || []).length > 0) {
| requiredPermissions = route.children
| .filter((i) => !i.ignore)
| .map((item) => item.key);
| oneOfPerm = true;
| } else {
| requiredPermissions = [route.key];
| oneOfPerm = false;
| }
| // console.warn(requiredPermissions, 'requiredPermissions', oneOfPerm);
|
| let visible = true;
| if (requiredPermissions) {
| visible = auth({ requiredPermissions, oneOfPerm }, userPermission);
| }
|
| if (!visible) {
| continue;
| }
| if (route.children && route.children.length) {
| const newRoute = { ...route, children: [] };
| filterRoute(route.children, newRoute.children);
| if (newRoute.children.length) {
| arr.push(newRoute);
| }
| } else {
| arr.push({ ...route });
| }
| }
|
| return arr;
| };
|
| const [permissionRoute, setPermissionRoute] = useState(routes);
|
| useEffect(() => {
| const newRoutes = filterRoute(routes);
| setPermissionRoute(newRoutes);
| }, [JSON.stringify(userPermission)]);
|
| const defaultRoute = useMemo(() => {
| const first = permissionRoute[0];
| if (first) {
| const firstRoute = first?.children?.[0]?.key || first.key;
| return firstRoute;
| }
| return '';
| }, [permissionRoute]);
|
| return [permissionRoute, defaultRoute];
| };
|
| export default useRoute;
|
|