electron.main copy.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. const { app, Tray, Menu, screen, BrowserWindow, ipcMain } = require('electron');
  2. const path = require('path');
  3. const Store = require('electron-store');
  4. // 初始化存储实例
  5. const store = new Store({
  6. username: {
  7. type: 'string',
  8. default: ''
  9. },
  10. password: {
  11. type: 'string',
  12. default: ''
  13. },
  14. token: {
  15. type: 'string',
  16. default: ''
  17. },
  18. // name: 'auth-data', // 存储文件名称(可选)
  19. // encryptionKey: 'your-encryption-key', // 可选加密(生产环境建议设置)
  20. });
  21. // store.set('username', '');
  22. // store.set('password', '');
  23. // if(store.get())
  24. store.delete('token');
  25. // console.log(store.get('username'))
  26. let tray = null;
  27. let win = null;
  28. let floatingWindow = null;
  29. let tooltipWindow = null;
  30. let smart_win = null;
  31. let isFloatingWindowVisible = true; // 新增:跟踪悬浮球可见状态
  32. // 新增:记录登录状态
  33. let isLoggedIn = false;
  34. // 尝试获取单实例锁
  35. const gotTheLock = app.requestSingleInstanceLock();
  36. let iconPath;
  37. if (app.isPackaged) {
  38. iconPath = path.join(path.dirname(app.getPath('exe')), 'resources/tray-icon.png');
  39. } else {
  40. iconPath = path.join(__dirname, '/tray-icon.png');
  41. }
  42. function createWindow(w = 1200, h = 800) {
  43. win = new BrowserWindow({
  44. width: w,
  45. height: h,
  46. // frame: false,
  47. icon: iconPath, // 使用平台特定图标
  48. backgroundColor: '#fff',
  49. webPreferences: {
  50. preload: path.join(__dirname, 'preload.js'),
  51. nodeIntegration: true, //启用nodejs集成
  52. contextIsolation: true,
  53. contentSecurityPolicy:
  54. "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'"
  55. }
  56. });
  57. // 禁用默认菜单栏(保留窗口控制按钮)
  58. Menu.setApplicationMenu(null);
  59. // 初始设置为登录页模式(固定大小,禁止调整)
  60. setLoginWindowMode();
  61. const primaryDisplay = screen.getPrimaryDisplay();
  62. const { width, height } = primaryDisplay.workAreaSize;
  63. // win.setPosition(width - 660 - 900, 100);
  64. win.center();
  65. if (process.env.NODE_ENV === 'development') {
  66. win.loadURL(' http://localhost:5173/');
  67. } else {
  68. // win.loadFile(path.join(__dirname, 'dist', 'index.html'));
  69. win.loadURL('http://25.214.216.111:9000/pdwyzz/ai-znwd/dist/index.html')
  70. }
  71. // 设置窗口标题
  72. win.setTitle('智库问答助手');
  73. win.on('close', (e) => {
  74. if (!app.isQuitting) {
  75. e.preventDefault();
  76. win.hide();
  77. }
  78. return false;
  79. });
  80. win.on('closed', function () {
  81. win = null;
  82. });
  83. }
  84. // 设置登录窗口模式(固定大小,禁止调整)
  85. function setLoginWindowMode() {
  86. if (win && !win.isDestroyed()) {
  87. win.setResizable(false); // 禁止调整大小
  88. win.setMaximizable(false); // 禁用最大化按钮
  89. win.setMinimumSize(1080, 608); // 设置最小尺寸(可选)
  90. win.setSize(1080, 608); // 设置为固定尺寸
  91. win.center(); // 窗口居中
  92. win.setTitle('智库问答助手');
  93. isLoggedIn = false;
  94. }
  95. }
  96. // 设置主窗口模式(可调整大小,有最小限制)
  97. function setMainWindowMode() {
  98. if (win && !win.isDestroyed()) {
  99. win.setResizable(true); // 允许调整大小
  100. win.setMaximizable(true); // 启用最大化按钮
  101. win.setMinimumSize(1200, 800); // 设置最小尺寸
  102. win.setTitle('智库问答助手');
  103. isLoggedIn = true;
  104. }
  105. }
  106. // 创建快捷对话窗口
  107. function createSmartWindow() {
  108. smart_win = new BrowserWindow({
  109. width: 520,
  110. height: 110,
  111. // maxHeight: 500,
  112. frame: false,
  113. icon: iconPath, // 使用平台特定图标
  114. resizable: false, // 禁止调整窗口大小
  115. maximizable: false, // 禁止最大化
  116. webPreferences: {
  117. preload: path.join(__dirname, 'preload.js'),
  118. nodeIntegration: true,
  119. contextIsolation: true,
  120. contentSecurityPolicy:
  121. "default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; connect-src 'self'"
  122. }
  123. });
  124. // 禁用默认菜单栏(保留窗口控制按钮)
  125. Menu.setApplicationMenu(null);
  126. const primaryDisplay = screen.getPrimaryDisplay();
  127. const { width, height } = primaryDisplay.workAreaSize;
  128. smart_win.setPosition(width - 660, 100);
  129. if (process.env.NODE_ENV === 'development') {
  130. smart_win.loadURL('http://localhost:5173/#/smart');
  131. } else {
  132. // smart_win.loadFile(path.join(__dirname, 'dist', 'index.html'), {
  133. // hash: '/smart'
  134. // });
  135. smart_win.loadURL('http://25.214.216.111:9000/pdwyzz/ai-znwd/dist/index.html#/smart')
  136. }
  137. }
  138. // 新增:设置悬浮窗事件监听
  139. function setupFloatingWindowEvents() {
  140. if (floatingWindow && !floatingWindow.isDestroyed()) {
  141. // 移除旧的事件监听器(如果存在)
  142. floatingWindow.removeAllListeners('closed');
  143. // 添加关闭事件处理
  144. floatingWindow.on('closed', () => {
  145. floatingWindow = null;
  146. });
  147. }
  148. }
  149. // 新增:切换悬浮球可见性
  150. function toggleFloatingWindow() {
  151. if (floatingWindow && !floatingWindow.isDestroyed()) {
  152. if (isFloatingWindowVisible) {
  153. smart_win?.close()
  154. // smart_win?.destroy()
  155. floatingWindow?.hide();
  156. hideTooltip(); // 隐藏可能显示的工具提示
  157. } else {
  158. floatingWindow?.show();
  159. // 重新设置悬浮窗事件监听
  160. setupFloatingWindowEvents();
  161. }
  162. isFloatingWindowVisible = !isFloatingWindowVisible;
  163. updateTrayMenu(); // 更新托盘菜单文本
  164. }
  165. }
  166. // 新增:更新托盘菜单
  167. function updateTrayMenu(show_float_item = true) {
  168. if (!tray) return;
  169. const menu_list = [
  170. {
  171. label: '打开',
  172. click: () => {
  173. if (win && !win.isDestroyed()) {
  174. if (win.isMinimized()) win.restore();
  175. win.show();
  176. win.focus();
  177. } else {
  178. createWindow();
  179. }
  180. }
  181. },
  182. // {
  183. // label: isFloatingWindowVisible ? '隐藏悬浮球' : '显示悬浮球',
  184. // click: toggleFloatingWindow
  185. // },
  186. {
  187. label: '退出',
  188. click: () => {
  189. app.isQuitting = true;
  190. if (floatingWindow) floatingWindow.destroy();
  191. app.quit();
  192. }
  193. }
  194. ]
  195. const float_item = {
  196. label: isFloatingWindowVisible ? '隐藏悬浮球' : '显示悬浮球',
  197. click: toggleFloatingWindow
  198. }
  199. if (show_float_item) {
  200. menu_list.splice(1, 0, float_item)
  201. }
  202. const contextMenu = Menu.buildFromTemplate(menu_list);
  203. tray.setContextMenu(contextMenu);
  204. }
  205. // 创建系统托盘
  206. function createTray() {
  207. let iconPath;
  208. if (app.isPackaged) {
  209. iconPath = path.join(path.dirname(app.getPath('exe')), 'resources/tray-icon.png');
  210. } else {
  211. iconPath = path.join(__dirname, '/tray-icon.png');
  212. }
  213. tray = new Tray(iconPath);
  214. updateTrayMenu(); // 使用函数设置菜单
  215. tray.setToolTip('智库问答助手');
  216. tray.on('click', () => {
  217. if (win && !win.isDestroyed()) {
  218. if (win.isMinimized()) win.restore();
  219. win.show();
  220. win.focus();
  221. } else {
  222. createWindow();
  223. }
  224. });
  225. }
  226. // 创建悬浮窗
  227. function createFloatingWindow() {
  228. floatingWindow = new BrowserWindow({
  229. width: 42,
  230. height: 42,
  231. frame: false,
  232. transparent: false,
  233. alwaysOnTop: true,
  234. skipTaskbar: true,
  235. resizable: false,
  236. backgroundColor: '#00000000', // 强制有颜色背景,否则 tooltip 不触发
  237. hasShadow: false,
  238. transparent: true,
  239. webPreferences: {
  240. preload: path.join(__dirname, 'preload.js'),
  241. nodeIntegration: true,
  242. contextIsolation: true
  243. },
  244. type: 'desktop'
  245. });
  246. floatingWindow.setAlwaysOnTop(true, 'screen-saver'); // 在macOS有效
  247. if (process.env.NODE_ENV === 'development') {
  248. floatingWindow.loadURL('http://localhost:5173/#/floating');
  249. } else {
  250. // floatingWindow.loadFile(path.join(__dirname, 'dist', 'index.html'), {
  251. // hash: '/floating'
  252. // });
  253. floatingWindow.loadURL('http://25.214.216.111:9000/pdwyzz/ai-znwd/dist/index.html#/floating')
  254. }
  255. const primaryDisplay = screen.getPrimaryDisplay();
  256. const { width, height } = primaryDisplay.workAreaSize;
  257. floatingWindow.setPosition(width - 50, height - 200);
  258. // 设置悬浮窗事件监听
  259. setupFloatingWindowEvents();
  260. }
  261. // 提示窗口
  262. function showTooltip(adsorbedPosition, x, y) {
  263. if (tooltipWindow) return;
  264. let tooltipX;
  265. if (adsorbedPosition === 0) {
  266. // 悬浮球吸附在左侧,tooltip 显示在右侧
  267. tooltipX = x + 50;
  268. } else {
  269. // 悬浮球吸附在右侧,tooltip 显示在左侧
  270. tooltipX = x - 125;
  271. }
  272. tooltipWindow = new BrowserWindow({
  273. width: 120,
  274. height: 40,
  275. x: tooltipX,
  276. y: y + 5,
  277. frame: false,
  278. alwaysOnTop: true,
  279. skipTaskbar: true,
  280. resizable: false,
  281. transparent: true,
  282. hasShadow: false,
  283. focusable: false,
  284. webPreferences: {
  285. nodeIntegration: true,
  286. contextIsolation: false
  287. }
  288. });
  289. if (process.env.NODE_ENV === 'development') {
  290. tooltipWindow.loadURL(' http://localhost:5173/#/tips');
  291. } else {
  292. // tooltipWindow.loadFile(path.join(__dirname, 'dist', 'index.html'), {
  293. // hash: '/tips'
  294. // });
  295. tooltipWindow.loadURL('http://25.214.216.111:9000/pdwyzz/ai-znwd/dist/index.html#/tips')
  296. }
  297. tooltipWindow.once('ready-to-show', () => {
  298. tooltipWindow?.showInactive();
  299. });
  300. }
  301. function hideTooltip() {
  302. if (tooltipWindow) {
  303. tooltipWindow.close();
  304. tooltipWindow = null;
  305. }
  306. }
  307. // 监听打开窗口事件
  308. ipcMain.on('open-window', () => {
  309. if (win && !win.isDestroyed()) {
  310. if (win.isMinimized()) win.restore();
  311. win.show();
  312. win.focus();
  313. } else {
  314. createWindow();
  315. }
  316. });
  317. // 监听窗口大小变化时间
  318. ipcMain.on('resize-main-window', (event, width, height) => {
  319. if (win && !win.isDestroyed()) {
  320. // 1. 先退出全屏状态(如果是全屏)
  321. if (win.isFullScreen()) {
  322. win.setFullScreen(false);
  323. // 等待全屏状态变更完成后再调整大小
  324. win.once('leave-full-screen', () => {
  325. adjustWindowSize(width, height);
  326. });
  327. }
  328. // 2. 再恢复窗口(如果是最大化)
  329. else if (win.isMaximized()) {
  330. win.unmaximize();
  331. // 等待窗口恢复后再调整大小
  332. win.once('unmaximize', () => {
  333. adjustWindowSize(width, height);
  334. });
  335. }
  336. // 3. 直接调整大小(普通状态)
  337. else {
  338. adjustWindowSize(width, height);
  339. }
  340. } else {
  341. createWindow(width, height);
  342. }
  343. });
  344. // 提取调整窗口大小的公共逻辑
  345. function adjustWindowSize(width, height) {
  346. win.setSize(width, height);
  347. win.center();
  348. }
  349. ipcMain.on('open-smart-window', () => {
  350. if (smart_win && !smart_win.isDestroyed()) {
  351. if (smart_win.isMinimized()) smart_win.restore();
  352. smart_win.show();
  353. smart_win.focus();
  354. } else {
  355. createSmartWindow();
  356. }
  357. });
  358. ipcMain.on('toggle-smart-window', () => {
  359. if (smart_win && !smart_win.isDestroyed()) {
  360. smart_win.hide();
  361. smart_win.close();
  362. smart_win.destroy()
  363. smart_win = null
  364. } else {
  365. createSmartWindow();
  366. smart_win.focus();
  367. }
  368. });
  369. ipcMain.on('close-smart-window', () => {
  370. if (smart_win && !smart_win.isDestroyed()) {
  371. smart_win.close();
  372. }
  373. });
  374. ipcMain.on('resize-smart-window', (event, height) => {
  375. if (smart_win && !smart_win.isDestroyed()) {
  376. // 获取当前窗口宽度
  377. const [width] = smart_win.getSize();
  378. // 设置新的窗口大小(保持宽度不变,修改高度)
  379. smart_win.setSize(width, height);
  380. }
  381. });
  382. ipcMain.on('set-float-window-position', (event, x, y) => {
  383. if (floatingWindow && !floatingWindow.isDestroyed()) {
  384. floatingWindow.setBounds({ x, y, width: 50, height: 50 });
  385. }
  386. });
  387. ipcMain.handle('get-float-window-position', () => {
  388. if (floatingWindow && !floatingWindow.isDestroyed()) {
  389. const [x, y] = floatingWindow.getPosition();
  390. return { x, y };
  391. }
  392. return { x: 0, y: 0 };
  393. });
  394. ipcMain.handle('get-smart-window-position', () => {
  395. if (smart_win && !smart_win.isDestroyed()) {
  396. const [x, y] = smart_win.getPosition();
  397. return { x, y };
  398. }
  399. return { x: 0, y: 0 };
  400. });
  401. ipcMain.on('set-smart-window-position', (event, x, y) => {
  402. if (smart_win && !smart_win.isDestroyed()) {
  403. smart_win.setBounds({ x, y });
  404. }
  405. });
  406. ipcMain.on('show-tooltip', (event, adsorbedPosition) => {
  407. if (!floatingWindow) return;
  408. const [x, y] = floatingWindow.getPosition();
  409. showTooltip(adsorbedPosition, x, y);
  410. });
  411. ipcMain.on('hide-tooltip', hideTooltip);
  412. ipcMain.on('hide-tray-float-window', (event) => {
  413. if (floatingWindow && !floatingWindow.isDestroyed()) {
  414. if (isFloatingWindowVisible) {
  415. if (smart_win) {
  416. smart_win?.close()
  417. // smart_win?.destroy()
  418. floatingWindow?.hide();
  419. hideTooltip(); // 隐藏可能显示的工具提示
  420. } else {
  421. floatingWindow?.close()
  422. }
  423. }
  424. updateTrayMenu(false); // 更新托盘菜单文本
  425. }
  426. })
  427. ipcMain.on('show-tray-float-window', (event) => {
  428. createFloatingWindow();
  429. floatingWindow.focus();
  430. isFloatingWindowVisible = true;
  431. updateTrayMenu(); // 更新托盘菜单文本
  432. })
  433. // 新增:监听登录状态变化
  434. ipcMain.on('set-login-state', (event, loggedIn) => {
  435. if (loggedIn) {
  436. setLoginWindowMode();
  437. } else {
  438. setMainWindowMode();
  439. }
  440. });
  441. // 封装存储操作
  442. // 保存认证信息
  443. ipcMain.handle('save-auth', (event, username, password, token) => {
  444. console.log('Saving auth:', username, password, token);
  445. try {
  446. console.log('Saving auth:', username, password, token);
  447. store.set('username', username);
  448. store.set('password', password);
  449. store.set('token', token);
  450. return { success: true, message: '认证信息保存成功' };
  451. } catch (error) {
  452. console.error('Failed to save auth:', error);
  453. return new Error('保存认证信息失败: ' + error.message);
  454. }
  455. });
  456. // 获取用户名
  457. ipcMain.handle('get-username', () => {
  458. return store.get('username') || '';
  459. });
  460. // 获取密码
  461. ipcMain.handle('get-password', () => {
  462. return store.get('password') || '';
  463. });
  464. // 获取 Token
  465. ipcMain.handle('get-token', () => {
  466. return store.get('token') || '';
  467. });
  468. // 删除认证信息
  469. ipcMain.handle('clear-auth', () => {
  470. // store.delete('username');n
  471. store.delete('token');
  472. // store.delete('password')
  473. });
  474. ipcMain.on('logout', () => {
  475. updateTrayMenu(false)
  476. if (smart_win && !smart_win.isDestroyed()) {
  477. smart_win.destroy();
  478. smart_win = null;
  479. }
  480. if (floatingWindow && !floatingWindow.isDestroyed()) {
  481. floatingWindow.destroy();
  482. floatingWindow = null;
  483. }
  484. });
  485. // 显示主窗口
  486. function showMainWindow() {
  487. if (win && !win.isDestroyed()) {
  488. if (win.isMinimized()) win.restore();
  489. win.show();
  490. win.focus();
  491. } else {
  492. createWindow();
  493. }
  494. }
  495. if (!gotTheLock) {
  496. // 如果没有获取到锁,说明已经有一个实例在运行,直接退出当前实例
  497. app.quit();
  498. } else {
  499. app.whenReady().then(() => {
  500. createWindow();
  501. createTray();
  502. createFloatingWindow();
  503. app.on('activate', function () {
  504. if (BrowserWindow.getAllWindows().length === 0) createWindow();
  505. });
  506. });
  507. app.on('window-all-closed', function () {
  508. if (process.platform !== 'darwin') app.quit();
  509. });
  510. // 再次启动时,如果有主窗口,则显示主窗口
  511. app.on('second-instance', showMainWindow);
  512. }