tray.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import { Menu, Tray, app } from "electron";
  2. import path from "node:path";
  3. import { DIST, ICON, PUBLIC, readIniFile, url, writeIniFile } from "./utils";
  4. import { showMainWin, extra, mainWindow } from "../win/mainWin";
  5. import log from "electron-log/main";
  6. import { handeGet, handeSet } from "./store";
  7. import io from "socket.io-client";
  8. export let socket: any = null;
  9. export const initTray = () => {
  10. const handlePath = (icon: string) => {
  11. return url ? path.join(PUBLIC!, icon) : path.join(DIST, icon);
  12. };
  13. const ICONS = {
  14. default: ICON,
  15. icon1: handlePath("./icon-1.png"),
  16. icon2: handlePath("./icon-2.png"),
  17. icon3: handlePath("./icon-3.png"),
  18. icon4: handlePath("./icon-4.png"),
  19. icon5: handlePath("./icon-5.png"),
  20. icon6: handlePath("./icon-6.png"),
  21. icon7: handlePath("./icon-7.png"),
  22. icon8: handlePath("./icon-8.png"),
  23. icon9: handlePath("./icon-9.png"),
  24. icon9plus: handlePath("./icon-9plus.png"),
  25. };
  26. let appIcon = new Tray(ICONS.default);
  27. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  28. let contextMenu = Menu.buildFromTemplate([
  29. {
  30. id: "open-win",
  31. label: `${isFocusOpenWin == 1 ? "关闭" : "开启"}主动弹窗`,
  32. click: () => {
  33. updateItem();
  34. },
  35. },
  36. {
  37. label: "打开弹窗",
  38. click: () => {
  39. showMainWin();
  40. },
  41. },
  42. {
  43. label: "退出弹窗",
  44. click: () => {
  45. extra.activeClose = true;
  46. setTimeout(() => {
  47. app.quit();
  48. });
  49. },
  50. },
  51. ]);
  52. // appIcon.setToolTip("菁苗健康");
  53. appIcon.setContextMenu(contextMenu);
  54. // 服务端地址
  55. const SOCKET_URL = "http://114.215.252.134:2120";
  56. socket = io(SOCKET_URL, {
  57. reconnection: true,
  58. reconnectionDelay: 1000,
  59. timeout: 20000,
  60. });
  61. // socket连接后等待 uid
  62. socket.on("connect", function () {
  63. console.log("Socket connected, waiting for uid...");
  64. });
  65. // 后端推送来消息时
  66. socket.on("new_msg", function (msg) {
  67. const fixedData = msg.replace(/"/g, '"');
  68. const userObj = JSON.parse(fixedData);
  69. const msgCount = userObj.count;
  70. if (msgCount <= 9) {
  71. if (msgCount === 0) {
  72. appIcon.setImage(ICONS.default);
  73. } else {
  74. appIcon.setImage(ICONS[`icon${msgCount}`]);
  75. }
  76. } else {
  77. appIcon.setImage(ICONS[`icon9plus`]);
  78. }
  79. appIcon.setToolTip(`未读消息数量(${msgCount})`);
  80. // appIcon.setToolTip(`菁苗健康-未读消息数量(${msgCount})`);
  81. if (userObj.msg_type !== 2) {
  82. // 向渲染进程发送消息
  83. mainWindow?.webContents.send("new-notification", {
  84. ...userObj,
  85. is_read: 0,
  86. });
  87. }
  88. });
  89. appIcon.addListener("click", function () {
  90. showMainWin();
  91. });
  92. // 更新item
  93. const updateItem = async () => {
  94. let isFocusOpenWin: number = handeGet("isFocusOpenWin");
  95. const openMenuItem = contextMenu.getMenuItemById("open-win");
  96. if (isFocusOpenWin == 1) {
  97. isFocusOpenWin = 2;
  98. openMenuItem!.label = `开启主动弹窗`;
  99. } else {
  100. isFocusOpenWin = 1;
  101. openMenuItem!.label = `关闭主动弹窗`;
  102. }
  103. contextMenu = Menu.buildFromTemplate(contextMenu.items);
  104. appIcon.setContextMenu(contextMenu);
  105. handeSet("isFocusOpenWin", isFocusOpenWin);
  106. log.info("isFocusOpenWin==>", isFocusOpenWin);
  107. };
  108. return appIcon;
  109. };