feat: 添加作品/草稿/AI配额等后端API
This commit is contained in:
@@ -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 };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user