Your Name 2 年前
父节点
当前提交
50b4608f67
共有 3 个文件被更改,包括 48 次插入2 次删除
  1. 28
    0
      src/components/Auth.jsx
  2. 5
    2
      src/layouts/index.jsx
  3. 15
    0
      src/utils/user.js

+ 28
- 0
src/components/Auth.jsx 查看文件

1
+import React from 'react';
2
+import { View } from '@tarojs/components';
3
+import { useModel } from '@/store';
4
+
5
+export default (props) => {
6
+  const { roles = [] } = props;
7
+  const { user } = useModel('user');
8
+
9
+  const targets = user?.dutyList || [];
10
+
11
+  let hasRights = false;
12
+  for(let role of roles) {
13
+    if (hasRights) {
14
+      continue;
15
+    }
16
+
17
+    const found = targets.indexOf(role) > -1;
18
+    if (found) {
19
+      hasRights = found;
20
+    }
21
+  }
22
+
23
+  return hasRights ? props.children : (
24
+    <View style={{width: '100%', height: '100%', display: 'grid', placeItems: 'center'}}>
25
+      <View>暂无权限</View>
26
+    </View>
27
+  );
28
+}

+ 5
- 2
src/layouts/index.jsx 查看文件

2
 import { View } from '@tarojs/components';
2
 import { View } from '@tarojs/components';
3
 import { useModel } from '@/store';
3
 import { useModel } from '@/store';
4
 import { Loading } from '@antmjs/vantui';
4
 import { Loading } from '@antmjs/vantui';
5
+import Auth from '@/components/Auth';
5
 import laySty from './layout.module.less';
6
 import laySty from './layout.module.less';
6
 
7
 
7
 export default (props) => {
8
 export default (props) => {
8
-  const { className, style } = props;
9
+  const { className, style, roles } = props;
9
 
10
 
10
   const { person } = useModel('user');
11
   const { person } = useModel('user');
11
 
12
 
16
       {
17
       {
17
         !person && (
18
         !person && (
18
           <View className={laySty.loading}>
19
           <View className={laySty.loading}>
19
-             <Loading size="32px" vertical>
20
+            <Loading size="32px" vertical>
20
               加载中...
21
               加载中...
21
             </Loading>
22
             </Loading>
22
           </View>
23
           </View>
23
         )
24
         )
24
       }
25
       }
26
+      <Auth roles={roles}>
25
       {props.children}
27
       {props.children}
28
+      </Auth>
26
     </View>
29
     </View>
27
   )
30
   )
28
 }
31
 }

+ 15
- 0
src/utils/user.js 查看文件

1
+
2
+// 督察员
3
+export const ROLE_INSPECTOR = 'inspector';
4
+
5
+// 管理员
6
+export const ROLE_MANAGER = 'manager';
7
+
8
+// 单位人员
9
+export const ROLE_ORG_USER = 'org_user';
10
+
11
+// 查询员
12
+export const ROLE_QUERY_PERSON = 'query_person';
13
+
14
+// 市民
15
+export const ROLE_CITIZEN = 'citizen';