1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
| <template> <div class="security-ide"> <header class="header"> <div class="controls"> <button class="btn primary" @click="handleOpenProject" :disabled="isLocked" > 🛡️ 打开并锁定工程 </button> <button class="btn danger" @click="handleUnlockProject" :disabled="!isLocked" > 🔓 解锁并关闭 </button> </div> <div class="status"> 状态: <span :class="['badge', isLocked ? 'locked' : 'unlocked']"> {{ isLocked ? 'ACL 权限独占中' : '普通模式' }} </span> <span class="path" v-if="projectPath">{{ projectPath }}</span> </div> </header>
<div> <label class="switch-container"> <input type="checkbox" v-model="isLocked_back" @change="handleToggleLock" /> <span class="slider"></span> <span class="label-text">{{ isLocked_back ? '🛡️ 独占模式' : '🔓 普通模式' }}</span> </label> </div>
<main class="workspace"> <aside class="sidebar" @contextmenu.prevent="onSidebarRightClick"> <div class="sidebar-title">资源管理器</div> <div v-if="!files.length" class="empty-list"> {{ isLocked ? '目录为空' : '未打开工程' }} </div>
<ul class="file-list"> <li v-for="file in files" :key="file.path" class="file-item" :class="{ active: currentFile?.path === file.path }" @click="selectFile(file)" @contextmenu.stop="onFileRightClick($event, file)" > <span class="icon">{{ file.type=== 'directory' ? '📁' : '📄' }}</span> <span class="name">{{ file.name }}</span> </li> </ul> </aside>
<section class="editor"> <div v-if="!currentFile" class="welcome"> <h3>验证说明</h3> <ol> <li>点击左上角打开一个测试文件夹。</li> <li><strong>验证锁定</strong>:打开系统资源管理器,尝试删除该文件夹下的文件,系统应提示“需要权限”。</li> <li><strong>验证特权</strong>:在下方编辑器修改内容,按 Ctrl+S 保存,应成功。</li> <li><strong>验证管理</strong>:在左侧列表右键,尝试新建或删除,应成功。</li> </ol> </div> <div v-else class="code-wrapper"> <div class="tab-header"> {{ currentFile.name }} <span v-if="isDirty" class="dot">●</span> </div> <textarea class="code-area" v-model="fileContent" @input="isDirty = true" @keydown.ctrl.s.prevent="saveFile" @keydown.meta.s.prevent="saveFile" ></textarea> </div> </section> </main>
<footer class="logger"> <div class="log-line" v-for="(log, idx) in logs" :key="idx"> <span class="time">[{{ log.time }}]</span> {{ log.msg }} </div> </footer>
<div v-if="contextMenu.visible" class="ctx-menu" :style="{ top: contextMenu.y + 'px', left: contextMenu.x + 'px' }" > <div class="menu-item disabled" v-if="contextMenu.target"> 操作: {{ contextMenu.target.name }} </div> <div class="divider"></div> <div class="menu-item" @click="handleCreate('file')">✨ 特权新建文件</div> <div class="menu-item" @click="handleCreate('directory')">📁 特权新建文件夹</div> <div class="menu-item danger" @click="handleDelete" v-if="contextMenu.target"> 🗑️ 特权删除 </div> </div>
<div v-if="inputBox.visible" class="modal-overlay"> <div class="modal-content"> <div class="modal-title">{{ inputBox.title }}</div> <input id="custom-input" v-model="inputBox.value" @keydown.enter="confirmInput" @keydown.esc="cancelInput" class="modal-input" type="text" autocomplete="off" /> <div class="modal-actions"> <button class="btn" @click="cancelInput">取消</button> <button class="btn primary" @click="confirmInput">确定</button> </div> </div> </div> </div> </template>
<script setup lang="ts"> import { ref, reactive, onBeforeUnmount, onMounted } from 'vue'
// --- 类型定义 --- interface FileNode { name: string path: string type: string children?: FileNode[] }
interface Log { time: string msg: string }
// --- 状态数据 --- const isLocked = ref(false) const isLocked_back = ref(false) const projectPath = ref('') const files = ref<FileNode[]>([]) const currentFile = ref<FileNode | null>(null) const fileContent = ref('') const isDirty = ref(false) const logs = ref<Log[]>([])
// 右键菜单状态 const contextMenu = reactive({ visible: false, x: 0, y: 0, target: null as FileNode | null // null 代表在空白处点击 })
// --- 新增:Input 弹窗的状态 --- const inputBox = reactive({ visible: false, title: '', value: '', resolve: null as ((val: string | null) => void) | null })
// --- 新增:封装一个 Promise 化的输入框函数 --- const showInputBox = (title: string, defaultValue = '') => { inputBox.title = title inputBox.value = defaultValue inputBox.visible = true // 自动聚焦输入框 (使用 nextTick) setTimeout(() => { document.getElementById('custom-input')?.focus() }, 100)
return new Promise<string | null>((resolve) => { inputBox.resolve = resolve }) }
// --- 新增:弹窗确认与取消 --- const confirmInput = () => { if (inputBox.resolve) inputBox.resolve(inputBox.value) inputBox.visible = false }
const cancelInput = () => { if (inputBox.resolve) inputBox.resolve(null) inputBox.visible = false }
// --- 日志工具 --- const addLog = (msg: string) => { const time = new Date().toLocaleTimeString() logs.value.unshift({ time, msg }) }
// --- 1. 工程操作 ---
const handleOpenProject = async () => { try { addLog('正在请求系统权限锁定...') // const path = await window.securityAPI.openProject() const path = await window.projectAPI.open() if (path) { projectPath.value = path isLocked.value = true addLog(`✅ 工程已锁定: ${path}`) addLog('🔒 此时外部应用无法写入或删除此目录内容') await refreshFiles() } else { addLog('用户取消了操作') } } catch (e: any) { addLog(`❌ 锁定失败: ${e.message}`) } }
// 2. ✨ 核心:切换模式 const handleToggleLock = async () => { try { console.log('正在切换模式...', isLocked_back.value) // 直接调用统一接口,后端负责上锁或解锁 await window.projectAPI.toggleLock(isLocked_back.value) alert(isLocked_back.value ? '已开启独占锁定!外部无法修改文件。' : '已切换回普通模式。') } catch (e) { console.error(e) // 如果切换失败(比如权限不够),回滚开关状态 isLocked_back.value = !isLocked_back.value alert('切换失败,请检查日志') } }
const handleUnlockProject = async () => { try { // await window.securityAPI.closeProject() await window.projectAPI.close() isLocked.value = false projectPath.value = '' files.value = [] currentFile.value = null addLog('🔓 工程已解锁,恢复系统默认权限') } catch (e: any) { addLog(`❌ 解锁失败: ${e.message}`) } }
const refreshFiles = async () => { if (!projectPath.value) return try { // 假设后端 ipc 实现了 sec-read-dir // 如果没有实现,请在后端补充 fs.readdir 逻辑 const {tree} = await window.projectAPI.readDir(projectPath.value) files.value = tree console.log('Files:', files.value) } catch (e) { addLog('读取文件列表失败 (请确认后端实现了 readDir)') } }
// --- 2. 文件操作 ---
const selectFile = async (file: FileNode) => { if (file.type == 'directory') return try { // 这里为了演示简单,假设你有一个 readFile 的 API // 或者我们直接显示一段 Mock 内容,重点验证 Save 功能 const content = await window.projectAPI.readFile(file.path) currentFile.value = file // fileContent.value = content fileContent.value = `// 这是文件 ${file.name}\n// 它是只读的 (对于外部程序)\n// 试着修改并按 Ctrl+S 保存。\n// ${content}` isDirty.value = false addLog(`已打开: ${file.name}`) } catch (e) { addLog('读取文件内容失败') } }
const saveFile = async () => { if (!currentFile.value) return addLog(`正在通过特权通道保存 ${currentFile.value.name}...`) try { // ✨ 核心验证点:特权写入 // await window.securityAPI.writeFile(currentFile.value.path, fileContent.value) await window.projectAPI.writeFile(currentFile.value.path, fileContent.value) isDirty.value = false addLog(`✅ 保存成功!(ACL 权限临时提权完成)`) } catch (e: any) { addLog(`❌ 保存失败: ${e.message}`) } }
// --- 3. 右键菜单与新建/删除 ---
const onSidebarRightClick = (e: MouseEvent) => { showMenu(e.clientX, e.clientY, null) }
const onFileRightClick = (e: MouseEvent, file: FileNode) => { showMenu(e.clientX, e.clientY, file) }
const showMenu = (x: number, y: number, target: FileNode | null) => { contextMenu.visible = true contextMenu.x = x contextMenu.y = y contextMenu.target = target }
const closeMenu = () => { contextMenu.visible = false }
const handleCreate = async (type: 'file' | 'directory') => { closeMenu() if (!projectPath.value) return
// const name = prompt(`请输入${type === 'file' ? '文件' : '文件夹'}名称:`) // ✅ 替换为自定义弹窗: const name = await showInputBox(`请输入${type === 'file' ? '文件' : '文件夹'}名称:`) if (!name) return
const targetPath = `${projectPath.value}/${name}` // 简单拼接,实际应用请用 path.join try { addLog(`正在创建: ${name}...`) // ✨ 核心验证点:特权创建 await window.projectAPI.create(targetPath, type) addLog(`✅ 创建成功`) await refreshFiles() } catch (e: any) { addLog(`❌ 创建失败: ${e.message}`) } }
const handleDelete = async () => { closeMenu() const target = contextMenu.target if (!target) return
if (!confirm(`确定要强制删除 ${target.name} 吗?`)) return
try { addLog(`正在删除: ${target.name}...`) // ✨ 核心验证点:特权删除 // await window.securityAPI.delete(target.path) // 💥 同样不需要判断模式 await window.projectAPI.delete(target.path) addLog(`✅ 删除成功`) if (currentFile.value?.path === target.path) { currentFile.value = null fileContent.value = '' } await refreshFiles() } catch (e: any) { addLog(`❌ 删除失败: ${e.message}`) } }
const handleRename = async (file: any) => { const newName = await showInputBox('请输入新名称', file.name) if (!newName || newName === file.name) return
try { // ✨ 无论是否锁定,直接调用统一接口 await window.projectAPI.rename(file.path, newName) console.log('重命名成功') // 刷新文件列表 await refreshFiles() } catch (e) { console.error('重命名失败', e) alert('重命名失败:权限不足或文件被占用') } }
// 全局点击关闭菜单 onMounted(() => { window.addEventListener('click', closeMenu) })
onBeforeUnmount(() => { window.removeEventListener('click', closeMenu) // 防止开发时刷新页面导致锁死 if (isLocked.value) { window.projectAPI.close() } }) </script>
<style scoped> .security-ide { display: flex; flex-direction: column; height: 100vh; background: #1e1e1e; color: #ccc; font-family: 'Segoe UI', sans-serif; user-select: none; }
/* Header */ .header { height: 50px; background: #252526; border-bottom: 1px solid #111; display: flex; align-items: center; justify-content: space-between; padding: 0 16px; } .btn { padding: 6px 12px; border: none; border-radius: 4px; cursor: pointer; margin-right: 10px; font-weight: bold; color: white; opacity: 0.9; } .btn:hover:not(:disabled) { opacity: 1; transform: translateY(-1px); } .btn:disabled { opacity: 0.3; cursor: not-allowed; } .primary { background: #0e639c; } .danger { background: #ab1212; }
.status { font-size: 13px; display: flex; align-items: center; gap: 8px;} .badge { padding: 2px 6px; border-radius: 3px; font-size: 12px; color: #111; font-weight: bold;} .badge.locked { background: #4caf50; } .badge.unlocked { background: #888; } .path { color: #888; font-family: monospace; }
/* Workspace */ .workspace { flex: 1; display: flex; overflow: hidden; }
.sidebar { width: 250px; background: #252526; border-right: 1px solid #111; display: flex; flex-direction: column; } .sidebar-title { padding: 10px 16px; font-size: 11px; font-weight: bold; color: #888; text-transform: uppercase; } .empty-list { padding: 20px; text-align: center; color: #555; font-size: 13px; } .file-list { list-style: none; padding: 0; margin: 0; overflow-y: auto; } .file-item { padding: 4px 16px; cursor: pointer; display: flex; align-items: center; font-size: 13px; } .file-item:hover { background: #2a2d2e; } .file-item.active { background: #37373d; color: white; } .icon { margin-right: 6px; }
.editor { flex: 1; display: flex; flex-direction: column; background: #1e1e1e; } .welcome { flex: 1; display: flex; flex-direction: column; justify-content: center; align-items: center; color: #666; padding: 40px; line-height: 1.8; } .welcome ol { text-align: left; } .code-wrapper { display: flex; flex-direction: column; height: 100%; } .tab-header { background: #2d2d2d; height: 35px; display: flex; align-items: center; padding: 0 16px; font-size: 13px; border-top: 1px solid #007fd4; color: white; } .dot { margin-left: 8px; font-size: 20px; line-height: 10px; } .code-area { flex: 1; background: #1e1e1e; color: #d4d4d4; border: none; resize: none; padding: 16px; font-family: 'Consolas', monospace; outline: none; font-size: 14px; }
/* Logger */ .logger { height: 120px; background: #111; border-top: 1px solid #333; padding: 8px; overflow-y: auto; font-family: monospace; font-size: 12px; } .log-line { margin-bottom: 4px; color: #ccc; } .time { color: #569cd6; margin-right: 8px; }
/* Context Menu */ .ctx-menu { position: fixed; background: #252526; border: 1px solid #454545; box-shadow: 0 4px 10px rgba(0,0,0,0.5); z-index: 9999; min-width: 160px; border-radius: 4px; padding: 4px 0; } .menu-item { padding: 6px 12px; cursor: pointer; font-size: 13px; color: #ccc; } .menu-item:hover:not(.disabled) { background: #094771; color: white; } .menu-item.disabled { color: #666; cursor: default; border-bottom: 1px solid #333; padding-bottom: 8px; margin-bottom: 4px;} .menu-item.danger:hover { background: #881111; } .divider { height: 1px; background: #454545; margin: 4px 0; }
/* ✅ 新增:弹窗样式 */ .modal-overlay { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; z-index: 10000; }
.modal-content { background: #252526; border: 1px solid #454545; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5); width: 300px; padding: 16px; border-radius: 4px; }
.modal-title { margin-bottom: 12px; font-size: 14px; color: #ccc; font-weight: bold; }
.modal-input { width: 100%; background: #3c3c3c; border: 1px solid #3c3c3c; color: white; padding: 6px; outline: 1px solid transparent; margin-bottom: 16px; font-family: inherit; }
.modal-input:focus { outline-color: #0e639c; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; } </style>
|