Python笔记_78_购物车实现_配置_后端API_前端提交数据_vuex组件_购物车列表页

文章目录

    • 购物车实现
      • 配置
        • 创建子应用 cart
        • 注册子应用cart
        • 配置信息
      • 添加课程商品到购物车的API接口实现
      • 提供访问路由
      • 前端提交课程到后端添加购物车数据
      • 前端展示商品课程的总数
        • vuex组件
          • 安装vuex
          • 把vuex注册到vue中
      • 购物车列表页
        • 后端提供获取购物车课程信息
        • 前端请求并显示课程信息
        • 切换勾选状态和课程有效期

购物车实现

配置

创建子应用 cart

cd luffyapi/apps
python ../../manage.py startapp cart

注册子应用cart

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

    'home',
    'users',
    'courses',
    'cart',
]

因为购物车中的商品(课程)信息会经常被用户操作,所以为了减轻mysql服务器的压力,可以选择把购物车信息通过redis来存储.

配置信息

# 设置redis缓存
CACHES = {
    # 默认缓存
    ....
    
    "cart":{
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/3",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    },
}

接下来商品信息存储以下内容:

购物车商品信息格式:
    商品数量[因为目前路飞学城的商品是视频,所以没有数量限制,如果以后做到真实商品,则必须有数量]
    
    商品id
    用户id
    课程有效期
    商品勾选状态

五种数据类型
    string字符串
        键:hash哈希字典
        键:{:,:,
        }
    list列表
        键:[1,2,....]
    set集合
        键:{1,2,....}
    zset有序集合
        键:{
            权重值1:,
            权重值2:,
        }

经过比较可以发现没有一种数据类型,可以同时存储4个字段数据的,所以我们才有2种数据结构来保存购物车数据
可以发现,上面5种数据类型中,哈希hash可以存储的数据量是最多的。
hash:
    键[用户ID]:{[商品ID]:[课程有效期],[商品ID]:[课程有效期],[商品ID]:[课程有效期],[商品ID]:[课程有效期],
    }
set:[用户ID]:{商品ID1,商品ID2....}

添加课程商品到购物车的API接口实现

cart/views.py视图,代码:

from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.permissions import IsAuthenticated
from courses.models import Course
from rest_framework.response import Response
from rest_framework import status
from django_redis import get_redis_connection

import logging
log = logging.getLogger("django")

from rest_framework.decorators import action
class CartAPIView(ViewSet):
    """读取多条数据"""
    permission_classes = [IsAuthenticated, ]
    
    @action(methods=["POST"],detail=False)
    def add_course(self,request):
        """添加商品到购物车中"""
        """获取商品ID,用户ID,有效期选项,购物车勾选状态"""""
        user_id = request.user.id
        course_id = request.data.get("course_id")
        is_selected = True # 勾选状态
        expire = 0 # 默认为0,0表示永久有效

        # 查找和验证数据
        try:
            course = Course.objects.get(is_delete=False, is_show=True, pk=course_id)
        except:
            return Response({"message": "对不起,您购买的商品不存在!"}, status=status.HTTP_400_BAD_REQUEST)

        # 添加数据到购物车中
        try:
            redis = get_redis_connection("cart")
            pip = redis.pipeline()
            pip.multi()

            # 保存商品信息到购物车中
            pip.hset("cart_%s" % user_id, course_id, expire )
            # 保存商品勾选状态到购物车中
            pip.sadd("selected_%s" % user_id, course_id )
            # 执行管道中命令
            pip.execute()

            # 获取当前用户的购物车中商品的数量
            total = redis.hlen("cart_%s" % user_id)
        except:
            log.error("购物车商品添加失败,redis操作出错!")
            return Response({"message":"商品添加失败,请联系客服工作人员!"},status=status.HTTP_507_INSUFFICIENT_STORAGE)

        # 返回购物车的状态信息
        return Response({"message":"添加商品成功!","total":total},status=status.HTTP_201_CREATED)

提供访问路由

总路由,代码:

urlpatterns = [
	...
    path('cart/', include("cart.urls") ),
]

子应用路由cart/urls.py,代码:

from django.urls import path, re_path
from . import views
urlpatterns = []

from rest_framework.routers import DefaultRouter

router = DefaultRouter()

router.register("course",views.CartAPIView,"cart")
print(router.urls)
urlpatterns += router.urls

前端提交课程到后端添加购物车数据

component/Course.vue

<template>
    <div class="detail">
      <Header/>
      <div class="main">
        <div class="course-info">
          <div class="wrap-left">
            <videoPlayer v-if="course.course_video" class="video-player vjs-custom-skin"
           ref="videoPlayer"
           :playsinline="true"
           :options="playerOptions"
           @play="onPlayerPlay($event)"
           @pause="onPlayerPause($event)"
            >
            videoPlayer>
            <img v-if="!course.course_video" :src="course.course_img" alt="">
          div>
          <div class="wrap-right">
            <h3 class="course-name">{{course.name}}h3>
            <p class="data">{{course.students}}人在学    课程总时长:{{course.lessons}}课时/{{course.lessons==course.pub_lessons?'更新完成':`已更新${course.pub_lessons}课时`}}    难度:{{course.level_name}}p>
            <div class="sale-time">
              <p class="sale-type">限时免费p>
              <p class="expire">距离结束:仅剩 01天 04小时 33分 <span class="second">08span>p>
            div>
            <p class="course-price">
              <span>活动价span>
              <span class="discount">¥0.00span>
              <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" @click="addCartHander"><img src="/static/image/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 class="brief_box" v-html="course.real_brief">div>
            div>
            <div class="tab-item" v-if="tabIndex==2">
              <div class="tab-item-title">
                <p class="chapter">课程章节p>
                <p class="chapter-length">共{{course.chapter_list.length}}章 {{course.lessons}}个课时p>
              div>
              <div class="chapter-item" v-for="chapter,key in course.chapter_list" :key="key">
                <p class="chapter-title"><img src="/static/image/1.svg" alt="">第{{chapter.chapter}}章·{{chapter.name}}p>
                <ul class="lesson-list">
                  <li class="lesson-item"  v-for="lesson,key in chapter.lesson_list" :key="key">
                    <p class="name"><span class="index">{{chapter.chapter}}-{{key+1}}span> {{lesson.name}}<span class="free" v-if="lesson.free_trail">免费span>p>
                    <p class="time">{{lesson.duration}} <img src="/static/image/chapter-player.svg">p>
                    <button v-if="lesson.free_trail && lesson.section_type==2" class="try"><router-link :to="`/course/lesson/video/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="lesson.free_trail && lesson.section_type==0" class="try"><router-link :to="`/course/lesson/doc/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="lesson.free_trail && lesson.section_type==1" class="try"><router-link :to="`/course/lesson/exam/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="!lesson.free_trail" 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="/static/image/8268683.png">
                   <div class="name">
                     <p class="teacher-name">李泳谊p>
                     <p class="teacher-title">老男孩LInux学科带头人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 {
        course:{
            id: 0, // 课程ID
        },
        tabIndex: 1, // 当前选项卡显示的下标
        playerOptions:{
          playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
          autoplay: true, //如果true,则自动播放
          muted: false,   // 默认情况下将会消除任何音频。
          loop: false, // 循环播放
          preload: 'auto',     // 建议浏览器在
          language: 'zh-CN',
          aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
          fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
          sources: [{ // 播放资源和资源格式
            type: "video/mp4",
            src: "" //你的视频地址(必填)
          }],
          poster: "../static/image/course-cover.jpeg", //视频封面图
          width: document.documentElement.clientWidth, // 默认视频全屏时的最大宽度
          notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        },
        total: 0,
      }
    },
    created(){
        // 获取路由参数
        this.course.id = this.$route.params.course;
        // 获取课程信息
        this.get_course();
    },
    methods: {
      onPlayerPlay(){
          // alert("视频开始播放");
      },
      onPlayerPause(){
          // alert("视频暂停播放");
      },
      get_course(){
          // 获取课程详情信息
          this.$axios.get(`${this.$settings.Host}/course/${this.course.id}/`).then(response=>{
              this.course = response.data;
              // 修改封面
              this.playerOptions.poster = response.data.course_img;
              // 修改视频地址
              this.playerOptions.sources[0].src = response.data.course_video;
          }).catch(error=>{
              console.log(error);
              // this.$router.go(-1);
          });
      },
      addCartHander(){
          let user_token = localStorage.user_token || sessionStorage.user_token;

          if( !user_token ){
              // 判断用户是否登录了
              this.$confirm("对不起,您尚未登录!请登录后继续操作!","警告").then(()=>{
                  this.$router.push("/user/login");
              });
          }

          // 添加商品到购物车
          this.$axios.post(`${this.$settings.Host}/cart/course/add_course/`,{
              course_id: this.course.id,
          },{
              headers:{
                  "Authorization": "jwt " + user_token,
              }
          }).then(response=>{
            this.total = response.data.total;
            this.$message("成功添加商品到购物车中");
          }).catch(error=>{
              console.log(error.response);
          });
      }
    },
    components:{
      Header,
      Footer,
      videoPlayer,
    }
}
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;
}
.try a{
  color: #fff;
}
style>

后端返回了当前用户的购物车商品总数,所以我们要把这个值展示到页面头部中,但是这个页面头部,是大部分页面的公共头部,所以我们需要把这个值保存到一个全局访问的地方,让所有的页面加载头部时,都可以共享访问

前端展示商品课程的总数

获取商品总数是在头部组件中使用到,并展示出来,但是我们后面可以在购物车中,或者商品课程的详情页中修改购物车中商品总数,因为对于一些数据,需要在多个组件中即时共享,这种情况,我们可以使用本地存储来完成,但是也可以通过vuex组件来完成这个功能。

vuex组件

安装vuex
npm install -S vuex
把vuex注册到vue中
  1. src目录下创建store目录,并在store目录下创建一个index.js文件,src/store/index.js文件代码:

    import Vue from 'vue'
    
    import Vuex from 'vuex'
    
    Vue.use(Vuex);
    
    export default new Vuex.Store({
      // 数据仓库,类似vue里面的data
      state: {
    
      },
      // 数据操作方法,类似vue里面的methods
      mutations: {
    
      }
    });
    
    
  2. 把上面index.js中创建的store对象注册到main.js的vue中。

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store/index';

Vue.config.productionTip = false;

// elementUI 导入
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// 调用插件
Vue.use(ElementUI);

// 加载全局初始化样式
import "../static/css/reset.css";

// 加载项目的自定义配置文件
import settings from "./settings"
// 把全局配置设置一个属性
Vue.prototype.$settings = settings;

// 加载ajax组件
import axios from 'axios';
// 允许ajax发送请求时附带cookie
axios.defaults.withCredentials = true;

Vue.prototype.$axios = axios; // 把对象挂载vue中

// 导入极验验证
import "../static/js/gt.js"

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


/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: ''
})

接下来,我们就可以在组件使用到store中state里面保存的共享数据了.

先到vuex中添加数据,store/inde.js,代码

import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
  // 数据仓库,类似vue里面的data
  state: {
    // 购物车数据
    cart:{
      total: 0,
    }
  },
  
  // 数据操作方法,类似vue里面的methods
  mutations: {
    
  }
});

Header.vue头部组件中,直接读取store里面的数据

          <div v-if="token" class="login-bar full-right">
            <div class="shop-cart full-left">
              <span class="shop-cart-total">{{$store.state.cart.total}}span>

// this是可以省略不写。
          <div v-if="token" class="login-bar full-right">
            <div class="shop-cart full-left">
              <span class="shop-cart-total">{{$store.state.cart.total}}span>

Python笔记_78_购物车实现_配置_后端API_前端提交数据_vuex组件_购物车列表页_第1张图片

store/index.js中新增mutations的方法,代码:

import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex);

export default new Vuex.Store({
  // 数据仓库,类似vue里面的data
  state: {
    // 购物车数据
    cart:{
      total: 0,
    }
  },

  // 数据操作方法,类似vue里面的methods
  mutations: {
     // 修改购物车的商品总数
    get_total(state,data){
      state.cart.total = data;
    }
  }
});

我们就可以在Detail.vue课程详情的组件调用上面的get_total方法, 修改商品总数。

<template>
    <div class="detail">
      <Header/>
      <div class="main">
        <div class="course-info">
          <div class="wrap-left">
            <videoPlayer v-if="course.course_video" class="video-player vjs-custom-skin"
           ref="videoPlayer"
           :playsinline="true"
           :options="playerOptions"
           @play="onPlayerPlay($event)"
           @pause="onPlayerPause($event)"
            >
            videoPlayer>
            <img v-if="!course.course_video" :src="course.course_img" alt="">
          div>
          <div class="wrap-right">
            <h3 class="course-name">{{course.name}}h3>
            <p class="data">{{course.students}}人在学    课程总时长:{{course.lessons}}课时/{{course.lessons==course.pub_lessons?'更新完成':`已更新${course.pub_lessons}课时`}}    难度:{{course.level_name}}p>
            <div class="sale-time">
              <p class="sale-type">限时免费p>
              <p class="expire">距离结束:仅剩 01天 04小时 33分 <span class="second">08span>p>
            div>
            <p class="course-price">
              <span>活动价span>
              <span class="discount">¥0.00span>
              <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" @click="addCartHander"><img src="/static/image/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 class="brief_box" v-html="course.real_brief">div>
            div>
            <div class="tab-item" v-if="tabIndex==2">
              <div class="tab-item-title">
                <p class="chapter">课程章节p>
                <p class="chapter-length">共{{course.chapter_list.length}}章 {{course.lessons}}个课时p>
              div>
              <div class="chapter-item" v-for="chapter,key in course.chapter_list" :key="key">
                <p class="chapter-title"><img src="/static/image/1.svg" alt="">第{{chapter.chapter}}章·{{chapter.name}}p>
                <ul class="lesson-list">
                  <li class="lesson-item"  v-for="lesson,key in chapter.lesson_list" :key="key">
                    <p class="name"><span class="index">{{chapter.chapter}}-{{key+1}}span> {{lesson.name}}<span class="free" v-if="lesson.free_trail">免费span>p>
                    <p class="time">{{lesson.duration}} <img src="/static/image/chapter-player.svg">p>
                    <button v-if="lesson.free_trail && lesson.section_type==2" class="try"><router-link :to="`/course/lesson/video/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="lesson.free_trail && lesson.section_type==0" class="try"><router-link :to="`/course/lesson/doc/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="lesson.free_trail && lesson.section_type==1" class="try"><router-link :to="`/course/lesson/exam/${lesson.id}/`">立即试学router-link>button>
                    <button v-if="!lesson.free_trail" 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="/static/image/8268683.png">
                   <div class="name">
                     <p class="teacher-name">李泳谊p>
                     <p class="teacher-title">老男孩LInux学科带头人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 {
        course:{
            id: 0, // 课程ID
        },
        tabIndex: 1, // 当前选项卡显示的下标
        playerOptions:{
          playbackRates: [0.7, 1.0, 1.5, 2.0], // 播放速度
          autoplay: true, //如果true,则自动播放
          muted: false,   // 默认情况下将会消除任何音频。
          loop: false, // 循环播放
          preload: 'auto',     // 建议浏览器在
          language: 'zh-CN',
          aspectRatio: '16:9', // 将播放器置于流畅模式,并在计算播放器的动态大小时使用该值。值应该代表一个比例 - 用冒号分隔的两个数字(例如"16:9"或"4:3")
          fluid: true, // 当true时,Video.js player将拥有流体大小。换句话说,它将按比例缩放以适应其容器。
          sources: [{ // 播放资源和资源格式
            type: "video/mp4",
            src: "" //你的视频地址(必填)
          }],
          poster: "../static/image/course-cover.jpeg", //视频封面图
          width: document.documentElement.clientWidth, // 默认视频全屏时的最大宽度
          notSupportedMessage: '此视频暂无法播放,请稍后再试', //允许覆盖Video.js无法播放媒体源时显示的默认信息。
        },
      }
    },
    created(){
        // 获取路由参数
        this.course.id = this.$route.params.course;
        // 获取课程信息
        this.get_course();
    },
    methods: {
      onPlayerPlay(){
          // alert("视频开始播放");
      },
      onPlayerPause(){
          // alert("视频暂停播放");
      },
      get_course(){
          // 获取课程详情信息
          this.$axios.get(`${this.$settings.Host}/course/${this.course.id}/`).then(response=>{
              this.course = response.data;
              // 修改封面
              this.playerOptions.poster = response.data.course_img;
              // 修改视频地址
              this.playerOptions.sources[0].src = response.data.course_video;
          }).catch(error=>{
              console.log(error);
              // this.$router.go(-1);
          });
      },
      addCartHander(){
          let user_token = localStorage.user_token || sessionStorage.user_token;

          if( !user_token ){
              // 判断用户是否登录了
              this.$confirm("对不起,您尚未登录!请登录后继续操作!","警告").then(()=>{
                  this.$router.push("/user/login");
              });
          }

          // 添加商品到购物车
          this.$axios.post(`${this.$settings.Host}/cart/course/add_course/`,{
              course_id: this.course.id,
          },{
              headers:{
                  "Authorization": "jwt " + user_token,
              }
          }).then(response=>{
            localStorage.user_total = response.data.total;
            // 把购物车中的商品总数保存到vuex中
            this.$store.commit("get_total",response.data.total);
            this.$message("成功添加商品到购物车中");
          }).catch(error=>{
              console.log(error.response);
          });
      }
    },
    components:{
      Header,
      Footer,
      videoPlayer,
    }
}
script>

购物车列表页

购物车页面有两部分构成:

Cart.vue,代码:

<template>
    <div class="cart">
      <Header>Header>
      <div class="cart_info">
        <div class="cart_title">
          <span class="text">我的购物车span>
          <span class="total">共4门课程span>
        div>
        <div class="cart_table">
          <div class="cart_head_row">
            <span class="doing_row">span>
            <span class="course_row">课程span>
            <span class="expire_row">有效期span>
            <span class="price_row">单价span>
            <span class="do_more">操作span>
          div>
          <div class="cart_course_list">
            <CartItem>CartItem>
            <CartItem>CartItem>
            <CartItem>CartItem>
            <CartItem>CartItem>
          div>
          <div class="cart_footer_row">
            <span class="cart_select"><label> <el-checkbox v-model="checked">el-checkbox><span>全选span>label>span>
            <span class="cart_delete"><i class="el-icon-delete">i> <span>删除span>span>
            <span class="goto_pay">去结算span>
            <span class="cart_total">总计:¥0.0span>
          div>
        div>
      div>
      <Footer>Footer>
    div>
template>

<script>
import Header from "./common/Header"
import Footer from "./common/Footer"
import CartItem from "./common/CartItem"
export default {
    name: "Cart",
    data(){
      return {
        checked: false,
      }
    },
    methods:{

    },
    components:{
      Header,
      Footer,
      CartItem,
    }
}
script>

<style scoped>
.cart_info{
  width: 1200px;
  margin: 0 auto 200px;
}
.cart_title{
  margin: 25px 0;
}
.cart_title .text{
  font-size: 18px;
  color: #666;
}
.cart_title .total{
  font-size: 12px;
  color: #d0d0d0;
}
.cart_table{
  width: 1170px;
}
.cart_table .cart_head_row{
  background: #F7F7F7;
  width: 100%;
  height: 80px;
  line-height: 80px;
  padding-right: 30px;
}
.cart_table .cart_head_row::after{
  content: "";
  display: block;
  clear: both;
}
.cart_table .cart_head_row .doing_row,
.cart_table .cart_head_row .course_row,
.cart_table .cart_head_row .expire_row,
.cart_table .cart_head_row .price_row,
.cart_table .cart_head_row .do_more{
  padding-left: 10px;
  height: 80px;
  float: left;
}
.cart_table .cart_head_row .doing_row{
  width: 78px;
}
.cart_table .cart_head_row .course_row{
  width: 530px;
}
.cart_table .cart_head_row .expire_row{
  width: 188px;
}
.cart_table .cart_head_row .price_row{
  width: 162px;
}
.cart_table .cart_head_row .do_more{
  width: 162px;
}

.cart_footer_row{
  padding-left: 30px;
  background: #F7F7F7;
  width: 100%;
  height: 80px;
  line-height: 80px;
}
.cart_footer_row .cart_select span{
  margin-left: -7px;
  font-size: 18px;
  color: #666;
}
.cart_footer_row .cart_delete{
  margin-left: 58px;
}
.cart_delete .el-icon-delete{
  font-size: 18px;
}

.cart_delete span{
  margin-left: 15px;
  cursor: pointer;
  font-size: 18px;
  color: #666;
}
.cart_total{
  float: right;
  margin-right: 62px;
  font-size: 18px;
  color: #666;
}
.goto_pay{
  float: right;
  width: 159px;
  height: 80px;
  outline: none;
  border: none;
  background: #ffc210;
  font-size: 18px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
style>

Cartitem.vue,代码:

<template>
    <div class="cart_item">
      <div class="cart_column column_1">
        <el-checkbox class="my_el_checkbox" v-model="checked">el-checkbox>
      div>
      <div class="cart_column column_2">
        <img src="/static/image/course-cover.png" alt="">
        <span><router-link to="/course/detail/1">爬虫从入门到进阶router-link>span>
      div>
      <div class="cart_column column_3">
        <el-select v-model="expire" size="mini" placeholder="请选择购买有效期" class="my_el_select">
          <el-option label="1个月有效" value="30" key="30">el-option>
          <el-option label="2个月有效" value="60" key="60">el-option>
          <el-option label="3个月有效" value="90" key="90">el-option>
          <el-option label="永久有效" value="10000" key="10000">el-option>
        el-select>
      div>
      <div class="cart_column column_4">¥499.0div>
      <div class="cart_column column_4">删除div>
    div>
template>

<script>
export default {
    name: "CartItem",
    data(){
      return {
        checked:false,
        expire: "1个月有效",
      }
    }
}
script>

<style scoped>
.cart_item::after{
  content: "";
  display: block;
  clear: both;
}
.cart_column{
  float: left;
  height: 250px;
}
.cart_item .column_1{
  width: 88px;
  position: relative;
}
.my_el_checkbox{
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  top: 0;
  margin: auto;
  width: 16px;
  height: 16px;
}
.cart_item .column_2 {
  padding: 67px 10px;
  width: 520px;
  height: 116px;
}
.cart_item .column_2 img{
  width: 175px;
  height: 115px;
  margin-right: 35px;
  vertical-align: middle;
}
.cart_item .column_3{
  width: 197px;
  position: relative;
  padding-left: 10px;
}
.my_el_select{
  width: 117px;
  height: 28px;
  position: absolute;
  top: 0;
  bottom: 0;
  margin: auto;
}
.cart_item .column_4{
  padding: 67px 10px;
  height: 116px;
  width: 142px;
  line-height: 116px;
}

style>

前端路由:

import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
// @ 表示src目录
// ...
import Cart from "@/components/Cart"
// ....
export default new Router({
  mode:"history",
  routes: [
    // ....
    {
      path: '/cart',
      name: 'Cart',
      component: Cart,
    },
		// ....
  ]
})

后端提供获取购物车课程信息

cart/views.py

from django.shortcuts import render
from rest_framework.viewsets import ViewSet
from rest_framework.permissions import IsAuthenticated
from courses.models import Course
from rest_framework.response import Response
from rest_framework import status
from django_redis import get_redis_connection
from django.conf import settings

import logging
log = logging.getLogger("django")

from rest_framework.decorators import action

class CartAPIView(ViewSet):
    """读取多条数据"""
    # permission_classes = [IsAuthenticated, ]

    @action(methods=["POST"],detail=False)
    def add_course(self,request):
        """添加商品到购物车中"""
        """获取商品ID,用户ID,有效期选项,购物车勾选状态"""""
        user_id = request.user.id
        course_id = request.data.get("course_id")
        is_selected = True # 勾选状态
        expire = 0 # 默认为0,0表示永久有效

        # 查找和验证数据
        try:
            course = Course.objects.get(is_delete=False, is_show=True, pk=course_id)
        except:
            return Response({"message": "对不起,您购买的商品不存在!"}, status=status.HTTP_400_BAD_REQUEST)

        # 添加数据到购物车中
        try:
            redis = get_redis_connection("cart")
            pip = redis.pipeline()
            pip.multi()

            # 保存商品信息到购物车中
            pip.hset("cart_%s" % user_id, course_id, expire )
            # 保存商品勾选状态到购物车中
            pip.sadd("selected_%s" % user_id, course_id )
            # 执行管道中命令
            pip.execute()

            # 获取当前用户的购物车中商品的数量
            total = redis.hlen("cart_%s" % user_id)
        except:
            log.error("购物车商品添加失败,redis操作出错!")
            return Response({"message":"商品添加失败,请联系客服工作人员!"},status=status.HTTP_507_INSUFFICIENT_STORAGE)

        # 返回购物车的状态信息
        return Response({"message":"添加商品成功!","total":total},status=status.HTTP_201_CREATED)

    @action(methods=["get"],detail=False)
    def get(self,request):
        """购物车商品列表"""
        user_id = 1 # request.user.id
        redis = get_redis_connection("cart")
        # 从hash里面读取购物车基本信息
        cart_course_list = redis.hgetall("cart_%s" % user_id)
        # 从set集合中查询所有已经勾选的商品ID
        cart_selected_list = redis.smembers("selected_%s" % user_id)

        # 如果提取到的商品购物车信息为空!,则直接返回空列表
        if len(cart_course_list) < 1:
            return Response([])

        data = []

        # 苟泽我们就要组装商品课程新返回给客户端
        for course_bytes, expire_bytes in cart_course_list.items():
            # print("课程ID", course_bytes)
            # print("有效期", expire_bytes)
            course_id = course_bytes.decode()
            try:
                course = Course.objects.get(pk=course_id)
            except Course.DoesNotExist:
                # 当前商品不存在!
                pass

            data.append({
                "id": course_id,
                "name": course.name,
                "course_img": settings.DOMAIL_IMAGE_URL + course.course_img.url,
                "price": course.price,
                "is_selected": True if course_bytes in cart_selected_list else False
            })


        return Response(data)

前端请求并显示课程信息

Cart.vue

<template>
    <div class="cart">
      <Header>Header>
      <div class="cart_info">
        <div class="cart_title">
          <span class="text">我的购物车span>
          <span class="total">共{{$store.state.total}}门课程span>
        div>
        <div class="cart_table">
          <div class="cart_head_row">
            <span class="doing_row">span>
            <span class="course_row">课程span>
            <span class="expire_row">有效期span>
            <span class="price_row">单价span>
            <span class="do_more">操作span>
          div>
          <div class="cart_course_list">
            <CartItem v-for="cart in cart_list" :cart="cart" :key="cart.id">CartItem>
          div>
          <div class="cart_footer_row">
            <span class="cart_select"><label> <el-checkbox v-model="checked">el-checkbox><span>全选span>label>span>
            <span class="cart_delete"><i class="el-icon-delete">i> <span>删除span>span>
            <span class="goto_pay">去结算span>
            <span class="cart_total">总计:¥0.0span>
          div>
        div>
      div>
      <Footer>Footer>
    div>
template>

<script>
import Header from "./common/Header"
import Footer from "./common/Footer"
import CartItem from "./common/CartItem"
export default {
    name: "Cart",
    data(){
      return {
        cart_list: [], // 购物车的商品信息
        checked: false,
      }
    },
    created(){
      this.user_token = this.check_user_login();
      this.get_cart();
    },
    methods:{
      check_user_login(){
        // 检查用户是否登录了
        let user_token = localStorage.user_token || sessionStorage.user_token;
        if( !user_token ){
            // 判断用户是否登录了
            this.$confirm("对不起,您尚未登录!请登录后继续操作!","警告").then(()=>{
                this.$router.push("/user/login");
            });
        }

        return user_token;
      },
      get_cart(){
        this.$axios.get(`${this.$settings.Host}/cart/course/get`,{
            headers:{
                "Authorization": "jwt " + this.user_token,
            }
        }).then(response=>{
            this.cart_list = response.data;
        }).catch(error=>{
            console.log( error.response )
        })
      }
    },
    components:{
      Header,
      Footer,
      CartItem,
    }
}
script>

<style scoped>
...
style>

CartItem.vue

<template>
    <div class="cart_item">
      <div class="cart_column column_1">
        <el-checkbox class="my_el_checkbox" v-model="cart.is_selected">el-checkbox>
      div>
      <div class="cart_column column_2">
        <img :src="cart.course_img" alt="">
        <span><router-link :to="`/course/${cart.id}`">{{cart.name}}router-link>span>
      div>
      <div class="cart_column column_3">
        <el-select v-model="expire" size="mini" placeholder="请选择购买有效期" class="my_el_select">
          <el-option label="1个月有效" value="30" key="30">el-option>
          <el-option label="2个月有效" value="60" key="60">el-option>
          <el-option label="3个月有效" value="90" key="90">el-option>
          <el-option label="永久有效" value="10000" key="10000">el-option>
        el-select>
      div>
      <div class="cart_column column_4">¥{{cart.price.toFixed(2)}}div>
      <div class="cart_column column_4">删除div>
    div>
template>

<script>
export default {
    name: "CartItem",
    props:["cart"],
    data(){
      return {
        checked:false,
        expire: "1个月有效",
      }
    }
}
script>

<style scoped>
...

style>

切换勾选状态和课程有效期

后端提供修改勾选状态的接口

视图代码:

    def put(self,request):
        """修改购物车中的商品信息"""
        user_id = request.user.id
        course_id = request.data.get("course_id")
        try:
            course_info = Course.objects.get(pk=course_id)
        except:
            return Response({"message": "请求有误,请联系客服"}, status=status.HTTP_507_INSUFFICIENT_STORAGE)

        redis = get_redis_connection("cart")

        # 操作商品信息之前,必须先确保当前课程在购物车中
        try:
            rs = redis.hget("cart_%s" % user_id, course_id)
            if rs is None:
                raise Exception
        except:
            return Response({"message": "请求有误,请联系客服"}, status=status.HTTP_507_INSUFFICIENT_STORAGE)

        # 修改勾选状态
        selected = request.data.get("selected",None)
        if selected is not None:
            if selected == True:
                redis.sadd("cart_select_%s" % user_id, course_info.id)
            else:
                redis.srem("cart_select_%s" % user_id, course_info.id)

        # 修改有效期
        course_expire = request.data.get("course_expire",None)
        if course_expire is not None:
            redis.hset("cart_%s" % user_id, course_info.id, course_expire )

        return Response({"message":"ok"}, status=status.HTTP_200_OK)

CartItem.vue

<template>
  <div class="cart-item">
          <el-row>
             <el-col :span="2" class="checkbox"><el-checkbox v-model="course.selected" :checked="course.selected" name="type">el-checkbox>el-col>
             <el-col :span="10" class="course-info">
               <img :src="this.$settings.host+course.course_img" alt="">
                <span>{{course.name}}span>
             el-col>
             <el-col :span="4">
                 <el-select v-model="course.course_expire">
                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">el-option>
                  el-select>
             el-col>
             <el-col :span="4" class="course-price">¥{{course.price}}el-col>
             <el-col :span="4" class="course-delete"><span @click="removeCartHander">删除span>el-col>
           el-row>
  div>
template>

<script>
  export default {
    name:"CartItem",
    props:["course"],
    data(){
      return {
        token: localStorage.token || sessionStorage.token,
        user_id: localStorage.user_id || sessionStorage.user_id,
        options:[ // 有效期
          {value:30,label:"一个月有效"},
          {value:60,label:"二个月有效"},
          {value:90,label:"三个月有效"},
          {value:0,label:"永久有效"},
        ]
      }
    },
    methods:{
      removeCartHander(){
        // 从购物车中删除商品信息
        this.$axios.delete(this.$settings.host+"/course/cart/",{
          course_id: this.course.course_id,
        },{
        headers:{
          // 附带已经登录用户的jwt token 提供给后端,一定不能疏忽这个空格
          'Authorization':'JWT '+this.token
        },
      }).then(response=>{
          console.log(response.data)
        }).catch(error=>{
          console.log(error.response)
        });
      },
    },
    watch:{
      "course.course_expire": function(value){
        // 切换当前课程的有效期
        this.$axios.put(this.$settings.host+"/cart/course/",{
          course_id: this.course.course_id,
          course_expire: value
        },{
        headers:{
          // 附带已经登录用户的jwt token 提供给后端,一定不能疏忽这个空格
          'Authorization':'JWT '+this.token
        },
      }).then(response=>{
          console.log(response.data)
        }).catch(error=>{
          console.log(error.response)
        });
      },
      "course.selected": function (value) {
        // 切换当前课程的勾选状态
        this.$axios.put(this.$settings.host+"/cart/course/",{
          course_id: this.course.course_id,
          selected: value
        },{
        headers:{
          // 附带已经登录用户的jwt token 提供给后端,一定不能疏忽这个空格
          'Authorization':'JWT '+this.token
        },
      }).then(response=>{
          console.log(response.data)
        }).catch(error=>{
          console.log(error.response)
        });
      }
    }
  }
script>

<style scoped>
.cart-item{
  height: 250px;
}
.cart-item .el-row{
  height: 100%;
}
.course-delete{
    font-size: 14px;
    color: #ffc210;
    cursor: pointer;
}
.el-checkbox,.el-select,.course-price,.course-delete{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
.el-checkbox{
    padding-top: 55px;
}
.el-select{
    padding-top: 45px;
    width: 118px;
    height: 28px;
    font-size: 12px;
    color: #666;
    line-height: 18px;
}
.course-info img{
    width: 175px;
    height: 115px;
    margin-right: 35px;
    vertical-align: middle;
}
.cart-item .el-col{
    padding: 67px 10px;
    vertical-align: middle!important;
}
.course-info{

}
style>

你可能感兴趣的:(Python笔记_78_购物车实现_配置_后端API_前端提交数据_vuex组件_购物车列表页)