courseHome.vue 9.5 KB

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