Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
飞
飞鹤小程序
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
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
FH
飞鹤小程序
Commits
64904fb4
Commit
64904fb4
authored
Oct 25, 2025
by
jt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
e14b386c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
116 additions
and
30 deletions
+116
-30
SeckillSection.vue
components/SeckillSection.vue
+116
-30
No files found.
components/SeckillSection.vue
View file @
64904fb4
...
...
@@ -243,6 +243,58 @@ const getGoodsListClass = () => {
return
''
;
};
// 智能定位场次索引
const
getTargetSessionIndex
=
(
sessions
)
=>
{
if
(
!
sessions
||
sessions
.
length
===
0
)
return
0
;
// 1. 有正在秒杀的就定位到正在秒杀的场次
const
ongoingSessions
=
sessions
.
filter
(
session
=>
session
.
status
===
'ongoing'
);
if
(
ongoingSessions
.
length
>
0
)
{
// 如果有多个正在秒杀的场次,选择开始时间最早的
const
earliestOngoingSession
=
ongoingSessions
.
reduce
((
earliest
,
current
)
=>
{
const
getStartTimeFromTags
=
(
session
)
=>
{
const
timeTag
=
session
.
timeTags
.
find
(
tag
=>
tag
.
includes
(
'开始'
)
&&
/^
\d{2}
:
\d{2}
开始$/
.
test
(
tag
));
if
(
timeTag
)
{
const
timeStr
=
timeTag
.
replace
(
'开始'
,
''
);
const
[
hours
,
minutes
]
=
timeStr
.
split
(
':'
).
map
(
Number
);
return
hours
*
60
+
minutes
;
}
return
0
;
};
return
getStartTimeFromTags
(
current
)
<
getStartTimeFromTags
(
earliest
)
?
current
:
earliest
;
});
return
sessions
.
findIndex
(
session
=>
session
.
id
===
earliestOngoingSession
.
id
);
}
// 2. 只有已结束和未开始时定位到即将开始的场次
const
upcomingSessions
=
sessions
.
filter
(
session
=>
session
.
status
===
'upcoming'
);
if
(
upcomingSessions
.
length
>
0
)
{
// 选择开始时间最早的即将开始的场次
const
earliestUpcomingSession
=
upcomingSessions
.
reduce
((
earliest
,
current
)
=>
{
const
getStartTimeFromTags
=
(
session
)
=>
{
const
timeTag
=
session
.
timeTags
.
find
(
tag
=>
tag
.
includes
(
'开始'
)
&&
/^
\d{2}
:
\d{2}
开始$/
.
test
(
tag
));
if
(
timeTag
)
{
const
timeStr
=
timeTag
.
replace
(
'开始'
,
''
);
const
[
hours
,
minutes
]
=
timeStr
.
split
(
':'
).
map
(
Number
);
return
hours
*
60
+
minutes
;
}
return
0
;
};
return
getStartTimeFromTags
(
current
)
<
getStartTimeFromTags
(
earliest
)
?
current
:
earliest
;
});
return
sessions
.
findIndex
(
session
=>
session
.
id
===
earliestUpcomingSession
.
id
);
}
// 3. 全部结束定位到最后一个场次
const
allEnded
=
sessions
.
every
(
session
=>
session
.
status
===
'ended'
);
if
(
allEnded
)
{
return
sessions
.
length
-
1
;
}
// 4. 所有场次未开始时定位到第一个场次
return
0
;
};
// 开始倒计时
const
startCountdown
=
(
sessionId
,
initialSeconds
)
=>
{
if
(
countdownTimers
.
value
[
sessionId
])
{
...
...
@@ -254,7 +306,8 @@ const startCountdown = (sessionId, initialSeconds) => {
seconds
--
;
if
(
seconds
<=
0
)
{
clearInterval
(
countdownTimers
.
value
[
sessionId
]);
// 倒计时结束,可以触发相应逻辑
// 倒计时结束,自动切换到下一场
handleCountdownEnd
(
sessionId
);
}
// 更新对应场次的倒计时数据
const
session
=
creditsSaleData
.
value
.
sessions
.
find
(
s
=>
s
.
id
===
sessionId
);
...
...
@@ -264,11 +317,66 @@ const startCountdown = (sessionId, initialSeconds) => {
},
1000
);
};
// 处理倒计时结束
const
handleCountdownEnd
=
(
endedSessionId
)
=>
{
const
sessions
=
creditsSaleData
.
value
.
sessions
;
const
currentIndex
=
sessions
.
findIndex
(
s
=>
s
.
id
===
endedSessionId
);
const
endedSession
=
sessions
[
currentIndex
];
// 如果当前场次是"即将开始"状态,转换为"正在秒杀"
if
(
endedSession
&&
endedSession
.
status
===
'upcoming'
)
{
// 更新场次状态为正在秒杀
endedSession
.
status
=
'ongoing'
;
// 重新计算结束时间倒计时
const
now
=
new
Date
();
const
endTime
=
new
Date
(
endedSession
.
endTime
);
const
remainingMs
=
endTime
.
getTime
()
-
now
.
getTime
();
if
(
remainingMs
>
0
)
{
const
hours
=
Math
.
floor
(
remainingMs
/
(
1000
*
60
*
60
));
const
minutes
=
Math
.
floor
((
remainingMs
%
(
1000
*
60
*
60
))
/
(
1000
*
60
));
const
seconds
=
Math
.
floor
((
remainingMs
%
(
1000
*
60
))
/
1000
);
const
totalSeconds
=
hours
*
3600
+
minutes
*
60
+
seconds
;
// 更新倒计时显示
endedSession
.
countdown
=
`
${
hours
.
toString
().
padStart
(
2
,
'0'
)}
:
${
minutes
.
toString
().
padStart
(
2
,
'0'
)}
:
${
seconds
.
toString
().
padStart
(
2
,
'0'
)}
`
;
// 重新启动倒计时
if
(
totalSeconds
>
0
)
{
startCountdown
(
endedSessionId
,
totalSeconds
);
}
console
.
log
(
'场次开始,状态转换为正在秒杀:'
,
endedSession
);
}
}
else
if
(
endedSession
&&
endedSession
.
status
===
'ongoing'
)
{
// 正在秒杀场次结束,标记为已结束
endedSession
.
status
=
'ended'
;
// 如果当前场次不是最后一场,切换到下一场
if
(
currentIndex
<
sessions
.
length
-
1
)
{
const
nextSession
=
sessions
[
currentIndex
+
1
];
// 检查下一场是否已经开始或即将开始
if
(
nextSession
.
status
===
'ongoing'
||
nextSession
.
status
===
'upcoming'
)
{
currentSessionIndex
.
value
=
currentIndex
+
1
;
console
.
log
(
'自动切换到下一场:'
,
nextSession
);
}
}
}
};
// 初始化倒计时
const
initCountdowns
=
()
=>
{
if
(
creditsSaleData
.
value
.
sessions
&&
Array
.
isArray
(
creditsSaleData
.
value
.
sessions
))
{
// 检查是否全部结束
const
allEnded
=
creditsSaleData
.
value
.
sessions
.
every
(
session
=>
session
.
status
===
'ended'
);
if
(
allEnded
)
{
return
;
// 全部结束,不需要倒计时
}
// 其他情况都需要倒计时
creditsSaleData
.
value
.
sessions
.
forEach
(
session
=>
{
if
(
session
.
status
===
'ongoing'
)
{
if
(
session
.
status
===
'ongoing'
||
session
.
status
===
'upcoming'
)
{
// 将倒计时字符串转换为秒数
const
timeParts
=
session
.
countdown
.
split
(
':'
);
const
hours
=
parseInt
(
timeParts
[
0
])
||
0
;
...
...
@@ -368,7 +476,9 @@ const mapSeckillDataToCreditsSale = (seckillData) => {
countdown
,
timeTags
,
goods
,
sessionKey
:
config
.
sessionKey
sessionKey
:
config
.
sessionKey
,
startTime
:
config
.
start
,
endTime
:
config
.
end
};
});
...
...
@@ -390,33 +500,9 @@ const mapSeckillDataToCreditsSale = (seckillData) => {
creditsSaleData
.
value
.
sessions
=
sessions
;
// 自动定位到正在秒杀的场次
const
ongoingSessions
=
sessions
.
filter
(
session
=>
session
.
status
===
'ongoing'
);
if
(
ongoingSessions
.
length
>
0
)
{
// 如果有多个正在秒杀的场次,选择开始时间最早的
const
earliestOngoingSession
=
ongoingSessions
.
reduce
((
earliest
,
current
)
=>
{
const
getStartTimeFromTags
=
(
session
)
=>
{
const
timeTag
=
session
.
timeTags
.
find
(
tag
=>
tag
.
includes
(
'开始'
)
&&
/^
\d{2}
:
\d{2}
开始$/
.
test
(
tag
));
if
(
timeTag
)
{
const
timeStr
=
timeTag
.
replace
(
'开始'
,
''
);
const
[
hours
,
minutes
]
=
timeStr
.
split
(
':'
).
map
(
Number
);
return
hours
*
60
+
minutes
;
}
return
0
;
};
return
getStartTimeFromTags
(
current
)
<
getStartTimeFromTags
(
earliest
)
?
current
:
earliest
;
});
// 找到最早场次的索引
const
targetIndex
=
sessions
.
findIndex
(
session
=>
session
.
id
===
earliestOngoingSession
.
id
);
if
(
targetIndex
!==
-
1
)
{
currentSessionIndex
.
value
=
targetIndex
;
}
}
else
{
// 没有正在秒杀的场次,定位到第一个
currentSessionIndex
.
value
=
0
;
}
// 智能定位场次逻辑
const
targetIndex
=
getTargetSessionIndex
(
sessions
);
currentSessionIndex
.
value
=
targetIndex
;
console
.
log
(
'映射后的秒杀数据:'
,
creditsSaleData
.
value
);
console
.
log
(
'当前选中场次索引:'
,
currentSessionIndex
.
value
);
...
...
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