Skip to content

Commit 3daf2fa

Browse files
committed
修复返回结果中文乱码
1 parent 066009c commit 3daf2fa

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

server/router/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const path = require('path')
22
const fs = require('fs-extra')
3+
const iconv = require('iconv-lite')
34
const router = require('koa-router')()
45
const market = require('../data/market')
56
const { useResponse } = require('../utils')
@@ -91,16 +92,18 @@ router.post('/exec', async (ctx) => {
9192
const promises = commands.map((command, index) => {
9293
return new Promise((resolve, reject) => {
9394
if (!command) resolve()
95+
let content = ''
9496
const child = spawn('cmd.exe', ['/c', command], {
95-
cwd: data.path
97+
cwd: data.path,
98+
encoding: 'utf8'
9699
})
97100
const item = {
98101
command,
99102
child
100103
}
101104
current.push(item)
102105
child.stdout.on('data', data => {
103-
console.log(`${command} 输出:\n${data}\n`)
106+
content += iconv.decode(data, 'gbk')
104107
})
105108
child.stderr.on('data', data => {
106109
console.error(`${command} 错误:\n${data}\n`)
@@ -112,24 +115,25 @@ router.post('/exec', async (ctx) => {
112115
})
113116
result[index] = {
114117
command,
115-
code
118+
code,
119+
content
116120
}
117-
if (code === 0) resolve()
121+
if (code === 0) resolve(content)
118122
else reject(code)
119123
})
120124
})
121125
})
122126
console.log(commands)
123-
const execPromise = () => Promise.all(promises).then(() => {
124-
useResponse(ctx, result.slice(data.nodeVersion ? 3 : 2))
127+
const execPromise = () => Promise.all(promises).then((data) => {
128+
useResponse(ctx, result.slice(data.nodeVersion ? 1 : 0), 200)
125129
}).catch(error => {
126130
useResponse(ctx, {}, 500, error.toString())
127131
}).finally(() => {
128132
if (data.uuid) {
129133
commandList[data.uuid] = commandList[data.uuid].filter(item => item !== current)
130134
}
131135
})
132-
if (data.await) {
136+
if (data.wait) {
133137
return execPromise()
134138
} else {
135139
execPromise()

0 commit comments

Comments
 (0)