Skip to content

Commit

Permalink
fix: 判断node—nim模块是否存在,而不是判断操作系统
Browse files Browse the repository at this point in the history
  • Loading branch information
Haochen Duan committed Jun 30, 2024
1 parent 2f90c8b commit f621f2a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 34 deletions.
38 changes: 20 additions & 18 deletions packages/main/src/ipcHandle/nodeNimHandleLogin.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,28 @@ import { ipcMain, type IpcMainInvokeEvent } from 'electron';
import type NodeNim from 'node-nim';
import type { NIMResCode, LoginRes } from 'node-nim';
import { NodeNimLoginHandleChannel } from '../channelEnum.js';
import { require, isWindowsArm } from '../utils.mjs';
import { require } from '../utils.mjs';

const nimLoginAccountSet: Set<string> = new Set(); // 记录当前已登录的账号
let nodeNimInitiated: boolean = false; // 记录NodeNim是否已初始化

/* 加载node-nim模块 */
function requireNodeMim(): typeof NodeNim {
const nodeNim: { default: typeof NodeNim } | typeof NodeNim = require('node-nim');
export const nodeNim: typeof NodeNim | undefined = ((): typeof NodeNim | undefined => {
let node_nim: typeof NodeNim | undefined = undefined;

return 'default' in nodeNim ? nodeNim.default : nodeNim;
}
try {
const nodeNimModule: { default: typeof NodeNim } | typeof NodeNim = require('node-nim');

node_nim = ('default' in nodeNimModule) ? nodeNimModule.default : nodeNimModule;
} catch { /* noop */ }

return node_nim;
})();

/* 窗口关闭后需要清除和重置状态 */
export function nodeNimCleanup(): void {
if (!isWindowsArm) {
const node_nim: typeof NodeNim = requireNodeMim();

node_nim.nim.client.cleanup('');
if (nodeNim) {
nodeNim.nim.client.cleanup('');
nodeNimInitiated = false;
nimLoginAccountSet.clear();
}
Expand Down Expand Up @@ -50,37 +54,35 @@ export function nodeNimHandleLogin(): void {
ipcMain.handle(
NodeNimLoginHandleChannel.NodeNimLogin,
async function(event: IpcMainInvokeEvent, options: NodeNimLoginOptions): Promise<string | null> {
if (isWindowsArm) return null;

const node_nim: typeof NodeNim = requireNodeMim();
if (!nodeNim) return null;

if (!nodeNimInitiated) {
// 清除app data目录
await deleteAppDataDir(options.appDataDir);

const clientInitResult: boolean = node_nim.nim.client.init(
const clientInitResult: boolean = nodeNim.nim.client.init(
atob(options.appKey), options.appDataDir, '', {});

if (!clientInitResult) return null;

node_nim.nim.initEventHandlers();
nodeNim.nim.initEventHandlers();
nodeNimInitiated = true;
}

// 登录
if (!nimLoginAccountSet.has(options.account)) {
const [loginRes]: [LoginRes] = await node_nim.nim.client.login(
const [loginRes]: [LoginRes] = await nodeNim.nim.client.login(
atob(options.appKey), options.account, options.token, null, '');

if (loginRes.res_code_ !== node_nim.NIMResCode.kNIMResSuccess) return null;
if (loginRes.res_code_ !== nodeNim.NIMResCode.kNIMResSuccess) return null;

nimLoginAccountSet.add(options.account);
}

const [resEnterCode, roomEnterResult]: [NIMResCode, string] = await node_nim.nim.plugin.chatRoomRequestEnterAsync(
const [resEnterCode, roomEnterResult]: [NIMResCode, string] = await nodeNim.nim.plugin.chatRoomRequestEnterAsync(
options.roomId, null, '');

if (resEnterCode !== node_nim.NIMResCode.kNIMResSuccess) return null;
if (resEnterCode !== nodeNim.NIMResCode.kNIMResSuccess) return null;

return roomEnterResult;
});
Expand Down
3 changes: 0 additions & 3 deletions packages/main/src/utils.mts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ export const isDevelopment: boolean = process.env.NODE_ENV === 'development';
/* 文件夹路径 */
export const wwwPath: string = path.join(__dirname, '..');

/* 判断是否是Windows系统和ARM架构 */
export const isWindowsArm: boolean = process.platform === 'win32' && process.arch === 'arm64';

/**
* worker.js文件路径
* 有asar文件:app.asar.unpacked
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import type { ChannelInfo } from 'nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK/QChatCha
import type { QChatSystemNotification } from 'nim-web-sdk-ng/dist/QCHAT_BROWSER_SDK/QChatMsgServiceInterface';
import type { ChatRoomMessage } from 'node-nim';
import QChatSocket from '../../../sdk/QChatSocket';
import NodeNimChatroomSocket from '../../../sdk/NodeNimChatroomSocket';
import { isWindowsArm } from '../../helper';
import NodeNimChatroomSocket, { nodeNim } from '../../../sdk/NodeNimChatroomSocket';
import { qChatSocketList, nimChatroomList } from '../../../QQBotModals/Basic';
import { getRoomMessage, getLogMessage, log, type RoomMessageArgs } from './pocket48V2Utils';
import { pocket48LiveRoomSendGiftText, pocket48LiveRoomSendGiftLeaderboardText, type GiftItem } from './giftCompute';
Expand Down Expand Up @@ -145,7 +144,7 @@ class Pocket48V2Expand {

// 直播统计
if (
!isWindowsArm
nodeNim
&& pocket48LiveListener
&& pocket48Account
&& pocket48Token
Expand Down
24 changes: 14 additions & 10 deletions packages/qqtools/src/QQ/sdk/NodeNimChatroomSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type * as NodeNim from 'node-nim';
import type { NIMChatRoomEnterStep, ChatRoomInfo, ChatRoomMemberInfo, ChatRoomMessage } from 'node-nim';
import { NodeNimLoginHandleChannel } from '@qqtools3/main/src/channelEnum';
import appKey from './appKey.mjs';
import { isWindowsArm } from '../function/helper';

type OnMessage = (t: NodeNimChatroomSocket, event: Array<ChatRoomMessage>) => void | Promise<void>;

Expand All @@ -13,9 +12,20 @@ interface Queue {
onmsgs(msg: Array<ChatRoomMessage>): void;
}

export const nodeNim: typeof NodeNim | undefined = ((): typeof NodeNim | undefined => {
let node_nim: typeof NodeNim | undefined = undefined;

try {
const nodeNimModule: { default: typeof NodeNim } | typeof NodeNim = globalThis.require('node-nim');

node_nim = 'default' in nodeNimModule ? nodeNimModule.default : nodeNimModule;
} catch { /* noop */ }

return node_nim;
})();

/* 网易云信C++ sdk的socket连接 */
class NodeNimChatroomSocket {
#nodeNim: typeof NodeNim | undefined = undefined;
public account: string;
public token: string;
public roomId: number;
Expand All @@ -25,12 +35,6 @@ class NodeNimChatroomSocket {
public queues: Array<Queue>;

constructor(account: string, token: string, roomId: number, appDataDir: string, onMessage?: OnMessage) {
if (!isWindowsArm) {
const nodeNim: { default: typeof NodeNim } | typeof NodeNim = globalThis.require('node-nim');

this.#nodeNim = 'default' in nodeNim ? nodeNim.default : nodeNim;
}

this.account = account; // 账号
this.token = token; // token
this.roomId = roomId; // 房间id
Expand All @@ -41,7 +45,7 @@ class NodeNimChatroomSocket {
// chatroom初始化
chatroomInit(): Promise<void> {
return new Promise((resolve: Function, reject: Function): void => {
this.chatroom = new this.#nodeNim!.ChatRoom();
this.chatroom = new nodeNim!.ChatRoom();
this.chatroom.init('', '');
this.chatroom.initEventHandlers();

Expand Down Expand Up @@ -72,7 +76,7 @@ class NodeNimChatroomSocket {
}

async init(): Promise<boolean> {
if (!this.#nodeNim) return false;
if (!nodeNim) return false;

const chatroomRequestLoginResult: string | null = await ipcRenderer.invoke(NodeNimLoginHandleChannel.NodeNimLogin, {
appKey,
Expand Down

0 comments on commit f621f2a

Please sign in to comment.