audio.controller.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 AudioService = __importStar(require("./audio.service"));
  41. const errorHandler_1 = require("../../middleware/errorHandler");
  42. const auth_1 = require("../../middleware/auth");
  43. const router = new router_1.default();
  44. // 获取音频列表
  45. router.get('/list', auth_1.optionalAuth, async (ctx) => {
  46. const userId = ctx.state.user?.userId;
  47. const { page = 1, pageSize = 10, isFavorite, keyword, category } = ctx.query;
  48. const result = await AudioService.getAudioList(userId || '', {
  49. page: Number(page) || 1,
  50. pageSize: Number(pageSize) || 10,
  51. isFavorite: isFavorite === 'true' ? true : isFavorite === 'false' ? false : undefined,
  52. keyword,
  53. category,
  54. });
  55. ctx.body = {
  56. code: 0,
  57. message: 'success',
  58. data: result,
  59. };
  60. });
  61. // 获取收藏列表
  62. router.get('/favorites', auth_1.optionalAuth, async (ctx) => {
  63. const userId = ctx.state.user?.userId;
  64. const { page = 1, pageSize = 20 } = ctx.query;
  65. const result = await AudioService.getAudioList(userId || '', {
  66. page: Number(page) || 1,
  67. pageSize: Number(pageSize) || 20,
  68. isFavorite: true,
  69. });
  70. ctx.body = {
  71. code: 0,
  72. message: 'success',
  73. data: result,
  74. };
  75. });
  76. // 获取单个音频详情
  77. router.get('/:id', auth_1.optionalAuth, async (ctx) => {
  78. const userId = ctx.state.user?.userId;
  79. const { id } = ctx.params;
  80. const audio = await AudioService.getAudioById(id, userId);
  81. if (!audio) {
  82. throw new errorHandler_1.NotFoundError('音频不存在');
  83. }
  84. ctx.body = {
  85. code: 0,
  86. message: 'success',
  87. data: audio,
  88. };
  89. });
  90. // 删除音频
  91. router.delete('/:id', auth_1.optionalAuth, async (ctx) => {
  92. const userId = ctx.state.user?.userId;
  93. const { id } = ctx.params;
  94. const success = await AudioService.deleteAudio(id, userId);
  95. if (!success) {
  96. throw new errorHandler_1.NotFoundError('音频不存在或无权删除');
  97. }
  98. ctx.body = {
  99. code: 0,
  100. message: '删除成功',
  101. data: null,
  102. };
  103. });
  104. // 切换收藏状态
  105. router.put('/:id/favorite', auth_1.optionalAuth, async (ctx) => {
  106. const userId = ctx.state.user?.userId;
  107. const { id } = ctx.params;
  108. const audio = await AudioService.toggleFavorite(id, userId);
  109. if (!audio) {
  110. throw new errorHandler_1.NotFoundError('音频不存在');
  111. }
  112. ctx.body = {
  113. code: 0,
  114. message: 'success',
  115. data: { isFavorite: audio.isFavorite },
  116. };
  117. });
  118. // 更新音频信息
  119. router.put('/:id', auth_1.optionalAuth, async (ctx) => {
  120. const userId = ctx.state.user?.userId;
  121. const { id } = ctx.params;
  122. const { title, tags } = ctx.request.body;
  123. const audio = await AudioService.updateAudio(id, userId, { title, tags });
  124. if (!audio) {
  125. throw new errorHandler_1.NotFoundError('音频不存在');
  126. }
  127. ctx.body = {
  128. code: 0,
  129. message: '更新成功',
  130. data: audio,
  131. };
  132. });
  133. // 获取用户统计
  134. router.get('/stats/overview', auth_1.optionalAuth, async (ctx) => {
  135. const userId = ctx.state.user?.userId;
  136. const stats = await AudioService.getUserStats(userId);
  137. ctx.body = {
  138. code: 0,
  139. message: 'success',
  140. data: stats,
  141. };
  142. });
  143. exports.default = router;