公众号验证码功能完成 yeah!!!

This commit is contained in:
2025-11-21 15:33:03 +08:00
parent ad35173301
commit 6330f6faa1
614 changed files with 31574 additions and 4571 deletions

View File

@@ -16,7 +16,7 @@ const crypto = require('crypto');
require('dotenv').config();
const app = express();
const port = 30009;
const port =30009;
@@ -56,6 +56,14 @@ if (typeof File === "undefined") {
// 启用 CORS
app.use(cors());
// app.use(cors({
// origin: ["https://ai.satabot.com"], // 允许的域
// methods: "GET,POST,PUT,DELETE,OPTIONS",
// allowedHeaders: "Content-Type,Authorization",
// credentials: true
// }));
// app.options('*', cors());
app.use(express.json({ limit: "200mb" })); // 支持大 JSON
app.use(express.urlencoded({ extended: true })); // 支持 form-data 字段
@@ -107,16 +115,68 @@ const client1 = new OpenAI({
// 获取 access_token
// getAccessToken();
async function getAccessToken() {
const url = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`;
const res = await axios.get(url);
if (res.data.access_token) {
console.log('✅ 获取 access_token 成功');
console.log('✅ 获取 access_token 成功',res.data);
return res.data.access_token;
} else {
throw new Error('❌ 获取 access_token 失败: ' + JSON.stringify(res.data));
}
}
const WECHAT_OAUTH_TOKEN_URL = 'https://api.weixin.qq.com/sns/oauth2/access_token';
app.get('/wechat/oauth/access-token', async (req, res) => {
try {
console.log(req.query)
// 1. 接收并校验前端传递的 code 参数
const { code } = req.query; // 从URL查询参数中获取 code
if (!code) {
return res.status(400).json({
code: -1,
message: '参数错误:缺少 code(微信授权临时票据)'
});
}
// 3. 调用微信 OAuth2 接口,传递必要参数
const response = await axios.get(WECHAT_OAUTH_TOKEN_URL, {
params: {
appid: "wx364b8ebf17fe45fe", // 你的微信appid
secret: "9b7476020991a2c71f1416da7f011d31", // 你的微信secret
code: code, // 前端传递的授权code
grant_type: 'authorization_code' // 固定值
}
});
// 4. 处理微信返回的结果
const wechatData = response.data;
// 微信接口失败(返回 errcode 字段)
if (wechatData.errcode) {
throw new Error(`微信接口错误:${wechatData.errmsg}(错误码:${wechatData.errcode})`);
}
// 5. 成功响应:返回微信返回的所有信息(包含 access_token、openid 等)
res.status(200).json({
code: 0,
message: '获取 OAuth2 access_token 成功',
data: wechatData // 包含 access_token、expires_in、refresh_token、openid 等
});
} catch (error) {
// 异常处理:捕获网络错误、微信接口错误等
console.error('获取 OAuth2 access_token 失败:', error.message);
res.status(500).json({
code: -3,
message: '获取 access_token 失败:' + error.message
});
}
});
// 创建菜单的接口
@@ -422,9 +482,10 @@ app.get("/wechat/userinfo", async (req, res) => {
});
app.post("/user", async (req, res) => {
try {
const { userId } = req.body; // 从请求体里拿到 user_id
const { userId } = req.body; // 从请求体里拿到 user_id
if (!userId) {
if (!userId&&userId!="NaN") {
return res.status(400).send("缺少 user_id");
}
@@ -496,18 +557,19 @@ app.get('/wechat', (req, res) => {
else res.send('验证失败');
});
// 查询用户所有会话列表
app.get("/sessions", async (req, res) => {
console.log("收到的 userId:", req.query.userId);
try {
const userId = req.query.userId;
if (!userId || userId == "NaN" )
if (!userId || userId == "NaN" || isNaN(Number(userId)))
{
console.log("userId:", req.query.userId);
res.json({
code: 300,
code: 300,
// error:'https://gsgg.com'
// error: "http://ri.satabot.com/#/dhhdghgbshwadhg"
error: "http://ri.satabot.com/#/login?goRemote=1"
});
return;