courseHome.vue 15 KB

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