courseHome.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <view class="container">
  3. <view class="header-box">
  4. <u-search @search="toSearch" :show-action="false" shape="round" placeholder="搜索您想要的内容"
  5. v-model="searchForm.keyword"></u-search>
  6. <!-- tabs -->
  7. <view class="tabs-container">
  8. <u-tabs
  9. :list="tabsList"
  10. :is-scroll="true"
  11. font-size="32"
  12. :bold="false"
  13. inactive-color="#000000"
  14. active-color="#ffffff"
  15. :bar-style="{'background-color': '#2979ff'}"
  16. :active-item-style="{'background-color': '#2979ff', 'border-radius':'30rpx'}"
  17. :gutter="40"
  18. height="55"
  19. v-model="currentTab"
  20. :show-bar="false"
  21. duration="0"
  22. @change="changeTab"></u-tabs>
  23. <!-- <scroll-view scroll-x="true" style="width:100%;" :scroll-with-animation="true" :show-scrollbar="true">
  24. </scroll-view> -->
  25. <!-- <u-icon name="list" style="flex: 0 0 auto;padding-left: 20rpx;"></u-icon> -->
  26. </view>
  27. </view>
  28. <!-- 搜索 -->
  29. <!-- 列表 -->
  30. <view class="course-list">
  31. <view v-for="(course,index) in filterCourses" :key="course.id" class="course-list-item">
  32. <view class="course-item">
  33. <view class="course-item-image">
  34. <image class="course-image" :src="course.thumImg ? course.thumImg
  35. : (course.cover ? course.cover
  36. : `${FILE_URL}/peixunyugao.jpg`)"
  37. mode="aspectFit" @click="toPage(course)"></image>
  38. </view>
  39. <view class="course-item-content">
  40. <view class="course-title">
  41. <text @click="toPage(course)">{{ course.title }}</text>
  42. <!-- 1.隐藏收藏功能 -->
  43. <!-- <image style="width: 25rpx;height: 25rpx;padding-left: 20rpx;"
  44. :src="course.hasFavi ? `${FILE_URL}/edu-icon/favi-icon.png` : `${FILE_URL}/edu-icon/no-favi-icon.png`"
  45. @click.stop="collectCourse(course.id, index, course.hasFavi)">
  46. </image> -->
  47. </view>
  48. <view class="course-type">{{ course.type }}</view>
  49. <view class="course-teacher">
  50. <u-icon name="account" size="28"></u-icon>
  51. {{ course.lecturer }}
  52. </view>
  53. <view class="course-date">
  54. <u-icon name="clock" size="28"></u-icon>
  55. {{ getDateWeek(course.date) }}
  56. </view>
  57. <!-- <view class="course-price"
  58. v-if=" (course.viewMode === '3') || (isMember && course.viewMode==='2')">
  59. ¥{{ isMember ? course.priceMember : course.price }}元
  60. </view>
  61. <view v-else style="height: 30rpx;width: 100%;"></view> -->
  62. <!-- 如果没结束 -->
  63. <view class="func" v-if="course.status !== '2' && course.status !== '3'"
  64. @click="toPage(course)">
  65. <!-- 如果是免费,直接显示免费 -->
  66. <view v-if="course.viewMode==='1'" class="func-box" style="justify-content: flex-end;">
  67. <view class="button free">免费</view>
  68. </view>
  69. <view v-else-if="course.viewMode==='2' && isMember" class="func-box">
  70. <view class="price">¥{{ course.price }}元</view>
  71. <view class="button member-free">会员免费</view>
  72. </view>
  73. <view v-else-if="course.viewMode==='2' && !isMember" class="func-box">
  74. <view style="flex: 0 0 auto;display: flex;align-items: center;">
  75. <view class="member-free">
  76. 会员免费
  77. </view>
  78. <view class="not-member-price">
  79. 非会员:¥{{course.price}}元
  80. </view>
  81. </view>
  82. <view :class="['button', course.hasBuy ? 'free' : 'buy']">
  83. {{course.hasBuy ? '已购买' : '立即购买'}}
  84. </view>
  85. </view>
  86. <view v-else-if="course.viewMode==='3' && course.price===course.priceMember"
  87. class="func-box">
  88. <view class="price">¥{{ course.price }}元</view>
  89. <view :class="['button', course.hasBuy ? 'free' : 'buy']">
  90. {{course.hasBuy ? '已购买' : '立即购买'}}
  91. </view>
  92. </view>
  93. <view v-else-if="course.viewMode==='3' && course.price!==course.priceMember"
  94. class="func-box">
  95. <view style="flex: 0 0 auto;display: flex;align-items: center;">
  96. <view class="member-free">
  97. {{`会员:${course.priceMember}元`}}
  98. </view>
  99. <view class="not-member-price">
  100. 非会员:¥{{course.price}}元
  101. </view>
  102. </view>
  103. <view :class="['button', course.hasBuy ? 'free' : 'buy']">
  104. {{course.hasBuy ? '已购买' : '立即购买'}}
  105. </view>
  106. </view>
  107. </view>
  108. <!-- <button :class="['button', getButtonClass(course)]">{{ getButtonText(course) }}</button> -->
  109. <!-- 如果课程结束了 -->
  110. <view class="func" v-else>
  111. <!-- regType: 0 线下 ;1 线上 -->
  112. <view v-if="course.regType==='0' && course.status === '2'" class="func-box"
  113. style="justify-content: flex-end;">
  114. <view class="button over" @click="toPage(course)">已结束</view>
  115. </view>
  116. <view v-if="course.regType==='0' && course.status === '3' && !course.actId" class="func-box"
  117. style="justify-content: flex-end;">
  118. <view class="button over" @click="toPage(course)">已结束</view>
  119. </view>
  120. <view v-if="course.regType==='0' && course.status === '3' && course.actId" class="func-box"
  121. style="justify-content: flex-end;">
  122. <view class="button-long over" @click="toAct(course.actId)">已结束,点击查看活动</view>
  123. </view>
  124. <view v-if="course.regType==='1'" class="func-box" style="justify-content: flex-end;">
  125. <view class="button-long over" @click="toPage(course)">已结束,点击查看回放</view>
  126. </view>
  127. </view>
  128. </view>
  129. </view>
  130. <u-line />
  131. </view>
  132. </view>
  133. <view style="margin: 20rpx 0;">
  134. <u-loadmore :status="loadMoreStatus" @loadmore="loadmore" :load-text="loadText" />
  135. <!-- <uni-load-more :status="loadMoreStatus" :load-text="loadText" @loadmore="loadmore"></uni-load-more> -->
  136. </view>
  137. </view>
  138. </template>
  139. <script setup>
  140. import {
  141. ref,
  142. onMounted,
  143. watch,
  144. } from 'vue';
  145. import {
  146. onReachBottom
  147. } from '@dcloudio/uni-app'
  148. import {
  149. loadCourseCate,
  150. loadCourseList,
  151. courseFavi,
  152. courseCancelFavi
  153. } from "@/api/edu.js"
  154. import {
  155. useCourseStore
  156. } from "@/store/courseStore.js"
  157. import {
  158. useAuthStore
  159. } from '@/store/authStore.js';
  160. import configService from '@/utils/baseurl.js'
  161. const FILE_URL = configService.FILE_URL;
  162. const courseStore = useCourseStore();
  163. const authStore = useAuthStore();
  164. const isMember = ref(false);
  165. const tabsList = ref([]);
  166. const currentTab = ref(courseStore.currentTab);
  167. const searchForm = ref({
  168. keyword: "",
  169. pageNumber: 1,
  170. pageSize: 10,
  171. type: ""
  172. })
  173. const pageNumber = ref(1)
  174. const pageSize = ref(10)
  175. const total = ref(0)
  176. const loadMoreStatus = ref('loadmore')
  177. const courseMember = {
  178. 1: "免费",
  179. 2: "会员免费",
  180. 3: "付费",
  181. }
  182. const loadText = {
  183. loadmore: '点击或上拉加载更多',
  184. loading: '努力加载中',
  185. nomore: '已加载全部数据'
  186. }
  187. // 展示的课程
  188. const filterCourses = ref([]);
  189. const courses = ref([{
  190. id: 1,
  191. title: "前端开发基础前端开发基础前端开发基础",
  192. type: "精英训练营",
  193. name: "张老师",
  194. date: "2023-10-01",
  195. imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.YKoZzgmubNBxQ8j-mmoTKAHaEK?rs=1&pid=ImgDetMain",
  196. price: 99.00,
  197. hasBuy: false,
  198. hasFavi: true,
  199. viewMode: "免费" // 新增字段,标识课程的付费类型
  200. }, ]);
  201. // 列表样式-按钮的文字
  202. function getButtonText(course) {
  203. console.log(course)
  204. // const currentDate = new Date();
  205. // const classDate = new Date(formatDateS(course.date));
  206. // if(currentDate > classDate) {
  207. // }
  208. if (course.viewMode === '1') {
  209. return "免费";
  210. }
  211. if (course.viewMode === '2') {
  212. if (isMember.value) return "会员免费";
  213. if (!course.hasBuy && !isMember.value) return "会员免费";
  214. if (course.hasBuy) {
  215. // return currentDate < classDate ? "点击查看" : "点击查看回放";
  216. return "点击查看";
  217. }
  218. }
  219. if (course.viewMode === '3') {
  220. if (!course.hasBuy) return "点击购买";
  221. return currentDate < classDate ? "点击查看" : "点击查看回放";
  222. }
  223. return "error"; // 默认返回
  224. }
  225. // 列表样式-按钮的样式
  226. function getButtonClass(course) {
  227. if (courseMember[course.viewMode] === '免费') return 'free';
  228. if (courseMember[course.viewMode] === '会员免费') {
  229. if (isMember.value) return 'member-free';
  230. return course.hasBuy ? (new Date() < new Date(course.date) ? 'purchased' : 'replay') :
  231. 'member-free';
  232. }
  233. if (courseMember[course.viewMode] === '付费') {
  234. return course.hasBuy ? (new Date() < new Date(course.date) ? 'purchased' : 'replay') : 'purchase';
  235. }
  236. return 'error';
  237. }
  238. function formatDateS(dateStr) {
  239. return dateStr.replace(" ", "T");
  240. }
  241. // 日期格式:xxxx年xx月xx日(星期x)
  242. function getDateWeek(dateStr) {
  243. // 将日期字符串转换为 Date 对象
  244. const date = new Date(dateStr.replace(/-/g, '/'));
  245. // 检查日期是否有效
  246. if (isNaN(date.getTime())) {
  247. return dateStr; // 如果无效,返回原字符串
  248. }
  249. // 获取年月日
  250. const year = date.getFullYear();
  251. const month = String(date.getMonth() + 1).padStart(2, '0');
  252. const day = String(date.getDate()).padStart(2, '0');
  253. // 获取星期几
  254. const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
  255. const weekday = weekdays[date.getDay()];
  256. // 获取时分
  257. const hours = String(date.getHours()).padStart(2, '0');
  258. const minutes = String(date.getMinutes()).padStart(2, '0');
  259. // 组合成新格式
  260. return `${year}-${month}-${day}(星期${weekday}) ${hours}:${minutes}`;
  261. }
  262. // 切换搜索框下面的tab
  263. function changeTab(index) {
  264. // 0618重写,切换时,pageNumber为1
  265. currentTab.value = index;
  266. courseStore.setCurrentTab(index)
  267. pageNumber.value = 1
  268. search(searchForm.value.keyword)
  269. }
  270. // 搜索
  271. function toSearch(e) {
  272. // 搜索时pageNumber为1
  273. pageNumber.value = 1
  274. search(e)
  275. }
  276. // 获取列表数据
  277. function search(e) {
  278. // 如果是全部,则不传type这个值
  279. searchForm.value = {
  280. keyword: e,
  281. pageNumber: pageNumber.value,
  282. pageSize: pageSize.value,
  283. ...(tabsList.value[currentTab.value].code && {
  284. courseType: tabsList.value[currentTab.value].code
  285. })
  286. }
  287. loadMoreStatus.value = 'loading'
  288. loadCourseList(searchForm.value).then(res => {
  289. if (res?.data) {
  290. total.value = res.count;
  291. // 如果pageNumber为1,清空courses
  292. courses.value = pageNumber.value == 1 ? [] : courses.value
  293. courses.value = [...courses.value, ...res.data];
  294. filterCourses.value = courses.value
  295. loadMoreStatus.value = total.value === courses.value.length ? 'nomore' : 'loadmore';
  296. pageNumber.value++;
  297. }
  298. })
  299. }
  300. // 收藏
  301. async function collectCourse(id, index, hasFavi) {
  302. try {
  303. let res;
  304. if (hasFavi) {
  305. res = await courseCancelFavi({
  306. id
  307. })
  308. } else {
  309. res = await courseFavi({
  310. id
  311. })
  312. }
  313. if (res.code == 0) {
  314. courses.value[index].hasFavi = !courses.value[index].hasFavi
  315. }
  316. } catch (err) {
  317. courses.value[index].hasFavi = courses.value[index].hasFavi
  318. }
  319. }
  320. // 初始化:获取分类数据和列表数据
  321. async function init() {
  322. const res = await loadCourseCate()
  323. if (res?.data) {
  324. tabsList.value = [{
  325. code: '',
  326. name: '全部',
  327. }, ...res.data]
  328. pageNumber.value = 1;
  329. search("");
  330. }
  331. }
  332. // 跳转到课程详情页
  333. function toPage(course) {
  334. uni.navigateTo({
  335. url: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${course.id}&title=${course.title}`
  336. });
  337. }
  338. // 加载更多
  339. function loadmore() {
  340. if (courses.value.length === total.value) {
  341. return;
  342. }
  343. search(searchForm.value.keyword);
  344. }
  345. function toAct(actId) {
  346. // console.log(actId);
  347. uni.navigateTo({
  348. url: `/pages/dynamic/dynamicDetail/dynamicDetail?id=${actId}`
  349. })
  350. }
  351. onMounted(() => {
  352. isMember.value = authStore.userInfo.isMember === 1 ? true : false
  353. init();
  354. watch(currentTab, (newValue) => {
  355. courseStore.setCurrentTab(newValue); // 如果需要在切换时更新 Pinia 状态
  356. });
  357. });
  358. onReachBottom(() => {
  359. loadmore()
  360. })
  361. </script>
  362. <style>
  363. ::v-deep(.u-scroll-box .u-tab-item) {
  364. margin-right: 20rpx;
  365. }
  366. </style>
  367. <style lang="scss" scoped>
  368. .container {
  369. // padding: 20px;
  370. // width: 100vw;
  371. // overflow: hidden;
  372. .header-box{
  373. @include topMagnet();
  374. background: #ffffff;
  375. }
  376. }
  377. .title {
  378. font-size: 48rpx;
  379. margin-bottom: 30rpx;
  380. }
  381. .tabs-container {
  382. padding: 40rpx 0 35rpx;
  383. // display: flex;
  384. // width: 100%;
  385. /* 确保宽度足够 */
  386. // overflow-x: auto;
  387. /* 横向滚动 */
  388. // -webkit-overflow-scrolling: touch;
  389. /* 在iOS上启用流畅滚动 */
  390. }
  391. .course-list-item{
  392. padding: 20rpx 0;
  393. &:first-child{
  394. padding: 0 0 20rpx;
  395. }
  396. }
  397. .course-item {
  398. // margin: 20rpx 0;
  399. display: flex;
  400. overflow: hidden;
  401. .course-item-image {
  402. width: 200rpx;
  403. height: 260rpx;
  404. flex: 0 0 auto;
  405. margin-right: 20rpx;
  406. .course-image {
  407. width: 100%;
  408. height: 100%;
  409. }
  410. }
  411. .course-item-content {
  412. flex: 1;
  413. position: relative;
  414. view {
  415. margin-bottom: 15rpx;
  416. }
  417. .course-title {
  418. font-weight: bold;
  419. margin-right: 10px;
  420. font-size: 28rpx;
  421. color: #000;
  422. }
  423. .course-type,
  424. .course-teacher,
  425. .course-date,
  426. .course-price {
  427. font-size: 30rpx;
  428. color: #000;
  429. }
  430. .course-price {
  431. color: #fe0000;
  432. letter-spacing: 2rpx;
  433. }
  434. .func {
  435. display: flex;
  436. justify-content: space-between;
  437. align-items: flex-end;
  438. font-size: $uni-font-size-2;
  439. font-weight: bold;
  440. margin-top: 10rpx;
  441. view {
  442. margin-bottom: 0rpx;
  443. }
  444. .func-box {
  445. display: flex;
  446. justify-content: space-between;
  447. width: 100%;
  448. align-items: center;
  449. }
  450. .button {
  451. text-align: center;
  452. width: 130rpx;
  453. }
  454. .button-long {
  455. text-align: center;
  456. width: 270rpx;
  457. }
  458. .price {
  459. color: $uni-color-error;
  460. font-size: $uni-title-font-size-2;
  461. }
  462. .not-member-price {
  463. font-size: $uni-font-size-3;
  464. color: $uni-text-color-grey;
  465. text-align: end;
  466. }
  467. .buy {
  468. padding: 6rpx 25rpx;
  469. background-color: $uni-color-error;
  470. border-radius: $uni-card-border-radius;
  471. color: $uni-text-color-inverse;
  472. }
  473. .free {
  474. padding: 6rpx 25rpx;
  475. background-color: $uni-color-primary;
  476. border-radius: $uni-card-border-radius;
  477. color: $uni-text-color-inverse;
  478. }
  479. .over {
  480. padding: 6rpx 25rpx;
  481. border-radius: $uni-card-border-radius;
  482. color: $uni-text-color-inverse;
  483. background-color: #8f9092;
  484. }
  485. .member-free {
  486. padding: 10rpx 20rpx;
  487. @include backgroundImg('https://www.gzrea.cn/wxmp/static/icon/bg-label.png');
  488. color: $uni-text-color;
  489. }
  490. }
  491. .button::after {
  492. content: none;
  493. /* 移除内容 */
  494. }
  495. // .button {
  496. // position: absolute;
  497. // right: 0;
  498. // bottom: 0;
  499. // min-width: 80px;
  500. // padding: 0 40rpx;
  501. // white-space: nowrap;
  502. // height: 45rpx;
  503. // line-height: 45rpx;
  504. // font-size: 22rpx;
  505. // /* padding: 0; */
  506. // border-radius: 50rpx;
  507. // color: white;
  508. // border: none;
  509. // }
  510. // .free {
  511. // background-color: #006af4;
  512. // }
  513. // .purchase {
  514. // background-color: #fe0000;
  515. // }
  516. // .member-free {
  517. // background-color: transparent;
  518. // background-image: url('https://www.gzrea.cn/wxmp/static/icon/bg-label.png');
  519. // background-size: cover;
  520. // background-repeat: no-repeat;
  521. // color: #000;
  522. // height: initial;
  523. // padding: 6rpx 0 3rpx;
  524. // border-radius: 0;
  525. // }
  526. // .replay {
  527. // background-color: #006af4;
  528. // }
  529. // .purchased {
  530. // background-color: #006af4;
  531. // }
  532. }
  533. }
  534. </style>