player.controller.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. var desc = Object.getOwnPropertyDescriptor(m, k);
  5. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  6. desc = { enumerable: true, get: function() { return m[k]; } };
  7. }
  8. Object.defineProperty(o, k2, desc);
  9. }) : (function(o, m, k, k2) {
  10. if (k2 === undefined) k2 = k;
  11. o[k2] = m[k];
  12. }));
  13. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  14. Object.defineProperty(o, "default", { enumerable: true, value: v });
  15. }) : function(o, v) {
  16. o["default"] = v;
  17. });
  18. var __importStar = (this && this.__importStar) || (function () {
  19. var ownKeys = function(o) {
  20. ownKeys = Object.getOwnPropertyNames || function (o) {
  21. var ar = [];
  22. for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
  23. return ar;
  24. };
  25. return ownKeys(o);
  26. };
  27. return function (mod) {
  28. if (mod && mod.__esModule) return mod;
  29. var result = {};
  30. if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
  31. __setModuleDefault(result, mod);
  32. return result;
  33. };
  34. })();
  35. var __importDefault = (this && this.__importDefault) || function (mod) {
  36. return (mod && mod.__esModule) ? mod : { "default": mod };
  37. };
  38. Object.defineProperty(exports, "__esModule", { value: true });
  39. const router_1 = __importDefault(require("@koa/router"));
  40. const PlayerService = __importStar(require("./player.service"));
  41. const errorHandler_1 = require("../../middleware/errorHandler");
  42. const auth_1 = require("../../middleware/auth");
  43. const models_1 = require("../../models");
  44. // 测试用户ID(开发环境使用)
  45. const TEST_USER_ID = '1';
  46. const router = new router_1.default();
  47. // 获取播放进度列表
  48. router.get('/progress', auth_1.optionalAuth, async (ctx) => {
  49. // 开发环境使用测试用户ID
  50. const userId = ctx.state.user?.userId || TEST_USER_ID;
  51. const { audioId } = ctx.query;
  52. const records = await PlayerService.getPlayProgress(userId, audioId ? parseInt(audioId) : undefined);
  53. ctx.body = {
  54. code: 0,
  55. message: 'success',
  56. data: records,
  57. };
  58. });
  59. // 保存播放进度
  60. router.post('/progress', auth_1.optionalAuth, async (ctx) => {
  61. // 开发环境使用测试用户ID
  62. const userId = ctx.state.user?.userId || TEST_USER_ID;
  63. const body = ctx.request.body;
  64. const { audioId, progress, duration } = body;
  65. if (!audioId || typeof progress !== 'number' || typeof duration !== 'number') {
  66. throw new errorHandler_1.BadRequestError('参数错误');
  67. }
  68. const record = await PlayerService.savePlayProgress(userId, audioId, progress, duration);
  69. ctx.body = {
  70. code: 0,
  71. message: 'success',
  72. data: record,
  73. };
  74. });
  75. // 更新播放进度 (在 DELETE 路由之前定义)
  76. router.put('/progress/:audioId', auth_1.optionalAuth, async (ctx) => {
  77. // 开发环境使用测试用户ID
  78. const userId = ctx.state.user?.userId || TEST_USER_ID;
  79. const audioId = parseInt(ctx.params.audioId);
  80. const body = ctx.request.body;
  81. const { progress, duration } = body;
  82. if (typeof progress !== 'number') {
  83. throw new errorHandler_1.BadRequestError('进度参数错误');
  84. }
  85. const record = await PlayerService.updatePlayProgress(userId, audioId, progress, duration);
  86. ctx.body = {
  87. code: 0,
  88. message: 'success',
  89. data: record,
  90. };
  91. });
  92. // 删除播放记录
  93. router.delete('/progress/:audioId', auth_1.optionalAuth, async (ctx) => {
  94. // 开发环境使用测试用户ID
  95. const userId = ctx.state.user?.userId || TEST_USER_ID;
  96. const audioId = parseInt(ctx.params.audioId);
  97. await PlayerService.deletePlayRecord(userId, audioId);
  98. ctx.body = {
  99. code: 0,
  100. message: '删除成功',
  101. };
  102. });
  103. // ============ 临时 API:为播放器页面适配书籍章节音频 ============
  104. // 获取播放列表(适配旧播放器)- 必须放在 /:id 之前
  105. router.get('/audio/list', async (ctx) => {
  106. const { page = '1', pageSize = '100' } = ctx.query;
  107. // 获取所有有音频的章节
  108. const chapters = await models_1.prisma.bookChapter.findMany({
  109. where: {
  110. audioUrl: {
  111. not: null,
  112. },
  113. AND: [
  114. { audioUrl: { not: '' } }
  115. ]
  116. },
  117. include: { book: true },
  118. orderBy: [
  119. { bookId: 'asc' },
  120. { number: 'asc' },
  121. ],
  122. skip: (parseInt(page) - 1) * parseInt(pageSize),
  123. take: parseInt(pageSize),
  124. });
  125. const total = await models_1.prisma.bookChapter.count({
  126. where: {
  127. audioUrl: {
  128. not: null,
  129. },
  130. AND: [
  131. { audioUrl: { not: '' } }
  132. ]
  133. },
  134. });
  135. const list = chapters.map((chapter) => ({
  136. id: chapter.id,
  137. _id: chapter.id,
  138. title: chapter.title,
  139. summary: chapter.summary || '',
  140. text: chapter.content || '',
  141. audioUrl: chapter.audioUrl || '',
  142. audioDuration: chapter.audioDuration || 0,
  143. wordCount: chapter.wordCount || 0,
  144. albumId: chapter.bookId,
  145. albumName: chapter.book?.title || '默认专辑',
  146. isFavorite: false,
  147. }));
  148. ctx.body = {
  149. code: 0,
  150. message: 'success',
  151. data: { list, total, page: parseInt(page), pageSize: parseInt(pageSize) },
  152. };
  153. });
  154. // 获取章节音频详情(适配旧播放器)
  155. router.get('/audio/:id', async (ctx) => {
  156. const chapterId = parseInt(ctx.params.id);
  157. if (isNaN(chapterId)) {
  158. ctx.status = 400;
  159. ctx.body = { code: 1, message: '无效的音频ID' };
  160. return;
  161. }
  162. const chapter = await models_1.prisma.bookChapter.findUnique({
  163. where: { id: chapterId },
  164. include: { book: true },
  165. });
  166. if (!chapter) {
  167. ctx.status = 404;
  168. ctx.body = { code: 1, message: '音频不存在' };
  169. return;
  170. }
  171. // 适配旧的 AudioItem 格式
  172. const audioItem = {
  173. id: chapter.id,
  174. _id: chapter.id,
  175. title: chapter.title,
  176. summary: chapter.summary || '',
  177. text: chapter.content || '',
  178. audioUrl: chapter.audioUrl || '',
  179. audioDuration: chapter.audioDuration || 0,
  180. wordCount: chapter.wordCount || 0,
  181. albumId: chapter.bookId,
  182. albumName: chapter.book?.title || '默认专辑',
  183. isFavorite: false,
  184. };
  185. ctx.body = {
  186. code: 0,
  187. message: 'success',
  188. data: audioItem,
  189. };
  190. });
  191. exports.default = router;