courseHome.vue 15 KB

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