courseHome.vue 9.2 KB

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