课程详情页显示
课程详情页 views/Info.vue,代码:
<template>
<div class="detail">
<Header/>
<div class="main">
<div class="course-info">
<div class="wrap-left">
<!-- 课程封面或封面商品 -->
</div>
<div class="wrap-right">
<h3 class="course-name">Linux系统基础5周入门精讲</h3>
<p class="data">23475人在学 课程总时长:148课时/180小时 难度:初级</p>
<div class="sale-time">
<p class="sale-type">限时免费</p>
<p class="expire">距离结束:仅剩 01天 04小时 33分 <span class="second">08</span> 秒</p>
</div>
<p class="course-price">
<span>活动价</span>
<span class="discount">¥0.00</span>
<span class="original">¥29.00</span>
</p>
<div class="buy">
<div class="buy-btn">
<button class="buy-now">立即购买</button>
<button class="free">免费试学</button>
</div>
<div class="add-cart"><img src="../assets/cart-yellow.svg" alt="">加入购物车</div>
</div>
</div>
</div>
<div class="course-tab">
<ul class="tab-list">
<li :class="state.tabIndex===1?'active':''" @click="state.tabIndex=1">详情介绍</li>
<li :class="state.tabIndex===2?'active':''" @click="state.tabIndex=2">课程章节 <span :class="state.tabIndex!==2?'free':''">(试学)</span></li>
<li :class="state.tabIndex===3?'active':''" @click="state.tabIndex=3">用户评论 (42)</li>
<li :class="state.tabIndex===4?'active':''" @click="state.tabIndex=4">常见问题</li>
</ul>
</div>
<div class="course-content">
<div class="course-tab-list">
<div class="tab-item" v-if="state.tabIndex===1">
<p><img alt="" src="//hcdn2.luffycity.com/media/frontend/activity/详情页_01.png"></p>
</div>
<div class="tab-item" v-if="state.tabIndex===2">
<div class="tab-item-title">
<p class="chapter">课程章节</p>
<p class="chapter-length">共11章 147个课时</p>
</div>
<div class="chapter-item">
<p class="chapter-title"><img src="../assets/1.svg" alt="">第1章·Linux硬件基础</p>
<ul class="lesson-list">
<li class="lesson-item">
<p class="name"><span class="index">1-1</span> 课程介绍-学习流程<span class="free">免费</span></p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即试学</button>
</li>
<li class="lesson-item">
<p class="name"><span class="index">1-2</span> 服务器硬件-详解<span class="free">免费</span></p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即试学</button>
</li>
</ul>
</div>
<div class="chapter-item">
<p class="chapter-title"><img src="../assets/1.svg" alt="">第2章·Linux发展过程</p>
<ul class="lesson-list">
<li class="lesson-item">
<p class="name"><span class="index">2-1</span> 操作系统组成-Linux发展过程</p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即购买</button>
</li>
<li class="lesson-item">
<p class="name"><span class="index">2-2</span> 自由软件-GNU-GPL核心讲解</p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即购买</button>
</li>
</ul>
</div>
</div>
<div class="tab-item" v-if="state.tabIndex===3">
用户评论
</div>
<div class="tab-item" v-if="state.tabIndex===4">
常见问题
</div>
</div>
<div class="course-side">
<div class="teacher-info">
<h4 class="side-title"><span>授课老师</span></h4>
<div class="teacher-content">
<div class="cont1">
<img src="../assets/avatar.jpg">
<div class="name">
<p class="teacher-name">Avrion</p>
<p class="teacher-title">老男孩LInux学科带头人</p>
</div>
</div>
<p class="narrative" >路飞学城高级讲师,曾参与新加坡南洋理工大学大数据医疗相关项目,就职过多家互联网企业,有着多年开发经验,精通java,python,go等编程语言</p>
</div>
</div>
</div>
</div>
</div>
<Footer/>
</div>
</template>
<script setup>
import {reactive,ref,watch} from "vue"
import {useRoute} from "vue-router"
import Header from "../components/Header.vue"
import Footer from "../components/Footer.vue"
let route = useRoute()
const state = reactive({
course_id: route.params.id,
tabIndex: 2,
})
</script>
<style scoped>
.main{
background: #fff;
padding-top: 30px;
}
.course-info{
width: 1200px;
margin: 0 auto;
overflow: hidden;
}
.wrap-left{
float: left;
width: 690px;
height: 388px;
background-color: #000;
}
.wrap-right{
float: left;
position: relative;
height: 388px;
}
.course-name{
font-size: 20px;
color: #333;
padding: 10px 23px;
letter-spacing: .45px;
}
.data{
padding-left: 23px;
padding-right: 23px;
padding-bottom: 16px;
font-size: 14px;
color: #9b9b9b;
}
.sale-time{
width: 464px;
background: #fa6240;
font-size: 14px;
color: #4a4a4a;
padding: 10px 23px;
overflow: hidden;
}
.sale-type {
font-size: 16px;
color: #fff;
letter-spacing: .36px;
float: left;
}
.sale-time .expire{
font-size: 14px;
color: #fff;
float: right;
}
.sale-time .expire .second{
width: 24px;
display: inline-block;
background: #fafafa;
color: #5e5e5e;
padding: 6px 0;
text-align: center;
}
.course-price{
background: #fff;
font-size: 14px;
color: #4a4a4a;
padding: 5px 23px;
}
.discount{
font-size: 26px;
color: #fa6240;
margin-left: 10px;
display: inline-block;
margin-bottom: -5px;
}
.original{
font-size: 14px;
color: #9b9b9b;
margin-left: 10px;
text-decoration: line-through;
}
.buy{
width: 464px;
padding: 0px 23px;
position: absolute;
left: 0;
bottom: 20px;
overflow: hidden;
}
.buy .buy-btn{
float: left;
}
.buy .buy-now{
width: 125px;
height: 40px;
border: 0;
background: #ffc210;
border-radius: 4px;
color: #fff;
cursor: pointer;
margin-right: 15px;
outline: none;
}
.buy .free{
width: 125px;
height: 40px;
border-radius: 4px;
cursor: pointer;
margin-right: 15px;
background: #fff;
color: #ffc210;
border: 1px solid #ffc210;
}
.add-cart{
float: right;
font-size: 14px;
color: #ffc210;
text-align: center;
cursor: pointer;
margin-top: 10px;
}
.add-cart img{
width: 20px;
height: 18px;
margin-right: 7px;
vertical-align: middle;
}
.course-tab{
width: 100%;
background: #fff;
margin-bottom: 30px;
box-shadow: 0 2px 4px 0 #f0f0f0;
}
.course-tab .tab-list{
width: 1200px;
margin: auto;
color: #4a4a4a;
overflow: hidden;
}
.tab-list li{
float: left;
margin-right: 15px;
padding: 26px 20px 16px;
font-size: 17px;
cursor: pointer;
}
.tab-list .active{
color: #ffc210;
border-bottom: 2px solid #ffc210;
}
.tab-list .free{
color: #fb7c55;
}
.course-content{
width: 1200px;
margin: 0 auto;
background: #FAFAFA;
overflow: hidden;
padding-bottom: 40px;
}
.course-tab-list{
width: 880px;
height: auto;
padding: 20px;
background: #fff;
float: left;
box-sizing: border-box;
overflow: hidden;
position: relative;
box-shadow: 0 2px 4px 0 #f0f0f0;
}
.tab-item{
width: 880px;
background: #fff;
padding-bottom: 20px;
box-shadow: 0 2px 4px 0 #f0f0f0;
}
.tab-item-title{
justify-content: space-between;
padding: 25px 20px 11px;
border-radius: 4px;
margin-bottom: 20px;
border-bottom: 1px solid #333;
border-bottom-color: rgba(51,51,51,.05);
overflow: hidden;
}
.chapter{
font-size: 17px;
color: #4a4a4a;
float: left;
}
.chapter-length{
float: right;
font-size: 14px;
color: #9b9b9b;
letter-spacing: .19px;
}
.chapter-title{
font-size: 16px;
color: #4a4a4a;
letter-spacing: .26px;
padding: 12px;
background: #eee;
border-radius: 2px;
display: -ms-flexbox;
display: flex;
-ms-flex-align: center;
align-items: center;
}
.chapter-title img{
width: 18px;
height: 18px;
margin-right: 7px;
vertical-align: middle;
}
.lesson-list{
padding:0 20px;
}
.lesson-list .lesson-item{
padding: 15px 20px 15px 36px;
cursor: pointer;
justify-content: space-between;
position: relative;
overflow: hidden;
}
.lesson-item .name{
font-size: 14px;
color: #666;
float: left;
}
.lesson-item .index{
margin-right: 5px;
}
.lesson-item .free{
font-size: 12px;
color: #fff;
letter-spacing: .19px;
background: #ffc210;
border-radius: 100px;
padding: 1px 9px;
margin-left: 10px;
}
.lesson-item .time{
font-size: 14px;
color: #666;
letter-spacing: .23px;
opacity: 1;
transition: all .15s ease-in-out;
float: right;
}
.lesson-item .time img{
width: 18px;
height: 18px;
margin-left: 15px;
vertical-align: text-bottom;
}
.lesson-item .try{
width: 86px;
height: 28px;
background: #ffc210;
border-radius: 4px;
font-size: 14px;
color: #fff;
position: absolute;
right: 20px;
top: 10px;
opacity: 0;
transition: all .2s ease-in-out;
cursor: pointer;
outline: none;
border: none;
}
.lesson-item:hover{
background: #fcf7ef;
box-shadow: 0 0 0 0 #f3f3f3;
}
.lesson-item:hover .name{
color: #333;
}
.lesson-item:hover .try{
opacity: 1;
}
.course-side{
width: 300px;
height: auto;
margin-left: 20px;
float: right;
}
.teacher-info{
background: #fff;
margin-bottom: 20px;
box-shadow: 0 2px 4px 0 #f0f0f0;
}
.side-title{
font-weight: normal;
font-size: 17px;
color: #4a4a4a;
padding: 18px 14px;
border-bottom: 1px solid #333;
border-bottom-color: rgba(51,51,51,.05);
}
.side-title span{
display: inline-block;
border-left: 2px solid #ffc210;
padding-left: 12px;
}
.teacher-content{
padding: 30px 20px;
box-sizing: border-box;
}
.teacher-content .cont1{
margin-bottom: 12px;
overflow: hidden;
}
.teacher-content .cont1 img{
width: 54px;
height: 54px;
margin-right: 12px;
float: left;
}
.teacher-content .cont1 .name{
float: right;
}
.teacher-content .cont1 .teacher-name{
width: 188px;
font-size: 16px;
color: #4a4a4a;
padding-bottom: 4px;
}
.teacher-content .cont1 .teacher-title{
width: 188px;
font-size: 13px;
color: #9b9b9b;
white-space: nowrap;
}
.teacher-content .narrative{
font-size: 14px;
color: #666;
line-height: 24px;
}
</style>
路由显示,router/index.js
,代码:
import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import store from "../store";
// 路由列表
const routes = [
{
meta:{
title: "luffy2.0-站点首页",
keepAlive: true
},
path: '/', // uri访问地址
name: "Home",
component: ()=> import("../views/Home.vue")
},
{
meta:{
title: "luffy2.0-用户登录",
keepAlive: true
},
path:'/login', // uri访问地址
name: "Login",
component: ()=> import("../views/Login.vue")
},
{
meta:{
title: "luffy2.0-用户注册",
keepAlive: true
},
path: '/register',
name: "Register", // 路由名称
component: ()=> import("../views/Register.vue"), // uri绑定的组件页面
},
{
meta:{
title: "luffy2.0-个人中心",
keepAlive: true,
authorization: true,
},
path: '/user',
name: "User",
component: ()=> import("../views/User.vue"),
},
{
meta:{
title: "luffy2.0-课程列表",
keepAlive: true,
},
path: '/project',
name: "Course",
component: ()=> import("../views/Course.vue"),
},
{
meta:{
title: "luffy2.0-课程详情",
keepAlive: true
},
path: '/project/:id', // :id vue的路径参数,代表了课程的ID
name: "Info",
component: ()=> import("../views/Info.vue"),
}
]
// 路由对象实例化
const router = createRouter({
// history, 指定路由的模式
history: createWebHistory(),
// 路由列表
routes,
});
// 导航守卫
router.beforeEach((to, from, next)=>{
document.title=to.meta.title
// 登录状态验证
if (to.meta.authorization && !store.getters.getUserInfo) {
next({"name": "Login"})
}else{
next()
}
})
// 暴露路由对象
export default router
课程列表的组件中, 打通点击前往详情页的链接地址,views/Course.vue
,代码:
<ul class="course-list clearfix">
<li class="course-card" v-for="course_info in course.course_list">
<router-link :to="`/project/${course_info.id}`">
<div class="img"><img :src="course_info.course_cover" alt=""></div>
<p class="title ellipsis2">{{course_info.name}}</p>
<p class="one">
<span>{{course_info.get_level_display}} · {{course_info.students}}人报名</span>
<span class="discount r">
<i class="name" v-if="course_info.discount.type">{{course_info.discount.type}}</i>
<i class="countdown" v-if="course_info.discount.expire">{{parseInt(course_info.discount.expire/86400)}}<span class="day">天</span>{{fill0(parseInt(course_info.discount.expire/3600%24))}}:{{fill0(parseInt(course_info.discount.expire/60%60))}}:{{fill0(parseInt(course_info.discount.expire%60))}}</i>
</span>
</p>
<p class="two clearfix">
<span class="price l red bold" v-if="course_info.discount.price">¥{{parseFloat(course_info.discount.price).toFixed(2)}}</span>
<span class="price l red bold" v-else>¥{{parseFloat(course_info.price).toFixed(2)}}</span>
<span class="origin-price l delete-line" v-if="course_info.discount.price">¥{{parseFloat(course_info.price).toFixed(2)}}</span>
<span class="add-shop-cart r"><img class="icon imv2-shopping-cart" src="../assets/cart2.svg">加购物车</span>
</p>
</router-link>
</li>
</ul>
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 客户端展示课程详情页"
git push
视频播放器
针对客户端的课程详情页的左上角内容,我们可以显示课程的详情图片,如果有课程的介绍视频,也可以优先显示视频,
当然播放视频,肯定需要对应的播放器插件。市面上很多:百度云、腾讯云、网易云、阿里云、又拍云、七牛云或者其他第三方。
OK,接下来使用的播放器组件,选择使用了阿里云播放器(vue-alipayer视频播放组件),所以我们需要先预安装。
vue-alipayer地址:https://github.com/liho98/vue-aliplayer-v2
演示效果:https://player.alicdn.com/aliplayer/index.html
安装依赖
cd /home/moluo/Desktop/luffycity/luffycityweb/
yarn add vue-aliplayer-v3
Info.vue页面组件中调用播放器组件,代码:
<div class="wrap-left">
<!-- 课程封面或封面商品 -->
<AliPlayerV3
ref="player"
class="h-64 md:h-96 w-full rounded-lg"
style="height: 100%; width: 100%;"
:options="options"
@play="onPlay($event)"
@pause="onPause($event)"
@playing="onPlaying($event)"
/>
</div>
<script setup>
import {reactive,ref,watch} from "vue"
import {useRoute} from "vue-router"
import Header from "../components/Header.vue"
import Footer from "../components/Footer.vue"
import { AliPlayerV3 } from "vue-aliplayer-v3"
let route = useRoute()
let player = ref(null)
const state = reactive({
course_id: route.params.id,
tabIndex: 2,
})
const options = reactive({
source: "/src/assets/1.mp4",
cover: "/src/assets/course-1.png",
autoplay: false,
preload: true,
isLive: false, //切换为直播流的时候必填true
// format: 'm3u8' //切换为直播流的时候必填
})
const onPlay = (event)=>{
console.log("播放视频");
console.log(player.value.getCurrentTime());
}
const onPause = (event)=>{
console.log("暂停播放");
console.log(player.value.getCurrentTime());
}
const onPlaying = (event)=>{
console.log("播放中");
console.log(player.value.getCurrentTime());
}
</script>
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 客户端基于aliplayer播放器插件展示课程封面图片与视频"
git push
后端提供课程详情页数据接口
courses/serializers.py
,序列化器代码:
from .models import Teacher
class CourseTearchModelSerializer(serializers.ModelSerializer):
"""课程老师信息"""
class Meta:
model = Teacher
fields = ["id", "name", "avatar", "role", "get_role_display", "title", "signature", "brief"]
class CourseRetrieveModelSerializer(serializers.ModelSerializer):
"""课程详情的序列化器"""
diretion_name = serializers.CharField(source="diretion.name")
# diretion = serializers.SlugRelatedField(read_only=True, slug_field='name')
category_name = serializers.CharField(source="category.name")
# 序列化器嵌套
teacher = CourseTearchModelSerializer()
class Meta:
model = Course
fields = [
"name", "course_cover", "course_video", "level", "get_level_display",
"description", "pub_date", "status", "get_status_display", "students","discount",
"lessons", "pub_lessons", "price", "diretion", "diretion_name", "category", "category_name", "teacher"
]
视图代码:
from rest_framework.generics import RetrieveAPIView
from .models import Course
from .serializers import CourseRetrieveModelSerializer
class CourseRetrieveAPIView(RetrieveAPIView):
"""课程详情信息"""
queryset = Course.objects.filter(is_show=True, is_delete=False).all()
serializer_class = CourseRetrieveModelSerializer
路由代码:
urlpatterns = [
# 。。。。
re_path("^(?P<pk>\d+)/$", views.CourseRetrieveAPIView.as_view()),
]
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 服务端提供课程详情的api接口"
git push
客户端请求api接口并展示课程详情信息
src/api/course.js
,代码:
import http from "../utils/http";
import {reactive, ref} from "vue"
const course = reactive({
// 中间代码省略...
course_id: null, // 课程ID
info: { // 课程详情信息
teacher:{}, // 课程相关的老师信息
discount:{ // 课程相关的折扣信息
type: ""
}
},
tabIndex: 1, // 课程详情页中默认展示的课程信息的选项卡
// 中间代码省略...
// 获取课程详情
get_course(){
return http.get(`/courses/${this.course_id}`)
},
})
export default course;
views/Info.vue
,代码:
<template>
<div class="detail">
<Header/>
<div class="main">
<div class="course-info">
<div class="wrap-left">
<!-- 课程封面或封面商品 -->
<AliPlayerV3
ref="player"
class="h-64 md:h-96 w-full rounded-lg"
style="height: 100%; width: 100%;"
:source="course.info.course_video"
:cover="course.info.course_cover"
:options="options"
@play="onPlay($event)"
@pause="onPause($event)"
@playing="onPlaying($event)"
v-if="course.info.course_video"
/>
<img :src="course.info.course_cover" style="width: 100%;" alt="" v-else>
</div>
<div class="wrap-right">
<h3 class="course-name">{{course.info.name}}</h3>
<p class="data">{{course.info.students}}人在学 课程总时长:{{course.info.pub_lessons}}课时/{{course.info.lessons}}课时 难度:{{course.info.get_level_display}}</p>
<div class="sale-time" v-if="course.info.discount.type">
<p class="sale-type">{{course.info.discount.type}}</p>
<p class="expire" v-if="course.info.discount.expire>0">距离结束:仅剩 {{parseInt(course.info.discount.expire/86400)}}天 {{fill0(parseInt(course.info.discount.expire/3600%24))}}小时 {{fill0(parseInt(course.info.discount.expire/60%60))}}分 <span class="second">{{fill0(parseInt(course.info.discount.expire%60))}}</span> 秒</p>
</div>
<div class="sale-time" v-if="!course.info.discount.type">
<p class="sale-type">课程价格 ¥{{parseFloat(course.info.price).toFixed(2)}}</p>
</div>
<p class="course-price" v-if="course.info.discount.price">
<span>活动价</span>
<span class="discount">¥{{parseFloat(course.info.discount.price).toFixed(2)}}</span>
<span class="original">¥{{parseFloat(course.info.price).toFixed(2)}}</span>
</p>
<div class="buy">
<div class="buy-btn">
<button class="buy-now">立即购买</button>
<button class="free">免费试学</button>
</div>
<div class="add-cart"><img src="../assets/cart-yellow.svg" alt="">加入购物车</div>
</div>
</div>
</div>
<div class="course-tab">
<ul class="tab-list">
<li :class="course.tabIndex===1?'active':''" @click="course.tabIndex=1">详情介绍</li>
<li :class="course.tabIndex===2?'active':''" @click="course.tabIndex=2">课程章节 <span :class="course.tabIndex!==2?'free':''">(试学)</span></li>
<li :class="course.tabIndex===3?'active':''" @click="course.tabIndex=3">用户评论 (42)</li>
<li :class="course.tabIndex===4?'active':''" @click="course.tabIndex=4">常见问题</li>
</ul>
</div>
<div class="course-content">
<div class="course-tab-list">
<div class="tab-item" v-if="course.tabIndex===1" v-html="course.info.description">
</div>
<div class="tab-item" v-if="course.tabIndex===2">
<div class="tab-item-title">
<p class="chapter">课程章节</p>
<p class="chapter-length">共11章 147个课时</p>
</div>
<div class="chapter-item">
<p class="chapter-title"><img src="../assets/1.svg" alt="">第1章·Linux硬件基础</p>
<ul class="lesson-list">
<li class="lesson-item">
<p class="name"><span class="index">1-1</span> 课程介绍-学习流程<span class="free">免费</span></p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即试学</button>
</li>
<li class="lesson-item">
<p class="name"><span class="index">1-2</span> 服务器硬件-详解<span class="free">免费</span></p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即试学</button>
</li>
</ul>
</div>
<div class="chapter-item">
<p class="chapter-title"><img src="../assets/1.svg" alt="">第2章·Linux发展过程</p>
<ul class="lesson-list">
<li class="lesson-item">
<p class="name"><span class="index">2-1</span> 操作系统组成-Linux发展过程</p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即购买</button>
</li>
<li class="lesson-item">
<p class="name"><span class="index">2-2</span> 自由软件-GNU-GPL核心讲解</p>
<p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
<button class="try">立即购买</button>
</li>
</ul>
</div>
</div>
<div class="tab-item" v-if="course.tabIndex===3">
用户评论
</div>
<div class="tab-item" v-if="course.tabIndex===4">
常见问题
</div>
</div>
<div class="course-side">
<div class="teacher-info">
<h4 class="side-title"><span>授课老师</span></h4>
<div class="teacher-content">
<div class="cont1">
<img :src="course.info.teacher.avatar">
<div class="name">
<p class="teacher-name">{{course.info.teacher.name}}</p>
<p class="teacher-title">{{course.info.teacher.get_role_display}},{{course.info.teacher.title}}</p>
</div>
</div>
<div class="narrative" v-html="course.info.teacher.brief"></div>
</div>
</div>
</div>
</div>
</div>
<Footer/>
</div>
</template>
<script setup>
import {reactive,ref, watch} from "vue"
import {useRoute, useRouter} from "vue-router"
import Header from "../components/Header.vue"
import Footer from "../components/Footer.vue"
import { AliPlayerV3 } from "vue-aliplayer-v3"
import course from "../api/course"
import { ElMessage } from 'element-plus'
import {fill0} from "../utils/func";
let route = useRoute()
let router= useRouter()
let player = ref(null)
// 获取url地址栏上的课程ID
course.course_id = route.params.id;
// 简单判断课程ID是否合法
if(course.course_id > 0){
// 根据课程ID到服务端获取课程详情数据
course.get_course().then(response=>{
course.info = response.data;
clearInterval(course.timer);
course.timer = setInterval(()=>{
if(course.info.discount.expire && course.info.discount.expire>0){
course.info.discount.expire--
}
},1000);
}).catch(error=>{
ElMessage.error({
message: "非法的URL地址,无法获取课程信息!",
duration: 1000,
onClose(){
router.go(-1)
}
})
})
}else{
ElMessage.error({
message: "非法的URL地址,无法获取课程信息!",
duration: 1000,
onClose(){
router.go(-1)
}
})
}
// 阿里云播放器的选项参数
const options = reactive({
// source: "/src/assets/1.mp4",
// cover: "/src/assets/course-1.png",
autoplay: false, // 是否自动播放
preload: true, // 是否自动预加载
isLive: false, // 切换为直播流的时候必填true
// format: 'm3u8' // 切换为直播流的时候必填
})
const onPlay = (event)=>{
console.log("播放视频");
console.log(player.value.getCurrentTime()); // 当前视频播放时间
}
const onPause = (event)=>{
console.log("暂停播放");
console.log(player.value.getCurrentTime());
}
const onPlaying = (event)=>{
console.log("播放中");
console.log(player.value.getCurrentTime());
console.log(player.value.getDuration()); // 获取视频长度
}
</script>
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 客户端展示课程详情信息"
git push
服务端提供课程对应的章节列表和课时列表信息
courses/models.py,给章节模型新增返回课时列表字段,代码:
class CourseChapter(BaseModel):
"""课程章节"""
orders = models.SmallIntegerField(default=1, verbose_name="第几章")
summary = RichTextUploadingField(blank=True, null=True, verbose_name="章节介绍")
pub_date = models.DateField(auto_now_add=True, verbose_name="发布日期")
course = models.ForeignKey("Course", related_name='chapter_list', on_delete=models.CASCADE, db_constraint=False, verbose_name="课程名称")
class Meta:
db_table = "fg_course_chapter"
verbose_name = "课程章节"
verbose_name_plural = verbose_name
def __str__(self):
return "%s-第%s章-%s" % (self.course.name, self.orders, self.name)
# 自定义字段
def text(self):
return self.__str__()
# admin站点配置排序规则和显示的字段文本提示
text.short_description = "章节名称"
text.allow_tags = True
text.admin_order_field = "orders"
def get_lesson_list(self):
"""返回当前章节的课时列表"""
lesson_list = self.lesson_list.filter(is_deleted=False, is_show=True).order_by("orders").all()
return [{
"id":lesson.id,
"name":lesson.name,
"orders":lesson.orders,
"duration":lesson.duration,
"lesson_type":lesson.lesson_type,
"lesson_link":lesson.lesson_link,
"free_trail":lesson.free_trail
} for lesson in lesson_list]
courses.serializers,序列化器,代码:
from .models import CourseChapter
class CourseChapterModelSerializer(serializers.ModelSerializer):
"""课程章节序列化器"""
class Meta:
model = CourseChapter
fields = ["id", "orders", "name", "summary", "get_lesson_list"]
courses/views.py视图,代码:
from .models import CourseChapter
from .serializers import CourseChapterModelSerializer
class CourseChapterListAPIView(ListAPIView):
"""课程章节列表"""
serializer_class = CourseChapterModelSerializer
def get_queryset(self):
"""列表页数据"""
course = int(self.kwargs.get("course", 0))
try:
ret = Course.objects.filter(pk=course).all()
except:
return []
queryset = CourseChapter.objects.filter(course=course,is_show=True, is_deleted=False).order_by("orders", "id")
return queryset.all()
courses.urls,路由,代码:
urlpatterns = [
# 。。。。
re_path("^(?P<course>\d+)/chapters/$", views.CourseChapterListAPIView.as_view()),
]
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 服务端提供课程对应的章节列表和课时列表信息"
git push
客户端请求章节信息展示到页面中
src/api/course.js
,代码:
import http from "../utils/http";
import {reactive, ref} from "vue"
const course = reactive({
// 中间代码省略....
chapter_list: [], // 课程章节列表
// 中间代码省略....
get_course_chapters(){
// 获取指定课程的章节列表
return http.get(`/courses/${this.course_id}/chapters`)
}
})
export default course;
views/Info.vue
,代码:
<div class="tab-item" v-if="course.tabIndex===2">
<div class="tab-item-title">
<p class="chapter">课程章节</p>
<p class="chapter-length">共{{course.chapter_list.length}}章 {{course.info.lessons}}个课时</p>
</div>
<div class="chapter-item" v-for="chapter in course.chapter_list">
<p class="chapter-title"><img src="../assets/1.svg" alt="">第{{chapter.orders}}章·{{chapter.name}}</p>
<div class="chapter-title" style="padding-left: 2.4rem;" v-if="chapter.summary" v-html="chapter.summary"></div>
<ul class="lesson-list">
<li class="lesson-item" v-for="lesson in chapter.get_lesson_list">
<p class="name">
<span class="index">{{chapter.orders}}-{{lesson.orders}}</span>
{{lesson.name}}
<span class="free" v-if="lesson.free_trail">免费</span>
</p>
<p class="time">{{lesson.duration}} <img src="../assets/chapter-player.svg"></p>
<button class="try" v-if="lesson.free_trail">立即试学</button>
<button class="try" v-else>购买课程</button>
</li>
</ul>
</div>
</div>
<script setup>
import {reactive,ref, watch} from "vue"
import {useRoute, useRouter} from "vue-router"
import Header from "../components/Header.vue"
import Footer from "../components/Footer.vue"
import { AliPlayerV3 } from "vue-aliplayer-v3"
import course from "../api/course"
import { ElMessage } from 'element-plus'
import {fill0} from "../utils/func";
let route = useRoute()
let router= useRouter()
let player = ref(null)
// 获取url地址栏上的课程ID
course.course_id = route.params.id;
// 简单判断课程ID是否合法
if(course.course_id > 0){
// 根据课程ID到服务端获取课程详情数据
course.get_course().then(response=> {
course.info = response.data;
clearInterval(course.timer);
course.timer = setInterval(() => {
if (course.info.discount.expire && course.info.discount.expire > 0) {
course.info.discount.expire--
}
}, 1000);
}).catch(error=>{
ElMessage.error({
message: "非法的URL地址,无法获取课程信息!",
duration: 1000,
onClose(){
router.go(-1);
}
})
})
// 获取课程章节信息
course.get_course_chapters().then(response=>{
course.chapter_list = response.data
})
}else{
ElMessage.error({
message: "非法的URL地址,无法获取课程信息!",
duration: 1000,
onClose(){
router.go(-1)
}
})
}
// 阿里云播放器的选项参数
const options = reactive({
// source: "/src/assets/1.mp4",
// cover: "/src/assets/course-1.png",
autoplay: false, // 是否自动播放
preload: true, // 是否自动预加载
isLive: false, // 切换为直播流的时候必填true
// format: 'm3u8' // 切换为直播流的时候必填
})
const onPlay = (event)=>{
console.log("播放视频");
console.log(player.value.getCurrentTime()); // 当前视频播放时间
}
const onPause = (event)=>{
console.log("暂停播放");
console.log(player.value.getCurrentTime());
}
const onPlaying = (event)=>{
console.log("播放中");
console.log(player.value.getCurrentTime());
console.log(player.value.getDuration()); // 获取视频长度
}
</script>
服务端课程详情信息接口新增返回试学
的判断状态。
courses.models
,代码:
class Course(BaseModel):
course_type = (
(0, '付费购买'),
(1, '会员专享'),
(2, '学位课程'),
)
level_choices = (
(0, '初级'),
(1, '中级'),
(2, '高级'),
)
status_choices = (
(0, '上线'),
(1, '下线'),
(2, '预上线'),
)
# course_cover = models.ImageField(upload_to="course/cover", max_length=255, verbose_name="封面图片", blank=True, null=True)
course_cover = StdImageField(variations={
'thumb_1080x608': (1080, 608), # 高清图
'thumb_540x304': (540, 304), # 中等比例,
'thumb_108x61': (108, 61, True), # 小图(第三个参数表示保持图片质量),
}, max_length=255, delete_orphans=True, upload_to="course/cover", null=True, verbose_name="封面图片",blank=True)
course_video = models.FileField(upload_to="course/video", max_length=255, verbose_name="封面视频", blank=True, null=True)
course_type = models.SmallIntegerField(choices=course_type,default=0, verbose_name="付费类型")
level = models.SmallIntegerField(choices=level_choices, default=1, verbose_name="难度等级")
description = RichTextUploadingField(null=True, blank=True, verbose_name="详情介绍")
pub_date = models.DateField(auto_now_add=True, verbose_name="发布日期")
period = models.IntegerField(default=7, verbose_name="建议学习周期(day)")
attachment_path = models.FileField(max_length=1000, blank=True, null=True, verbose_name="课件路径")
attachment_link = models.CharField(max_length=1000, blank=True, null=True, verbose_name="课件链接")
status = models.SmallIntegerField(choices=status_choices, default=0, verbose_name="课程状态")
students = models.IntegerField(default=0, verbose_name="学习人数")
lessons = models.IntegerField(default=0, verbose_name="总课时数量")
pub_lessons = models.IntegerField(default=0, verbose_name="已更新课时数量")
price = models.DecimalField(max_digits=10,decimal_places=2, verbose_name="课程原价",default=0)
recomment_home_hot = models.BooleanField(default=False, verbose_name="是否推荐到首页新课栏目")
recomment_home_top = models.BooleanField(default=False, verbose_name="是否推荐到首页必学栏目")
direction = models.ForeignKey("CourseDirection", related_name="course_list", on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name="学习方向")
category = models.ForeignKey("CourseCategory", related_name="course_list", on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name="课程分类")
teacher = models.ForeignKey("Teacher", related_name="course_list", on_delete=models.DO_NOTHING, null=True, blank=True, db_constraint=False, verbose_name="授课老师")
class Meta:
db_table = "fg_course_info"
verbose_name = "课程信息"
verbose_name_plural = verbose_name
def __str__(self):
return "%s" % self.name
def course_cover_small(self):
if self.course_cover:
return mark_safe(f'<img style="border-radius: 0%;" src="{self.course_cover.thumb_108x61.url}">')
return ""
course_cover_small.short_description = "封面图片(108x61)"
course_cover_small.allow_tags = True
course_cover_small.admin_order_field = "course_cover"
def course_cover_medium(self):
if self.course_cover:
return mark_safe(f'<img style="border-radius: 0%;" src="{self.course_cover.thumb_540x304.url}">')
return ""
course_cover_medium.short_description = "封面图片(540x304)"
course_cover_medium.allow_tags = True
course_cover_medium.admin_order_field = "course_cover"
def course_cover_large(self):
if self.course_cover:
return mark_safe(f'<img style="border-radius: 0%;" src="{self.course_cover.thumb_1080x608.url}">')
return ""
course_cover_large.short_description = "封面图片(1080x608)"
course_cover_large.allow_tags = True
course_cover_large.admin_order_field = "course_cover"
@property
def discount(self):
# todo 将来通过计算获取当前课程的折扣优惠相关的信息
import random
return {
"type": ["限时优惠","限时减免"].pop(random.randint(0,1)), # 优惠类型
"expire": random.randint(100000, 1200000), # 优惠倒计时
"price": float(self.price - random.randint(1,10) * 10), # 优惠价格
}
def discount_json(self):
# 必须转成字符串才能保存到es中。所以该方法提供给es使用的。
return json.dumps(self.discount)
@property
def can_free_study(self):
"""是否允许试学"""
lesson_list = self.lesson_list.filter(is_deleted=False, is_show=True).order_by("orders").all()
return len(lesson_list) > 0
courses/serializers.py
,代码:
class CourseRetrieveModelSerializer(serializers.ModelSerializer):
"""课程详情的序列化器"""
diretion_name = serializers.CharField(source="diretion.name")
# diretion = serializers.SlugRelatedField(read_only=True, slug_field='name')
category_name = serializers.CharField(source="category.name")
# 序列化器嵌套
teacher = CourseTearchModelSerializer()
class Meta:
model = Course
fields = [
"name", "course_cover", "course_video", "level", "get_level_display",
"description", "pub_date", "status", "get_status_display", "students","discount",
"lessons", "pub_lessons", "price", "diretion", "diretion_name", "category", "category_name", "teacher","can_free_study"
]
客户端直接根据con_free_study来判断是否显示试学。
views/Info.vue
,代码:
<ul class="tab-list">
<li :class="state.tabIndex===1?'active':''" @click="state.tabIndex=1">详情介绍</li>
<li :class="state.tabIndex===2?'active':''" @click="state.tabIndex=2">课程章节 <span :class="state.tabIndex!==2?'free':''" v-if="course.con_free_study">(试学)</span></li>
<li :class="state.tabIndex===3?'active':''" @click="state.tabIndex=3">用户评论 (42)</li>
<li :class="state.tabIndex===4?'active':''" @click="state.tabIndex=4">常见问题</li>
</ul>
后台添加模拟数据。
INSERT INTO luffycity.fg_course_chapter (id, name, is_deleted, is_show, created_time, updated_time, orders, summary, pub_date, course_id) VALUES (1, 'Typescript快速入门', 0, 1, '2022-03-21 05:39:39.925451', '2022-03-21 05:39:39.925775', 1, '<p>Typescript快速入门的相关概念以及基本安装和基本使用</p>', '2022-03-21', 1);
INSERT INTO luffycity.fg_course_chapter (id, name, is_deleted, is_show, created_time, updated_time, orders, summary, pub_date, course_id) VALUES (2, 'Typescript的基本语法', 0, 1, '2022-03-21 05:40:38.672697', '2022-03-21 05:40:38.672749', 1, '<p>注释、数据类型、类型注解、函数、面向对象语法、泛型等</p>', '2022-03-21', 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (1, 'Typescript基本介绍', 0, 1, '2022-03-21 05:41:47.975350', '2022-03-21 05:41:47.975495', 1, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '5:00', '2022-03-21 05:41:47.975554', 1, 1, 1, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (2, 'Typescript与javascript的关系', 0, 1, '2022-03-21 05:42:13.059002', '2022-03-21 05:42:13.059077', 2, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '3:00', '2022-03-21 05:42:13.059128', 1, 1, 1, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (3, 'Typescript基本安装', 0, 1, '2022-03-21 05:42:29.797695', '2022-03-21 05:42:29.797750', 3, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '10:00', '2022-03-21 05:42:29.797796', 1, 1, 1, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (4, 'Typescript快速使用', 0, 1, '2022-03-21 05:42:43.776543', '2022-03-21 05:42:43.776618', 4, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '10:00', '2022-03-21 05:42:43.776672', 1, 1, 1, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (5, 'Typescript的解释器基本使用', 0, 1, '2022-03-21 05:43:07.315028', '2022-03-21 05:43:07.315092', 5, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '10:00', '2022-03-21 05:43:07.315150', 1, 1, 1, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (6, 'Typescript的注释写法', 0, 1, '2022-03-21 05:43:43.696556', '2022-03-21 05:43:43.696611', 1, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '4:00', '2022-03-21 05:43:43.696656', 1, 0, 2, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (7, 'Typescript的变量声明', 0, 1, '2022-03-21 05:44:06.271049', '2022-03-21 05:44:06.271109', 2, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '4:00', '2022-03-21 05:44:06.271160', 1, 0, 2, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (8, 'Typescript的类型注解', 0, 1, '2022-03-21 05:44:17.103618', '2022-03-21 05:44:17.103717', 3, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '4:00', '2022-03-21 05:44:17.103765', 1, 0, 2, 1);
INSERT INTO luffycity.fg_course_lesson (id, name, is_deleted, is_show, created_time, updated_time, orders, lesson_type, lesson_link, duration, pub_date, free_trail, recomment, chapter_id, course_id) VALUES (9, 'Typescript的函数声明', 0, 1, '2022-03-21 05:44:44.347650', '2022-03-21 05:44:44.347716', 4, 2, 'https://luffycityoline.oss-cn-beijing.aliyuncs.com/uploads/course/video/1.mp4', '4:00', '2022-03-21 05:44:44.347764', 1, 0, 2, 1);
提交代码版本
cd /home/moluo/Desktop/luffycity/
git add .
git commit -m "feature: 客户端请求章节信息展示到页面中"
git push