courseHome.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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" @click="toPage(course)">
  16. <view class="course-item">
  17. <view class="course-item-image">
  18. <image class="course-image" :src="course.imgUrl" mode="aspectFill"></image>
  19. </view>
  20. <view class="course-item-content">
  21. <view class="course-title">
  22. <text>{{ course.courseName }}</text>
  23. <image style="width: 25rpx;height: 25rpx;padding-left: 20rpx;"
  24. :src="course.hasFavi ? `${FILE_URL}/edu-icon/favi-icon.png` : `${FILE_URL}/edu-icon/no-favi-icon.png`"
  25. @click.stop="collectCourse(course.id, index, course.hasFavi)">
  26. </image>
  27. </view>
  28. <view class="course-type">{{ course.courseType }}</view>
  29. <view class="course-teacher">
  30. <u-icon name="account" size="28"></u-icon>
  31. {{ course.name }}
  32. </view>
  33. <view class="course-date">
  34. <u-icon name="clock" size="28"></u-icon>
  35. {{ getDateWeek(course.courseDate) }}
  36. </view>
  37. <view class="course-price">¥{{ course.price }}元</view>
  38. <button :class="['button', getButtonClass(course)]">{{ getButtonText(course) }}</button>
  39. </view>
  40. </view>
  41. <u-line />
  42. </view>
  43. </view>
  44. <view style="margin: 20rpx 0;">
  45. <u-loadmore :status="loadMoreStatus" @loadmore="loadmore" :load-text="loadText" />
  46. <!-- <uni-load-more :status="loadMoreStatus" :load-text="loadText" @loadmore="loadmore"></uni-load-more> -->
  47. </view>
  48. </view>
  49. </template>
  50. <script setup>
  51. import {
  52. ref,
  53. onMounted,
  54. watch,
  55. } from 'vue';
  56. import {
  57. onReachBottom
  58. } from '@dcloudio/uni-app'
  59. import {
  60. loadCourseCate,
  61. loadCourseList,
  62. courseFavi,
  63. courseCancelFavi
  64. } from "@/api/edu.js"
  65. import {
  66. useCourseStore
  67. } from "@/store/courseStore.js"
  68. import {
  69. useAuthStore
  70. } from '@/store/authStore.js';
  71. import configService from '@/utils/baseurl.js'
  72. const FILE_URL = configService.FILE_URL;
  73. const courseStore = useCourseStore();
  74. const authStore = useAuthStore();
  75. const isMember = ref(false);
  76. const tabsList = ref([]);
  77. const currentTab = ref(courseStore.currentTab);
  78. const searchForm = ref({
  79. keyword: "",
  80. pageNumber: 1,
  81. pageSize: 10,
  82. })
  83. const pageNumber = ref(1)
  84. const pageSize = ref(20)
  85. const total = ref(0)
  86. const loadMoreStatus = ref('loadmore')
  87. const courseMember = {
  88. 1: "免费",
  89. 2: "会员免费",
  90. 3: "付费",
  91. }
  92. const loadText = {
  93. loadmore: '点击或上拉加载更多',
  94. loading: '努力加载中',
  95. nomore: '实在没有了'
  96. }
  97. // 展示的课程
  98. const filterCourses = ref([]);
  99. const courses = ref([{
  100. id: 1,
  101. courseName: "前端开发基础前端开发基础前端开发基础",
  102. courseType: "精英训练营",
  103. name: "张老师",
  104. courseDate: "2023-10-01",
  105. imgUrl: "https://tse3-mm.cn.bing.net/th/id/OIP-C.YKoZzgmubNBxQ8j-mmoTKAHaEK?rs=1&pid=ImgDetMain",
  106. price: 99.00,
  107. hasBuy: false,
  108. hasFavi: true,
  109. viewMode: "免费" // 新增字段,标识课程的付费类型
  110. }, ]);
  111. // 按钮的文字
  112. function getButtonText(course) {
  113. const currentDate = new Date();
  114. const classDate = new Date(course.courseDate);
  115. if (course.viewMode === '1') {
  116. return "免费";
  117. }
  118. if (course.viewMode === '2') {
  119. if (isMember.value) return "会员免费";
  120. if (!course.hasBuy && !isMember.value) return "会员免费";
  121. if (course.hasBuy) {
  122. return currentDate < classDate ? "点击查看" : "点击查看回放";
  123. }
  124. }
  125. if (course.viewMode === '3') {
  126. if (!course.hasBuy) return "点击购买";
  127. return currentDate < classDate ? "点击查看" : "点击查看回放";
  128. }
  129. return "error"; // 默认返回
  130. }
  131. // 按钮的样式
  132. function getButtonClass(course) {
  133. if (courseMember[course.viewMode] === '免费') return 'free';
  134. if (courseMember[course.viewMode] === '会员免费') {
  135. if (isMember.value) return 'member-free';
  136. return course.hasBuy ? (new Date() < new Date(course.courseDate) ? 'purchased' : 'replay') :
  137. 'member-free';
  138. }
  139. if (courseMember[course.viewMode] === '付费') {
  140. return course.hasBuy ? (new Date() < new Date(course.courseDate) ? 'purchased' : 'replay') : 'purchase';
  141. }
  142. return 'error';
  143. }
  144. // 日期格式:xxxx年xx月xx日(星期x)
  145. function getDateWeek(val) {
  146. const date = new Date(val);
  147. const daysOfWeek = ['日', '一', '二', '三', '四', '五', '六'];
  148. const year = date.getFullYear();
  149. const month = date.getMonth() + 1; // 注意:月份从0开始
  150. const day = date.getDate();
  151. const dayOfWeek = daysOfWeek[date.getUTCDay()];
  152. // const result = `${year}年${month}月${day}日(星期${dayOfWeek})`
  153. return `${year}年${month}月${day}日(星期${dayOfWeek})`
  154. }
  155. // 切换搜索框下面的tab
  156. function changeTab(index) {
  157. currentTab.value = index;
  158. courseStore.setCurrentTab(index)
  159. if (index === 0) {
  160. filterCourses.value = courses.value
  161. return
  162. }
  163. filterCourses.value = courses.value.filter(item => item.courseType == tabsList.value[index].name)
  164. }
  165. function toSearch(e) {
  166. pageNumber.value = 1
  167. search(e)
  168. }
  169. // 搜索
  170. function search(e) {
  171. searchForm.value.keyword = e
  172. searchForm.value.pageNumber = pageNumber.value
  173. searchForm.value.pageSize = pageSize.value
  174. courses.value = pageNumber.value == 1 ? [] : courses.value
  175. loadMoreStatus.value = 'loading'
  176. loadCourseList(searchForm.value).then(res => {
  177. if (res?.data) {
  178. total.value = res.count;
  179. courses.value = [...courses.value, ...res.data];
  180. const i = currentTab.value
  181. if (i == 0) {
  182. filterCourses.value = [...courses.value];
  183. } else {
  184. filterCourses.value = courses.value.filter(item => item.courseType == tabsList.value[i].name)
  185. }
  186. loadMoreStatus.value = total.value === courses.value.length ? 'nomore' : 'loadmore';
  187. pageNumber.value++;
  188. }
  189. })
  190. }
  191. // 收藏
  192. async function collectCourse(id, index, hasFavi) {
  193. try {
  194. let res;
  195. if (hasFavi) {
  196. res = await courseCancelFavi({
  197. id
  198. })
  199. } else {
  200. res = await courseFavi({
  201. id
  202. })
  203. }
  204. if (res.code == 0) {
  205. courses.value[index].hasFavi = !courses.value[index].hasFavi
  206. }
  207. } catch (err) {
  208. courses.value[index].hasFavi = courses.value[index].hasFavi
  209. }
  210. }
  211. // 初始化
  212. function init() {
  213. loadCourseCate().then(res => {
  214. if (res?.data) {
  215. tabsList.value = [{
  216. code: '',
  217. name: '全部'
  218. }, ...res.data]
  219. }
  220. })
  221. pageNumber.value = 1;
  222. search("");
  223. // 初始化页面,获取数据
  224. // filterCourses.value = courses.value
  225. }
  226. function toPage(course) {
  227. uni.navigateTo({
  228. url: `/pages/goOnEdu/course/courseDetail/courseDetail?id=${course.id}&name=${course.courseName}`
  229. });
  230. }
  231. function loadmore() {
  232. if (courses.value.length === total.value) {
  233. return;
  234. }
  235. search(searchForm.value.keyword);
  236. }
  237. onMounted(() => {
  238. isMember.value = authStore.userInfo.isMember === 1 ? true : false
  239. // console.log(authStore.userInfo.isMember)
  240. init();
  241. watch(currentTab, (newValue) => {
  242. courseStore.setCurrentTab(newValue); // 如果需要在切换时更新 Pinia 状态
  243. });
  244. });
  245. onReachBottom(() => {
  246. loadmore()
  247. })
  248. </script>
  249. <style lang="scss" scoped>
  250. .container {
  251. // padding: 20px;
  252. }
  253. .title {
  254. font-size: 48rpx;
  255. margin-bottom: 30rpx;
  256. }
  257. .course-item {
  258. margin: 20rpx 0;
  259. display: flex;
  260. overflow: hidden;
  261. .course-item-image {
  262. width: 200rpx;
  263. height: 260rpx;
  264. flex: 0 0 auto;
  265. margin-right: 20rpx;
  266. .course-image {
  267. width: 100%;
  268. }
  269. }
  270. .course-item-content {
  271. flex: 1;
  272. position: relative;
  273. view {
  274. margin-bottom: 15rpx;
  275. }
  276. .course-title {
  277. font-weight: bold;
  278. margin-right: 10px;
  279. font-size: 28rpx;
  280. color: #000;
  281. }
  282. .course-type,
  283. .course-teacher,
  284. .course-date,
  285. .course-price {
  286. font-size: 30rpx;
  287. color: #000;
  288. }
  289. .course-price {
  290. color: #fe0000;
  291. letter-spacing: 2rpx;
  292. }
  293. .button::after {
  294. content: none;
  295. /* 移除内容 */
  296. }
  297. .button {
  298. position: absolute;
  299. right: 0;
  300. bottom: 0;
  301. min-width: 80px;
  302. padding: 0 40rpx;
  303. white-space: nowrap;
  304. height: 45rpx;
  305. line-height: 45rpx;
  306. font-size: 22rpx;
  307. /* padding: 0; */
  308. border-radius: 50rpx;
  309. color: white;
  310. border: none;
  311. }
  312. .free {
  313. background-color: #006af4;
  314. }
  315. .purchase {
  316. background-color: #fe0000;
  317. }
  318. .member-free {
  319. background-color: transparent;
  320. background-image: url('http://www.gzrea.org.cn:8543/icon/wxmp/bg-label.png');
  321. background-size: cover;
  322. background-repeat: no-repeat;
  323. color: #000;
  324. height: initial;
  325. padding: 6rpx 0 3rpx;
  326. border-radius: 0;
  327. }
  328. .replay {
  329. background-color: #006af4;
  330. }
  331. .purchased {
  332. background-color: #006af4;
  333. }
  334. }
  335. }
  336. </style>