feat: 添加作品/草稿/AI配额等后端API

This commit is contained in:
2026-03-03 17:24:22 +08:00
parent 5baa95f514
commit 7fc6779fce
2 changed files with 149 additions and 0 deletions

View File

@@ -128,6 +128,95 @@ const UserModel = {
const [rows] = await pool.query(sql, params);
return rows;
},
// 获取我的作品
async getMyWorks(userId) {
try {
const [rows] = await pool.query(
`SELECT id, title, description, category, cover_url, play_count, like_count,
comment_count, status, created_at, updated_at,
COALESCE(earnings, 0) as earnings
FROM stories
WHERE author_id = ?
ORDER BY created_at DESC`,
[userId]
);
return rows;
} catch (e) {
// 表可能还没有author_id字段返回空数组
return [];
}
},
// 获取草稿箱
async getDrafts(userId) {
try {
const [rows] = await pool.query(
`SELECT id, title, category, node_count, source, created_at, updated_at
FROM story_drafts
WHERE user_id = ?
ORDER BY updated_at DESC`,
[userId]
);
return rows;
} catch (e) {
// 表可能不存在,返回空数组
return [];
}
},
// 获取最近游玩
async getRecentPlayed(userId, limit = 10) {
const [rows] = await pool.query(
`SELECT s.id, s.title, s.category, s.description, s.cover_url,
up.current_node_key, up.is_completed,
CASE WHEN up.is_completed THEN '已完成' ELSE '进行中' END as progress
FROM user_progress up
JOIN stories s ON up.story_id = s.id
WHERE up.user_id = ?
ORDER BY up.updated_at DESC
LIMIT ?`,
[userId, limit]
);
return rows;
},
// 获取AI创作历史
async getAIHistory(userId, limit = 20) {
try {
const [rows] = await pool.query(
`SELECT id, gen_type, input_prompt, output_content, status, created_at
FROM ai_generations
WHERE user_id = ?
ORDER BY created_at DESC
LIMIT ?`,
[userId, limit]
);
return rows;
} catch (e) {
return [];
}
},
// 获取AI配额
async getAIQuota(userId) {
try {
const [rows] = await pool.query(
`SELECT daily_free_total as daily, daily_free_used as used,
purchased_quota as purchased, gift_quota as gift
FROM user_ai_quota
WHERE user_id = ?`,
[userId]
);
if (rows.length > 0) {
return rows[0];
}
// 没有记录则返回默认值
return { daily: 3, used: 0, purchased: 0, gift: 0 };
} catch (e) {
return { daily: 3, used: 0, purchased: 0, gift: 0 };
}
}
};