access-control.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. import { useUserStore } from '@/stores';
  2. import { message } from 'ant-design-vue';
  3. /**
  4. *
  5. * @param _vue
  6. */
  7. const use = (_vue, options = { }) => {
  8. // 注册自定义指令
  9. _vue.use(Directives);
  10. const { router, disableDevTools = false, disableContextmenu = false } = options;
  11. if(router) {
  12. routerIntercept(router)
  13. }
  14. if(disableDevTools) {
  15. //禁止F12键盘事件
  16. document.addEventListener('keydown', function(event){
  17. return 123 != event.keyCode || event.keyCode == 17 || event.keyCode == 16 || (event.returnValue = false)
  18. });
  19. checkConsoleOpen();
  20. }
  21. if(disableContextmenu) {
  22. //禁止右键、选择、复制
  23. document.addEventListener('contextmenu', function(event){
  24. return event.returnValue = false
  25. })
  26. }
  27. };
  28. const routerIntercept = (_router) => {
  29. const {router, ignorePaths = [], homePagePath } = _router;
  30. router.beforeEach((to, from, next) => {
  31. if(ignorePaths.findIndex(v => to.path.endsWith(v)) !== -1 || to?.meta?.noAccessControl || useUserStore().isLogin ) {
  32. next();
  33. } else {
  34. message.warn("无权限访问该页面!")
  35. next({
  36. fullPath: (homePagePath ? homePagePath : getHomePagePath()) + '?r=nologin&redirect_url=' + encodeURI(location.href),
  37. })
  38. }
  39. })
  40. }
  41. const checkConsoleOpen = () => {
  42. let n = 0;
  43. let devtoolsOpen = false;
  44. const nums = new Array(50).fill(new Array(20).fill(0).map((v, i) => i + ''))
  45. function checkDevTools() {
  46. const start = performance.now();
  47. console.table(nums);
  48. const end = performance.now();
  49. console.log('end - start', end - start);
  50. console.clear()
  51. n = Math.max(end-start, n);
  52. // document.title = n;
  53. if (end - start > 5) {
  54. devtoolsOpen = true;
  55. }
  56. if (devtoolsOpen) {
  57. // console.warn("控制台可能已打开");
  58. // 可添加其他操作,如弹窗提醒、隐藏敏感信息等
  59. window.close()
  60. }
  61. }
  62. setInterval(checkDevTools, 1000);
  63. }
  64. const getHomePagePath = () => {
  65. return window.location.origin + '/#/home'
  66. }
  67. const Directives = {
  68. install(Vue) {
  69. Vue.directive('loginRequired', loginRequired);
  70. Vue.directive('displayControl', displayControl);
  71. }
  72. };
  73. let replaceStr = '*';
  74. const displayControl = {
  75. mounted: function (el, { value }) {
  76. checkContent(el, value);
  77. },
  78. updated: function (el, { value }) {
  79. checkContent(el, value);
  80. }
  81. };
  82. const checkContent = (el, value = []) => {
  83. if (!Array.isArray(value)) {
  84. value = [value, '*'];
  85. }
  86. replaceStr = value[1] || '*';
  87. el.textContent = useUserStore().isLogin ? value[0] : replaceStr;
  88. watch(useUserStore(), () => {
  89. el.textContent = useUserStore().isLogin ? value[0] : replaceStr;
  90. });
  91. };
  92. const skipVNodeScopeIds = [];
  93. const loginRequired = {
  94. mounted: async function (el, { value = { event: 'onClick', skip: false } }, vNode) {
  95. value = Object.assign({ event: 'onClick', skip: false }, value);
  96. if (value.skip) {
  97. const { scopeId, key } = vNode;
  98. skipVNodeScopeIds.push(scopeId + key);
  99. }
  100. if (value.event === 'onClick') {
  101. const clickHandler = (event) => {
  102. const { isLogin } = useUserStore();
  103. const { scopeId, key } = vNode;
  104. if (!isLogin && !skipVNodeScopeIds.includes(scopeId + key)) {
  105. message.warn({
  106. content: '请登录后再使用该功能!',
  107. duration: 1
  108. });
  109. setTimeout(() => {
  110. vNode.ctx.appContext.config.globalProperties.$showLoginModal();
  111. }, 1000);
  112. event.stopPropagation();
  113. // 阻止默认行为,例如组件使用到输入框、label、等。这些也拦截掉。
  114. event.preventDefault();
  115. }
  116. };
  117. el.addEventListener('click', clickHandler, true);
  118. }
  119. }
  120. };
  121. export default {
  122. use,
  123. Directives
  124. };