Skip to content
This repository has been archived by the owner on Aug 17, 2021. It is now read-only.

Commit

Permalink
重构以解决原生模块及dll加载失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
lunnlew committed Jan 4, 2020
1 parent 56d4d0f commit 7cdb4cd
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 87 deletions.
96 changes: 40 additions & 56 deletions src/backend/WeChatCtl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,75 +21,59 @@ if (process.env.NODE_ENV == 'development') {
}

class WeChatCtl extends events.EventEmitter {
constructor(weicaiNative) {
constructor() {
super()
this.WeChatHelperWorker = null
console.log('加载' + 'WeicaiBinding.node')
this.weicaiNative = weicaiNative
this.isWeChatCtl = false
this.WeChatHelperWorker = child_process.fork(WeChatHelperWorkerPath, [], {
cwd: process.cwd(),
env: process.env,
stdio: [0, 1, 2, 'ipc'],
encoding: 'utf-8'
})
this.WeChatHelperWorker.on('message', function(msg) {
if (typeof msg == 'object') {
if (msg.event == 'WeChatHelperInjectCompleted') {
console.log('WeiChatWorker WeChatHelperInjectCompleted')
}
}
})
}
async startWechatCtl() {
const self = this
let p_WeChatCtl_path = path.join(p_WeChatDll_dir, "WeChatCtl.dll")
console.log('加载' + p_WeChatCtl_path)
let loadCtlSuccess = self.weicaiNative.startCtrlClient(p_WeChatCtl_path)
if (loadCtlSuccess) {
self.isWeChatCtl = true
console.log('加载控制端成功')
} else {
console.log('加载控制端失败')
}
self.WeChatHelperWorker.send({
'event': 'startWechatCtl',
'data': {}
})
}
async sendCtlMsg(cname, mtype) {
const self = this
self.WeChatHelperWorker.send({
'event': 'sendCtlMsg',
'data': {
cname: cname,
mtype: mtype
}
})
}
async openNewWechat() {
const self = this
let p_WeChatDll_name = `WeChatHelper.dll`
if (process.env.NODE_ENV == 'development') {
let wechatVersion = self.weicaiNative.GetWechatVersion().replace(/\./g, '')
p_WeChatDll_dir = path.join(__dirname, "../dist_electron/native")
p_WeChatDll_name = `WeChatHelper${wechatVersion}.dll`
}

let p_WeChatCtl_path = path.join(p_WeChatDll_dir, p_WeChatDll_name)
console.log('加载' + p_WeChatCtl_path)

let ret = self.weicaiNative.openNewWechat(p_WeChatDll_dir, p_WeChatDll_name)
if (ret) {
console.log('新开微信成功')
} else {
console.log('新开微信失败')
}
self.WeChatHelperWorker.send({
'event': 'openNewWechat',
'data': {}
})
}
async closeAllWechat() {
const self = this
let ret = self.weicaiNative.closeAllWechat()
if (ret) {
console.log('关闭微信成功')
} else {
console.log('关闭微信失败')
}
self.WeChatHelperWorker.send({
'event': 'weicaiNative',
'data': {}
})
}
async closeWechat(processId) {
const self = this
let ret = self.weicaiNative.closeWechatProcess(parseInt(processId))
if (ret) {
console.log('关闭微信成功')
} else {
console.log('关闭微信失败')
}
}
async startWechatHelperInject() {
const self = this
self.WeChatHelperWorker = child_process.fork(WeChatHelperWorkerPath, [], {
cwd: process.cwd(),
env: process.env,
stdio: [0, 1, 2, 'ipc'],
encoding: 'utf-8'
})
self.WeChatHelperWorker.on('message', function(msg) {
if (typeof msg == 'object') {
if (msg.event == 'WeChatHelperInjectCompleted') {
console.log('WeiChatWorker WeChatHelperInjectCompleted')
}
self.WeChatHelperWorker.send({
'event': 'closeWechat',
'data': {
processId: processId
}
})
}
Expand Down
17 changes: 11 additions & 6 deletions src/backend/weicai/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,22 @@ const fs = require('fs-extra')
const json2csv = require('json2csv')
const shell = require('electron').shell

// prod模式下
// 由于没权限直接加载c盘二进制资源,须在其他盘处理
console.log('NODE_ENV:' + process.env.NODE_ENV)
let p_WeChatDll_dir = 'd:/weicai-scraper/native'
if (process.env.NODE_ENV == 'development') {
p_WeChatDll_dir = path.join(__dirname, "../dist_electron/native")
} else {
fs.copySync(path.join(__dirname, "../native"), p_WeChatDll_dir)
}

const AppServer = require('./AppServer')
const expressApp = require('./expressApp')
const Recorder = require('./Recorder')
const ProxyServer = require('./ProxyServer')
const WeChatCtl = require('../WeChatCtl')
const WeicaiBinding = require('../../native/WeicaiBinding/build/Release/WeicaiBinding.node')
let weChatCtl = new WeChatCtl(WeicaiBinding)
let weChatCtl = new WeChatCtl()
global.recorder = new Recorder()
let appServer = new AppServer()
appServer.bindApp(expressApp, recorder)
Expand Down Expand Up @@ -224,15 +232,12 @@ var requestStrToMap = function(e) {

appServer.route(function(self) {
self.app.all('/wechatCtl', async function(req, res) {
console.log(req.body)
let action = req.query.act || ''
switch (action) {
case "startMonitor":
{
// 启动控制端
weChatCtl.startWechatCtl();
// 开始微信进程注入
weChatCtl.startWechatHelperInject();
res.send({ code: 200, msg: '启动控制端完成', data: {} })
break;
}
Expand Down Expand Up @@ -276,7 +281,7 @@ appServer.route(function(self) {
console.log(req.body)

setTimeout(function() {
WeicaiBinding.sendCtlMsg(req.body.WeChatHelperName, 502)
weChatCtl.sendCtlMsg(req.body.WeChatHelperName, 502)
}, 200)

if (self.ws['wcclient']) {
Expand Down
85 changes: 60 additions & 25 deletions src/worker/WeChatHelperWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,77 @@ const path = require('path')
const fs = require('fs-extra')

let weicaiNative
let p_WeicaiBinding = path.join(__dirname, '../native/WeicaiBinding.node').replace(/\\/g, '/')

// 预定义DLL地址
// 发布模式配置
let p_WeicaiBinding = path.join(__dirname, '../native/WeicaiBinding.node').replace(/\\/g, '/')
let p_WeChatDll_dir = 'd:/weicai-scraper/native'
let p_WeChatHelper_path = path.join(p_WeChatDll_dir, "WeChatHelper.dll")
console.log('NODE_ENV:' + process.env.NODE_ENV)

let wechatVersion

if (process.env.NODE_ENV == 'development') {

// 开发模式
console.log('[dev]加载' + '../../dist_electron/native/WeicaiBinding.node')
weicaiNative = require('../../dist_electron/native/WeicaiBinding.node')
wechatVersion = weicaiNative.GetWechatVersion().replace(/\./g, '')
p_WeChatDll_dir = path.join(__dirname, "../../dist_electron/native")
p_WeChatDll_name = `WeChatHelper${wechatVersion}.dll`

} else {

// 发布模式
console.log('[prod]加载' + p_WeicaiBinding)
weicaiNative = require(p_WeicaiBinding)
wechatVersion = weicaiNative.GetWechatVersion().replace(/\./g, '')
p_WeChatDll_name = `WeChatHelper.dll`
// 由于未知原因无法直接从C盘下加载DLL资源
// 于是复制到另外的盘符下来加载
try {
if (!fs.existsSync(p_WeChatHelper_path)) {
fs.copySync(path.join(__dirname, `../native/WeChatHelper${wechatVersion}.dll`), p_WeChatHelper_path)
console.log('copy WeChatHelper.dll success')
} else {
console.log('WeChatHelper.dll exists')
}
let wechatVersion = weicaiNative.GetWechatVersion().replace(/\./g, '')

let p_WeChatDll_name = `WeChatHelper${wechatVersion}.dll`

process.on('message', async (msg) => {
if (typeof msg == 'object') {
if (msg.event == 'startWechatCtl') {
let p_WeChatCtl_path = path.join(p_WeChatDll_dir, "WeChatCtl.dll")
console.log('加载' + p_WeChatCtl_path)
let loadCtlSuccess = weicaiNative.startCtrlClient(p_WeChatCtl_path)
if (loadCtlSuccess) {
console.log('加载控制端成功')
} else {
console.log('加载控制端失败')
}
}
} catch (err) {
console.log('copy WeChatHelper.dll err: ' + err)
}

}
if (msg.event == 'openNewWechat') {
let p_WeChatCtl_path = path.join(p_WeChatDll_dir, p_WeChatDll_name)
console.log('加载' + p_WeChatCtl_path)
let ret = weicaiNative.openNewWechat(p_WeChatDll_dir, p_WeChatDll_name)
if (ret) {
console.log('新开微信成功')
} else {
console.log('新开微信失败')
}
}

if (msg.event == 'closeAllWechat') {
let ret = weicaiNative.closeAllWechat()
if (ret) {
console.log('关闭微信成功')
} else {
console.log('关闭微信失败')
}
}

if (msg.event == 'closeWechat') {
let processId = msg.data.processId
let ret = weicaiNative.closeWechatProcess(parseInt(processId))
if (ret) {
console.log('关闭微信成功')
} else {
console.log('关闭微信失败')
}
}

if (msg.event == 'sendCtlMsg') {
let cname = msg.data.cname
let mtype = msg.data.mtype
let ret = weicaiNative.sendCtlMsg(cname, parseInt(mtype))
if (ret) {
console.log('发送控制消息成功')
} else {
console.log('发送控制消息失败')
}
}
}
})

0 comments on commit 7cdb4cd

Please sign in to comment.