Skip to content

Commit

Permalink
fix: 🐛 npm 包访问登陆网页空白 (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
danni-cool authored Nov 29, 2024
2 parents bb60c93 + 33d4697 commit 2b3644f
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wechatbot-webhook",
"version": "2.8.1-beta.2",
"version": "2.8.1",
"description": "给微信里加个 webhook 机器人,支持docker部署",
"keywords": [
"wechat",
Expand Down
37 changes: 27 additions & 10 deletions packages/cli/scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,37 @@ fs.copyFileSync(
)

function copyDirSync(src, dest) {
fs.mkdirSync(dest, { recursive: true });
fs.readdirSync(src).forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
if (fs.lstatSync(srcPath).isDirectory()) {
copyDirSync(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
if (!src) {
throw new Error('src is null or undefined');
}

try {
// 如果目标目录存在,先清空它
if (fs.existsSync(dest)) {
fs.rmSync(dest, { recursive: true, force: true });
}
});

// 创建新的目标目录
fs.mkdirSync(dest, { recursive: true });

// 复制文件
fs.readdirSync(src).forEach(file => {
const srcPath = path.join(src, file);
const destPath = path.join(dest, file);
if (fs.lstatSync(srcPath).isDirectory()) {
copyDirSync(srcPath, destPath);
} else {
fs.copyFileSync(srcPath, destPath);
}
});
} catch (err) {
console.error(err);
throw err;
}
}

copyDirSync(
path.join(__dirname, '../../../static'),
path.join(__dirname, '../../../src/static'),
path.join(__dirname, '../static')
)

Expand Down
13 changes: 11 additions & 2 deletions src/route/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Middleware = require('../middleware/index')
const { serveStatic } = require('@hono/node-server/serve-static')
const fs = require('fs')
const path = require('path')
/**
* 注册路由
* @param {Object} param
Expand All @@ -19,7 +20,15 @@ module.exports = function registerRoute({ app, bot }) {
app.use('*', attachData)
// 全局鉴权
app.use(Middleware.verifyToken)
app.get('/static/*', serveStatic({ root: './' }))

// bugfix serveStatic cannot use a project root path, it actually based on cwd path
app.get('/static/*', async (c) => {
//获取*号的路径
const filePath = path.join(__dirname, `../${c.req.path}`)
return c.body(fs.readFileSync(filePath, {
encoding: 'utf-8'
}))
})

require('./msg')({ app, bot })
require('./login')({ app, bot })
Expand Down
27 changes: 10 additions & 17 deletions src/route/login.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { streamSSE } = require('hono/streaming');
const { streamSSE } = require('hono/streaming')

/**
* 注册login路由和处理上报逻辑
Expand Down Expand Up @@ -72,7 +72,8 @@ module.exports = function registerLoginCheck({ app, bot }) {
})
eventSource.addEventListener ("login", (event) => {
window.location.reload()
eventSource.close()
window.location.reload()
})
</script>
</body>
Expand All @@ -83,23 +84,15 @@ module.exports = function registerLoginCheck({ app, bot }) {
}
)

let id = 0
app.get('/sse', async (c) => {
const { readable, writable } = new TransformStream();

id++

return streamSSE(c, async (stream) => {


// c.bot.on('scan', async (qrcode) => {
// console.log({qrcode})
// })

await stream.writeSSE({
event: !success ? 'qrcode' : 'login',
data: message
})
while (true) {
await stream.writeSSE({
event: !success ? 'qrcode' : 'login',
data: message
})
await stream.sleep(1000)
}
})
})

Expand Down
File renamed without changes.

0 comments on commit 2b3644f

Please sign in to comment.