feat:项目打包成Electron
This commit is contained in:
69
desktop/preload/index.ts
Normal file
69
desktop/preload/index.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
// ============================================================
|
||||
// Preload 脚本 — 安全桥接 Main Process 与 Renderer
|
||||
// 职责:通过 contextBridge 暴露安全的 API 给前端
|
||||
// ============================================================
|
||||
|
||||
import { contextBridge, ipcRenderer } from 'electron'
|
||||
|
||||
// 暴露给渲染进程的 API
|
||||
const electronAPI = {
|
||||
/**
|
||||
* 获取后端 WebSocket 地址
|
||||
*/
|
||||
getBackendUrl: (): string => {
|
||||
return ipcRenderer.sendSync('get-backend-url') as string
|
||||
},
|
||||
|
||||
/**
|
||||
* 设置后端 WebSocket 地址
|
||||
*/
|
||||
setBackendUrl: (url: string): void => {
|
||||
ipcRenderer.send('set-backend-url', url)
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取应用版本号
|
||||
*/
|
||||
getVersion: (): string => {
|
||||
return ipcRenderer.sendSync('get-version') as string
|
||||
},
|
||||
|
||||
/**
|
||||
* 请求摄像头权限(触发 macOS 系统弹窗)
|
||||
* 必须在用户交互事件中调用,否则系统弹窗可能被忽略
|
||||
*/
|
||||
requestCameraAccess: async (): Promise<boolean> => {
|
||||
return await ipcRenderer.invoke('request-camera-access') as boolean
|
||||
},
|
||||
|
||||
/**
|
||||
* 请求麦克风权限(触发 macOS 系统弹窗)
|
||||
*/
|
||||
requestMicAccess: async (): Promise<boolean> => {
|
||||
return await ipcRenderer.invoke('request-mic-access') as boolean
|
||||
},
|
||||
|
||||
/**
|
||||
* 监听权限授予事件(主进程检测到用户在系统设置中授权后触发)
|
||||
* callback 参数 type: 'camera' | 'microphone'
|
||||
*/
|
||||
onPermissionGranted: (callback: (type: string) => void): void => {
|
||||
ipcRenderer.on('permission-granted', (_event, type: string) => callback(type))
|
||||
},
|
||||
|
||||
/**
|
||||
* 平台信息
|
||||
*/
|
||||
platform: process.platform,
|
||||
|
||||
/**
|
||||
* 判断是否在 Electron 环境中运行
|
||||
*/
|
||||
isElectron: true,
|
||||
}
|
||||
|
||||
// 通过 contextBridge 安全地暴露 API
|
||||
contextBridge.exposeInMainWorld('electronAPI', electronAPI)
|
||||
|
||||
// 类型声明,让前端 TypeScript 能识别
|
||||
export type ElectronAPI = typeof electronAPI
|
||||
Reference in New Issue
Block a user