修改一个bug
This commit is contained in:
134
server2.js
134
server2.js
@@ -16,7 +16,8 @@ const crypto = require('crypto');
|
||||
require('dotenv').config();
|
||||
|
||||
const app = express();
|
||||
const port =30009;
|
||||
const port = 30009;
|
||||
const TOKEN = 'my_token_123'; // 与公众号后台配置一致
|
||||
|
||||
|
||||
|
||||
@@ -177,22 +178,135 @@ app.get('/wechat/oauth/access-token', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
/*菜单部分 */
|
||||
|
||||
async function createMenu() {
|
||||
const accessToken = await getAccessToken();
|
||||
const url = `https://api.weixin.qq.com/cgi-bin/menu/create?access_token=${accessToken}`;
|
||||
const target = encodeURIComponent("https://ai.satabot.com/?from=weixin");
|
||||
|
||||
|
||||
// 创建菜单的接口
|
||||
app.post('/create-menu', async (req, res) => {
|
||||
const menuData = {
|
||||
"button": [
|
||||
{
|
||||
"name": "产品中心",
|
||||
"sub_button": [
|
||||
{
|
||||
"type": "view",
|
||||
"name": "整机产品",
|
||||
"url": "http://www.jsmaibu.com/products/"
|
||||
},
|
||||
{
|
||||
"type": "view",
|
||||
"name": "智能电控",
|
||||
"url": "http://www.jsmaibu.com/Products/%E6%8E%A7%E5%88%B6%E5%99%A8%20MC100.html"
|
||||
},
|
||||
{
|
||||
"type": "view",
|
||||
"name": "资料下载",
|
||||
"url": "http://www.jsmaibu.com/Support/Download.html"
|
||||
},
|
||||
{
|
||||
"type": "click",
|
||||
"name": "AI智能体",
|
||||
"key": "AI_INTELLIGENT"
|
||||
}
|
||||
// {
|
||||
// "type": "view",
|
||||
// "name": "AI智能体",
|
||||
// // "url":`https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx364b8ebf17fe45fe&redirect_uri= encodeURIComponent("https://ai.satabot.com/?from=weixin")&response_type=code&scope=snsapi_login&state=123#wechat_redirect`,
|
||||
// "url": "https://ai.satabot.com/middle.html"
|
||||
|
||||
// }
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "服务中心",
|
||||
"sub_button": [
|
||||
{
|
||||
"type": "view",
|
||||
"name": "解决方案",
|
||||
"url": "http://www.jsmaibu.com/Solution.html"
|
||||
},
|
||||
{
|
||||
"type": "view",
|
||||
"name": "培训视频",
|
||||
"url": "http://www.jsmaibu.com/Support/index.html"
|
||||
},
|
||||
{
|
||||
"type": "view",
|
||||
"name": "一键呼叫",
|
||||
"url": "http://www.jsmaibu.com/contact/"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "飒沓智能",
|
||||
"sub_button": [
|
||||
{
|
||||
"type": "view",
|
||||
"name": "关于我们",
|
||||
"url": "http://www.jsmaibu.com/about-us.html#start"
|
||||
},
|
||||
{
|
||||
"type": "view",
|
||||
"name": "加入我们",
|
||||
"url": "https://www.zhipin.com/gongsi/job/d1e30046e726d2d01nN72Ni4EFQ~.html?ka=all-jobs-hot"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
};
|
||||
const res = await axios.post(url, menuData);
|
||||
if (res.data.errcode === 0) {
|
||||
console.log('✅ 菜单创建成功');
|
||||
} else {
|
||||
console.error('❌ 菜单创建失败:', res.data);
|
||||
}
|
||||
}
|
||||
// ========== 查询菜单 ==========
|
||||
async function getMenu() {
|
||||
const accessToken = await getAccessToken();
|
||||
const menuData = getMenuForPlatform(req); // 根据请求判断平台并获取菜单
|
||||
const url = `https://api.weixin.qq.com/cgi-bin/menu/get?access_token=${accessToken}`;
|
||||
const res = await axios.get(url);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
const createMenuUrl = `https://api.weixin.qq.com/cgi-bin/menu/create?access_token=${accessToken}`;
|
||||
// ========== 删除菜单(可选)==========
|
||||
async function deleteMenu() {
|
||||
const accessToken = await getAccessToken();
|
||||
const url = `https://api.weixin.qq.com/cgi-bin/menu/delete?access_token=${accessToken}`;
|
||||
const res = await axios.get(url);
|
||||
return res.data;
|
||||
}
|
||||
|
||||
app.get('/createMenu', async (req, res) => {
|
||||
try {
|
||||
const response = await axios.post(createMenuUrl, menuData);
|
||||
res.send(`菜单创建成功: ${JSON.stringify(response.data)}`);
|
||||
} catch (error) {
|
||||
res.status(500).send(`菜单创建失败: ${error}`);
|
||||
await createMenu();
|
||||
res.send('✅ 菜单创建成功!');
|
||||
} catch (err) {
|
||||
res.send('❌ 创建失败: ' + err.message);
|
||||
}
|
||||
});
|
||||
app.get('/getMenu', async (req, res) => {
|
||||
try {
|
||||
const data = await getMenu();
|
||||
res.json(data);
|
||||
} catch (err) {
|
||||
res.send('❌ 查询失败: ' + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/deleteMenu', async (req, res) => {
|
||||
try {
|
||||
const data = await deleteMenu();
|
||||
res.json(data);
|
||||
} catch (err) {
|
||||
res.send('❌ 删除失败: ' + err.message);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
|
||||
app.post('/wechat', async (req, res) => {
|
||||
try {
|
||||
@@ -544,7 +658,7 @@ app.post("/getuserByopenid", async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
const TOKEN = 'my_token_123'; // 与公众号后台配置一致
|
||||
|
||||
|
||||
|
||||
app.get('/wechat', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user