青锋的功能权限控制使用了两种方式进行实现。

判断和计算属性实现

1、查询用户信息-查询用户享有的功能权限集合。
2、在列表页面(list)中获取权限集合,通过includes包含字段进行判断。
3、数据权限,调用计算属性的authOpera进行处理。
代码参考src/system/user/index.vue,目前通过判断实现权限的控制只保留此处,用于大家的参考与学习。

数据获取

image.png
image.png
在上面代码中通过查询的数据在store中进行存储,方便后续直接使用。

功能权限控制

通过:v-if=”authButton.includes(‘user:info’)” 进行判断 ,目前代码中仅在User/index.vue保留判断控制权限的模式,供大家参考学习。

  1. <a-button
  2. size="small"
  3. @click="() => info(record)"
  4. v-if="authButton.includes('user:info')"
  5. >详情</a-button
  6. >
  7. <a-button
  8. size="small"
  9. type="primary"
  10. v-if="authButton.includes('user:edit')"
  11. @click="() => edit(record)"
  12. >编辑</a-button
  13. >
  14. <a-button
  15. size="small"
  16. type="danger"
  17. v-if="authButton.includes('user:del')"
  18. @click="() => del(record.id)"
  19. >删除</a-button
  20. >
  21. <a-button
  22. size="small"
  23. type="primary"
  24. v-if="authButton.includes('user:resetPwd')"
  25. @click="() => resetPwd(record)"
  26. >密码重置</a-button
  27. >

数据权限控制

数据权限通过authOpera(record)进行判断,主要对数据的创建人和创建组织进行判断。 authButton.includes(‘user:edit’) && authOpera(record)

  1. <a-menu-item
  2. v-if="
  3. authButton.includes('user:edit') &&
  4. authOpera(record)
  5. "
  6. ><a @click="() => edit(record)">编辑</a></a-menu-item
  7. >
  8. <a-menu-item
  9. v-if="
  10. authButton.includes('user:del') && authOpera(record)
  11. "
  12. ><a @click="() => del(record.id)"
  13. >删除</a
  14. ></a-menu-item
  15. >
  1. //处理数据权限-操作权限
  2. authOpera() {
  3. return function (record) {
  4. let bol = false;
  5. let authOrgIds = this.authOrganize.authOrgIds;
  6. let authParams = this.authOrganize.authParams;
  7. let user_id = this.authOrganize.user_id;
  8. var createParams = record.create_organize + ":Y";
  9. if (authOrgIds == "" || authOrgIds == undefined) {
  10. // console.log(user_id == record.create_user);
  11. if (user_id == record.create_user) {
  12. bol = true;
  13. }
  14. } else {
  15. // console.log(
  16. // authParams.indexOf(createParams) > -1 ||
  17. // user_id == record.create_user
  18. // );
  19. if (
  20. authParams.indexOf(createParams) > -1 ||
  21. user_id == record.create_user
  22. ) {
  23. bol = true;
  24. }
  25. }
  26. record.authOpera = bol;
  27. return bol;
  28. };
  29. },

指令权限控制实现

1、查询用户信息-查询用户享有的功能权限集合。
2、创建指令,在指令中对数据进行逻辑判断。
3、指令的引用和使用。
根据实际的效果,编写了两个指令:
1、action -控制功能权限
2、action-dauth -控制功能+数据权限(一般只有del和uedit使用。)
数据查询参考【上面的数据获取】

action指令的编写

  1. import Vue from 'vue'
  2. import store from '@/store'
  3. /**
  4. * Action 权限指令
  5. * 指令用法:
  6. * - 在需要控制 action 级别权限的组件上使用 v-action:[method] , 如下:
  7. * <a-button v-action:add >添加用户</a-button>
  8. * <a-button v-action:delete>删除用户</a-button>
  9. * <a v-action:edit @click="edit(record)">修改</a>
  10. *
  11. * - 当前用户没有权限时,组件上使用了该指令则会被隐藏
  12. * - 当后台权限跟 提供的模式不同时,只需要针对这里的权限过滤进行修改即可
  13. */
  14. const action = Vue.directive('action', {
  15. inserted: function (el, binding, vnode) {
  16. const actionName = binding.arg
  17. const roles = store.getters.roles
  18. const permissionButtonList = roles.permissionButtonList
  19. if(!permissionButtonList.includes(actionName)){
  20. el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
  21. }
  22. },
  23. })
  24. export default action

action-dauth指令的编写

  1. import Vue from 'vue'
  2. import store from '@/store'
  3. /**
  4. * Action 权限指令 - 数据权限
  5. * 指令用法:
  6. * - 在需要控制 action 级别权限的组件上使用 v-action:[method] , 如下:
  7. * <a-button v-action:add >添加用户</a-button>
  8. * <a-button v-action:delete>删除用户</a-button>
  9. * <a v-action:edit @click="edit(record)">修改</a>
  10. *
  11. * - 当前用户没有权限时,组件上使用了该指令则会被隐藏
  12. * - 当后台权限跟 提供的模式不同时,只需要针对这里的权限过滤进行修改即可
  13. */
  14. const actionDauth = Vue.directive('action-dauth', {
  15. inserted: function (el, binding, vnode) {
  16. const roles = store.getters.roles
  17. const permissionButtonList = roles.permissionButtonList
  18. const authOrganize = roles.authOrganize;
  19. const record = binding.arg;
  20. const actionName = record.auth
  21. let authOrgIds = authOrganize.authOrgIds;
  22. let authParams = authOrganize.authParams;
  23. let user_id = authOrganize.user_id;
  24. var createParams = record.create_organize + ":Y";
  25. let bol = false;
  26. if(permissionButtonList.includes(actionName)){
  27. if (authOrgIds == "" || authOrgIds == undefined) {
  28. if (user_id == record.create_user) {
  29. bol = true;
  30. }
  31. } else {
  32. if (
  33. authParams.indexOf(createParams) > -1 ||
  34. user_id == record.create_user
  35. ) {
  36. bol = true;
  37. }
  38. }
  39. }
  40. if(!bol){
  41. el.parentNode && el.parentNode.removeChild(el) || (el.style.display = 'none')
  42. }
  43. },
  44. })
  45. export default actionDauth

指令的引用

需要在main.js中对指令进行引用,如下:

  1. import action from '@/core/directives/action.js'
  2. import actionDauth from '@/core/directives/action-dauth.js'
  3. Vue.use(action)
  4. Vue.use(actionDauth)

指令的使用

功能权限指令的使用: v-action:role:info (案例)
功能权限+数据权限指令的使用:v-action-dauth:[{…record,auth:role:edit}] (案例)

  1. <a-button
  2. size="small"
  3. v-action:role:info
  4. @click="() => info(record)"
  5. >详情</a-button
  6. >
  7. <a-button
  8. size="small"
  9. type="primary"
  10. v-action-dauth:[{...record,auth:`role:edit`}]
  11. @click="() => edit(record)"
  12. >编辑</a-button
  13. >
  14. <a-button
  15. size="small"
  16. type="danger"
  17. v-action-dauth:[{...record,auth:`role:del`}]
  18. @click="() => del(record.id)"
  19. >删除</a-button
  20. >
  21. <a-button
  22. size="small"
  23. type="primary"
  24. v-action:role:assignAuth
  25. @click="() => auth(record)"
  26. >分配权限</a-button
  27. >
  28. <a-button
  29. size="small"
  30. type="danger"
  31. v-action:role:status
  32. v-show="record.status == '0'"
  33. @click="() => updateStatus(record.id, '1')"
  34. >禁用</a-button
  35. >
  36. <a-button
  37. size="small"
  38. type="primary"
  39. v-action:role:status
  40. v-show="record.status == '1'"
  41. @click="() => updateStatus(record.id, '0')"
  42. >启用</a-button
  43. >