RecommendationsSection.vue
3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<template>
<section class="mb-7">
<div class="flex justify-between items-center mb-3">
<h3 class="font-medium">为您推荐</h3>
<button
class="text-xs text-gray-500 flex items-center"
@click="refresh_recommendations"
>
换一批
<svg xmlns="http://www.w3.org/2000/svg" class="h-3 w-3 ml-1" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
</svg>
</button>
</div>
<div class="grid grid-cols-2 gap-4">
<FrostedGlass
v-for="(item, index) in displayed_recommendations"
:key="index"
class="p-3 rounded-xl"
>
<div class="flex flex-col h-full" @click="go_to_course_detail(item)">
<div class="h-28 mb-2 rounded-lg overflow-hidden relative">
<img
:src="item.cover || 'https://cdn.ipadbiz.cn/mlaj/images/default_block.png'"
:alt="item.title"
class="w-full h-full object-cover"
/>
<div
v-if="item?.is_buy"
class="absolute top-0 left-0 bg-orange-500 text-white text-xs px-2 py-1 rounded-br-lg font-medium"
style="background-color: rgba(249, 115, 22, 0.85)"
>
已购
</div>
</div>
<h4 class="font-medium text-sm mb-1 line-clamp-1">{{ item.title }}</h4>
</div>
</FrostedGlass>
</div>
</section>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import FrostedGlass from '@/components/ui/FrostedGlass.vue'
import { getCourseListAPI } from '@/api/course'
const router = useRouter()
const all_recommendations = ref([])
const displayed_recommendations = ref([])
/**
* @description 获取推荐列表(用于“为您推荐”模块)
* @returns {Promise<void>}
*/
const fetch_recommendations = async () => {
const res = await getCourseListAPI({ sn: 'RMKC' })
if (res && res.code) {
all_recommendations.value = Array.isArray(res.data) ? res.data : []
displayed_recommendations.value = get_recommendations()
} else {
all_recommendations.value = []
displayed_recommendations.value = []
}
}
/**
* @description 获取推荐内容(可随机)
* @param {boolean} random 是否随机
* @returns {Array} 推荐列表
*/
const get_recommendations = (random = false) => {
if (!Array.isArray(all_recommendations.value)) return []
if (random) {
const shuffled = [...all_recommendations.value].sort(() => 0.5 - Math.random())
return shuffled.slice(0, 4)
}
return all_recommendations.value.slice(0, 4)
}
/**
* @description 换一批推荐
* @returns {void}
*/
const refresh_recommendations = () => {
displayed_recommendations.value = get_recommendations(true)
}
/**
* @description 跳转到课程详情
* @param {Object} course 课程对象
* @returns {void}
*/
const go_to_course_detail = (course) => {
if (!course || !course.id) return
router.push(`/courses/${course.id}`)
}
onMounted(async () => {
await fetch_recommendations()
})
</script>