手机号注册后刷新存放当前用户可用数量的map

This commit is contained in:
2025-12-18 13:37:02 +08:00
parent fd8d44e4ce
commit 278743fab3
3 changed files with 115 additions and 72 deletions

View File

@@ -896,7 +896,6 @@ async function setQuota(userId,num) {
async function hasQuota(userId) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
console.log("更新用户对话次数:", userId, num);
return aiChatNum.has(userId);
});
}
@@ -904,7 +903,8 @@ async function hasQuota(userId) {
async function getQuota(userId) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
console.log("更新用户对话次数:", userId, num);
console.log("获取用户对话次数1:", userId, aiChatNum);
console.log("获取用户对话次数:", userId, aiChatNum.get(userId));
return aiChatNum.get(userId);
});
}
@@ -918,43 +918,54 @@ async function removeQuota(userId) {
}
// ====================== API ======================
app.post("/updateAiChatNum", async (req, res) => {
app.post("/refreshAiChatNum", async (req, res) => {
const { userId } = req.body;
console.log(userId,98888888888);
if(!hasQuota(userId)){
return res.status(400).json({ error: "用户不存在" });
}else{
// 删除当前内存map存放的可用数量
removeQuota(userId);
await pool.query(
"update sys_user set ai_chat_num = ? where user_id= ?",
[aiChatNum.get(userId),userId]
console.log(userId,"刷新对话次数");
try {
const [userRemainChatNum] = await pool.query(
"SELECT ai_chat_num from sys_user where user_id= ?",
[userId]
);
res.json({ code: 0, message: "更新成功" });
}
if (userRemainChatNum.length > 0) {
const quota = userRemainChatNum[0].ai_chat_num;
console.log(userId,"刷新对话次数",quota);
await setQuota(userId,quota);
}
} catch (error) {
console.error("更新失败:", error);
}
})
app.post("/analyze", upload.single("file"), async (req, res) => {
try
const { userId } = req.body;
try
{
const { userId } = req.body;
if(!hasQuota(userId)){
const exists = await hasQuota(userId);
console.log(exists,"exists");
if (!exists) {
const [userRemainChatNum] = await pool.query(
"SELECT ai_chat_num from sys_user where user_id= ?",
"SELECT ai_chat_num from sys_user where user_id= ?",
[userId]
);
if (userRemainChatNum.length>0) {
setQuota(userId,userRemainChatNum[0].ai_chat_num);
console.log(userRemainChatNum,"userRemainChatNum");
if (userRemainChatNum.length > 0) {
const quota = userRemainChatNum[0].ai_chat_num;
console.log(quota,"quota");
console.log(quota <= 0 && quota !== -1,"quota2");
if (quota <= 0 && quota !== -1) {
return res.status(400).json({ error: "对话次数已用完,请完成账号注册" });
}
await setQuota(userId,quota);
}
}else{
if(getQuota(userId)<=0 && getQuota(userId)!==-1){
} else {
const quota = await getQuota(userId);
if (quota <= 0 && quota !== -1) {
return res.status(400).json({ error: "对话次数已用完,请完成账号注册" });
}
}
}
const { requestId } = req.body;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.setHeader("Transfer-Encoding", "chunked");
@@ -983,21 +994,20 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
}
const { userPrompt, imageBase64, sessionId } = req.body;
const [rows] = await pool.query(
`
SELECT EXISTS(
SELECT 1
FROM ai_chat_messages
WHERE session_id = ?
AND user_id = ?
AND title IS NOT NULL
AND title != ''
) AS hasTitle
`,
[sessionId, userId]
);
const existingTitle = rows[0].hasTitle === 1; // true / false
console.log(existingTitle, rows?.[0], 9999, rows)
`
SELECT EXISTS(
SELECT 1
FROM ai_chat_messages
WHERE session_id = ?
AND user_id = ?
AND title IS NOT NULL
AND title != ''
) AS hasTitle
`,
[sessionId, userId]
);
const existingTitle = rows[0].hasTitle === 1; // true / false
console.log(existingTitle, rows?.[0], 9999, rows)
// 如果前端没有传 sessionId,生成一个
const sessId = sessionId || Date.now().toString();
@@ -1246,20 +1256,36 @@ const existingTitle = rows[0].hasTitle === 1; // true / false
console.log("====== 分析完成 ======");
console.timeEnd("analyze");
// 更新用户剩余对话次数
let userChatNum = aiChatNum.get(userId);
if(userChatNum!==-1){
userChatNum = Math.max(0, userChatNum - 1); // 防止变成负数
setQuota(userId,userChatNum);
console.log(`用户剩余AI 对话次数: ${userChatNum}`);
}
// // 更新用户剩余对话次数
// let userChatNum = await getQuota(userId);
// if(userChatNum!==-1){
// userChatNum = Math.max(0, userChatNum - 1); // 防止变成负数
// await setQuota(userId,userChatNum);
// console.log(`用户剩余AI 对话次数: ${userChatNum}`);
// }
}
catch (err)
{
console.error("❌ 调用失败:", err.response?.data || err.message);
res.status(500).json({ code: 1, error: err.response?.data || err.message });
}
}
finally {
let userChatNum = await getQuota(userId);
console.log(userChatNum,"userChatNum2");
if (userChatNum !== -1 && userChatNum !== undefined) {
userChatNum = Math.max(0, userChatNum - 1); // 防止变成负数
console.log(userChatNum,"userChatNum最终");
await setQuota(userId, userChatNum);
console.log(`用户剩余AI 对话次数: ${userChatNum}`);
// 更新数据库
await pool.query(
"update sys_user set ai_chat_num = ? where user_id= ?",
[userChatNum, userId]
);
console.log("更新用户对话次数:", userId, userChatNum);
}
}
});
app.post("/stop", express.json(), (req, res) => {