const express = require('express'); const banner = require('../../db/models/banner'); const order = require('../../db/models/order'); const course = require('../../db/models/course'); const chapter = require('../../db/models/chapter'); const teacher = require('../../db/models/teacher'); const { COURSE_TYPE, ORDER_STATUS, COURSE_STATUS } = require('../config'); const { Op } = require('sequelize'); const { getAuthUrl } = require('../../utils/qiniu'); const router = express.Router(); function m3u8List(data) { const { video } = data; const link = video.slice(0, video.lastIndexOf('.')).replace('static.fuxi', 'video.fuxi'); data.m3u8 = [ { name: '超清', definition: '1080p', url: getAuthUrl(`${link}/1080/index.m3u8?pm3u8/0`), }, { name: '高清', definition: '720p', url: getAuthUrl(`${link}/720/index.m3u8?pm3u8/0`), }, { name: '标清', definition: '480p', url: getAuthUrl(`${link}/480/index.m3u8?pm3u8/0`), }, ]; } /* GET home page. */ router.get('/:id', async (req, res) => { const { id } = req.params; const cdata = await course.findOne({ where: { id, status: COURSE_STATUS.已上线 } }); if (!cdata) { return res.renderEx('error', { title: '发生错误了' }); } cdata.payHref = `/pay/${id}`; const otherdata = await course.findAll({ where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 }, attributes: ['id', 'detailimg', 'title', 'price'], }); const tdata = await Promise.all( cdata.teacherId.map(item => { return teacher.findOne({ where: { id: item } }); }) ); const option = { title: '课程详情页', course: cdata, otherCourse: otherdata, teacher: tdata, }; // 未登陆 if (!req.uid) { return res.renderEx('detail', option); } const odata = await order.findOne({ where: { uid: req.uid, cid: id, status: { [Op.in]: [ORDER_STATUS.已支付, ORDER_STATUS.渠道] } }, }); // 未购买 if (!odata) { return res.renderEx('detail', option); } const chdata = await chapter.findAll({ where: { cid: id, status: 1 }, }); if (chdata.length) { [option.curChapter] = chdata; option.curChapter.current = true; m3u8List(option.curChapter); } option.chapter = chdata; res.renderEx('video', option); }); router.get('/:id/:chapterId', async (req, res) => { const { id, chapterId } = req.params; // 未登陆 if (!req.uid) { return res.redirect(`/course/${id}`); } const odata = await order.findOne({ where: { uid: req.uid, cid: id, status: { [Op.in]: [ORDER_STATUS.已支付, ORDER_STATUS.渠道] } }, }); if (!odata) { return res.redirect(`/course/${id}`); } const cdata = await course.findOne({ where: { id } }); if (!cdata) { return res.renderEx('error', { title: '发生错误了' }); } const otherdata = await course.findAll({ where: { type: cdata.type, id: { [Op.not]: id }, status: COURSE_STATUS.已上线 }, attributes: ['id', 'detailimg', 'title', 'price'], }); const tdata = await Promise.all( cdata.teacherId.map(item => { return teacher.findOne({ where: { id: item } }); }) ); const chdata = await chapter.findAll({ where: { cid: id, status: 1 }, }); const option = { title: '课程详情页', course: cdata, otherCourse: otherdata, teacher: tdata, chapter: chdata, curChapter: null, }; for (const i in chdata) { // eslint-disable-next-line eqeqeq if (chdata[i].id == chapterId) { chdata[i].current = true; option.curChapter = chdata[i]; m3u8List(option.curChapter); break; } } if (!option.curChapter) { return res.renderEx('error', { title: '章节不存在' }); } res.renderEx('video', option); }); module.exports = router;