feat: 前端防调试

This commit is contained in:
WANGFAN\wangf 2025-03-11 15:20:10 +08:00
parent 8f97f0410d
commit 69476c78c1
3 changed files with 150 additions and 1 deletions

View File

@ -55,6 +55,13 @@
<a-slider :default-value="gapInfo[0]" :min="0" :max="300" :style="{ width: '100px' }" @change="onWatermarkGap" />
</div>
</div>
<div>
<div class="title">系统设置</div>
<div class="flex-row">
<div>防调试</div>
<a-switch v-model="debugPrevention" />
</div>
</div>
</div>
</a-drawer>
</template>
@ -65,6 +72,7 @@ import { storeToRefs } from "pinia";
import { useRoutesConfigStore } from "@/store/modules/route-config";
import { useThemeConfig } from "@/store/modules/theme-config";
import { currentlyRoute } from "@/router/route-output";
import { DebugControl } from "@/utils/debug-prevention";
const themeStore = useThemeConfig();
const routerStore = useRoutesConfigStore();
const {
@ -77,7 +85,8 @@ const {
watermarkStyle,
watermarkRotate,
watermarkGap,
darkMode
darkMode,
debugPrevention
} = storeToRefs(themeStore);
const { tabsList, cacheRoutes } = storeToRefs(routerStore);
const route = useRoute();
@ -106,6 +115,23 @@ const tabsChange = (e: Boolean) => {
currentlyRoute(route.name as string);
}
};
// debug
const debugControl = new DebugControl();
watch(
() => debugPrevention.value,
newValue => {
if (newValue) {
debugControl.start();
} else {
debugControl.stop();
}
},
{
immediate: true
}
);
const emits = defineEmits(["systemCancel"]);
const handleCancel = () => {
emits("systemCancel");

View File

@ -34,6 +34,8 @@ const themeConfig = () => {
const watermarkRotate = ref<number>(330);
// 水印间隙
const watermarkGap = ref<[number, number]>([100, 100]);
// 防止调试
const debugPrevention = ref<Boolean>(false);
// 布局模式layoutDefaults、layoutHead、layoutMixing
const layoutType = ref<string>("layoutDefaults");
// 色弱模式
@ -92,6 +94,7 @@ const themeConfig = () => {
watermarkStyle,
watermarkRotate,
watermarkGap,
debugPrevention,
layoutType,
colorWeakMode,
grayMode,

View File

@ -0,0 +1,120 @@
/**
*
*/
class KeydownControl {
/**
*
*
* 1ctrl+shift+i
* 2F12
* 3ctrl+shift+c
* 4shift+f10
* @param e
*/
private keydown = (e: KeyboardEvent) => {
const code = e.code; // 具体按键
const ctrl = e.ctrlKey; // Control键是否按下
const shift = e.shiftKey; // Shift键是否按下
// ctrl+shift+i
const isCSI = ctrl && shift && code === "KeyI";
// F12
const isF12 = code === "F12";
// ctrl+shift+c
const isCSC = ctrl && shift && code === "KeyC";
// shift+f10
const isSF10 = shift && code === "F10";
// 禁止打开控制台
if (isF12 || isCSI || isCSC || isSF10) {
e.preventDefault();
}
};
// 监听键盘事件
start() {
document.addEventListener("keydown", this.keydown);
}
// 移除键盘事件监听
stop() {
document.removeEventListener("keydown", this.keydown);
}
}
/**
*
*/
class RightMouseControl {
/**
*
* @param e
*/
private rightClick = (e: MouseEvent) => {
e.preventDefault();
};
// 监听鼠标右键
start() {
// 禁用鼠标右键菜单
document.addEventListener("contextmenu", this.rightClick);
}
// 移除鼠标右键监听
stop() {
document.removeEventListener("contextmenu", this.rightClick);
}
}
/**
* 使debugger关键字阻止打开控制台
*/
class DebugProtector {
private isActive = false;
start() {
if (this.isActive) return;
this.isActive = true;
this.asyncCheck();
}
private asyncCheck() {
if (!this.isActive) return;
debugger;
// 异步调度避免栈溢出
setTimeout(() => {
this.asyncCheck();
}, 200); // 保持0.2秒间隔
}
stop() {
this.isActive = false;
}
}
/**
*
* 1
* 2
* 3使debugger关键字阻止打开控制台
*/
class DebugControl {
private keydown = new KeydownControl();
private rightMouse = new RightMouseControl();
private protector = new DebugProtector();
/**
*
*/
start() {
this.keydown.start();
this.rightMouse.start();
this.protector.start();
}
/**
*
*/
stop() {
this.keydown.stop();
this.rightMouse.stop();
this.protector.stop();
}
}
export { DebugControl };