MyTask.vue
20.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<template>
<view class="task-box">
<view class="tab-box">
<view class="tab-list">
<view @click="handleTabClick(index)" :class="['tab-item', tabIndex === index ? 'active' : '']"
v-for="(item, index) in tabList" :key="index">
<view class="tab-item-title">{{ item }}</view>
</view>
</view>
<view class="finish-tab" v-if="tabIndex === 1">
<view class="finish-tab-item" @click="handleFinishTabClick(2)" :class="{ 'finish-active': finishIndex === 2 }">
长课程
</view>
<view class="finish-tab-item" @click="handleFinishTabClick(1)" :class="{ 'finish-active': finishIndex === 1 }">
短课程
</view>
</view>
</view>
<view class="tips" v-if="tabIndex === 0">
<view class="sprite-icon icon-study-tips"></view>
<view class="tips-text">带有"限时学"标签的课程:规定时间内未完成任务,禁止提单!请按时学习。</view>
</view>
<scroll-view scroll-y ref="scrollViewRef" :scroll-top="scrollTop" @scrolltolower="pageonReachBottom"
:class="[getScrollHeight]">
<template v-if="taskList.length">
<view class="task-list">
<view class="task-item" v-for="(item, index) in taskList" :key="index" @click="handleTaskItemClick(item)">
<view class="time-limit-tag" v-if="tabIndex === 0 && item.punishType == 1"></view>
<view v-if="tabIndex === 0 && item.punishType == 1" class="time-limit-countdown ">
<template v-if="item._timeout">
<view class="countdown-label timeout-label">已超时</view>
</template>
<template v-else-if="item._showCountdown">
<view class="countdown-label">剩</view>
<view class="countdown-num">{{ item._countdownHour }}</view>
<view class="countdown-separator">:</view>
<view class="countdown-num">{{ item._countdownMinute }}</view>
<view class="countdown-separator">:</view>
<view class="countdown-num">{{ item._countdownSecond }}</view>
</template>
<template v-else-if="item._showDays">
<view class="countdown-label">剩</view>
<view class="countdown-num">{{ item._leftDays }}</view>
<view class="countdown-label">天</view>
</template>
<template v-else>
<view class="countdown-label">剩</view>
<view class="countdown-num">--</view>
<view class="countdown-separator">:</view>
<view class="countdown-num">--</view>
<view class="countdown-separator">:</view>
<view class="countdown-num">--</view>
</template>
</view>
<image
class="task-item-img"
:src="item.courseType === 1
? require('../../../../assets/images/common/short-course-default.webp')
: require('../../../../assets/images/common/course-default-bg.webp')"
mode="aspectFill"
/>
<view class="task-item-content">
<view class="task-item-title">{{ item.taskTitle }}</view>
<view class="task-item-text">
<view class="task-item-label">任务进度</view>
<view class="task-item-progress" v-if="item.completeStatus == 0">
<view class="task-item-progress-bar" :style="{ width: Math.floor(item.taskProgress) + '%', minWidth: item.taskProgress > 0 ? '10px' : '0' }"></view>
<view class="task-item-progress-text">{{ Math.floor(item.taskProgress) }}%</view>
</view>
<view class="task-item-value" v-else><view class="dot-value"></view><view class="green">已完成</view></view>
</view>
<view class="task-item-text" v-if="item.courseType === 2">
<view class="task-item-label">课程数</view>
<view class="task-item-value">
{{ item.courseTotalCount }}节 |
{{ (item.totalDuration || item.totalDuration === 0) ? `${item.totalDuration}min` : '-' }}
</view>
</view>
<view class="task-item-text">
<view class="task-item-label">完成时间</view>
<view class="task-item-value">{{ tabIndex === 0 ? formatTime(item.taskDeadline, 'YYYY-MM-DD HH:mm') : formatTime(item.finishDate, 'YYYY-MM-DD HH:mm') }}</view>
</view>
<view class="task-item-status">{{ item.status }}</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 dayjs from 'dayjs'
export default {
name: 'MyTask',
data() {
return {
tabList: ['待完成', '已完成'],
tabIndex: 0,
finishIndex: 2,
nothing: false,
total: 0,
pageNum: 1,
pageSize: 12,
scrollTop: 0,
taskList: [],
timer: null // 只需要一个定时器
}
},
computed: {
getScrollHeight() {
const list = this.taskList;
if (!list || list.length === 0) {
return '';
}
switch (this.tabIndex) {
case 0:
return 'task-list-box';
case 1:
return 'task-finish-box';
default:
return '';
}
}
},
mounted() {
this.getCourseTaskPage(this.tabIndex)
},
beforeDestroy() {
this.clearCountdownTimer();
// 移除事件监听,防止内存泄漏
uni.$off && uni.$off('backStudyCenter');
},
methods: {
// 清除倒计时定时器
clearCountdownTimer() {
if (this.timer) {
clearInterval(this.timer);
this.timer = null;
}
},
// 启动倒计时定时器
startCountdownForAll() {
this.clearCountdownTimer();
this.updateAllCountdowns(); // 立即更新一次
this.timer = setInterval(() => {
this.updateAllCountdowns();
}, 1000);
},
// 更新所有需要倒计时的任务
updateAllCountdowns() {
const now = new Date();
let hasCountdown = false;
this.taskList.forEach(item => {
if (
this.tabIndex === 0 &&
item.punishType == 1 &&
item.taskDeadline
) {
const deadline = new Date(item.taskDeadline);
const diff = deadline - now;
const oneDayMs = 24 * 60 * 60 * 1000;
if (diff > 0 && diff < oneDayMs) {
// 小于1天,展示24小时倒计时
hasCountdown = true;
const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
this.$set(item, '_countdownHour', hours.toString().padStart(2, '0'));
this.$set(item, '_countdownMinute', minutes.toString().padStart(2, '0'));
this.$set(item, '_countdownSecond', seconds.toString().padStart(2, '0'));
this.$set(item, '_showCountdown', true);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_timeout', false);
} else if (diff >= oneDayMs) {
// 超过等于1天,显示剩余天数(不足x天则展示x-1天)
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', true);
// days = Math.ceil(diff / oneDayMs) - 1, 最小为1天
let days = Math.ceil(diff / oneDayMs) - 1;
if (days < 1) days = 1;
this.$set(item, '_leftDays', days);
this.$set(item, '_countdownHour', null);
this.$set(item, '_countdownMinute', null);
this.$set(item, '_countdownSecond', null);
this.$set(item, '_timeout', false);
} else if (diff <= 0) {
// 已超时
this.$set(item, '_timeout', true);
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_countdownHour', null);
this.$set(item, '_countdownMinute', null);
this.$set(item, '_countdownSecond', null);
}
} else {
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_timeout', false);
}
});
// 如果没有任何需要倒计时的,清除定时器
if (!hasCountdown) {
this.clearCountdownTimer();
}
},
// 格式化时间
formatTime(time, formatStr) {
if (!time) return '-';
return dayjs(time).format(formatStr);
},
// 初始化倒计时字段(用于新获取的数据)
initCountdownFieldsForList(list) {
const now = new Date();
list.forEach(item => {
if (this.tabIndex === 0 && item.punishType == 1 && item.taskDeadline) {
const deadline = new Date(item.taskDeadline);
const diff = deadline - now;
if (diff > 0 && diff <= 24 * 60 * 60 * 1000) {
// 需要倒计时
const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
this.$set(item, '_countdownHour', hours.toString().padStart(2, '0'));
this.$set(item, '_countdownMinute', minutes.toString().padStart(2, '0'));
this.$set(item, '_countdownSecond', seconds.toString().padStart(2, '0'));
this.$set(item, '_showCountdown', true);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_timeout', false);
} else if (diff > 24 * 60 * 60 * 1000) {
// 超过24小时,显示剩余天数
const days = Math.ceil(diff / (24 * 60 * 60 * 1000));
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', true);
this.$set(item, '_leftDays', days);
this.$set(item, '_countdownHour', null);
this.$set(item, '_countdownMinute', null);
this.$set(item, '_countdownSecond', null);
this.$set(item, '_timeout', false);
} else if (diff <= 0) {
// 已超时
this.$set(item, '_timeout', true);
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_countdownHour', null);
this.$set(item, '_countdownMinute', null);
this.$set(item, '_countdownSecond', null);
}
} else {
this.$set(item, '_showCountdown', false);
this.$set(item, '_showDays', false);
this.$set(item, '_leftDays', null);
this.$set(item, '_timeout', false);
}
});
},
// 获取我的任务接口, completeStatus: 完成状态 0=待完成 1=已完成, courseType: 课程类型 1=短课程 2=长课程
async getCourseTaskPage(completeStatus, courseType) {
try {
uni.showLoading({
mask: true
})
const { pageNum, pageSize } = this
const params = {
completeStatus, // 完成状态 0=待完成 1=已完成
recommendStatus: 0, // 推荐状态 0=无推荐 1=推荐课程
pageNum,
pageSize
}
if (courseType) {
params.courseType = courseType
}
const res = await uni.$api.commonApi.getCourseTaskPage(params)
// 处理倒计时相关字段
let newList = res.list || [];
// 对新获取的数据初始化倒计时字段
if (completeStatus === 0) {
this.initCountdownFieldsForList(newList);
}
if (pageNum == 1) {
this.taskList = newList
} else {
// 合并新数据到已有数据
this.taskList = this.taskList.concat(newList)
}
uni.hideLoading()
this.total = res.total
this.nothing = res.length ? true : false
// 只在tabIndex为0时处理倒计时
if (completeStatus === 0) {
// 启动定时器(不管是第一页还是翻页都要)
this.startCountdownForAll();
} else {
// 非待完成tab,清除定时器
this.clearCountdownTimer();
}
} catch (error) {
uni.showToast({
title: error,
icon: "none",
});
}
},
pageonReachBottom() {
console.log('到底了');
// 解构赋值,设置 nothing 的默认值
let { pageNum, total, pageSize, tabIndex, finishIndex } = this;
if (Math.ceil(total / pageSize) > pageNum) {
this.pageNum += 1;
if (tabIndex == 0) {
this.getCourseTaskPage(tabIndex)
} else {
this.getCourseTaskPage(tabIndex, finishIndex)
}
} else {
this.nothing = true;
}
},
handleTabClick(index) {
this.tabIndex = index
this.pageNum = 1
this.nothing = false
this.taskList = []
this.clearCountdownTimer();
if (index == 1) {
this.finishIndex = 2
this.getCourseTaskPage(index, this.finishIndex) // 已完成,默认查长课程
} else {
this.getCourseTaskPage(index)
}
this.scrollTop = 1
setTimeout(() => {
this.scrollTop = 0
}, 100);
},
handleFinishTabClick(index) {
this.finishIndex = index
this.pageNum = 1
this.nothing = false
this.taskList = []
this.clearCountdownTimer();
this.getCourseTaskPage(this.tabIndex, index)
},
handleTaskItemClick(item) {
// console.log(item)
const {courseType, id} = item
if (courseType == 1) {
uni.$track.add({eventCode: 'app_short_course', businessId: id, pagePath: '/pages/equity/index'})
}
uni.navigateTo({
url: `/pages/common/studyDetail/${courseType == 1 ? 'shortCourse' : 'longCourse'}?row=${encodeURIComponent(JSON.stringify(item))}&completeStatus=${this.tabIndex}`,
fail: (err) => {
console.error('页面跳转失败:', err);
},
// 页面跳转后,监听返回事件
success: () => {
// 兼容H5/小程序/APP,返回时触发自定义事件
// 这里不需要处理,onShow和backStudyCenter事件已处理倒计时恢复
}
});
},
findBeiSenSSOToken () {
return new Promise(resolve => {
const url = 'https%3A%2F%2Fcloud.italent.cn%2FPageHome%2FIndex%3Fproduct%3DLearning%26keyName%3DNusion%26pageCode%3DLearningNewHomepage%26appCode%3DLearning%26_qsrcapp%3DLearning%26_qrt%3Dhtml%26quark_s%3D5102bff371c00da5b19968ee53a8c3f1499ecd2447a19f72f3e3ba2390cc9617%23%2FviewDynamic%3Fshadow_context%3D%257BappModel%253A%2522italent%2522%252Cuppid%253A%25221%2522%257D%26quark_s%3D028930f1066f8ef8d28790940d53ae64337f44bde3a44cf89a290e4a3611e3e2'
uni.$api.commonApi.findBeiSenSSOToken({}).then(res => {
uni.$storage.set('beisenToken', res.data)
const studyUrl = res.data ? `${res.data}&return_url=${url}` : ''
resolve(studyUrl)
})
})
},
}
}
</script>
<style lang="scss" scoped>
.task-box {
padding-top: toRpx(32);
height: calc(100% - #{toRpx(100)});
}
.tab-list {
display: flex;
width: toRpx(436);
height: toRpx(80);
background-color: #F5F6F7;
border-radius: toRpx(16);
color: #666666;
font-size: toRpx(28);
.tab-item {
box-sizing: border-box;
display: flex;
flex: 1;
align-items: center;
justify-content: center;
margin: toRpx(6);
&.active {
background-color: #fff;
color: #1A1A1A;
border-radius: toRpx(10);
font-weight: 600;
}
}
}
.tips {
display: flex;
align-items: center;
padding: toRpx(8) toRpx(22);
background-color: rgba(255, 243, 224, 0.8);
border-radius: toRpx(8);
font-size: toRpx(24);
color: #FF9D0A;
height: toRpx(48);
margin: toRpx(24) toRpx(40) 0;
&-text {
margin-top: toRpx(4);
}
.icon-study-tips {
margin-right: toRpx(8);
}
}
.tab-box {
display: flex;
align-items: center;
justify-content: space-between;
margin: 0 toRpx(40);
}
.finish-tab {
display: flex;
&-item {
width: toRpx(148);
height: toRpx(64);
border-radius: toRpx(48);
background: #f2f2f2;
color: #666666;
font-size: toRpx(28);
line-height: toRpx(68);
text-align: center;
margin-right: toRpx(16);
&:last-child {
margin-right: 0;
}
&.finish-active {
background: #2d66f71a;
font-weight: 500;
color: #2D66F7;
}
}
}
.task-list-box {
margin-top: toRpx(24);
height: calc(100% - #{toRpx(210)});
}
.task-finish-box {
margin-top: toRpx(24);
height: calc(100% - #{toRpx(138)});
}
.task-list {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: toRpx(32);
padding: 0 toRpx(40) toRpx(32);
.task-item {
position: relative;
width: 100%;
height: toRpx(432);
background-color: #fff;
border-radius: toRpx(24);
font-size: 0;
box-shadow: 0 toRpx(8) toRpx(24) 0 #2a61eb1a;
.time-limit-tag {
position: absolute;
top: 0;
left: 0;
width: toRpx(122);
height: toRpx(48);
background-image: url('../../../../assets/images/common/limit-page.webp');
background-size: cover;
background-repeat: no-repeat;
background-position: center;
z-index: 1;
color: #fff;
}
.time-limit-countdown {
position: absolute;
right: toRpx(18);
top: toRpx(16);
display: flex;
align-items: center;
justify-content: center;
font-size: toRpx(24);
z-index: 1;
.countdown-num {
display: flex;
align-items: center;
justify-content: center;
color: #fff;
width: toRpx(40);
height: toRpx(40);
border-radius: toRpx(4);
background: #00000059;
margin: 0 toRpx(4);
}
.countdown-separator {
color: #fff;
}
.countdown-label,
.countdown-unit {
color: #00000066;
margin: 0 toRpx(4);
}
.timeout-label {
height: toRpx(40);
line-height: toRpx(44);
border-radius: toRpx(4);
background: #00000059;
color: #fff;
padding: toRpx(4) toRpx(8);
padding: 0 toRpx(12);
font-size: toRpx(24);
}
}
.task-item-img {
width: 100%;
height: toRpx(180);
border-radius: toRpx(16) toRpx(16) 0 0;
}
.task-item-content {
padding: toRpx(32);
}
.task-item-title {
font-size: toRpx(30);
color: #292D33;
font-weight: 500;
margin-bottom: toRpx(24);
width: toRpx(340);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.task-item-text {
position: relative;
display: flex;
align-items: center;
font-size: toRpx(24);
margin-bottom: toRpx(24);
.task-item-label {
width: toRpx(96);
color: #999EA8;
margin-right: toRpx(24);
}
.task-item-value {
color: #666666;
display: inline-flex;
align-items: center;
justify-content: center;
.dot-value {
width: toRpx(12);
height: toRpx(12);
border-radius: 50%;
background: #2CCA74;
margin-right: toRpx(8);
}
.green {
color: #2CCA74;
}
}
}
.task-item-progress {
width: toRpx(214);
height: toRpx(16);
background: linear-gradient(128.8deg, rgba(117, 188, 255, .35) 0%, rgba(44, 102, 247, .35) 100%);
border-radius: toRpx(8);
.task-item-progress-text {
color: #3B73FF;
font-size: toRpx(24);
position: absolute;
top: 50%;
right: 0;
transform: translateY(-50%);
}
.task-item-progress-bar {
height: 100%;
background: linear-gradient(128.8deg, #75bcff 0%, #2c66f7 100%);
border-radius: toRpx(8);
}
}
}
}
.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;
font-size: toRpx(24);
}
/deep/ {
.uni-scroll-view-content {
height: calc(100% - #{toRpx(20)});
}
}
</style>