courseHome.vue 14 KB

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