tray.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 } 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 const initTray = () => {
  9. const handlePath = (icon: string) => {
  10. return url ? path.join(PUBLIC!, icon) : path.join(DIST, icon);
  11. };
  12. const ICONS = {
  13. default: ICON,
  14. icon1: handlePath("./icon-1.png"),
  15. icon2: handlePath("./icon-2.png"),
  16. icon3: handlePath("./icon-3.png"),
  17. icon4: handlePath("./icon-4.png"),
  18. icon5: handlePath("./icon-5.png"),
  19. icon6: handlePath("./icon-6.png"),
  20. icon7: handlePath("./icon-7.png"),
  21. icon8: handlePath("./icon-8.png"),
  22. icon9: handlePath("./icon-9.png"),
  23. // icon9plus: handlePath("./icon-9plus.png"),
  24. };
  25. let appIcon = new Tray(ICONS.default);
  26. const isFocusOpenWin: number = handeGet("isFocusOpenWin");
  27. let contextMenu = Menu.buildFromTemplate([
  28. {
  29. id: "open-win",
  30. label: `${isFocusOpenWin == 1 ? "关闭" : "开启"}主动弹窗`,
  31. click: () => {
  32. updateItem();
  33. },
  34. },
  35. {
  36. label: "打开弹窗",
  37. click: () => {
  38. showMainWin();
  39. },
  40. },
  41. {
  42. label: "退出弹窗",
  43. click: () => {
  44. extra.activeClose = true;
  45. setTimeout(() => {
  46. app.quit();
  47. });
  48. },
  49. },
  50. ]);
  51. appIcon.setToolTip("菁苗健康");
  52. appIcon.setContextMenu(contextMenu);
  53. // 服务端地址
  54. const SOCKET_URL = "http://114.215.252.134:2120";
  55. const socket = io(SOCKET_URL, {
  56. reconnection: true,
  57. reconnectionDelay: 1000,
  58. timeout: 20000,
  59. });
  60. let msgCount = 0;
  61. // socket连接后以uid登录
  62. socket.on("connect", function () {
  63. console.log("Connected via CDN ES6");
  64. socket.emit("login", 158);
  65. });
  66. // 后端推送来消息时
  67. socket.on("new_msg", function (msg) {
  68. msgCount++;
  69. if (msgCount <= 9) {
  70. appIcon.setImage(ICONS[`icon${msgCount}`]);
  71. } else {
  72. appIcon.setImage(ICONS[`icon9`]);
  73. }
  74. appIcon.setToolTip(`菁苗健康-未读消息数量(${msgCount})`);
  75. });
  76. appIcon.addListener("click", function () {
  77. showMainWin();
  78. });
  79. // 更新item
  80. const updateItem = async () => {
  81. let isFocusOpenWin: number = handeGet("isFocusOpenWin");
  82. const openMenuItem = contextMenu.getMenuItemById("open-win");
  83. if (isFocusOpenWin == 1) {
  84. isFocusOpenWin = 2;
  85. openMenuItem!.label = `开启主动弹窗`;
  86. } else {
  87. isFocusOpenWin = 1;
  88. openMenuItem!.label = `关闭主动弹窗`;
  89. }
  90. contextMenu = Menu.buildFromTemplate(contextMenu.items);
  91. appIcon.setContextMenu(contextMenu);
  92. handeSet("isFocusOpenWin", isFocusOpenWin);
  93. log.info("isFocusOpenWin==>", isFocusOpenWin);
  94. };
  95. return appIcon;
  96. };