首页 > 其他 > 详细

luffy_05

时间:2019-08-04 23:25:53      阅读:140      评论:0      收藏:0      [点我收藏+]

05课程详情页

CKEditor富文本编辑器

富文本即具备丰富样式格式的文本。在运营后台,运营人员需要录入课程的相关描述,可以是包含了HTML语法格式的字符串。为了快速简单的让用户能够在页面中编辑带html格式的文本,我们引入富文本编辑器。

富文本编辑器:ueditor、ckeditor、kindeditor

1. 安装

pip install django-ckeditor

2. 添加应用

在INSTALLED_APPS中添加

INSTALLED_APPS = [
    ...
    'ckeditor',  # 富文本编辑器
    'ckeditor_uploader',  # 富文本编辑器上传图片模块
    ...
]

3. 添加CKEditor设置

在settings/dev.py中添加

# 富文本编辑器ckeditor配置
CKEDITOR_CONFIGS = {
    'default': {
        'toolbar': 'full',  # 工具条功能
        'height': 300,      # 编辑器高度
        # 'width': 300,     # 编辑器宽
    },
}
CKEDITOR_UPLOAD_PATH = ''  # 上传图片保存路径,留空则调用django的文件上传功能

4. 添加ckeditor路由

在总路由中添加

path(r'^ckeditor/', include('ckeditor_uploader.urls')),

5. 为模型类添加字段

ckeditor提供了两种类型的Django模型类字段

  • ckeditor.fields.RichTextField 不支持上传文件的富文本字段
  • ckeditor_uploader.fields.RichTextUploadingField 支持上传文件的富文本字段

修改course/models.py里面的字段信息,记得要重新数据迁移

from ckeditor_uploader.fields import RichTextUploadingField
class Course(models.Model):
    """
    专题课程
    """
    ...
    
    brief = RichTextUploadingField(max_length=2048, verbose_name="课程概述", null=True, blank=True)
    

效果:

技术分享图片

课程详情页显示

因为接下来的组件中使用了vue-video视频播放组件,所以我们需要先预安装。

安装依赖

npm install vue-video-player --save

在main.js中注册加载组件

require('video.js/dist/video-js.css');
require('vue-video-player/src/custom-theme.css');
import VideoPlayer from 'vue-video-player'
Vue.use(VideoPlayer);

Detail.vue组件代码:

<template>
    <div class="detail">
    <Header></Header>
    <div class="warp">
        <div class="course-info">
          <div class="warp-left" style="width: 690px;height: 388px;background-color: #000;">
          </div>
          <div class="warp-right">
              <h3 class="course-title">Python开发21天入门</h3>
              <p class="course-data">37400人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:154课时/30小时&nbsp;&nbsp;&nbsp;&nbsp;难度:初级</p>
              <div class="preferential">
                <p class="price-service">限时免费</p>
                <p class="timer">距离结束:仅剩 28天 14小时 10分 <span>57</span> 秒</p>
              </div>
              <p class="course-price">
                <span>活动价</span>
                <span class="real-price">¥0.00</span>
                <span class="old-price">¥9.00</span>
              </p>
              <div class="buy-course">
                <p class="buy-btn">
                  <span class="btn1">立即购买</span>
                  <span class="btn2">免费试学</span>
                </p>
                <p class="add-cart">
                  <img src="../../static/images/cart.svg" alt="">加入购物车
                </p>
              </div>
          </div>
        </div>
        <div class="course-tab">
            <ul>
                <li  class="active">详情介绍</li>
                <li>课程章节 <span>(试学)</span></li>
                <li>用户评论 (83)</li>
                <li>常见问题</li>
            </ul>
        </div>
        <div class="course-section">
          <section class="course-section-left">
            <img src="../../static/images/21天01_1547098127.6672518.jpeg" alt="">
          </section>

        </div>
    </div>
    <Footer></Footer>
    </div>
</template>

<script>
import Header from "./common/Header"
import Footer from "./common/Footer"
export default {
  name: 'CourseDetail',
  data(){
    return {

    }
  },
  components:{
    Header,
    Footer,

  },
  methods:{

  },
  created(){

  }
};
</script>

<style scoped>
.detail{
  margin-top: 80px;
}
.course-info{
  padding-top: 30px;
  width:1200px;
  height: 388px;
  margin: auto;
}
.warp-left,.warp-right{
  float: left;
}
.warp-right{
  height: 388px;
  position: relative;
}
.course-title{
    font-size: 20px;
    color: #333;
    padding: 10px 23px;
    letter-spacing: .45px;
    font-weight: normal;
}
.course-data{
    padding-left: 23px;
    padding-right: 23px;
    padding-bottom: 16px;
    font-size: 14px;
    color: #9b9b9b;
}
.preferential{
    width: 100%;
    height: auto;
    background: #fa6240;
    font-size: 14px;
    color: #4a4a4a;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    -ms-flex-pack: justify;
    justify-content: space-between;
    padding: 10px 23px;
}
.price-service{
    font-size: 16px;
    color: #fff;
    letter-spacing: .36px;
}
.timer{
    font-size: 14px;
    color: #fff;
}
.course-price{
    width: 100%;
    background: #fff;
    height: auto;
    font-size: 14px;
    color: #4a4a4a;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: end;
    align-items: flex-end;
    padding: 5px 23px;
}
.real-price{
    font-size: 26px;
    color: #fa6240;
    margin-left: 10px;
    display: inline-block;
    margin-bottom: -5px;
}
.old-price{
    font-size: 14px;
    color: #9b9b9b;
    margin-left: 10px;
    text-decoration: line-through;
}
.buy-course{
    position: absolute;
    left: 0;
    bottom: 20px;
    width: 100%;
    height: auto;
    -ms-flex-pack: justify;
    justify-content: space-between;
    padding-left: 23px;
    padding-right: 23px;
}
.buy-btn{
  float: left;
}
.buy-btn .btn1{
    display: inline-block;
    width: 125px;
    height: 40px;
    background: #ffc210;
    border-radius: 4px;
    color: #fff;
    cursor: pointer;
    margin-right: 15px;
    text-align: center;
    vertical-align: middle;
    line-height: 40px;
}
.buy-btn .btn2{
    width: 125px;
    height: 40px;
    border-radius: 4px;
    cursor: pointer;
    margin-right: 15px;
    display: inline-block;
    background: #fff;
    color: #ffc210;
    border: 1px solid #ffc210;
    text-align: center;
    vertical-align: middle;
    line-height: 40px;
}
.add-cart{
    font-size: 14px;
    color: #ffc210;
    text-align: center;
    cursor: pointer;
    float: right;
    margin-top: 10px;
}
.add-cart img{
    width: 20px;
    height: auto;
    margin-right: 7px;
}
.course-tab{
    width: 100%;
    height: auto;
    background: #fff;
    margin-bottom: 30px;
    box-shadow: 0 2px 4px 0 #f0f0f0;
}
.course-tab>ul{
    padding: 0;
    margin: 0 auto;
    list-style: none;
    width: 1200px;
    height: auto;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    color: #4a4a4a;
}
.course-tab>ul>li{
    margin-right: 15px;
    padding: 26px 20px 16px;
    font-size: 17px;
    cursor: pointer;
}
.course-tab>ul>.active{
    color: #ffc210;
    border-bottom: 2px solid #ffc210;
}
.course-section{
    background: #FAFAFA;
    overflow: hidden;
    padding-bottom: 40px;
    width: 1200px;
    height: auto;
    margin: 0 auto;
}
.course-section-left{
    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;
}
</style>

注册路由

routers/index.js

import CourseDetail from "../components/CourseDetail"

    ,{
      name:"CourseDetail",
      path: "/detail",
      component: CourseDetail,
    }

完善从课程列表跳转到课程详情的链接

Course.vue:31行,代码:

<p class="box-title"><router-link :to="{path: '/detail',query:{id:course.id}}">{{course.name}}</router-link></p>

CourseDetail.vue:104行,接受来自课程列表的课程ID, 代码:

  mounted(){
    // 获取地址上面的课程ID
    let id = this.$route.query.id - 0;
    console.log(id);
    if( isNaN(id) || id < 1 ){
      alert("非法请求!")
      this.$router.go(-1);
    }
  }

后端提供课程详情页数据接口

序列化器代码:

class TeacherDetailModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = Teacher
        fields = ("id","name","title","role","signature","image","brief")


class CourseDetailModelSerializer(serializers.ModelSerializer):
    """课程详情页的序列化器"""
    teacher = TeacherDetailModelSerializer()
    class Meta:
        model = Course
        fields = ("id","name","course_img","students","lessons","pub_lessons","price","teacher","course_level","brief")

视图代码:

from rest_framework.generics import RetrieveAPIView
from .serializers import CourseDetailModelSerializer
class CourseDeitalAPIView(RetrieveAPIView):
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by("orders")
    serializer_class = CourseDetailModelSerializer

路由代码:

from django.urls import path, re_path
from . import views
urlpatterns = [
    re_path(r"detail/(?P<pk>\d+)",views.CourseDetailAPIView.as_view())
]

前端请求api接口并显示数据

<template>
    <div class="detail">
      <Header/>
      <div class="main">
        <div class="course-info">
          <div class="wrap-left">
            <video-player class="video-player vjs-custom-skin"
               ref="videoPlayer"
               :playsinline="true"
               :options="playerOptions"
               @play="onPlayerPlay($event)"
               @pause="onPlayerPause($event)"
            >
            </video-player>
          </div>
          <div class="wrap-right">
            <h3 class="course-name">{{course.name}}</h3>
            <p class="data">{{course.students}}人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:{{course.lessons}}课时/{{course.lessons==course.pub_lessons?'更新完成':('已更新'+course.pub_lessons+"课程")}}&nbsp;&nbsp;&nbsp;&nbsp;难度:{{course.course_level}}</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">¥{{course.price}}</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="tabIndex==1?'active':''" @click="tabIndex=1">详情介绍</li>
            <li :class="tabIndex==2?'active':''" @click="tabIndex=2">课程章节 <span :class="tabIndex!=2?'free':''">(试学)</span></li>
            <li :class="tabIndex==3?'active':''" @click="tabIndex=3">用户评论 (42)</li>
            <li :class="tabIndex==4?'active':''" @click="tabIndex=4">常见问题</li>
          </ul>
        </div>
        <div class="course-content">
          <div class="course-tab-list">
            <div class="tab-item" v-if="tabIndex==1">
              <div v-html="course.brief"></div>
            </div>
            <div class="tab-item" v-if="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="tabIndex==3">
              用户评论
            </div>
            <div class="tab-item" v-if="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.teacher.image">
                   <div class="name">
                     <p class="teacher-name">{{course.teacher.name}} {{course.teacher.title}}</p>
                     <p class="teacher-title">{{course.teacher.signature}}</p>
                   </div>
                 </div>
                 <p class="narrative" >Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸</p>
               </div>
             </div>
          </div>
        </div>
      </div>
      <Footer/>
    </div>
</template>

<script>
import Header from "./common/Header"
import Footer from "./common/Footer"

import {videoPlayer} from 'vue-video-player';

export default {
    name: "Detail",
    data(){
      return {
        tabIndex:1,  // 当前选项卡显示的下标
        course_id:0, // 当前页面对应的课程ID
        course: {
            teacher: {},
        },  // 课程详情信息
        playerOptions: {
          playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
          autoplay: false, //如果true,则自动播放
          muted: false, // 默认情况下将会消除任何音频。
          loop: false, // 循环播放
          preload: 'auto',  // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
          language: 'zh-CN',
          aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
          fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
          sources: [{ // 播放资源和资源格式
            type: "video/mp4",
            src: "http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4" //你的视频地址(必填)
          }],
          poster: "../static/courses/675076.jpeg", //视频封面图
          width: document.documentElement.clientWidth, // 默认视频全屏时的最大宽度
          notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        }
      }
    },
    watch:{
      course(data){
        while(data.brief.search(`"/media`) != -1 ){
          data.brief = data.brief.replace(`"/media`,`"${this.$settings.Host}/media`)
        }
      },
      tabIndex(){
        if(tabIndex==2){
          //获取当前课程对应的章节列表和课时列表
          
        }
      }
    },
    created(){
      // 获取当前课程ID
      this.course_id = this.$route.query.id - 0;
      // 判断ID基本有效性
      let _this = this;
      if( isNaN(this.course_id) || this.course_id < 1 ){
        _this.$alert("无效的课程ID!","错误",{
          callback(){
            _this.$router.go(-1);
          }});
      }
      // 发送请求获取后端课程数据
      this.$axios.get(this.$settings.Host+`/courses/detail/${this.course_id}/`).then(response=>{
        this.course = response.data;
        // 修改视频中的封面图片
        this.playerOptions.poster = this.course.course_img;
      }).catch(error=>{
        console.log(error.response)
      });


    },
    methods: {
      // 视频播放事件
      onPlayerPlay(player) {
        alert("play");
      },
      // 视频暂停播放事件
      onPlayerPause(player){
        alert("pause");
      },
      // 视频插件初始化
      player() {
        return this.$refs.videoPlayer.player;
      }
    },
    components:{
      Header,
      Footer,
      videoPlayer,
    }
}
</script>

后端提供当前课程对应的章节和课时列表信息

courses/serializers.py,序列化器,代码:

from .models import CourseLesson
class CourseLessonModelSerializer(serializers.ModelSerializer):
    """课程课时"""
    class Meta:
        model = CourseLesson
        fields = ["id","name","duration","free_trail"]

from .models import CourseChapter
class CourseChapterModelSerializer(serializers.ModelSerializer):
    """课程章节"""
    coursesections = CourseLessonModelSerializer(many=True)
    class Meta:
        model = CourseChapter
        fields = ("id","name","coursesections","chapter")

courses/views.py视图,代码:

from rest_framework.generics import ListAPIView
from .serializers import CourseChapterModelSerializer
from .models import CourseChapter
class CourseChapterAPIView(ListAPIView):
    """课程章节信息"""
    queryset = CourseChapter.objects.filter(is_delete=False, is_show=True).order_by("orders")
    serializer_class = CourseChapterModelSerializer
    filter_backends = [DjangoFilterBackend]
    filter_fields = ['course']

courses/urls.py路由,代码:

re_path(r"^chapters/$", views.CourseChapterAPIView.as_view()),

前端请求章节信息展示到页面中

<template>
    <div class="detail">
      <Header/>
      <div class="main">
        <div class="course-info">
          <div class="wrap-left">
            <video-player class="video-player vjs-custom-skin"
               ref="videoPlayer"
               :playsinline="true"
               :options="playerOptions"
               @play="onPlayerPlay($event)"
               @pause="onPlayerPause($event)"
            >
            </video-player>
          </div>
          <div class="wrap-right">
            <h3 class="course-name">{{course.name}}</h3>
            <p class="data">{{course.students}}人在学&nbsp;&nbsp;&nbsp;&nbsp;课程总时长:{{course.lessons}}课时/{{course.lessons==course.pub_lessons?'更新完成':('已更新'+course.pub_lessons+"课程")}}&nbsp;&nbsp;&nbsp;&nbsp;难度:{{course.course_level}}</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">¥{{course.price}}</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="tabIndex==1?'active':''" @click="tabIndex=1">详情介绍</li>
            <li :class="tabIndex==2?'active':''" @click="tabIndex=2">课程章节 <span :class="tabIndex!=2?'free':''">(试学)</span></li>
            <li :class="tabIndex==3?'active':''" @click="tabIndex=3">用户评论 (42)</li>
            <li :class="tabIndex==4?'active':''" @click="tabIndex=4">常见问题</li>
          </ul>
        </div>
        <div class="course-content">
          <div class="course-tab-list">
            <div class="tab-item" v-if="tabIndex==1">
              <div v-html="course.brief"></div>
            </div>
            <div class="tab-item" v-if="tabIndex==2">
              <div class="tab-item-title">
                <p class="chapter">课程章节</p>
                <p class="chapter-length">共{{chapter_list.length}}章 147个课时</p>
              </div>
              <div class="chapter-item" v-for="chapter in chapter_list">
                <p class="chapter-title"><img src="@/assets/1.svg" alt="">第{{chapter.chapter}}章·{{chapter.name}}</p>
                <ul class="lesson-list">
                  <li class="lesson-item" v-for="lesson in chapter.coursesections">
                    <p class="name"><span class="index">{{chapter.chapter}}-{{lesson.id}}</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>
            <div class="tab-item" v-if="tabIndex==3">
              用户评论
            </div>
            <div class="tab-item" v-if="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.teacher.image">
                   <div class="name">
                     <p class="teacher-name">{{course.teacher.name}} {{course.teacher.title}}</p>
                     <p class="teacher-title">{{course.teacher.signature}}</p>
                   </div>
                 </div>
                 <p class="narrative" >Linux运维技术专家,老男孩Linux金牌讲师,讲课风趣幽默、深入浅出、声音洪亮到爆炸</p>
               </div>
             </div>
          </div>
        </div>
      </div>
      <Footer/>
    </div>
</template>

<script>
import Header from "./common/Header"
import Footer from "./common/Footer"

import {videoPlayer} from 'vue-video-player';

export default {
    name: "Detail",
    data(){
      return {
        tabIndex:1,  // 当前选项卡显示的下标
        course_id:0, // 当前页面对应的课程ID
        course: {
            teacher: {},
        },  // 课程详情信息
        chapter_list:{},
        playerOptions: {
          playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
          autoplay: false, //如果true,则自动播放
          muted: false, // 默认情况下将会消除任何音频。
          loop: false, // 循环播放
          preload: 'auto',  // 建议浏览器在<video>加载元素后是否应该开始下载视频数据。auto浏览器选择最佳行为,立即开始加载视频(如果浏览器支持)
          language: 'zh-CN',
          aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
          fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
          sources: [{ // 播放资源和资源格式
            type: "video/mp4",
            src: "http://img.ksbbs.com/asset/Mon_1703/05cacb4e02f9d9e.mp4" //你的视频地址(必填)
          }],
          poster: "../static/courses/675076.jpeg", //视频封面图
          width: document.documentElement.clientWidth, // 默认视频全屏时的最大宽度
          notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        }
      }
    },
    watch:{
      course(data){
        while(data.brief.search(`"/media`) != -1 ){
          data.brief = data.brief.replace(`"/media`,`"${this.$settings.Host}/media`)
        }
      },
      tabIndex(data){
        if(data==2){
          //获取当前课程对应的章节列表和课时列表
          this.$axios.get(`${this.$settings.Host}/courses/chapters/?course=${this.course_id}`).then(response=>{
            this.chapter_list = response.data;
          }).catch(error=>{
            console.log(error.response)
          })
        }
      }
    },
    created(){
      // 获取当前课程ID
      this.course_id = this.$route.query.id - 0;
      // 判断ID基本有效性
      let _this = this;
      if( isNaN(this.course_id) || this.course_id < 1 ){
        _this.$alert("无效的课程ID!","错误",{
          callback(){
            _this.$router.go(-1);
          }});
      }
      // 发送请求获取后端课程数据
      this.$axios.get(this.$settings.Host+`/courses/detail/${this.course_id}/`).then(response=>{
        this.course = response.data;
        // 修改视频中的封面图片
        this.playerOptions.poster = this.course.course_img;
      }).catch(error=>{
        console.log(error.response)
      });


    },
    methods: {
      // 视频播放事件
      onPlayerPlay(player) {
        alert("play");
      },
      // 视频暂停播放事件
      onPlayerPause(player){
        alert("pause");
      },
      // 视频插件初始化
      player() {
        return this.$refs.videoPlayer.player;
      }
    },
    components:{
      Header,
      Footer,
      videoPlayer,
    }
}
</script>

效果:

技术分享图片

课程播放

使用保利威云视频服务来对视频进行加密.

官方网址: http://www.polyv.net/vod/

注意通过免费试用注册体验版账号,公司使用酷播尊享版

开发文档地址:http://dev.polyv.net/2017/videoproduct/v-playerapi/html5player/html5-docs/

要开发播放保利威的加密视频功能,需要在用户中心->设置->API接口和加密设置.

配置视频上传加密.

上传视频并记录视频的ID

后端获取保利威的视频播放授权token,提供接口api给前端

参考文档:http://dev.polyv.net/2019/videoproduct/v-api/v-api-play/create-playsafe-token/

视图代码:

from rest_framework.response import Response
from luffy.utils.polyv import PolyvPlayer

from rest_framework.views import APIView
class PolyvAPIView(APIView):
    def get(self, request):
        vid = request.query_params.get("vid")
        remote_addr = request.META.get("REMOTE_ADDR")
        user_id = 1
        user_name = "test"
        polyv_video = PolyvPlayer()
        verify_data = polyv_video.get_video_token(vid, remote_addr, user_id, user_name)
        return Response(verify_data["token"])

根据官方文档的案例,已经有其他人开源了,针对polvy的token生成的python版本了,我们可以直接拿来使用.

在utils下创建polyv.py,编写token生成工具函数

from django.conf import settings
import time
import requests
import hashlib

class PolyvPlayer(object):
    userId = settings.POLYV_CONFIG['userId']
    secretkey = settings.POLYV_CONFIG['secretkey']

    def tomd5(self, value):
        """取md5值"""
        return hashlib.md5(value.encode()).hexdigest()

    # 获取视频数据的token
    def get_video_token(self, videoId, viewerIp, viewerId=None, viewerName='', extraParams='HTML5'):
        """
        :param videoId: 视频id
        :param viewerId: 看视频用户id
        :param viewerIp: 看视频用户ip
        :param viewerName: 看视频用户昵称
        :param extraParams: 扩展参数
        :param sign: 加密的sign
        :return: 返回点播的视频的token
        """
        ts = int(time.time() * 1000)  # 时间戳
        plain = {
            "userId": self.userId,
            'videoId': videoId,
            'ts': ts,
            'viewerId': viewerId,
            'viewerIp': viewerIp,
            'viewerName': viewerName,
            'extraParams': extraParams
        }

        # 按照ASCKII升序 key + value + key + value... + value 拼接
        plain_sorted = {}
        key_temp = sorted(plain)
        for key in key_temp:
            plain_sorted[key] = plain[key]
        print(plain_sorted)

        plain_string = ''
        for k, v in plain_sorted.items():
            plain_string += str(k) + str(v)
        print(plain_string)

        sign_data = self.secretkey + plain_string + self.secretkey

        # 取sign_data的md5的大写
        sign = self.tomd5(sign_data).upper()

        # 新的带有sign的字典
        plain.update({'sign': sign})

        result = requests.post(
            url='https://hls.videocc.net/service/v1/token',
            headers={"Content-type": "application/x-www-form-urlencoded"},
            data=plain
        ).json()
        data = {} if isinstance(result, str) else result.get("data", {})

        return {"token": data}

客户端请求token并播放视频

在 index.html 中加载保利威视频播放器的js核心类库

<script src='https://player.polyv.net/script/polyvplayer.min.js'></script>

在组件中,直接配置保利威播放器需要的参数:

<template>
    <div class="player">
    <div id="player"></div>
    </div>
</template>

<script>
export default {
  name:"Player",
  data () {
    return {

    }
  },
  methods: {

  },
  mounted(){
    let _this = this;
    var player = polyvObject('#player').videoPlayer({
        wrap: '#player',
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        forceH5: true,
        vid: '06e090212218afd78ccadfcc3b954385_0',
        code: 'myRandomCodeValue',
        // 视频加密播放的配置
        playsafe: function (vid, next) {// 向后端发送请求获取加密的token
            _this.$axios.get(_this.$settings.host+`/course/video/${vid}/`).then(function (data) {
                console.log(data);
                next(data.data.token)
            })

        }
    });
  },
  computed: {
  }
}
</script>

<style scoped>
</style>

完善点击课程详情的立即试学按钮跳转到视频播放页面

<span class="btn2"><router-link :to="{path: 'player',query:{vid:'06e090212218afd78ccadfcc3b954385_0'}}">免费试学</router-link></span>

完善API接口的身份认证

后端视图代码

from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from utils.polyv import PolyvPlayer
from rest_framework.response import Response
class CoursePlayerAPIView(APIView):
    """实际上而言,需要在播放页面保存当前访问者只能是用户,不能是游客"""
    permission_classes = (IsAuthenticated,)
    """生成保利威视频播放的token"""
    def get(self,request,vid):
        """生成token"""
        # 获取视频浏览者的IP
        remote_addr = request.META.get("REMOTE_ADDR")
        # user_id = request.user.id  # 用户ID
        user_id = 1
        # user_name = request.user.username # 用户名
        user_name = "admin"
        # 引入utils下刚刚声明的保利威工具类
        polyv_video = PolyvPlayer()
        verify_data = polyv_video.get_video_token(vid, remote_addr, user_id, user_name)
        return Response(verify_data["token"])

前端在请求后端提供视频加密播放的token时需要附带jwt token

Player.vue,代码:

<script>
export default {
  name:"Player",
  data () {
    return {
      user_id: sessionStorage.user_id || localStorage.user_id,
      token: sessionStorage.token || localStorage.token,
    }
  },
  methods: {

  },
  mounted(){
    let _this = this;
    let vid = this.$route.query.vid;
    var player = polyvObject('#player').videoPlayer({
        wrap: '#player',
        width: document.documentElement.clientWidth,
        height: document.documentElement.clientHeight,
        forceH5: true,
        vid: vid,
        code: 'myRandomCodeValue',
        // 视频加密播放的配置
        playsafe: function (vid, next) {// 向后端发送请求获取加密的token
            _this.$axios.get(_this.$settings.host+`/course/video/${vid}/`,
              // 因为本次访问的api接口设置了身份认证,所以在请求头中必须附带token值
              {
                headers:{
                  // 附带已经登录用户的jwt token 提供给后端,一定不能疏忽这个空格
                  'Authorization':'JWT '+_this.token
                },
              }).then(function (data) {
                console.log(data);
                next(data.data.token)
            }).catch(error=>{
                alert("非法请求");
                _this.$router.go(-1);
            })
        }
    });
  },
  computed: {
  }
}
</script>

luffy_05

原文:https://www.cnblogs.com/pankypan/p/11300268.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!