courseDetail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. <template>
  2. <view>
  3. <view v-if="!loginShow" class="container" :style="{ height : `${loginShow ? '100vh' : 'auto' }`,
  4. overflow: `${loginShow ? 'hidden' : 'auto' }` }">
  5. <view class="container-poster" style="width: 100%;padding: 0 20rpx;">
  6. <channel-live v-if="feedShow" :feed-id="feedId" :finder-user-name="finderId"></channel-live>
  7. <image v-else show-menu-by-longpress :src="courseDetail.poster?courseDetail.poster:''" mode="widthFix"
  8. style="width: 100%;"></image>
  9. </view>
  10. <view class="course-tab-list">
  11. <view class="course-tab-item" v-for="(data, index) in items" :key="index" @click="onClickItem(index)"
  12. :class="currentTab === index ? 'tab-active' : ''">
  13. {{ data }}
  14. </view>
  15. </view>
  16. <view class="content" v-if="currentTab === 0" style="overflow: hidden;">
  17. <view class="content-text"
  18. :style="{
  19. marginBottom: `${videoShow?'140rpx':'0'}`,
  20. paddingBottom: `${videoShow?'150rpx':'0'}`
  21. }"
  22. style="height: 100%;overflow: scroll;padding-left: 20rpx;padding-right: 20rpx;">
  23. <view class="text-title">{{courseDetail.title}}</view>
  24. <view class="text-title">课程概述</view>
  25. <view class="text-content">{{courseDetail.summary}}</view>
  26. <view class="text-title">课程时间</view>
  27. <view class="text-content">{{courseDetail.date ? getDateWeek(courseDetail.date) : ''}}</view>
  28. <view class="text-title">培训地点</view>
  29. <view class="text-content">{{courseDetail.loc}}</view>
  30. <view class="text-tip" v-if="!isMember && courseDetail.viewMode === '2'">个人会员或单位会员免费,点击现在入会></view>
  31. </view>
  32. <view class="section-bottom" @click="toVideo" v-show="videoShow">
  33. <text>预约课程</text>
  34. </view>
  35. <view :class="['section-bottom', `${courseDetail.hasReg ? 'bg-blue' : ''}`]" v-show="regShow" @click="toReg">
  36. <text>{{ courseDetail?.hasReg ? '报名成功' : '点击报名' }}</text>
  37. </view>
  38. </view>
  39. <view class="content" v-if="currentTab === 1" style="overflow: hidden;">
  40. <view class=""
  41. style="margin-bottom: 140rpx;padding-bottom: 150rpx;height: 100%;overflow: scroll;padding-left: 20rpx;padding-right: 20rpx;">
  42. <view v-for="(comment, index) in sortedCommentList" :key="index" class="comment-list-item">
  43. <view class="comment-list-left">
  44. <image :src="comment.icon" class="comment-list-avator"></image>
  45. </view>
  46. <view class="comment-list-right">
  47. <view style="margin-bottom: 15rpx;">
  48. <text class="comment-list-username">{{ comment.username }}</text>
  49. <text class="comment-list-moment">{{ formatTime(comment.commentTime) }}</text>
  50. </view>
  51. <view>{{ comment.content }}</view>
  52. </view>
  53. </view>
  54. </view>
  55. <view class="section-bottom " style="background-color: #f2f2f2;">
  56. <view class="comment-input-box">
  57. <u-input :custom-style="inputStyle" class="comment-input" v-model="comment" :border="false"
  58. placeholder="写留言" height="60" adjust-position />
  59. <u-button class="comment-button" :hair-line="false" :custom-style="customStyle"
  60. @click="toSend">发送</u-button>
  61. </view>
  62. </view>
  63. </view>
  64. </view>
  65. <u-popup v-model="loginShow" :mask="false" :closeable='false' mode="bottom" :mask-close-able='false'
  66. safe-area-inset-bottom>
  67. <view style="height: 70vh;padding: 40rpx;position: relative;background:none;">
  68. <view style="text-align: center;margin: 70rpx 0;">
  69. <u-button size="medium" type="error" @click="toLogin">登录查看</u-button>
  70. </view>
  71. </view>
  72. </u-popup>
  73. <canvas canvas-id="shareCanvas"
  74. :style="{ position: 'absolute', top: '-10000px', width: '750px', height: '600px' }"></canvas>
  75. </view>
  76. </template>
  77. <script setup>
  78. import {
  79. loadCourseDetail,
  80. loadCommentList,
  81. sendComment,
  82. regCourse
  83. } from "@/api/edu.js"
  84. import {
  85. getToken
  86. } from '@/utils/auth.js'
  87. import {
  88. useAuthStore
  89. } from '@/store/authStore'
  90. import dayjs from 'dayjs'
  91. dayjs.locale('zh-cn')
  92. const authStore = useAuthStore();
  93. // const isMember = ref(false)
  94. const feedShow = ref(false)
  95. import {
  96. ref,
  97. computed
  98. } from 'vue'
  99. import {
  100. onLoad,
  101. onShow,
  102. onShareAppMessage,
  103. onShareTimeline
  104. } from '@dcloudio/uni-app'
  105. const courseDetail = ref({});
  106. const courseId = ref(null);
  107. const courseName = ref(null);
  108. const items = ref(['课程简介', '观看评论']);
  109. const currentTab = ref(0);
  110. const comment = ref("");
  111. const feedId = ref(null);
  112. const finderId = "sphIfs9sYiL5RB3"
  113. // 评论发送按钮样式
  114. const customStyle = ref({
  115. backgroundColor: '#e6e6e6',
  116. color: '#333333',
  117. fontWeight: 'bold',
  118. height: '60rpx',
  119. marginLeft: '20rpx',
  120. border: 'none',
  121. fontSize: '26rpx'
  122. })
  123. // 评论输入框样式
  124. const inputStyle = ref({
  125. backgroundColor: '#e6e6e6',
  126. color: '#333333',
  127. borderRadius: '5px',
  128. padding: '0 20rpx',
  129. fontSize: '26rpx'
  130. })
  131. // 评论列表
  132. const commentList = ref([])
  133. // 点击tabs,切换
  134. function onClickItem(e) {
  135. if (currentTab.value != e) {
  136. currentTab.value = e;
  137. if (e === 2) {
  138. getComment(courseId.value)
  139. }
  140. }
  141. }
  142. // 初始化
  143. function init(id) {
  144. loadCourseDetail(id).then(res => {
  145. if (res?.data) {
  146. courseDetail.value = res.data;
  147. showBuy.value = showBuyAction()
  148. if(courseDetail.value.regType === '1'
  149. && (courseDetail.value.status === '2' ||
  150. courseDetail.value.status === '3')){
  151. // console.log(123456)
  152. getVideo()
  153. }
  154. // console.log(courseDetail, "课程详情")
  155. }
  156. })
  157. }
  158. function getComment(id) {
  159. loadCommentList(id).then(res => {
  160. if (res?.data) {
  161. commentList.value = res.data;
  162. }
  163. })
  164. }
  165. // 购买课程
  166. function toBuy() {
  167. uni.navigateTo({
  168. url: "/pages/goOnEdu/course/courseDetail/courseOrder?id=" + courseId.value
  169. })
  170. // console.log("购买该课程", courseDetail.value.id)
  171. }
  172. // 获取图片信息
  173. const getImageInfo = (imgUrl) => {
  174. return new Promise((resolve) => {
  175. uni.getImageInfo({
  176. src: imgUrl,
  177. success: (res) => resolve(res),
  178. fail: () => resolve(null)
  179. });
  180. });
  181. };
  182. function toSend() {
  183. sendComment({
  184. courseId: courseId.value,
  185. content: comment.value,
  186. commentTime: formatDate(new Date())
  187. }).then(res => {
  188. getComment(courseId.value)
  189. comment.value = ""
  190. })
  191. }
  192. function formatDate(date) {
  193. const pad = (num) => num.toString().padStart(2, '0');
  194. const year = date.getFullYear();
  195. const month = pad(date.getMonth() + 1); // 月份从0开始,需加1
  196. const day = pad(date.getDate());
  197. const hours = pad(date.getHours());
  198. const minutes = pad(date.getMinutes());
  199. const seconds = pad(date.getSeconds());
  200. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  201. }
  202. function showBuyAction() {
  203. if (courseDetail.value.viewMode === '2' &&
  204. !isMember.value &&
  205. !courseDetail.value.hasBuy &&
  206. currentTab.value === 0) {
  207. // console.log(1)
  208. return true
  209. }
  210. // 付费,不管是不是会员,并且没买的
  211. if (courseDetail.value.viewMode === '3' &&
  212. !courseDetail.value.hasBuy &&
  213. currentTab.value === 0) {
  214. // console.log(2)
  215. return true
  216. }
  217. // console.log(3)
  218. return false
  219. }
  220. const showBuy = ref(false)
  221. const isMember = computed(() => {
  222. return authStore.userInfo.isMember == '0' ? false : true
  223. })
  224. const isLogin = computed(() => {
  225. if (getToken() && authStore.isAuthenticated) {
  226. return true
  227. } else {
  228. return false
  229. }
  230. })
  231. const loginShow = ref(true)
  232. function toLogin() {
  233. // loginShow.value = false
  234. let url = {
  235. url: '/pages/goOnEdu/course/courseDetail/courseDetail',
  236. id: courseId.value,
  237. title: courseName.value
  238. }
  239. uni.setStorageSync("redirect", url)
  240. uni.navigateTo({
  241. url: `/pages/login/login`
  242. })
  243. }
  244. // 初始化页面
  245. onLoad((option) => {
  246. const {
  247. id,
  248. title
  249. } = option;
  250. courseId.value = id
  251. courseName.value = title
  252. uni.setNavigationBarTitle({
  253. title: title
  254. });
  255. if (isLogin.value) {
  256. loginShow.value = false
  257. } else {
  258. loginShow.value = true
  259. }
  260. // toVideo()
  261. // loginShow.value = true
  262. })
  263. onShow(() => {
  264. if (isLogin.value) {
  265. init(courseId.value)
  266. getComment(courseId.value)
  267. }
  268. })
  269. function formatDateS(dateStr) {
  270. return dateStr.replace(" ", "T");
  271. }
  272. // 日期格式:xxxx年xx月xx日(星期x)
  273. function getDateWeek(dateStr) {
  274. console.log(dateStr,"dateStr")
  275. // 将日期字符串转换为 Date 对象
  276. const date = new Date(dateStr.replace(/-/g, '/'));
  277. // 检查日期是否有效
  278. if (isNaN(date.getTime())) {
  279. return dateStr; // 如果无效,返回原字符串
  280. }
  281. // 获取年月日
  282. const year = date.getFullYear();
  283. const month = String(date.getMonth() + 1).padStart(2, '0');
  284. const day = String(date.getDate()).padStart(2, '0');
  285. // 获取星期几
  286. const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
  287. const weekday = weekdays[date.getDay()];
  288. // 获取时分
  289. const hours = String(date.getHours()).padStart(2, '0');
  290. const minutes = String(date.getMinutes()).padStart(2, '0');
  291. // 组合成新格式
  292. return `${year}-${month}-${day}(星期${weekday}) ${hours}:${minutes}`;
  293. }
  294. function formatTime(timeString) {
  295. const commentDate = new Date(formatDateS(timeString));
  296. const now = new Date();
  297. const diff = now - commentDate;
  298. const minutes = Math.floor(diff / 60000);
  299. const hours = Math.floor(diff / 3600000);
  300. const days = Math.floor(diff / 86400000);
  301. if (minutes < 1) { // 修改这里以处理0分钟
  302. return "刚刚";
  303. } else if (minutes < 60) {
  304. return `${minutes}分钟前`;
  305. } else if (hours < 24) {
  306. return `${hours}小时前`;
  307. } else {
  308. return commentDate.toISOString().split('T')[0];
  309. }
  310. }
  311. const sortedCommentList = computed(() => {
  312. return commentList.value.sort((a, b) =>
  313. new Date(formatDateS(b.commentTime)) - new Date(formatDateS(a.commentTime))
  314. );
  315. });
  316. onShareAppMessage(async (res) => {
  317. const processedImage = courseDetail.value.cover;
  318. // console.log(processedImage)
  319. return {
  320. title: courseName.value,
  321. path: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${courseId.value}&title=${courseName.value}`,
  322. imageUrl: processedImage
  323. };
  324. })
  325. onShareTimeline(async () => {
  326. return {
  327. title: courseName.value,
  328. query: `id=${courseId.value}&title=${courseName.value}`,
  329. imageUrl: courseDetail.value.cover
  330. };
  331. })
  332. // 线上-点击预约-跳转预告
  333. function toVideo(){
  334. wx.getChannelsLiveNoticeInfo({
  335. 'finderUserName': "sphIfs9sYiL5RB3",
  336. success: (res)=>{
  337. let noticeId = null;
  338. if(res.otherInfos.length > 0){
  339. let date1 = new Date(formatDateS(courseDetail.value.date))
  340. res.otherInfos.forEach(item=>{
  341. let date = new Date(item.startTime*1000);
  342. // console.log(item,date1, date)
  343. if(date1.getTime()===date.getTime()){
  344. noticeId = item.noticeId;
  345. }
  346. })
  347. }
  348. // console.log(noticeId)
  349. if(!noticeId){
  350. noticeId = res.noticeId
  351. }
  352. // console.log(res)
  353. wx.reserveChannelsLive({
  354. "noticeId": noticeId,
  355. success: (resp)=>{
  356. console.log(resp)
  357. },
  358. fail: (err)=>{
  359. console.log(err)
  360. }
  361. })
  362. },
  363. fail(err) {
  364. console.error('调用失败:', err);
  365. }
  366. })
  367. }
  368. // 线上课程-获取视频号回放
  369. function getVideo(){
  370. const startDate = dayjs(courseDetail.value.date).format("YYYY-MM-DD");
  371. const endDate = dayjs(startDate).add(1, 'day').format("YYYY-MM-DD");
  372. // 获取时间戳
  373. const startTime = new Date(startDate).getTime();
  374. const endTime = new Date(endDate).getTime();
  375. wx.getChannelsLiveInfo({
  376. 'finderUserName': "sphIfs9sYiL5RB3",
  377. startTime: startTime,
  378. endTime: endTime,
  379. success: (res)=>{
  380. feedId.value = res.feedId;
  381. // console.log("直播信息", res)
  382. feedShow.value = true
  383. },
  384. fail(err) {
  385. console.error('调用失败:', err);
  386. }
  387. })
  388. }
  389. // 线上课程-进来后-是否显示预约按钮
  390. const videoShow = computed(()=>{
  391. // regType = 1 是线上课程
  392. // feedShow为true是回放
  393. if(courseDetail.value?.status==='2' || courseDetail.value?.status==='3'){
  394. return false;
  395. }
  396. return (courseDetail.value?.regType ==='1') && !feedShow.value
  397. })
  398. // 线下课程-是否显示报名按钮
  399. const regShow = computed(()=>{
  400. // regType = 0 是线上课程
  401. // 缺少判断是否购买
  402. return courseDetail.value?.regType ==='0'
  403. })
  404. function toReg(){
  405. let isReg = courseDetail.value.hasReg ?? false;
  406. let viewMode = courseDetail.value.viewMode
  407. if(isReg){
  408. return;
  409. }
  410. if(viewMode==='1' || (isMember.value && viewMode==='2')){
  411. // 直接报名
  412. console.log("直接报名")
  413. regCourse(courseId.value).then(res=>{
  414. if(res.code===0){
  415. init(courseId.value)
  416. }
  417. })
  418. return
  419. }
  420. uni.navigateTo({
  421. url: "/pages/goOnEdu/course/courseDetail/courseOrder?id=" + courseId.value
  422. })
  423. return
  424. }
  425. </script>
  426. <style lang="scss">
  427. .u-drawer-bottom {
  428. background-color: transparent !important;
  429. }
  430. </style>
  431. <style lang="scss" scoped>
  432. .container {
  433. // height: 100vh;
  434. width: 100vw;
  435. background-color: #fff;
  436. // padding: 0 20rpx;
  437. }
  438. .course-tab-list {
  439. display: flex;
  440. background-color: #f2f2f2;
  441. flex: 0 0 auto;
  442. margin: 0 20rpx;
  443. .course-tab-item {
  444. width: 100%;
  445. height: 80rpx;
  446. line-height: 80rpx;
  447. text-align: center;
  448. }
  449. .tab-active {
  450. border-bottom: 1px solid #0069f6;
  451. }
  452. }
  453. // .container-poster{
  454. // height: calc(100vh - 500rpx - env(safe-area-inset-bottom, 0));
  455. // }
  456. .content {
  457. overflow: scroll;
  458. // height: calc(100vh - 500rpx - env(safe-area-inset-bottom, 0));
  459. height: 700rpx;
  460. position: relative;
  461. .content-text {
  462. // env(safe-area-inset-bottom, 0)
  463. // padding: 0 20rpx 0;
  464. font-size: 38rpx;
  465. .text-title {
  466. font-weight: bold;
  467. margin-bottom: 15rpx;
  468. margin-top: 15rpx;
  469. }
  470. .text-content {
  471. font-size: 32rpx;
  472. margin-bottom: 20rpx;
  473. }
  474. .text-tip {
  475. color: red;
  476. // margin-bottom: 20rpx;
  477. margin-bottom: env(safe-area-inset-bottom, 0);
  478. }
  479. }
  480. .content-button-hold{
  481. width: 100%;
  482. height: 120rpx;
  483. }
  484. .content-button{
  485. width: 100%;
  486. // height: 100rpx;
  487. // line-height: 100rpx;
  488. text-align: center;
  489. background-color: #fe0000;
  490. color: #fff;
  491. // position: absolute;
  492. // bottom: 0;
  493. padding:20rpx 0 calc(20rpx + env(safe-area-inset-bottom, 0));
  494. }
  495. }
  496. .section-bottom {
  497. height: 90rpx;
  498. color: #fff;
  499. font-size: 34rpx;
  500. text-align: center;
  501. line-height: 80rpx;
  502. background-color: #fe0000;
  503. width: 100%;
  504. position: absolute;
  505. bottom: 0;
  506. box-sizing: content-box;
  507. padding-bottom: env(safe-area-inset-bottom, 0);
  508. }
  509. .bg-blue{
  510. background-color: #3097ff;
  511. }
  512. .comment-input-box {
  513. width: 100%;
  514. height: 100%;
  515. display: flex;
  516. box-sizing: border-box;
  517. padding: 0 20rpx;
  518. align-items: center;
  519. .comment-input {
  520. flex: 1;
  521. }
  522. .comment-button {
  523. flex: 0 0 auto;
  524. }
  525. }
  526. .comment-list-item {
  527. display: flex;
  528. padding: 20rpx 0;
  529. font-size: 28rpx;
  530. .comment-list-left {
  531. flex: 0 0 auto;
  532. padding-right: 20rpx;
  533. padding-left: 10rpx;
  534. .comment-list-avator {
  535. width: 100rpx;
  536. height: 100rpx;
  537. border-radius: 50%;
  538. }
  539. }
  540. .comment-list-right {
  541. flex: 1;
  542. .comment-list-username {
  543. padding-right: 25rpx;
  544. font-size: 32rpx;
  545. font-weight: bold;
  546. }
  547. }
  548. }
  549. </style>