electron.main.js 15 KB

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