123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- import { useUserStore } from '@/stores';
- import { message } from 'ant-design-vue';
- /**
- *
- * @param _vue
- */
- const use = (_vue, options = { }) => {
- // 注册自定义指令
- _vue.use(Directives);
- const { router, disableDevTools = false, disableContextmenu = false } = options;
- if(router) {
- routerIntercept(router)
- }
- if(disableDevTools) {
- //禁止F12键盘事件
- document.addEventListener('keydown', function(event){
- return 123 != event.keyCode || event.keyCode == 17 || event.keyCode == 16 || (event.returnValue = false)
- });
- checkConsoleOpen();
- }
- if(disableContextmenu) {
- //禁止右键、选择、复制
- document.addEventListener('contextmenu', function(event){
- return event.returnValue = false
- })
- }
- };
- const routerIntercept = (_router) => {
- const {router, ignorePaths = [], homePagePath } = _router;
- router.beforeEach((to, from, next) => {
- if(ignorePaths.findIndex(v => to.path.endsWith(v)) !== -1 || to?.meta?.noAccessControl || useUserStore().isLogin ) {
- next();
- } else {
- message.warn("无权限访问该页面!")
- next({
- fullPath: (homePagePath ? homePagePath : getHomePagePath()) + '?r=nologin&redirect_url=' + encodeURI(location.href),
- })
- }
- })
- }
- const checkConsoleOpen = () => {
- let n = 0;
- let devtoolsOpen = false;
- const nums = new Array(50).fill(new Array(20).fill(0).map((v, i) => i + ''))
- function checkDevTools() {
- const start = performance.now();
- console.table(nums);
- const end = performance.now();
- console.log('end - start', end - start);
- console.clear()
- n = Math.max(end-start, n);
- // document.title = n;
- if (end - start > 5) {
- devtoolsOpen = true;
- }
- if (devtoolsOpen) {
- // console.warn("控制台可能已打开");
- // 可添加其他操作,如弹窗提醒、隐藏敏感信息等
- window.close()
- }
- }
- setInterval(checkDevTools, 1000);
- }
- const getHomePagePath = () => {
- return window.location.origin + '/#/home'
- }
- const Directives = {
- install(Vue) {
- Vue.directive('loginRequired', loginRequired);
- Vue.directive('displayControl', displayControl);
- }
- };
- let replaceStr = '*';
- const displayControl = {
- mounted: function (el, { value }) {
- checkContent(el, value);
- },
- updated: function (el, { value }) {
- checkContent(el, value);
- }
- };
- const checkContent = (el, value = []) => {
- if (!Array.isArray(value)) {
- value = [value, '*'];
- }
- replaceStr = value[1] || '*';
- el.textContent = useUserStore().isLogin ? value[0] : replaceStr;
- watch(useUserStore(), () => {
- el.textContent = useUserStore().isLogin ? value[0] : replaceStr;
- });
- };
- const skipVNodeScopeIds = [];
- const loginRequired = {
- mounted: async function (el, { value = { event: 'onClick', skip: false } }, vNode) {
- value = Object.assign({ event: 'onClick', skip: false }, value);
- if (value.skip) {
- const { scopeId, key } = vNode;
- skipVNodeScopeIds.push(scopeId + key);
- }
- if (value.event === 'onClick') {
- const clickHandler = (event) => {
- const { isLogin } = useUserStore();
- const { scopeId, key } = vNode;
- if (!isLogin && !skipVNodeScopeIds.includes(scopeId + key)) {
- message.warn({
- content: '请登录后再使用该功能!',
- duration: 1
- });
- setTimeout(() => {
- vNode.ctx.appContext.config.globalProperties.$showLoginModal();
- }, 1000);
- event.stopPropagation();
- // 阻止默认行为,例如组件使用到输入框、label、等。这些也拦截掉。
- event.preventDefault();
- }
- };
- el.addEventListener('click', clickHandler, true);
- }
- }
- };
- export default {
- use,
- Directives
- };
|