Blame view

ant-design-vue-jeecg/src/utils/hasPermission.js 3.91 KB
肖超群 authored
1
import {USER_AUTH, SYS_BUTTON_AUTH} from "@/store/mutation-types"
肖超群 authored
2
3

const hasPermission = {
肖超群 authored
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  install(Vue, options) {
    //console.log(options);
    Vue.directive('has', {
      inserted: (el, binding, vnode) => {
        //console.log("页面权限控制----");
        //console.time()
        //节点权限处理,如果命中则不进行全局权限处理
        if (!filterNodePermission(el, binding, vnode)) {
          filterGlobalPermission(el, binding, vnode);
        }
        //console.timeEnd() //计时结束并输出时长
      }
    });
  }
肖超群 authored
18
19
20
21
22
23
24
25
26
27
28
29
};

/**
 * 流程节点权限控制
 */
export function filterNodePermission(el, binding, vnode) {
  let permissionList = [];
  try {
    let obj = vnode.context.$props.formData;
    if (obj) {
      let bpmList = obj.permissionList;
      for (let bpm of bpmList) {
肖超群 authored
30
        if (bpm.type != '2') {
肖超群 authored
31
32
33
          permissionList.push(bpm);
        }
      }
肖超群 authored
34
    } else {
肖超群 authored
35
36
37
38
39
      return false;
    }
  } catch (e) {
    //console.log("页面权限异常----", e);
  }
肖超群 authored
40
  if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <= 0) {
肖超群 authored
41
42
43
44
45
46
47
    //el.parentNode.removeChild(el)
    return false;
  }

  console.log("流程节点页面权限--NODE--");
  let permissions = [];
  for (let item of permissionList) {
肖超群 authored
48
    if (item.type != '2') {
肖超群 authored
49
50
51
52
53
54
55
56
      permissions.push(item.action);
    }
  }
  //console.log("页面权限----"+permissions);
  //console.log("页面权限----"+binding.value);
  if (!permissions.includes(binding.value)) {
    //el.parentNode.removeChild(el)
    return false;
肖超群 authored
57
  } else {
肖超群 authored
58
    for (let item2 of permissionList) {
肖超群 authored
59
      if (binding.value === item2.action) {
肖超群 authored
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
        return true;
      }
    }
  }
  return false;
}

/**
 * 全局权限控制
 */
export function filterGlobalPermission(el, binding, vnode) {
  //console.log("全局页面权限--Global--");

  let permissionList = [];
  let allPermissionList = [];

  //let authList = Vue.ls.get(USER_AUTH);
  let authList = JSON.parse(sessionStorage.getItem(USER_AUTH) || "[]");
  for (let auth of authList) {
肖超群 authored
79
    if (auth.type != '2') {
肖超群 authored
80
81
82
83
84
85
      permissionList.push(auth);
    }
  }
  //console.log("页面权限--Global--",sessionStorage.getItem(SYS_BUTTON_AUTH));
  let allAuthList = JSON.parse(sessionStorage.getItem(SYS_BUTTON_AUTH) || "[]");
  for (let gauth of allAuthList) {
肖超群 authored
86
    if (gauth.type != '2') {
肖超群 authored
87
88
89
90
91
      allPermissionList.push(gauth);
    }
  }
  //设置全局配置是否有命中
  let invalidFlag = false;//无效命中
肖超群 authored
92
  if (allPermissionList != null && allPermissionList != "" && allPermissionList != undefined && allPermissionList.length > 0) {
肖超群 authored
93
    for (let itemG of allPermissionList) {
肖超群 authored
94
95
      if (binding.value === itemG.action) {
        if (itemG.status == '0') {
肖超群 authored
96
97
98
99
100
101
          invalidFlag = true;
          break;
        }
      }
    }
  }
肖超群 authored
102
  if (invalidFlag) {
肖超群 authored
103
104
    return;
  }
肖超群 authored
105
  if (permissionList === null || permissionList === "" || permissionList === undefined || permissionList.length <= 0) {
肖超群 authored
106
107
108
109
110
111
    el.parentNode.removeChild(el);
    return;
  }
  let permissions = [];
  for (let item of permissionList) {
    //权限策略1显示2禁用
肖超群 authored
112
    if (item.type != '2') {
肖超群 authored
113
      //update--begin--autor:wangshuai-----date:20200729------for:按钮权限,授权标识的提示信息是多个用逗号分隔逻辑处理 gitee#I1OUGU-------
肖超群 authored
114
115
      if (item.action) {
        if (item.action.includes(",")) {
肖超群 authored
116
          let split = item.action.split(",")
肖超群 authored
117
118
119
120
121
          for (let i = 0; i < split.length; i++) {
            if (!split[i] || split[i].length == 0) {
              continue;
            }
            permissions.push(split[i]);
肖超群 authored
122
          }
肖超群 authored
123
        } else {
肖超群 authored
124
125
126
127
128
129
130
131
132
133
134
135
          permissions.push(item.action);
        }
      }
      //update--end--autor:wangshuai-----date:20200729------for:按钮权限,授权标识的提示信息是多个用逗号分隔逻辑处理 gitee#I1OUGU------
    }
  }
  if (!permissions.includes(binding.value)) {
    el.parentNode.removeChild(el);
  }
}

export default hasPermission;