courseDetail.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  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}}</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.loc === '微信视频号' && 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. // 定义 Canvas 画布尺寸(按 5:4 比例)
  165. const canvasWidth = ref(750); // 推荐 2倍图尺寸
  166. const canvasHeight = ref(600);
  167. const shareImageUrl = ref(''); // 存储处理后的图片路径
  168. // 获取图片信息
  169. const getImageInfo = (imgUrl) => {
  170. return new Promise((resolve) => {
  171. uni.getImageInfo({
  172. src: imgUrl,
  173. success: (res) => resolve(res),
  174. fail: () => resolve(null)
  175. });
  176. });
  177. };
  178. // 动态裁剪并填充背景
  179. const processShareImage = async (imgUrl) => {
  180. try {
  181. const imageInfo = await getImageInfo(imgUrl);
  182. if (!imageInfo) return imgUrl;
  183. const ctx = uni.createCanvasContext('shareCanvas', this);
  184. const targetRatio = 5 / 4;
  185. const originalRatio = imageInfo.width / imageInfo.height;
  186. // 1. 绘制背景填充
  187. ctx.setFillStyle('#FFFFFF'); // 自定义背景色
  188. ctx.fillRect(0, 0, canvasWidth.value, canvasHeight.value);
  189. // 2. 计算绘制区域
  190. if (originalRatio < targetRatio) {
  191. // 比例不足时:居中缩放 + 左右填充
  192. const scale = canvasHeight.value / imageInfo.height;
  193. const drawWidth = imageInfo.width * scale;
  194. ctx.drawImage(
  195. imageInfo.path,
  196. (canvasWidth.value - drawWidth) / 2, // 水平居中
  197. 0,
  198. drawWidth,
  199. canvasHeight.value
  200. );
  201. } else {
  202. // 比例足够时:裁剪中间区域
  203. const cropWidth = imageInfo.height * targetRatio;
  204. ctx.drawImage(
  205. imageInfo.path,
  206. (imageInfo.width - cropWidth) / 2, // 水平居中裁剪
  207. 0,
  208. cropWidth,
  209. imageInfo.height,
  210. 0,
  211. 0,
  212. canvasWidth.value,
  213. canvasHeight.value
  214. );
  215. }
  216. // 3. 导出临时图片
  217. ctx.draw(false, () => {
  218. setTimeout(async () => { // 解决 Canvas 渲染延迟
  219. const res = await uni.canvasToTempFilePath({
  220. canvasId: 'shareCanvas',
  221. fileType: 'jpg',
  222. quality: 0.9
  223. });
  224. shareImageUrl.value = res.tempFilePath;
  225. }, 300);
  226. });
  227. } catch (err) {
  228. return imgUrl; // 降级处理
  229. }
  230. };
  231. function toSend() {
  232. sendComment({
  233. courseId: courseId.value,
  234. content: comment.value,
  235. commentTime: formatDate(new Date())
  236. }).then(res => {
  237. getComment(courseId.value)
  238. comment.value = ""
  239. })
  240. }
  241. function formatDate(date) {
  242. const pad = (num) => num.toString().padStart(2, '0');
  243. const year = date.getFullYear();
  244. const month = pad(date.getMonth() + 1); // 月份从0开始,需加1
  245. const day = pad(date.getDate());
  246. const hours = pad(date.getHours());
  247. const minutes = pad(date.getMinutes());
  248. const seconds = pad(date.getSeconds());
  249. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  250. }
  251. function showBuyAction() {
  252. if (courseDetail.value.viewMode === '2' &&
  253. !isMember.value &&
  254. !courseDetail.value.hasBuy &&
  255. currentTab.value === 0) {
  256. // console.log(1)
  257. return true
  258. }
  259. // 付费,不管是不是会员,并且没买的
  260. if (courseDetail.value.viewMode === '3' &&
  261. !courseDetail.value.hasBuy &&
  262. currentTab.value === 0) {
  263. // console.log(2)
  264. return true
  265. }
  266. // console.log(3)
  267. return false
  268. }
  269. const showBuy = ref(false)
  270. const isMember = computed(() => {
  271. return authStore.userInfo.isMember == '0' ? false : true
  272. })
  273. const isLogin = computed(() => {
  274. if (getToken() && authStore.isAuthenticated) {
  275. return true
  276. } else {
  277. return false
  278. }
  279. })
  280. const loginShow = ref(true)
  281. function toLogin() {
  282. // loginShow.value = false
  283. let url = {
  284. url: '/pages/goOnEdu/course/courseDetail/courseDetail',
  285. id: courseId.value,
  286. title: courseName.value
  287. }
  288. uni.setStorageSync("redirect", url)
  289. uni.navigateTo({
  290. url: `/pages/login/login`
  291. })
  292. }
  293. // 初始化页面
  294. onLoad((option) => {
  295. const {
  296. id,
  297. title
  298. } = option;
  299. courseId.value = id
  300. courseName.value = title
  301. uni.setNavigationBarTitle({
  302. title: title
  303. });
  304. if (isLogin.value) {
  305. loginShow.value = false
  306. } else {
  307. loginShow.value = true
  308. }
  309. // toVideo()
  310. // loginShow.value = true
  311. })
  312. onShow(() => {
  313. if (isLogin.value) {
  314. init(courseId.value)
  315. getComment(courseId.value)
  316. }
  317. })
  318. function formatDateS(dateStr) {
  319. return dateStr.replace(" ", "T");
  320. }
  321. function formatTime(timeString) {
  322. const commentDate = new Date(formatDateS(timeString));
  323. const now = new Date();
  324. const diff = now - commentDate;
  325. const minutes = Math.floor(diff / 60000);
  326. const hours = Math.floor(diff / 3600000);
  327. const days = Math.floor(diff / 86400000);
  328. if (minutes < 1) { // 修改这里以处理0分钟
  329. return "刚刚";
  330. } else if (minutes < 60) {
  331. return `${minutes}分钟前`;
  332. } else if (hours < 24) {
  333. return `${hours}小时前`;
  334. } else {
  335. return commentDate.toISOString().split('T')[0];
  336. }
  337. }
  338. const sortedCommentList = computed(() => {
  339. return commentList.value.sort((a, b) =>
  340. new Date(formatDateS(b.commentTime)) - new Date(formatDateS(a.commentTime))
  341. );
  342. });
  343. onShareAppMessage(async (res) => {
  344. const processedImage = await processShareImage(courseDetail.value.cover);
  345. // console.log(processedImage)
  346. return {
  347. title: courseName.value,
  348. path: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${courseId.value}&title=${courseName.value}`,
  349. imageUrl: processedImage
  350. };
  351. })
  352. onShareTimeline(async () => {
  353. return {
  354. title: courseName.value,
  355. query: `id=${courseId.value}&title=${courseName.value}`,
  356. imageUrl: courseDetail.value.cover
  357. };
  358. })
  359. // 跳转预告
  360. function toVideo(){
  361. wx.getChannelsLiveNoticeInfo({
  362. 'finderUserName': 'sphIfs9sYiL5RB3',
  363. success: (res)=>{
  364. let noticeId = null;
  365. if(res.otherInfos.length > 0){
  366. let date1 = new Date(formatDateS(courseDetail.value.courseDate))
  367. res.otherInfos.forEach(item=>{
  368. let date = new Date(item.startTime*1000);
  369. // console.log(item,date1, date)
  370. if(date1.getTime()===date.getTime()){
  371. noticeId = item.noticeId;
  372. }
  373. })
  374. }
  375. // console.log(noticeId)
  376. if(!noticeId){
  377. noticeId = res.noticeId
  378. }
  379. // console.log(res)
  380. wx.reserveChannelsLive({
  381. "noticeId": noticeId,
  382. success: (resp)=>{
  383. console.log(resp)
  384. },
  385. fail: (err)=>{
  386. console.log(err)
  387. }
  388. })
  389. }
  390. })
  391. }
  392. function getVideo(){
  393. const startDate = new Date(formatDateS(courseDetail.value.courseDate));
  394. // 计算 endTime(startDate 加一天)
  395. const endDate = new Date(startDate);
  396. endDate.setDate(startDate.getDate() + 1);
  397. // 获取时间戳
  398. const startTime = startDate.getTime();
  399. const endTime = endDate.getTime();
  400. wx.getChannelsLiveInfo({
  401. 'finderUserName': 'sphIfs9sYiL5RB3',
  402. 'startTime': startTime,
  403. 'endTime': endTime,
  404. success: (res)=>{
  405. feedId.value = res.feedId;
  406. console.log("直播信息", res.feedId)
  407. feedShow.value = true
  408. // wx.openChannelsLive({
  409. // finderUserName: 'sphIfs9sYiL5RB3',
  410. // feedId: res.feedId,
  411. // nonceId: res.nonceId,
  412. // success: (resp)=>{
  413. // console.log(resp)
  414. // }
  415. // })
  416. }
  417. })
  418. }
  419. const videoShow = computed(()=>{
  420. return courseDetail.value?.loc==='微信视频号' && !feedShow.value
  421. })
  422. </script>
  423. <style lang="scss">
  424. .u-drawer-bottom {
  425. background-color: transparent !important;
  426. }
  427. </style>
  428. <style lang="scss" scoped>
  429. .container {
  430. // height: 100vh;
  431. width: 100vw;
  432. background-color: #fff;
  433. // padding: 0 20rpx;
  434. }
  435. .course-tab-list {
  436. display: flex;
  437. background-color: #f2f2f2;
  438. flex: 0 0 auto;
  439. margin: 0 20rpx;
  440. .course-tab-item {
  441. width: 100%;
  442. height: 80rpx;
  443. line-height: 80rpx;
  444. text-align: center;
  445. }
  446. .tab-active {
  447. border-bottom: 1px solid #0069f6;
  448. }
  449. }
  450. // .container-poster{
  451. // height: calc(100vh - 500rpx - env(safe-area-inset-bottom, 0));
  452. // }
  453. .content {
  454. overflow: scroll;
  455. // height: calc(100vh - 500rpx - env(safe-area-inset-bottom, 0));
  456. height: 700rpx;
  457. position: relative;
  458. .content-text {
  459. // env(safe-area-inset-bottom, 0)
  460. // padding: 0 20rpx 0;
  461. font-size: 38rpx;
  462. .text-title {
  463. font-weight: bold;
  464. margin-bottom: 15rpx;
  465. margin-top: 15rpx;
  466. }
  467. .text-content {
  468. font-size: 32rpx;
  469. margin-bottom: 20rpx;
  470. }
  471. .text-tip {
  472. color: red;
  473. // margin-bottom: 20rpx;
  474. margin-bottom: env(safe-area-inset-bottom, 0);
  475. }
  476. }
  477. .content-button-hold{
  478. width: 100%;
  479. height: 120rpx;
  480. }
  481. .content-button{
  482. width: 100%;
  483. // height: 100rpx;
  484. // line-height: 100rpx;
  485. text-align: center;
  486. background-color: #fe0000;
  487. color: #fff;
  488. // position: absolute;
  489. // bottom: 0;
  490. padding:20rpx 0 calc(20rpx + env(safe-area-inset-bottom, 0));
  491. }
  492. }
  493. .section-bottom {
  494. height: 90rpx;
  495. color: #fff;
  496. font-size: 34rpx;
  497. text-align: center;
  498. line-height: 80rpx;
  499. background-color: #fe0000;
  500. width: 100%;
  501. position: absolute;
  502. bottom: 0;
  503. box-sizing: content-box;
  504. padding-bottom: env(safe-area-inset-bottom, 0);
  505. }
  506. .comment-input-box {
  507. width: 100%;
  508. height: 100%;
  509. display: flex;
  510. box-sizing: border-box;
  511. padding: 0 20rpx;
  512. align-items: center;
  513. .comment-input {
  514. flex: 1;
  515. }
  516. .comment-button {
  517. flex: 0 0 auto;
  518. }
  519. }
  520. .comment-list-item {
  521. display: flex;
  522. padding: 20rpx 0;
  523. font-size: 28rpx;
  524. .comment-list-left {
  525. flex: 0 0 auto;
  526. padding-right: 20rpx;
  527. padding-left: 10rpx;
  528. .comment-list-avator {
  529. width: 100rpx;
  530. height: 100rpx;
  531. border-radius: 50%;
  532. }
  533. }
  534. .comment-list-right {
  535. flex: 1;
  536. .comment-list-username {
  537. padding-right: 25rpx;
  538. font-size: 32rpx;
  539. font-weight: bold;
  540. }
  541. }
  542. }
  543. </style>