课程详情页显示

课程详情页 views/Info.vue,代码:

  1. <template>
  2. <div class="detail">
  3. <Header/>
  4. <div class="main">
  5. <div class="course-info">
  6. <div class="wrap-left">
  7. <!-- 课程封面或封面商品 -->
  8. </div>
  9. <div class="wrap-right">
  10. <h3 class="course-name">Linux系统基础5周入门精讲</h3>
  11. <p class="data">23475人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:148课时/180小时&nbsp;&nbsp;&nbsp;&nbsp;难度:初级</p>
  12. <div class="sale-time">
  13. <p class="sale-type">限时免费</p>
  14. <p class="expire">距离结束:仅剩 01天 04小时 33分 <span class="second">08</span></p>
  15. </div>
  16. <p class="course-price">
  17. <span>活动价</span>
  18. <span class="discount">¥0.00</span>
  19. <span class="original">¥29.00</span>
  20. </p>
  21. <div class="buy">
  22. <div class="buy-btn">
  23. <button class="buy-now">立即购买</button>
  24. <button class="free">免费试学</button>
  25. </div>
  26. <div class="add-cart"><img src="../assets/cart-yellow.svg" alt="">加入购物车</div>
  27. </div>
  28. </div>
  29. </div>
  30. <div class="course-tab">
  31. <ul class="tab-list">
  32. <li :class="state.tabIndex===1?'active':''" @click="state.tabIndex=1">详情介绍</li>
  33. <li :class="state.tabIndex===2?'active':''" @click="state.tabIndex=2">课程章节 <span :class="state.tabIndex!==2?'free':''">(试学)</span></li>
  34. <li :class="state.tabIndex===3?'active':''" @click="state.tabIndex=3">用户评论 (42)</li>
  35. <li :class="state.tabIndex===4?'active':''" @click="state.tabIndex=4">常见问题</li>
  36. </ul>
  37. </div>
  38. <div class="course-content">
  39. <div class="course-tab-list">
  40. <div class="tab-item" v-if="state.tabIndex===1">
  41. <p><img alt="" src="//hcdn2.luffycity.com/media/frontend/activity/详情页_01.png"></p>
  42. </div>
  43. <div class="tab-item" v-if="state.tabIndex===2">
  44. <div class="tab-item-title">
  45. <p class="chapter">课程章节</p>
  46. <p class="chapter-length">共11章 147个课时</p>
  47. </div>
  48. <div class="chapter-item">
  49. <p class="chapter-title"><img src="../assets/1.svg" alt="">第1章·Linux硬件基础</p>
  50. <ul class="lesson-list">
  51. <li class="lesson-item">
  52. <p class="name"><span class="index">1-1</span> 课程介绍-学习流程<span class="free">免费</span></p>
  53. <p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
  54. <button class="try">立即试学</button>
  55. </li>
  56. <li class="lesson-item">
  57. <p class="name"><span class="index">1-2</span> 服务器硬件-详解<span class="free">免费</span></p>
  58. <p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
  59. <button class="try">立即试学</button>
  60. </li>
  61. </ul>
  62. </div>
  63. <div class="chapter-item">
  64. <p class="chapter-title"><img src="../assets/1.svg" alt="">第2章·Linux发展过程</p>
  65. <ul class="lesson-list">
  66. <li class="lesson-item">
  67. <p class="name"><span class="index">2-1</span> 操作系统组成-Linux发展过程</p>
  68. <p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
  69. <button class="try">立即购买</button>
  70. </li>
  71. <li class="lesson-item">
  72. <p class="name"><span class="index">2-2</span> 自由软件-GNU-GPL核心讲解</p>
  73. <p class="time">07:30 <img src="../assets/chapter-player.svg"></p>
  74. <button class="try">立即购买</button>
  75. </li>
  76. </ul>
  77. </div>
  78. </div>
  79. <div class="tab-item" v-if="state.tabIndex===3">
  80. 用户评论
  81. </div>
  82. <div class="tab-item" v-if="state.tabIndex===4">
  83. 常见问题
  84. </div>
  85. </div>
  86. <div class="course-side">
  87. <div class="teacher-info">
  88. <h4 class="side-title"><span>授课老师</span></h4>
  89. <div class="teacher-content">
  90. <div class="cont1">
  91. <img src="../assets/avatar.jpg">
  92. <div class="name">
  93. <p class="teacher-name">Avrion</p>
  94. <p class="teacher-title">老男孩LInux学科带头人</p>
  95. </div>
  96. </div>
  97. <p class="narrative" >路飞学城高级讲师,曾参与新加坡南洋理工大学大数据医疗相关项目,就职过多家互联网企业,有着多年开发经验,精通java,python,go等编程语言</p>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. </div>
  103. <Footer/>
  104. </div>
  105. </template>
  106. <script setup>
  107. import {reactive,ref,watch} from "vue"
  108. import {useRoute} from "vue-router"
  109. import Header from "../components/Header.vue"
  110. import Footer from "../components/Footer.vue"
  111. let route = useRoute()
  112. const state = reactive({
  113. course_id: route.params.id,
  114. tabIndex: 2,
  115. })
  116. </script>
  117. <style scoped>
  118. .main{
  119. background: #fff;
  120. padding-top: 30px;
  121. }
  122. .course-info{
  123. width: 1200px;
  124. margin: 0 auto;
  125. overflow: hidden;
  126. }
  127. .wrap-left{
  128. float: left;
  129. width: 690px;
  130. height: 388px;
  131. background-color: #000;
  132. }
  133. .wrap-right{
  134. float: left;
  135. position: relative;
  136. height: 388px;
  137. }
  138. .course-name{
  139. font-size: 20px;
  140. color: #333;
  141. padding: 10px 23px;
  142. letter-spacing: .45px;
  143. }
  144. .data{
  145. padding-left: 23px;
  146. padding-right: 23px;
  147. padding-bottom: 16px;
  148. font-size: 14px;
  149. color: #9b9b9b;
  150. }
  151. .sale-time{
  152. width: 464px;
  153. background: #fa6240;
  154. font-size: 14px;
  155. color: #4a4a4a;
  156. padding: 10px 23px;
  157. overflow: hidden;
  158. }
  159. .sale-type {
  160. font-size: 16px;
  161. color: #fff;
  162. letter-spacing: .36px;
  163. float: left;
  164. }
  165. .sale-time .expire{
  166. font-size: 14px;
  167. color: #fff;
  168. float: right;
  169. }
  170. .sale-time .expire .second{
  171. width: 24px;
  172. display: inline-block;
  173. background: #fafafa;
  174. color: #5e5e5e;
  175. padding: 6px 0;
  176. text-align: center;
  177. }
  178. .course-price{
  179. background: #fff;
  180. font-size: 14px;
  181. color: #4a4a4a;
  182. padding: 5px 23px;
  183. }
  184. .discount{
  185. font-size: 26px;
  186. color: #fa6240;
  187. margin-left: 10px;
  188. display: inline-block;
  189. margin-bottom: -5px;
  190. }
  191. .original{
  192. font-size: 14px;
  193. color: #9b9b9b;
  194. margin-left: 10px;
  195. text-decoration: line-through;
  196. }
  197. .buy{
  198. width: 464px;
  199. padding: 0px 23px;
  200. position: absolute;
  201. left: 0;
  202. bottom: 20px;
  203. overflow: hidden;
  204. }
  205. .buy .buy-btn{
  206. float: left;
  207. }
  208. .buy .buy-now{
  209. width: 125px;
  210. height: 40px;
  211. border: 0;
  212. background: #ffc210;
  213. border-radius: 4px;
  214. color: #fff;
  215. cursor: pointer;
  216. margin-right: 15px;
  217. outline: none;
  218. }
  219. .buy .free{
  220. width: 125px;
  221. height: 40px;
  222. border-radius: 4px;
  223. cursor: pointer;
  224. margin-right: 15px;
  225. background: #fff;
  226. color: #ffc210;
  227. border: 1px solid #ffc210;
  228. }
  229. .add-cart{
  230. float: right;
  231. font-size: 14px;
  232. color: #ffc210;
  233. text-align: center;
  234. cursor: pointer;
  235. margin-top: 10px;
  236. }
  237. .add-cart img{
  238. width: 20px;
  239. height: 18px;
  240. margin-right: 7px;
  241. vertical-align: middle;
  242. }
  243. .course-tab{
  244. width: 100%;
  245. background: #fff;
  246. margin-bottom: 30px;
  247. box-shadow: 0 2px 4px 0 #f0f0f0;
  248. }
  249. .course-tab .tab-list{
  250. width: 1200px;
  251. margin: auto;
  252. color: #4a4a4a;
  253. overflow: hidden;
  254. }
  255. .tab-list li{
  256. float: left;
  257. margin-right: 15px;
  258. padding: 26px 20px 16px;
  259. font-size: 17px;
  260. cursor: pointer;
  261. }
  262. .tab-list .active{
  263. color: #ffc210;
  264. border-bottom: 2px solid #ffc210;
  265. }
  266. .tab-list .free{
  267. color: #fb7c55;
  268. }
  269. .course-content{
  270. width: 1200px;
  271. margin: 0 auto;
  272. background: #FAFAFA;
  273. overflow: hidden;
  274. padding-bottom: 40px;
  275. }
  276. .course-tab-list{
  277. width: 880px;
  278. height: auto;
  279. padding: 20px;
  280. background: #fff;
  281. float: left;
  282. box-sizing: border-box;
  283. overflow: hidden;
  284. position: relative;
  285. box-shadow: 0 2px 4px 0 #f0f0f0;
  286. }
  287. .tab-item{
  288. width: 880px;
  289. background: #fff;
  290. padding-bottom: 20px;
  291. box-shadow: 0 2px 4px 0 #f0f0f0;
  292. }
  293. .tab-item-title{
  294. justify-content: space-between;
  295. padding: 25px 20px 11px;
  296. border-radius: 4px;
  297. margin-bottom: 20px;
  298. border-bottom: 1px solid #333;
  299. border-bottom-color: rgba(51,51,51,.05);
  300. overflow: hidden;
  301. }
  302. .chapter{
  303. font-size: 17px;
  304. color: #4a4a4a;
  305. float: left;
  306. }
  307. .chapter-length{
  308. float: right;
  309. font-size: 14px;
  310. color: #9b9b9b;
  311. letter-spacing: .19px;
  312. }
  313. .chapter-title{
  314. font-size: 16px;
  315. color: #4a4a4a;
  316. letter-spacing: .26px;
  317. padding: 12px;
  318. background: #eee;
  319. border-radius: 2px;
  320. display: -ms-flexbox;
  321. display: flex;
  322. -ms-flex-align: center;
  323. align-items: center;
  324. }
  325. .chapter-title img{
  326. width: 18px;
  327. height: 18px;
  328. margin-right: 7px;
  329. vertical-align: middle;
  330. }
  331. .lesson-list{
  332. padding:0 20px;
  333. }
  334. .lesson-list .lesson-item{
  335. padding: 15px 20px 15px 36px;
  336. cursor: pointer;
  337. justify-content: space-between;
  338. position: relative;
  339. overflow: hidden;
  340. }
  341. .lesson-item .name{
  342. font-size: 14px;
  343. color: #666;
  344. float: left;
  345. }
  346. .lesson-item .index{
  347. margin-right: 5px;
  348. }
  349. .lesson-item .free{
  350. font-size: 12px;
  351. color: #fff;
  352. letter-spacing: .19px;
  353. background: #ffc210;
  354. border-radius: 100px;
  355. padding: 1px 9px;
  356. margin-left: 10px;
  357. }
  358. .lesson-item .time{
  359. font-size: 14px;
  360. color: #666;
  361. letter-spacing: .23px;
  362. opacity: 1;
  363. transition: all .15s ease-in-out;
  364. float: right;
  365. }
  366. .lesson-item .time img{
  367. width: 18px;
  368. height: 18px;
  369. margin-left: 15px;
  370. vertical-align: text-bottom;
  371. }
  372. .lesson-item .try{
  373. width: 86px;
  374. height: 28px;
  375. background: #ffc210;
  376. border-radius: 4px;
  377. font-size: 14px;
  378. color: #fff;
  379. position: absolute;
  380. right: 20px;
  381. top: 10px;
  382. opacity: 0;
  383. transition: all .2s ease-in-out;
  384. cursor: pointer;
  385. outline: none;
  386. border: none;
  387. }
  388. .lesson-item:hover{
  389. background: #fcf7ef;
  390. box-shadow: 0 0 0 0 #f3f3f3;
  391. }
  392. .lesson-item:hover .name{
  393. color: #333;
  394. }
  395. .lesson-item:hover .try{
  396. opacity: 1;
  397. }
  398. .course-side{
  399. width: 300px;
  400. height: auto;
  401. margin-left: 20px;
  402. float: right;
  403. }
  404. .teacher-info{
  405. background: #fff;
  406. margin-bottom: 20px;
  407. box-shadow: 0 2px 4px 0 #f0f0f0;
  408. }
  409. .side-title{
  410. font-weight: normal;
  411. font-size: 17px;
  412. color: #4a4a4a;
  413. padding: 18px 14px;
  414. border-bottom: 1px solid #333;
  415. border-bottom-color: rgba(51,51,51,.05);
  416. }
  417. .side-title span{
  418. display: inline-block;
  419. border-left: 2px solid #ffc210;
  420. padding-left: 12px;
  421. }
  422. .teacher-content{
  423. padding: 30px 20px;
  424. box-sizing: border-box;
  425. }
  426. .teacher-content .cont1{
  427. margin-bottom: 12px;
  428. overflow: hidden;
  429. }
  430. .teacher-content .cont1 img{
  431. width: 54px;
  432. height: 54px;
  433. margin-right: 12px;
  434. float: left;
  435. }
  436. .teacher-content .cont1 .name{
  437. float: right;
  438. }
  439. .teacher-content .cont1 .teacher-name{
  440. width: 188px;
  441. font-size: 16px;
  442. color: #4a4a4a;
  443. padding-bottom: 4px;
  444. }
  445. .teacher-content .cont1 .teacher-title{
  446. width: 188px;
  447. font-size: 13px;
  448. color: #9b9b9b;
  449. white-space: nowrap;
  450. }
  451. .teacher-content .narrative{
  452. font-size: 14px;
  453. color: #666;
  454. line-height: 24px;
  455. }
  456. </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}}人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:{{course.info.pub_lessons}}课时/{{course.info.lessons}}课时&nbsp;&nbsp;&nbsp;&nbsp;难度:{{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