course.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const express = require('express');
  2. const banner = require('../../db/models/banner');
  3. const order = require('../../db/models/order');
  4. const course = require('../../db/models/course');
  5. const chapter = require('../../db/models/chapter');
  6. const teacher = require('../../db/models/teacher');
  7. const { COURSE_TYPE, ORDER_STATUS, COURSE_STATUS } = require('../config');
  8. const { Op } = require('sequelize');
  9. const { getAuthUrl } = require('../../utils/qiniu');
  10. const router = express.Router();
  11. function m3u8List(data) {
  12. const { video } = data;
  13. const link = video.slice(0, video.lastIndexOf('.')).replace('static.fuxi', 'video.fuxi');
  14. data.m3u8 = [
  15. {
  16. name: '超清',
  17. definition: '1080p',
  18. url: getAuthUrl(`${link}/1080/index.m3u8?pm3u8/0`),
  19. },
  20. {
  21. name: '高清',
  22. definition: '720p',
  23. url: getAuthUrl(`${link}/720/index.m3u8?pm3u8/0`),
  24. },
  25. {
  26. name: '标清',
  27. definition: '480p',
  28. url: getAuthUrl(`${link}/480/index.m3u8?pm3u8/0`),
  29. },
  30. ];
  31. }
  32. /* GET home page. */
  33. router.get('/:id', async (req, res) => {
  34. const { id } = req.params;
  35. const cdata = await course.findOne({ where: { id, status: COURSE_STATUS.已上线 } });
  36. if (!cdata) {
  37. return res.renderEx('error', { title: '发生错误了' });
  38. }
  39. cdata.payHref = `/pay/${id}`;
  40. const otherdata = await course.findAll({
  41. where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 },
  42. attributes: ['id', 'detailimg', 'title', 'price'],
  43. });
  44. const tdata = await Promise.all(
  45. cdata.teacherId.map(item => {
  46. return teacher.findOne({ where: { id: item } });
  47. })
  48. );
  49. const option = {
  50. title: '课程详情页',
  51. course: cdata,
  52. otherCourse: otherdata,
  53. teacher: tdata,
  54. };
  55. // 未登陆
  56. if (!req.uid) {
  57. return res.renderEx('detail', option);
  58. }
  59. const odata = await order.findOne({
  60. where: { uid: req.uid, cid: id, status: { [Op.in]: [ORDER_STATUS.已支付, ORDER_STATUS.渠道] } },
  61. });
  62. // 未购买
  63. if (!odata) {
  64. return res.renderEx('detail', option);
  65. }
  66. const chdata = await chapter.findAll({
  67. where: { cid: id, status: 1 },
  68. });
  69. if (chdata.length) {
  70. [option.curChapter] = chdata;
  71. option.curChapter.current = true;
  72. m3u8List(option.curChapter);
  73. }
  74. option.chapter = chdata;
  75. res.renderEx('video', option);
  76. });
  77. router.get('/:id/:chapterId', async (req, res) => {
  78. const { id, chapterId } = req.params;
  79. // 未登陆
  80. if (!req.uid) {
  81. return res.redirect(`/course/${id}`);
  82. }
  83. const odata = await order.findOne({
  84. where: { uid: req.uid, cid: id, status: { [Op.in]: [ORDER_STATUS.已支付, ORDER_STATUS.渠道] } },
  85. });
  86. if (!odata) {
  87. return res.redirect(`/course/${id}`);
  88. }
  89. const cdata = await course.findOne({ where: { id } });
  90. if (!cdata) {
  91. return res.renderEx('error', { title: '发生错误了' });
  92. }
  93. const otherdata = await course.findAll({
  94. where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 },
  95. attributes: ['id', 'detailimg', 'title', 'price'],
  96. });
  97. const tdata = await Promise.all(
  98. cdata.teacherId.map(item => {
  99. return teacher.findOne({ where: { id: item } });
  100. })
  101. );
  102. const chdata = await chapter.findAll({
  103. where: { cid: id, status: 1 },
  104. });
  105. const option = {
  106. title: '课程详情页',
  107. course: cdata,
  108. otherCourse: otherdata,
  109. teacher: tdata,
  110. chapter: chdata,
  111. curChapter: null,
  112. };
  113. for (const i in chdata) {
  114. // eslint-disable-next-line eqeqeq
  115. if (chdata[i].id == chapterId) {
  116. chdata[i].current = true;
  117. option.curChapter = chdata[i];
  118. m3u8List(option.curChapter);
  119. break;
  120. }
  121. }
  122. if (!option.curChapter) {
  123. return res.renderEx('error', { title: '章节不存在' });
  124. }
  125. res.renderEx('video', option);
  126. });
  127. module.exports = router;