share.service.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.shareService = exports.ShareService = void 0;
  4. const models_1 = require("../../models");
  5. /**
  6. * 分享服务
  7. * 生成分享链接和二维码
  8. */
  9. class ShareService {
  10. baseUrl;
  11. constructor() {
  12. // 从环境变量获取基础 URL,默认使用当前域名
  13. this.baseUrl = process.env.APP_URL || 'http://localhost:3000';
  14. }
  15. /**
  16. * 生成书籍章节分享链接
  17. * @param chapterId 章节 ID
  18. */
  19. generateShareLink(chapterId) {
  20. return `${this.baseUrl}/#/pages/player/index?id=${chapterId}`;
  21. }
  22. /**
  23. * 生成分享卡片数据
  24. * @param chapterId 章节 ID
  25. */
  26. async generateShareCard(chapterId) {
  27. const chapter = await models_1.prisma.bookChapter.findUnique({
  28. where: { id: parseInt(chapterId) },
  29. include: { book: true },
  30. });
  31. if (!chapter) {
  32. throw new Error('章节不存在');
  33. }
  34. // 生成更完整的描述
  35. let description = '';
  36. if (chapter.summary) {
  37. description = chapter.summary;
  38. }
  39. else if (chapter.content && chapter.content.length > 100) {
  40. description = chapter.content.slice(0, 100) + '...';
  41. }
  42. else if (chapter.content) {
  43. description = chapter.content;
  44. }
  45. else {
  46. description = 'AI 有声书精彩内容';
  47. }
  48. return {
  49. title: `${chapter.book?.title || 'AI 有声书'} - 第${chapter.number}章 ${chapter.title}`,
  50. description: description,
  51. imageUrl: this.generateCoverImage(chapter),
  52. link: this.generateShareLink(chapterId),
  53. chapterId: chapter.id,
  54. wordCount: chapter.wordCount,
  55. audioDuration: chapter.audioDuration,
  56. };
  57. }
  58. /**
  59. * 生成封面图片
  60. */
  61. generateCoverImage(chapter) {
  62. // 实际项目中可以生成真实的图片
  63. return {
  64. type: 'gradient',
  65. colors: ['#4f46e5', '#818cf8'],
  66. icon: '🎵',
  67. };
  68. }
  69. /**
  70. * 生成二维码数据
  71. * @param chapterId 章节 ID
  72. */
  73. generateQRCodeData(chapterId) {
  74. return this.generateShareLink(chapterId);
  75. }
  76. /**
  77. * 记录分享行为
  78. */
  79. async trackShare(chapterId, userId, platform) {
  80. console.log(`📤 用户 ${userId} 分享了章节 ${chapterId} 到 ${platform}`);
  81. }
  82. }
  83. exports.ShareService = ShareService;
  84. // 导出单例
  85. exports.shareService = new ShareService();