没绑定过手机号的 允许会话十次,弹出验证码弹窗

This commit is contained in:
2025-12-17 14:14:46 +08:00
parent c2f6cb9d47
commit fd8d44e4ce
34 changed files with 1998 additions and 80 deletions

View File

@@ -874,12 +874,86 @@ app.post("/session", async (req, res) => {
const stopFlags = new Map();
const { Mutex } = require('async-mutex');
const aiChatNum = new Map(); // userId -> number
const mutexes = new Map(); // userId -> Mutex
function getMutex(userId) {
if (!mutexes.has(userId)) mutexes.set(userId, new Mutex());
return mutexes.get(userId);
}
async function setQuota(userId,num) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
console.log("更新用户对话次数:", userId, num);
aiChatNum.set(userId, num);
});
}
async function hasQuota(userId) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
console.log("更新用户对话次数:", userId, num);
return aiChatNum.has(userId);
});
}
async function getQuota(userId) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
console.log("更新用户对话次数:", userId, num);
return aiChatNum.get(userId);
});
}
async function removeQuota(userId) {
const m = getMutex(userId);
return await m.runExclusive(async () => {
aiChatNum.delete(userId);
});
}
// ====================== API ======================
app.post("/updateAiChatNum", 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]
);
res.json({ code: 0, message: "更新成功" });
}
})
app.post("/analyze", upload.single("file"), async (req, res) => {
try
{
const { requestId } = req.body;
const { userId } = req.body;
if(!hasQuota(userId)){
const [userRemainChatNum] = await pool.query(
"SELECT ai_chat_num from sys_user where user_id= ?",
[userId]
);
if (userRemainChatNum.length>0) {
setQuota(userId,userRemainChatNum[0].ai_chat_num);
}
}else{
if(getQuota(userId)<=0 && getQuota(userId)!==-1){
return res.status(400).json({ error: "对话次数已用完,请完成账号注册" });
}
}
const { requestId } = req.body;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
@@ -907,7 +981,7 @@ app.post("/analyze", upload.single("file"), async (req, res) => {
console.log("缺少必要参数");
return res.status(400).json({ error: "缺少必要参数" });
}
const { userPrompt, imageBase64, userId, sessionId } = req.body;
const { userPrompt, imageBase64, sessionId } = req.body;
const [rows] = await pool.query(
`
SELECT EXISTS(
@@ -1171,6 +1245,15 @@ 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}`);
}
}
catch (err)
{