Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projecttwo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangtao
projecttwo
Commits
f9c1dd8e
Commit
f9c1dd8e
authored
Jun 13, 2025
by
wangzhengwen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
进度服务修改
parent
98078496
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
21 deletions
+40
-21
Course.php
app/api/controller/Course.php
+5
-4
CourseProgressService.php
app/api/service/CourseProgressService.php
+34
-16
Course.php
app/model/Course.php
+1
-1
No files found.
app/api/controller/Course.php
View file @
f9c1dd8e
...
...
@@ -157,7 +157,6 @@ class Course extends BaseController
$data
=
$request
->
param
();
$token
=
$request
->
header
(
'token'
);
$userId
=
TokenService
::
verifyToken
(
$token
)[
'user_id'
]
??
0
;
$data
=
(
new
CourseModel
())
->
with
([
'getSections'
=>
function
(
$query
)
use
(
$userId
)
{
...
...
@@ -170,9 +169,11 @@ class Course extends BaseController
}]);
},
'getCourseClass'
=>
function
(
$query
)
use
(
$userId
)
{
$query
->
with
([
'fileData'
,
'courseProgress'
=>
function
(
$query
)
use
(
$userId
)
{
$query
->
where
(
'user_id'
,
$userId
);
}])
//没有章节的课时查询
$query
->
where
(
'cate_id'
,
0
);
$query
->
with
([
'fileData'
,
'courseProgress'
=>
function
(
$query
)
use
(
$userId
)
{
$query
->
where
(
'user_id'
,
$userId
);
}])
->
append
([
'tvtime_str'
])
->
order
(
'sort'
,
'asc'
);
}
...
...
app/api/service/CourseProgressService.php
View file @
f9c1dd8e
...
...
@@ -25,17 +25,11 @@ class CourseProgressService
*/
public
static
function
updateProgress
(
$userId
,
$courseId
,
$classId
,
$currentTime
)
{
$duration
=
CourseClass
::
where
(
'id'
,
$classId
)
->
value
(
'tvtime'
);
// $duration = 50;
$duration
=
CourseClass
::
where
(
'id'
,
$classId
)
->
value
(
'tvtime'
);
if
(
!
$duration
)
{
return
false
;
}
// 判断是否看完(观看进度超过95%或最后10秒)
$isFinished
=
(
$currentTime
>=
$duration
-
self
::
MIN_CONSIDERED_FINISHED_SECONDS
)
||
(
$currentTime
>=
$duration
*
self
::
FINISHED_THRESHOLD_PERCENT
);
// 查找或创建记录
$progress
=
CourseProgress
::
where
([
'user_id'
=>
$userId
,
...
...
@@ -44,22 +38,38 @@ class CourseProgressService
])
->
findOrEmpty
();
if
(
$progress
->
isEmpty
())
{
// 新记录
// 新记录:初始化最大进度和最后一次进度
$isFinished
=
(
$currentTime
>=
$duration
-
self
::
MIN_CONSIDERED_FINISHED_SECONDS
)
||
(
$currentTime
>=
$duration
*
self
::
FINISHED_THRESHOLD_PERCENT
);
$progress
=
CourseProgress
::
create
([
'user_id'
=>
$userId
,
'course_id'
=>
$courseId
,
'class_id'
=>
$classId
,
'tvtime'
=>
$duration
,
'look_tvtime'
=>
$currentTime
,
'last_look_tvtime'
=>
$currentTime
,
'is_wc_look'
=>
$isFinished
?
1
:
0
]);
}
else
{
// 更新记录(只更新必要字段)
// 仅当当前时间 > 最大进度时才更新 look_tvtime
$shouldUpdateMaxProgress
=
$currentTime
>
$progress
->
look_tvtime
;
$updateData
=
[
'look_tvtime'
=>
$currentTime
,
'is_wc_look'
=>
$isFinished
?
1
:
$progress
->
is_wc_look
'last_look_tvtime'
=>
$currentTime
,
// 始终更新最后一次进度
];
if
(
$shouldUpdateMaxProgress
)
{
$updateData
[
'look_tvtime'
]
=
$currentTime
;
}
// 判断完成状态(基于更新后的 look_tvtime)
$newMaxProgress
=
$shouldUpdateMaxProgress
?
$currentTime
:
$progress
->
look_tvtime
;
$isFinished
=
(
$newMaxProgress
>=
$duration
-
self
::
MIN_CONSIDERED_FINISHED_SECONDS
)
||
(
$newMaxProgress
>=
$duration
*
self
::
FINISHED_THRESHOLD_PERCENT
);
$updateData
[
'is_wc_look'
]
=
$isFinished
?
1
:
$progress
->
is_wc_look
;
// 如果视频总时长有变化才更新
if
(
$duration
!=
$progress
->
tvtime
)
{
$updateData
[
'tvtime'
]
=
$duration
;
...
...
@@ -68,9 +78,9 @@ class CourseProgressService
$progress
->
save
(
$updateData
);
}
return
[
'progress'
=>
$progress
->
look_tvtime
,
'max_progress'
=>
$progress
->
look_tvtime
,
// 最大进度
'last_progress'
=>
$progress
->
last_look_tvtime
,
// 最后一次进度
'duration'
=>
$progress
->
tvtime
,
'is_finished'
=>
$progress
->
is_wc_look
];
...
...
@@ -144,7 +154,7 @@ class CourseProgressService
// 分页查询课程基本信息
$query
=
Course
::
where
(
'id'
,
'in'
,
$courseIds
)
->
with
([
'thumb'
])
->
field
(
'id,title,description,thumb,price'
);
->
field
(
'id,title,description,thumb,price
,content
'
);
// 根据类型筛选课程
if
(
$type
===
1
)
{
...
...
@@ -229,8 +239,9 @@ class CourseProgressService
// 统计计算
$finishedCount
=
0
;
$totalDuration
=
0
;
$learnedDuration
=
0
;
$totalDuration
=
0
;
// 课程总时长(所有课时的 tvtime 之和)
$learnedDuration
=
0
;
// 有效学习时长(用于计算进度)
$totalLearnedDuration
=
0
;
// 总观看时长(实际观看时间,不经过优化逻辑)
$classProgress
=
[];
foreach
(
$progressList
as
$item
)
{
...
...
@@ -240,6 +251,7 @@ class CourseProgressService
// 判断是否满足"视为完成"的条件(观看时间<10s 或 进度>95%)
$isConsideredFinished
=
(
$item
->
look_tvtime
<
self
::
MIN_CONSIDERED_FINISHED_SECONDS
)
||
(
$currentProgress
>
self
::
FINISHED_THRESHOLD_PERCENT
);
// 计算有效学习时长(用于进度计算)
if
(
$item
->
is_wc_look
||
$isConsideredFinished
)
{
$finishedCount
++
;
$learnedDuration
+=
$item
->
tvtime
;
// 视为完成则计入全部时长
...
...
@@ -247,6 +259,10 @@ class CourseProgressService
$learnedDuration
+=
min
(
$item
->
look_tvtime
,
$item
->
tvtime
);
}
// 总观看时长(直接累加 look_tvtime,不经过优化逻辑)
$totalLearnedDuration
+=
$item
->
look_tvtime
;
// 课程总时长(所有课时的 tvtime 之和)
$totalDuration
+=
$item
->
tvtime
;
$classProgress
[
$item
->
class_id
]
=
[
...
...
@@ -272,6 +288,8 @@ class CourseProgressService
'finished_count'
=>
$finishedCount
,
'total_classes'
=>
count
(
$progressList
),
'progress_percent'
=>
min
(
$progress_percent
,
100
),
// 确保不超过100%
'total_duration'
=>
$totalDuration
,
// 课程总时长(秒)
'total_learned_duration'
=>
$totalLearnedDuration
,
// 用户实际观看总时长(秒)
'classes'
=>
$classProgress
];
}
...
...
app/model/Course.php
View file @
f9c1dd8e
...
...
@@ -69,7 +69,7 @@ class Course extends Model
}
$list
=
$query
->
order
(
'click,tvclick,createtime'
,
'desc'
)
->
field
(
'id,thumb,title,description,tag_ids,teacher_id,description'
)
->
field
(
'id,thumb,title,description,tag_ids,teacher_id,description
,content
'
)
->
with
([
'thumb'
,
'getTeacher'
=>
[
'thumb'
]])
->
paginate
([
'page'
=>
$page
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment