Skip to content

Commit

Permalink
finished scan
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhangWei-KUMO committed Dec 15, 2024
1 parent 161db51 commit 59c8526
Show file tree
Hide file tree
Showing 15 changed files with 111 additions and 99 deletions.
10 changes: 1 addition & 9 deletions db/wechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,20 @@ const db = new sqlite3.Database(dbFile, (err) => {
});
}




export function getWechatConfig() {
return new Promise((resolve, reject) => {
db.get(`SELECT * FROM wechats WHERE id = 1`, [], (err, row) => { // Select the first row
db.get(`SELECT * FROM wechats WHERE id = 1`, [], (err, row) => {
if (err) {
reject(err);
} else if (!row) {
resolve(null); // No config found.
} else {

// Convert isEnable back to boolean
row.isEnable = !!row.isEnable;
row.isPushEnable = !!row.isPushEnable;
resolve(row);
}
});
});
}


export function saveWechatConfig(config) {
const { username, wechatid,avatar, friends, loginurl } = config;
return new Promise((resolve, reject) => {
Expand Down
32 changes: 26 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import {sendMessage} from './util/util.js'
import { config } from 'dotenv';
import express from 'express';
import {log} from './db/logs.js'
import { singleChat,groupChat,handleFile, handleImage,handleAudio,handleVideo,handleGIF,handleTransfer } from './util/handle.js';
import { singleChat,groupChat,handleFile, handleImage,handleAudio,handleVideo,handleGIF,handleTransfer,handlePush } from './util/handle.js';
import router from './router/router.js';
import staticRouter from './router/static.js';
import {saveWechatConfig} from './db/wechat.js';
// import {Jimp} from 'jimp';

const port = 3000;
config();
const app = express();
Expand Down Expand Up @@ -80,12 +82,26 @@ export async function prepareBot() {
});

bot.on('scan', (qrcode) => {
saveWechatConfig({loginurl:qrcode})
saveWechatConfig({loginurl:qrcode,wechatid:'',username:'',avatar:'',friends:''})
})

bot.on("login", (user) => {
console.log(user)
saveWechatConfig({username:user.payload.name,wechatid:user.payload.id,avatar:user.payload.avatar,loginurl:'',friends:JSON.stringify(user.payload.friendList)})
bot.on("login", async (user) => {
const self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gz3UbgrK0ihgewzmh7ArXNMxuSzBTtX26VnYvmSJlswsi75k5568fjTtYzdRvRGc91PcwN5jAknaoVcVo3cUVYrXhmtoLi42lFYCNSePy/X8qErsJSsiv4ZBnupzI+SVHzO3Q8+tOaMotnXi5EMrxqpn3DAMajAx2rO5e5HcXBkt1aK22unzMDjp7UrsEkZ9vEsKAuAFUZ3e/etbEc1zC17U478rBbjCR8sx7nGK0jCxDkXPDlqyWhnAbc7ZyFxjHTn86zk9S47HQWknkXO9mLqI8Nzu2+1ZlCw2cEm/Lthjyd344o0Hqf/2Q==';

try {
const selfAvatarFileBox = await selfContact.avatar();
if (selfAvatarFileBox.buffer) {
const buffer = await selfAvatarFileBox.toBuffer();
// const image = await Jimp.read(buffer);
// const compressedBuffer = await image.getBufferAsync(Jimp.MIME_JPEG);
const base64 = buffer.toString('base64');
dataUrl = `data:image/jpeg;base64,${base64}`;
}
} catch (e) {
log('error', "获取头像失败"+e);
}
saveWechatConfig({username:user.payload.name,wechatid:user.payload.id,avatar:dataUrl,loginurl:'',friends:''})
log('info', "机器人登录成功,账号名:"+user.payload.name);
})

Expand All @@ -94,7 +110,11 @@ export async function prepareBot() {
})

bot.on("error", (e) => {
log('error', "微信端传出错误"+e);
let text = e
if(`${e}`.includes("AssertionError: 400 != 400")){
text = "获取微信二维码超时5分钟"
}
log('error', text);
})

bot.on("ready",async ()=>{
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"express-session": "^1.18.1",
"fluent-ffmpeg": "^2.1.2",
"ioredis": "^5.3.2",
"jimp": "^1.6.0",
"mammoth": "^1.6.0",
"microsoft-cognitiveservices-speech-sdk": "^1.35.0",
"moment": "^2.30.1",
Expand Down
2 changes: 2 additions & 0 deletions public/js/moment-with-locales.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion public/chats.html → router/public/chats.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/[email protected]/dist/css/semi.css">
<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/moment-with-locales.min.js"></script>
<script src="./js/moment-with-locales.min.js"></script>
<style>
body {
margin: 0;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
155 changes: 76 additions & 79 deletions public/index.html → router/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,33 @@
<script src="https://unpkg.com/[email protected]/babel.min.js"></script>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>

<script src="https://unpkg.com/@douyinfe/[email protected]/dist/umd/semi-ui.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/[email protected]/dist/css/semi.css">

<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<script src="./js/qrcode.js"></script>
<style>
<style>
body {
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
background: linear-gradient(135deg, #2c3e50, #3498db);
color: #fff;
overflow-x: hidden; /* 防止水平滚动 */
color: #fff;
overflow-x: hidden;
}


.components-layout-demo {
display: flex;
height: 100vh;
}

.semi-layout-sider {
background: rgba(255, 255, 255, 0.1); /* 半透明白色背景 */
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
border-right: 1px solid rgba(255, 255, 255, 0.2);
border-radius: 10px;
}

.semi-layout-header {
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
Expand All @@ -51,16 +49,16 @@
}

.content-container {
padding: 30px;
padding: 30px;
background: rgba(255, 255, 255, 0.05);
border-radius: 10px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
margin: 20px;
backdrop-filter: blur(10px);
margin: 20px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
display: flex;
flex-direction: column;
align-items: center;
align-items: center;
}

.content-container h2 {
Expand All @@ -75,8 +73,8 @@
margin: 20px auto;
width: 228px;
background: #fff;
padding: 10px;
border-radius: 5px;
/* padding: 10px; */
border-radius: 5px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

Expand All @@ -85,25 +83,25 @@
text-align: center;
font-size: 1.2rem;
font-weight: 600;
letter-spacing: 1px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
color: #fff;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
letter-spacing: 1px;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
color: #fff;
border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

.sider-nav a {
display: block;
padding: 12px 20px;
color: #ddd;
text-decoration: none;
padding: 12px 20px;
color: #ddd;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s, color 0.3s;
margin: 5px 0;
transition: background-color 0.3s, color 0.3s;
margin: 5px 0;
}

.sider-nav a:hover {
background-color: rgba(255, 255, 255, 0.1);
color: #fff;
color: #fff;
}
.sider-nav a.active {
background-color: rgba(52, 152, 219, 0.3);
Expand All @@ -125,22 +123,57 @@
lineHeight: '76px',
padding: '0 24px',
fontWeight:"600",
color:"#fff",
color:"#fff",
};

function App() {
let {useState,useEffect} = React
let {useState,useEffect} = React
const [activeLink, setActiveLink] = React.useState(window.location.pathname);
const [title,setTitle] = useState('微信授权扫码登录');
const [wechat,setWechat] = useState({});

function connectWechat() {
try {
fetch('/api/wechat')
.then((res) => res.json())
.then((data) => {
if (data.loginurl) {
document.getElementById('qrcode').innerHTML = '';
new QRCode(document.getElementById('qrcode'),{
text: data.loginurl,
width: 228,
height: 228,
} );
setTitle("微信授权扫码登录")
setWechat({})
}else{
document.getElementById('qrcode').innerHTML = ''
console.log(data)
setTitle("微信已由AI接管")
setWechat(data)
}
});
} catch (error) {
console.log(error)
}

}
useEffect(() => {
const handleLocationChange = () => {
setActiveLink(window.location.pathname);
};

window.addEventListener('popstate', handleLocationChange);
connectWechat();

// Set up the interval
const intervalId = setInterval(connectWechat, 7000);

return () => {
window.removeEventListener('popstate', handleLocationChange);
clearInterval(intervalId); // Clear the interval when the component unmounts
};

}, []);

const links = [
Expand All @@ -149,60 +182,13 @@
{ path: "/logs", text: "日志记录" },
{ path: "/settings", text: "系统设置" },
{ path: "/docs", text: "开发文档" },
{ path: "/poster", text: "图片库" },
{ path: "/poster", text: "图片库" },
{ path: "/knowledge", text: "知识库" },
{ path: "/forgetpassword", text: "重置密码" },
{ path: "/voice", text: "语音设置" },
{ path: "/email", text: "邮箱设置" },
{ path: "/forgetpassword", text: "重置密码" },
{ path: "/voice", text: "语音设置" },
{ path: "/email", text: "邮箱设置" },
];

useEffect(() => {
let ws;
let reconnectAttempts = 0;

function connectWS() {
if (ws) { // 如果 ws 已经存在,先关闭连接
ws.close();
}
ws = new WebSocket('ws://localhost:1983');
ws.onopen = () => {
console.log("WebSocket connection established.");
reconnectAttempts = 0; // 重置重连次数
};

ws.onmessage = (event) => {
const qrcodeData = event.data;
console.log('收到WS二维码:', qrcodeData);
if(qrcodeData){
const qrcodeContainer = document.getElementById('qrcode');
if (qrcodeContainer) {
qrcodeContainer.innerHTML = '';
new QRCode(qrcodeContainer, {
text: qrcodeData,
width: 228,
height: 228,
correctLevel: QRCode.CorrectLevel.H
});
} else {
console.error("QRCode container not found.");
}
}
};

ws.onerror = (error) => {
console.error("WebSocket error:", error);
};
}

connectWS();

return () => {
if (ws) {
ws.close();
}
};
}, []);

return (
<Layout className="components-layout-demo">
<Sider style={{ width: '200px' }} className="sider-nav">
Expand All @@ -225,8 +211,19 @@
<Header style={commonStyle}>微信机器人后台管理系统</Header>
<Content style={{ minHeight: 'calc(100vh - 64px)' }}>
<div className="content-container">
<h2>微信授权扫码登录</h2>
<div id="qrcode" ></div>
<h2>{title}</h2>
<div>
{wechat.username && (
<center>
<img src={wechat.avatar} alt="avatar" style={{width: 150,height: 150,borderRadius: 12}} />
<h3>微信账户:{wechat.username}</h3>
<h5>(用户可在手机中解除授权)</h5>
</center>
)}
<div id="qrcode"></div>
</div>


</div>
</Content>
</Layout>
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion public/login.html → router/public/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/moment-with-locales.min.js"></script>
<script src="./js/moment-with-locales.min.js"></script>
<style>
body {
margin: 0;
Expand Down
2 changes: 1 addition & 1 deletion public/logs.html → router/public/logs.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/[email protected]/dist/css/semi.css">
<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/moment-with-locales.min.js"></script>
<script src="./js/moment-with-locales.min.js"></script>
<style>
body {
margin: 0;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion public/settings.html → router/public/settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<script src="https://cdn.jsdelivr.net/npm/[email protected]/min/moment-with-locales.min.js"></script>
<script src="./js/moment-with-locales.min.js"></script>
<style>
body {
margin: 0;
Expand Down
2 changes: 1 addition & 1 deletion public/voice.html → router/public/voice.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<script src="https://unpkg.com/@douyinfe/semi-icons@latest/dist/umd/semi-icons.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/@douyinfe/semi-icons@latest/dist/css/semi-icons.css">
<style>
body {
body {
margin: 0;
padding: 0;
font-family: 'Roboto', sans-serif;
Expand Down

0 comments on commit 59c8526

Please sign in to comment.