微信支付相关功能完善

98 阅读2分钟

1、扫码支付成功后,跳转课程详情页面

在pay/_pid.vue页面添加成功后清除定时器的方法。 在这里插入图片描述

2、课程的状态

免费课程或是已支付课程的状态,在详情页显示“立即观看”。如果课程是收费的,并且没有支付,应该显示“立即购买”。 (1)修改根据课程id查询课程详情接口,查询课程是否被购买

SELECT * FROM t_order o
 WHERE o.`course_id`=? AND o.`member_id`=? AND o.`status`=1;

(2)在service-order模块TOrderController中创建查询课程是否被某用户购买接口

@ApiOperation(value = "查询课程是否被某用户购买")
@GetMapping("isBuyCourse/{cid}/{mid}")
public boolean isBuyCourse(@PathVariable String cid,
                           @PathVariable String mid){
    QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
    wrapper.eq("course_id",cid);
    wrapper.eq("member_id",mid);
    wrapper.eq("status",1);
    int count = orderService.count(wrapper);
    return count>0;
}

(3)检查框架服务发现必备配置 在这里插入图片描述 在这里插入图片描述

(4)在service-edu模块中client目录中创建OrderClient接口,远程调用service-order服务 在这里插入图片描述

package com.atguigu.eduservice.client;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient("service-order")
@Component
public interface OrderClient {
    //查询课程是否被某用户购买
    @GetMapping("/orderservice/order/isBuyCourse/{cid}/{mid}")
    public boolean isBuyCourse(@PathVariable("cid") String cid,
                               @PathVariable("mid") String mid);

}

(5)在api/CourseApiController中添加根据id查询课程详情方法

@ApiOperation(value = "根据id查询课程详情")
@GetMapping("getCourseInfo/{id}")
public R getCourseInfo(@PathVariable String id,
                       HttpServletRequest request){
    //1查询课程详情信息
    CourseWebVo courseWebVo = courseService.getCourseWebVo(id);
    //2查询课程相关大纲数据
    List<ChapterVo> chapterVideoList = chapterService.getChapterVideoById(id);
    //3查询课程是否被购买
    String memberId = JwtUtils.getMemberIdByJwtToken(request);
    boolean isBuyCourse = orderClient.isBuyCourse(id, memberId);
    return R.ok().data("courseWebVo",courseWebVo)
            .data("chapterVideoList",chapterVideoList)
            .data("isBuyCourse",isBuyCourse);
}

3、实现前端,“立即购买”和“立即观看”功能

(1)修改course/_id.vue课程详情页面元素

</section>
            <section class="c-attr-mt" v-if="isbuy||Number(courseInfo.price)===0">
              <a href="#" title="立即观看" class="comm-btn c-btn-3">立即观看</a>
            </section>
            <section class="c-attr-mt" v-else>
              <a href="#" title="立即购买" class="comm-btn c-btn-3" @click="createOrderInfo()">立即购买</a>
            </section>

(2)出现下面的问题

Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@49e6234f]
memberId=

(3)js解决方案,不采用异步请求调用,用传统写法

export default {
  //异步请求操作
  asyncData({ params, error }) {
    //获取路由参数
    return {
        courseId: params.id
      }
  },
  data() {
    return {
      courseInfo: {},
      chapterVideoList: [],
      isbuy: false,
    };
  },
  created() {
     this.initCourseInfo() 
  },

  methods: {
    //获取课程详情信息
    initCourseInfo() {
      course.getCourseInfo(this.courseId).then(response => {
        this.courseInfo = response.data.data.courseWebVo;
        this.chapterVideoList = response.data.data.chapterVideoList;
        this.isbuy = response.data.data.isBuyCourse;
        console.log("this.isbuy=" + this.isbuy);
      });
    },

4、最终_id.vue页面

<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程详情 开始 -->
    <section class="container">
      <section class="path-wrap txtOf hLh30">
        <a href="#" title class="c-999 fsize14">首页</a>
        \
        <a href="#" title class="c-999 fsize14">课程列表</a>
        \
        <span class="c-333 fsize14">{{ courseInfo.title }}</span>
      </section>
      <div>
        <article class="c-v-pic-wrap" style="height: 357px;">
          <section id="videoPlay" class="p-h-video-box">
            <img :src="courseInfo.cover" :alt="courseInfo.title" class="dis c-v-pic">
          </section>
        </article>
        <aside class="c-attr-wrap">
          <section class="ml20 mr15">
            <h2 class="hLh30 txtOf mt15">
              <span class="c-fff fsize24">{{ courseInfo.title }}</span>
            </h2>
            <section class="c-attr-jg">
              <span class="c-fff">价格:</span>
              <b class="c-yellow" style="font-size:24px;">¥{{ courseInfo.price }}</b>
            </section>
            <section class="c-attr-mt c-attr-undis">
              <span class="c-fff fsize14">主讲: {{ courseInfo.teacherName }}&nbsp;&nbsp;&nbsp;</span>
            </section>
            <section class="c-attr-mt of">
              <span class="ml10 vam">
                <em class="icon18 scIcon"/>
                <a class="c-fff vam" title="收藏" href="#">收藏</a>
              </span>
            </section>
            <section v-if="isbuy||Number(courseInfo.price)===0" class="c-attr-mt">
              <a href="#" title="立即观看" class="comm-btn c-btn-3">立即观看</a>
            </section>
            <section v-else class="c-attr-mt">
              <a href="#" title="立即购买" class="comm-btn c-btn-3" @click="createOrderInfo()">立即购买</a>
            </section>
          </section>
        </aside>
        <aside class="thr-attr-box">
          <ol class="thr-attr-ol clearfix">
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">购买数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.buyCount }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">课时数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.lessonNum }}</h6>
              </aside>
            </li>
            <li>
              <p>&nbsp;</p>
              <aside>
                <span class="c-fff f-fM">浏览数</span>
                <br>
                <h6 class="c-fff f-fM mt10">{{ courseInfo.viewCount }}</h6>
              </aside>
            </li>
          </ol>
        </aside>
        <div class="clear"/>
      </div>
      <!-- /课程封面介绍 -->
      <div class="mt20 c-infor-box">
        <article class="fl col-7">
          <section class="mr30">
            <div class="i-box">
              <div>
                <section id="c-i-tabTitle" class="c-infor-tabTitle c-tab-title">
                  <a name="c-i" class="current" title="课程详情">课程详情</a>
                </section>
              </div>
              <article class="ml10 mr10 pt20">
                <div>
                  <h6 class="c-i-content c-infor-title">
                    <span>课程介绍</span>
                  </h6>
                  <div class="course-txt-body-wrap">
                    <section class="course-txt-body">
                      <p v-html="courseInfo.description">{{ courseInfo.description }}</p>
                    </section>
                  </div>
                </div>
                <!-- /课程介绍 -->
                <div class="mt50">
                  <h6 class="c-g-content c-infor-title">
                    <span>课程大纲</span>
                  </h6>
                  <section class="mt20">
                    <div class="lh-menu-wrap">
                      <menu id="lh-menu" class="lh-menu mt10 mr10">
                        <ul>
                          <!-- 文件目录 -->
                          <li
                            v-for=" chapter in chapterVideoList"
                            :key="chapter.id"
                            class="lh-menu-stair"
                          >
                            <a :title="chapter.title" href="javascript: void(0)" class="current-1">
                              <em class="lh-menu-i-1 icon18 mr10"/>
                              {{ chapter.title }}
                            </a>
                            <ol class="lh-menu-ol" style="display: block;">
                              <li
                                v-for="video in chapter.children"
                                :key="video.id"
                                class="lh-menu-second ml30"
                              >
                                <a
                                  :href="'/player/'+video.videoSourceId"
                                  :title="video.title"
                                  target="_blank"
                                >
                                  <span class="fr">
                                    <i class="free-icon vam mr10">免费试听</i>
                                  </span>
                                  <em class="lh-menu-i-2 icon16 mr5">&nbsp;</em>
                                  {{ video.title }}
                                </a>
                              </li>
                            </ol>
                          </li>
                        </ul>
                      </menu>
                    </div>
                  </section>
                </div>
                <!-- /课程大纲 -->
              </article>
            </div>
          </section>
        </article>
        <aside class="fl col-3">
          <div class="i-box">
            <div>
              <section class="c-infor-tabTitle c-tab-title">
                <a title href="javascript:void(0)">主讲讲师</a>
              </section>
              <section class="stud-act-list">
                <ul style="height: auto;">
                  <li>
                    <div class="u-face">
                      <a href="#">
                        <img :src="courseInfo.avatar" width="50" height="50" alt>
                      </a>
                    </div>
                    <section class="hLh30 txtOf">
                      <a class="c-333 fsize16 fl" href="#">{{ courseInfo.teacherName }}</a>
                    </section>
                    <section class="hLh20 txtOf">
                      <span class="c-999">{{ courseInfo.intro }}</span>
                    </section>
                  </li>
                </ul>
              </section>
            </div>
          </div>
        </aside>
        <div class="clear"/>
      </div>
    </section>
    <!-- /课程详情 结束 -->
  </div>
</template>

<script>
import course from '@/api/course'
import order from '@/api/order'

export default {
  // 异步请求操作
  asyncData({ params, error }) {
    // 获取路由参数
    return {
      courseId: params.id
    }
  },
  data() {
    return {
      courseInfo: {},
      chapterVideoList: [],
      isbuy: false
    }
  },
  created() {
    this.initCourseInfo()
  },

  methods: {
    // 获取课程详情信息
    initCourseInfo() {
      course.getCourseInfo(this.courseId).then(response => {
        this.courseInfo = response.data.data.courseWebVo
        this.chapterVideoList = response.data.data.chapterVideoList
        this.isbuy = response.data.data.isBuyCourse
        console.log('this.isbuy=' + this.isbuy)
      })
    },

    // 生成订单
    createOrderInfo() {
      order.createOrder(this.courseId).then(response => {
        // 创建订单成功后,跳转订单确认页面
        // 动态路由跳转
        this.$router.push({ path: '/order/' + response.data.data.orderNo })
      })
    }
  }
}
</script>