Files
ai_game/server/routes/user.js

185 lines
5.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const express = require('express');
const router = express.Router();
const UserModel = require('../models/user');
// 模拟微信登录实际环境需要调用微信API
router.post('/login', async (req, res) => {
try {
const { code, userInfo } = req.body;
// 实际项目中需要用code换取openid
// 这里为了开发测试暂时用code作为openid
const openid = code || `test_user_${Date.now()}`;
const user = await UserModel.findOrCreate(openid, userInfo);
res.json({
code: 0,
data: {
userId: user.id,
openid: user.openid,
nickname: user.nickname,
avatarUrl: user.avatar_url
}
});
} catch (err) {
console.error('登录失败:', err);
res.status(500).json({ code: 500, message: '登录失败' });
}
});
// 更新用户信息
router.post('/profile', async (req, res) => {
try {
const { userId, nickname, avatarUrl, gender } = req.body;
await UserModel.updateProfile(userId, { nickname, avatarUrl, gender });
res.json({ code: 0, message: '更新成功' });
} catch (err) {
console.error('更新用户信息失败:', err);
res.status(500).json({ code: 500, message: '更新用户信息失败' });
}
});
// 获取用户进度
router.get('/progress', async (req, res) => {
try {
const { userId, storyId } = req.query;
const progress = await UserModel.getProgress(
parseInt(userId),
storyId ? parseInt(storyId) : null
);
res.json({ code: 0, data: progress });
} catch (err) {
console.error('获取进度失败:', err);
res.status(500).json({ code: 500, message: '获取进度失败' });
}
});
// 保存用户进度
router.post('/progress', async (req, res) => {
try {
const { userId, storyId, currentNodeKey, isCompleted, endingReached } = req.body;
await UserModel.saveProgress(parseInt(userId), parseInt(storyId), {
currentNodeKey,
isCompleted,
endingReached
});
res.json({ code: 0, message: '保存成功' });
} catch (err) {
console.error('保存进度失败:', err);
res.status(500).json({ code: 500, message: '保存进度失败' });
}
});
// 点赞操作
router.post('/like', async (req, res) => {
try {
const { userId, storyId, isLiked } = req.body;
await UserModel.toggleLike(parseInt(userId), parseInt(storyId), isLiked);
res.json({ code: 0, message: isLiked ? '点赞成功' : '取消点赞' });
} catch (err) {
console.error('点赞操作失败:', err);
res.status(500).json({ code: 500, message: '点赞操作失败' });
}
});
// 收藏操作
router.post('/collect', async (req, res) => {
try {
const { userId, storyId, isCollected } = req.body;
await UserModel.toggleCollect(parseInt(userId), parseInt(storyId), isCollected);
res.json({ code: 0, message: isCollected ? '收藏成功' : '取消收藏' });
} catch (err) {
console.error('收藏操作失败:', err);
res.status(500).json({ code: 500, message: '收藏操作失败' });
}
});
// 获取收藏列表
router.get('/collections', async (req, res) => {
try {
const { userId } = req.query;
const collections = await UserModel.getCollections(parseInt(userId));
res.json({ code: 0, data: collections });
} catch (err) {
console.error('获取收藏列表失败:', err);
res.status(500).json({ code: 500, message: '获取收藏列表失败' });
}
});
// 获取已解锁结局
router.get('/endings', async (req, res) => {
try {
const { userId, storyId } = req.query;
const endings = await UserModel.getUnlockedEndings(
parseInt(userId),
storyId ? parseInt(storyId) : null
);
res.json({ code: 0, data: endings });
} catch (err) {
console.error('获取结局列表失败:', err);
res.status(500).json({ code: 500, message: '获取结局列表失败' });
}
});
// 获取我的作品
router.get('/my-works', async (req, res) => {
try {
const { userId } = req.query;
const works = await UserModel.getMyWorks(parseInt(userId));
res.json({ code: 0, data: works });
} catch (err) {
console.error('获取作品失败:', err);
res.status(500).json({ code: 500, message: '获取作品失败' });
}
});
// 获取草稿箱
router.get('/drafts', async (req, res) => {
try {
const { userId } = req.query;
const drafts = await UserModel.getDrafts(parseInt(userId));
res.json({ code: 0, data: drafts });
} catch (err) {
console.error('获取草稿失败:', err);
res.status(500).json({ code: 500, message: '获取草稿失败' });
}
});
// 获取最近游玩
router.get('/recent-played', async (req, res) => {
try {
const { userId, limit } = req.query;
const recent = await UserModel.getRecentPlayed(parseInt(userId), parseInt(limit) || 10);
res.json({ code: 0, data: recent });
} catch (err) {
console.error('获取最近游玩失败:', err);
res.status(500).json({ code: 500, message: '获取最近游玩失败' });
}
});
// 获取AI创作历史
router.get('/ai-history', async (req, res) => {
try {
const { userId, limit } = req.query;
const history = await UserModel.getAIHistory(parseInt(userId), parseInt(limit) || 20);
res.json({ code: 0, data: history });
} catch (err) {
console.error('获取AI历史失败:', err);
res.status(500).json({ code: 500, message: '获取AI历史失败' });
}
});
// 获取AI配额
router.get('/ai-quota', async (req, res) => {
try {
const { userId } = req.query;
const quota = await UserModel.getAIQuota(parseInt(userId));
res.json({ code: 0, data: quota });
} catch (err) {
console.error('获取AI配额失败:', err);
res.status(500).json({ code: 500, message: '获取AI配额失败' });
}
});
module.exports = router;