ToLearnCourse.vue
11.7 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<!-- 我的课程 - 待学习 -->
<!-- 学习中心首页 - 全部课程 -->
<template>
<view class="recommend-course">
<scroll-view scroll-y ref="scrollViewRef" :scroll-top="scrollTop" @scrolltolower="pageonReachBottom"
:class="getScrollHeight">
<template v-if="recommendCourseList.length">
<view class="recommend-course-list" :class="{ 'fixed-width': cardWidth > 0 }">
<view class="recommend-course-item" v-for="(item, index) in recommendCourseList" :key="index"
@click="startStudy(item, index)" :style="cardWidth > 0 ? `width: ${cardWidth}rpx` : ''">
<!-- 左上角:必修/选修标签 -->
<template v-if="item.planCourseType">
<image class="course-tag-left"
:src="item.planCourseType == 1 ? require('@/assets/images/studyCenter/Required.png') : require('@/assets/images/studyCenter/Elective.png')"
mode="aspectFit" />
</template>
<!-- 右上角:文档/视频标签 -->
<view class="course-tag-right" v-if="item.courseFileType">
<text class="tag-text">{{ COURSE_TYPE[item.courseFileType].name}}</text>
</view>
<image class="recommend-course-item-img"
:src="require(`@/assets/images/common/${COURSE_TYPE[item.courseFileType].image}.webp`)" mode="scaleToFill" />
<view class="recommend-course-content">
<view class="recommend-course-item-title">
{{ item.title }}
</view>
<view class="recommend-course-item-text">
<text class="duration-value">课程时长</text>
<text>{{ (item.totalDuration || item.totalDuration === 0) ?
`${item.totalDuration}min` : '-' }}</text>
</view>
<!-- 评分星星 -->
<view v-if="item.totalStar !== undefined" class="course-rating">
<view class="star-list">
<image v-for="star in 5" :key="star" class="star-icon" :src="getStarIcon(item.totalStar, star)"
mode="aspectFit" />
</view>
<text class="rating-text">{{ item.totalStar }}</text>
</view>
<!-- 学习人数 -->
<view class="student-count">
<image class="student-icon" :src="require('@/assets/images/studyCenter/rs.png')" mode="aspectFit" />
<text class="student-text">{{ item.learnTotal == null ? '-' : item.learnTotal }}人已学习</text>
</view>
</view>
</view>
</view>
</template>
<template v-else>
<view class="nothing flex">
<image class="icon" src="../../../../assets/images/common/noData.webp" />
暂无数据
</view>
</template>
<template v-if="nothing">
<view class="end-text">到底了!</view>
</template>
</scroll-view>
</view>
</template>
<script>
import { COURSE_TYPE } from '../contant.js'
export default {
name: 'ToLearnCourse',
props: {
// 卡片宽度,不传则使用默认的响应式布局
cardWidth: {
type: Number,
default: 0
},
// 是否显示简化版(只显示背景图、标题和时间)
simplified: {
type: Boolean,
default: false
}
},
data() {
return {
recommendCourseList: [],
nothing: false,
total: 0,
pageNum: 1,
pageSize: 12,
scrollTop: 0,
COURSE_TYPE
}
},
computed: {
getScrollHeight() {
const list = this.recommendCourseList;
if (!list || list.length === 0) {
return '';
}
return 'scroll-view'
}
},
mounted() {
this.getALlCourse()
},
methods: {
// 获取星星图标
getStarIcon(rating, position) {
const roundedRating = Math.round(rating) / 2 // 四舍五入到0.5
const diff = roundedRating - (position - 1)
if (diff >= 1) {
// 亮星
return require('@/assets/images/studyCenter/liang.png')
} else if (diff === 0.5) {
// 半星
return require('@/assets/images/studyCenter/zuo.png')
} else {
// 灰星
return require('@/assets/images/studyCenter/hui.png')
}
},
// 获取我的任务接口, completeStatus: 完成状态 0=待完成 1=已完成, courseType: 课程类型 1=短课程 2=长课程
async getALlCourse() {
try {
uni.showLoading({
mask: true
})
const { pageNum, pageSize } = this
const params = {
learnStatus: 0, // 学习状态 0:待学习 1: 正在学习 2:已学完
pageNum,
pageSize
}
const res = await uni.$api.commonApi.getMyCourseList(params)
console.log(res, '全部课程')
// 临时添加测试数据字段
const listWithTestData = res.list.map((item, index) => ({
...item,
required: index % 3 === 0, // 每3个课程第1个是必修
courseType: index % 2 === 0 ? 'video' : 'document', // 交替显示视频/文档
rating: (Math.random() * 2 + 3).toFixed(1), // 随机评分 3.0-5.0
studentCount: Math.floor(Math.random() * 1000) + 100 // 随机学习人数 100-1100
}))
if (pageNum == 1) {
this.recommendCourseList = listWithTestData
} else {
this.recommendCourseList = this.recommendCourseList.concat(listWithTestData)
}
uni.hideLoading()
this.total = res.total
this.nothing = res.length ? true : false
} catch (error) {
uni.showToast({
title: error,
icon: "none",
});
}
},
pageonReachBottom() {
console.log('到底了');
// 解构赋值,设置 nothing 的默认值
let { pageNum, total, nothing = 'nothing', pageSize } = this;
if (Math.ceil(total / pageSize) > pageNum) {
this.pageNum += 1;
this.getALlCourse()
} else {
this.nothing = true;
}
},
startStudy(item, index) {
console.log(item)
const beisenToken = uni.$storage.get('beisenToken')
if (!beisenToken) {
uni.showToast({
title: '培训账号未创建',
icon: 'none'
})
return
}
let { pageNum, total, nothing = 'nothing', pageSize } = this;
// uni.$track.add({ eventCode: 'app_recommend_course', businessId: item.id, pagePath: '/pages/equity/index' })
let businessParam = {
courseId:item.courseId,
title:item.title
}
console.log("businessParam",businessParam);
uni.$track.add({ eventCode: 'app_studyCourse_click', businessParam: businessParam })
uni.navigateTo({
url: `/pages/common/studyCenter/courseDetail?apiType=getMyCourseList&index=${index}&courseList=${encodeURIComponent(JSON.stringify(this.recommendCourseList))}&pageNum=${pageNum}&pageSize=${pageSize}&total=${total}&learnStatus=0`
})
},
}
}
</script>
<style lang="scss" scoped>
.recommend-course {
// height: calc(100% - #{toRpx(300)});
height: 100%;
// padding-bottom: toRpx(100);
.scroll-view {
margin-top: toRpx(40);
height: 100%;
.recommend-course-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: toRpx(32);
padding: 0 0 toRpx(32);
&.fixed-width {
grid-template-columns: repeat(4, toRpx(530));
justify-content: center;
.recommend-course-item {
width: toRpx(530) !important;
max-width: toRpx(530);
min-width: toRpx(530);
font-size: 0;
}
}
.recommend-course-item {
width: 100%;
min-height: toRpx(376);
box-shadow: 0 toRpx(8) toRpx(24) 0 #2a61eb1a;
border-radius: toRpx(24);
position: relative;
// 左上角标签
.course-tag-left {
position: absolute;
left: 0;
top: 0;
width: toRpx(94);
height: toRpx(48);
border-radius: toRpx(24) 0 toRpx(24) 0;
z-index: 2;
}
// 右上角标签
.course-tag-right {
position: absolute;
right: toRpx(16);
top: toRpx(16);
width: toRpx(72);
height: toRpx(40);
border-radius: toRpx(4);
background: rgba(0, 0, 0, 0.35);
display: flex;
align-items: center;
justify-content: center;
z-index: 2;
.tag-text {
width: toRpx(48);
height: toRpx(36);
color: #ffffff;
font-size: toRpx(24);
font-weight: 400;
font-family: "HarmonyOSSansSC";
text-align: center;
line-height: toRpx(36);
}
}
.recommend-course-item-img {
width: 100%;
height: toRpx(180);
border-radius: toRpx(24) toRpx(24) 0 0;
}
.recommend-course-content {
padding: toRpx(32);
background-color: #fff;
border-radius: 0 0 toRpx(16) toRpx(16);
.recommend-course-item-title {
font-size: toRpx(30);
color: #292D33;
display: -webkit-box;
-webkit-line-clamp: 1;
line-clamp: 1;
-webkit-box-orient: vertical;
overflow: hidden;
text-overflow: ellipsis;
word-break: break-all;
margin-bottom: toRpx(16);
height: calc(1 * 1.2em);
line-height: 1.2;
}
.recommend-course-item-text {
display: flex;
align-items: center;
font-size: toRpx(24);
color: #666666;
.duration-value {
color: #999ea8;
font-size: toRpx(24);
font-weight: 400;
font-family: "HarmonyOSSansSC";
margin-right: toRpx(24);
}
}
// 评分星星
.course-rating {
display: flex;
align-items: center;
margin-top: toRpx(12);
.star-list {
display: flex;
align-items: center;
.star-icon {
width: toRpx(40);
height: toRpx(40);
margin-right: toRpx(6);
&:last-child {
margin-right: 0;
}
}
}
.rating-text {
width: toRpx(40);
height: toRpx(36);
color: #666666;
font-size: toRpx(28);
font-weight: 400;
font-family: "HarmonyOSSansSC";
text-align: left;
line-height: toRpx(36);
margin-left: toRpx(6);
}
}
// 学习人数
.student-count {
display: flex;
align-items: center;
margin-top: toRpx(12);
.student-icon {
width: toRpx(32);
height: toRpx(32);
margin-right: toRpx(8);
}
.student-text {
height: toRpx(36);
color: #999ea8;
font-size: toRpx(24);
font-weight: 400;
font-family: "HarmonyOSSansSC";
text-align: left;
line-height: toRpx(36);
}
}
}
}
}
}
}
.nothing {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: toRpx(1000);
color: #999999;
font-size: toRpx(24);
.icon {
width: toRpx(344);
height: toRpx(208);
}
}
.end-text {
color: #999;
text-align: center;
padding: toRpx(30) 0 toRpx(50);
font-size: toRpx(24);
}
/deep/ {
.uni-scroll-view-content {
height: calc(100% - #{toRpx(20)});
}
}
</style>