courseHome.vue 14 KB

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