优化内容自动滚动
This commit is contained in:
191
server2.js
191
server2.js
@@ -6,10 +6,18 @@ const fs = require("fs");
|
||||
const path = require("path");
|
||||
const OpenAI = require("openai");
|
||||
const bodyParser = require("body-parser");
|
||||
const axios = require('axios');
|
||||
require('dotenv').config();
|
||||
|
||||
const app = express();
|
||||
const port = 30009;
|
||||
|
||||
|
||||
|
||||
const appid = process.env.WECHAT_APPID;
|
||||
const secret = process.env.WECHAT_SECRET;
|
||||
const accessTokenUrl = `https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`;
|
||||
|
||||
const mysql = require("mysql2/promise");
|
||||
// 初始化数据库连接池
|
||||
const pool = mysql.createPool({
|
||||
@@ -87,6 +95,91 @@ const client1 = new OpenAI({
|
||||
timeout: 120000
|
||||
});
|
||||
|
||||
|
||||
|
||||
// 公众号
|
||||
function getMenuForPlatform(req) {
|
||||
const userAgent = req.headers['user-agent'];
|
||||
// 判断请求是否来自微信客户端
|
||||
if (/MicroMessenger/i.test(userAgent)) {
|
||||
// 微信菜单
|
||||
return {
|
||||
button: [
|
||||
{
|
||||
type: 'click',
|
||||
name: '获取 OpenID',
|
||||
key: 'GET_OPENID',
|
||||
},
|
||||
{
|
||||
type: 'view',
|
||||
name: '访问官网',
|
||||
url: 'https://www.example.com',
|
||||
sub_button: [ // 二级菜单
|
||||
{
|
||||
type: 'view',
|
||||
name: '关于我们',
|
||||
url: 'https://www.example.com/about',
|
||||
},
|
||||
{
|
||||
type: 'view',
|
||||
name: '联系我们',
|
||||
url: 'https://www.example.com/contact',
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 获取 access_token
|
||||
async function getAccessToken() {
|
||||
try {
|
||||
const res = await axios.get(accessTokenUrl);
|
||||
return res.data.access_token;
|
||||
} catch (error) {
|
||||
console.error('获取 access_token 失败:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建菜单的接口
|
||||
app.post('/create-menu', async (req, res) => {
|
||||
const accessToken = await getAccessToken();
|
||||
const menuData = getMenuForPlatform(req); // 根据请求判断平台并获取菜单
|
||||
|
||||
const createMenuUrl = `https://api.weixin.qq.com/cgi-bin/menu/create?access_token=${accessToken}`;
|
||||
|
||||
try {
|
||||
const response = await axios.post(createMenuUrl, menuData);
|
||||
res.send(`菜单创建成功: ${JSON.stringify(response.data)}`);
|
||||
} catch (error) {
|
||||
res.status(500).send(`菜单创建失败: ${error}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 处理微信自定义菜单点击事件
|
||||
app.post('/wechat', (req, res) => {
|
||||
const xmlData = req.body.xml;
|
||||
|
||||
if (xmlData.Event === 'CLICK' && xmlData.EventKey === 'GET_OPENID') {
|
||||
const openid = xmlData.FromUserName;
|
||||
console.log(`用户的 openid: ${openid}`);
|
||||
|
||||
res.send(`<xml>
|
||||
<ToUserName><![CDATA[${openid}]]></ToUserName>
|
||||
<FromUserName><![CDATA[${xmlData.ToUserName}]]></FromUserName>
|
||||
<CreateTime>${Date.now()}</CreateTime>
|
||||
<MsgType><![CDATA[text]]></MsgType>
|
||||
<Content><![CDATA[您的 OpenID 是:${openid}]]></Content>
|
||||
</xml>`);
|
||||
} else {
|
||||
res.send('未处理的事件');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
app.get("/test-db", async (req, res) => {
|
||||
try {
|
||||
await pool.query(
|
||||
@@ -99,6 +192,31 @@ app.get("/test-db", async (req, res) => {
|
||||
res.send("FAIL");
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/wechat/userinfo", async (req, res) => {
|
||||
const { code } = req.query;
|
||||
if (!code) return res.status(400).send("缺少 code");
|
||||
|
||||
try {
|
||||
// 1. 用 code 换取 access_token 和 openid
|
||||
const tokenResp = await axios.get(
|
||||
`https://api.weixin.qq.com/sns/oauth2/access_token?appid=${appid}&secret=${secret}&code=${code}&grant_type=authorization_code`
|
||||
);
|
||||
const { access_token, openid } = tokenResp.data;
|
||||
|
||||
// 2. 拉取用户信息
|
||||
const userRes = await axios.get(
|
||||
`https://api.weixin.qq.com/sns/userinfo?access_token=${access_token}&openid=${openid}&lang=zh_CN`
|
||||
);
|
||||
const userInfo = userRes.data;
|
||||
|
||||
// 3. 返回给前端
|
||||
res.json({ code: 0, data: userInfo });
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.status(500).send("获取用户信息失败");
|
||||
}
|
||||
});
|
||||
app.post("/user", async (req, res) => {
|
||||
try {
|
||||
const { userId } = req.body; // 从请求体里拿到 user_id
|
||||
@@ -504,47 +622,46 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
|
||||
});
|
||||
|
||||
|
||||
const APPID = "wx549f4ae0f85eb3ea";
|
||||
const APPSECRET = "你的公众号APPSECRET";
|
||||
|
||||
// 获取微信授权回调接口
|
||||
app.get("/callback", async (req, res) => {
|
||||
const code = req.query.code;
|
||||
|
||||
try {
|
||||
// 用 code 换 openid
|
||||
const resp = await axios.get("https://api.weixin.qq.com/sns/oauth2/access_token", {
|
||||
params: {
|
||||
appid: APPID,
|
||||
secret: APPSECRET,
|
||||
code: code,
|
||||
grant_type: "authorization_code",
|
||||
},
|
||||
});
|
||||
// // 获取微信授权回调接口
|
||||
// app.get("/callback", async (req, res) => {
|
||||
// const code = req.query.code;
|
||||
|
||||
const data = resp.data;
|
||||
console.log("微信授权返回:", data);
|
||||
// try {
|
||||
// // 用 code 换 openid
|
||||
// const resp = await axios.get("https://api.weixin.qq.com/sns/oauth2/access_token", {
|
||||
// params: {
|
||||
// appid: APPID,
|
||||
// secret: APPSECRET,
|
||||
// code: code,
|
||||
// grant_type: "authorization_code",
|
||||
// },
|
||||
// });
|
||||
|
||||
if (data.openid) {
|
||||
res.json({
|
||||
code: 0,
|
||||
openid:data.openid
|
||||
})
|
||||
// 判断数据库里是否已经有这个 openid
|
||||
// 没有 → 注册;有 → 登录
|
||||
// res.send(`授权成功,openid: ${data.openid}`);
|
||||
} else {
|
||||
res.json({
|
||||
code: 500,
|
||||
msg:"微信授权失败"+ JSON.stringify(data)
|
||||
})
|
||||
// res.send("授权失败: " + JSON.stringify(data));
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
res.send("服务器错误");
|
||||
}
|
||||
});
|
||||
// const data = resp.data;
|
||||
// console.log("微信授权返回:", data);
|
||||
|
||||
// if (data.openid) {
|
||||
// res.json({
|
||||
// code: 0,
|
||||
// openid:data.openid
|
||||
// })
|
||||
// // 判断数据库里是否已经有这个 openid
|
||||
// // 没有 → 注册;有 → 登录
|
||||
// // res.send(`授权成功,openid: ${data.openid}`);
|
||||
// } else {
|
||||
// res.json({
|
||||
// code: 500,
|
||||
// msg:"微信授权失败"+ JSON.stringify(data)
|
||||
// })
|
||||
// // res.send("授权失败: " + JSON.stringify(data));
|
||||
// }
|
||||
// } catch (err) {
|
||||
// console.error(err);
|
||||
// res.send("服务器错误");
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user