courseDetail.vue 14 KB

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