mirror of
https://git.mirrors.martin98.com/https://github.com/sub-store-org/Sub-Store.git
synced 2025-08-11 11:28:59 +08:00
feat: 订阅支持开始日期和重置周期
This commit is contained in:
parent
518de2e919
commit
4b27d40602
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "sub-store",
|
"name": "sub-store",
|
||||||
"version": "2.14.246",
|
"version": "2.14.247",
|
||||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and ShadowRocket.",
|
||||||
"main": "src/main.js",
|
"main": "src/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
@ -111,7 +111,11 @@ async function getFlowInfo(req, res) {
|
|||||||
}
|
}
|
||||||
success(res, {
|
success(res, {
|
||||||
...parseFlowHeaders(flowHeaders),
|
...parseFlowHeaders(flowHeaders),
|
||||||
remainingDays: getRmainingDays($arguments.resetDay),
|
remainingDays: getRmainingDays({
|
||||||
|
resetDay: $arguments.resetDay,
|
||||||
|
startDate: $arguments.startDate,
|
||||||
|
cycleDays: $arguments.cycleDays,
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
failed(
|
failed(
|
||||||
|
@ -144,22 +144,58 @@ export function validCheck(flow) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRmainingDays(_resetDay) {
|
export function getRmainingDays(opt = {}) {
|
||||||
if (!_resetDay) return;
|
try {
|
||||||
const resetDay = parseInt(_resetDay);
|
let { resetDay, startDate, cycleDays } = opt;
|
||||||
if (isNaN(resetDay) || resetDay <= 0 || resetDay > 31) return;
|
if (['string', 'number'].includes(typeof opt)) {
|
||||||
|
resetDay = opt;
|
||||||
|
}
|
||||||
|
|
||||||
let now = new Date();
|
if (startDate && cycleDays) {
|
||||||
let today = now.getDate();
|
cycleDays = parseInt(cycleDays);
|
||||||
let month = now.getMonth();
|
if (isNaN(cycleDays) || cycleDays <= 0)
|
||||||
let year = now.getFullYear();
|
throw new Error('重置周期应为正整数');
|
||||||
let daysInMonth;
|
if (!startDate || !Date.parse(startDate))
|
||||||
|
throw new Error('开始日期不合法');
|
||||||
|
|
||||||
if (resetDay > today) {
|
const start = new Date(startDate);
|
||||||
daysInMonth = 0;
|
const today = new Date();
|
||||||
} else {
|
start.setHours(0, 0, 0, 0);
|
||||||
daysInMonth = new Date(year, month + 1, 0).getDate();
|
today.setHours(0, 0, 0, 0);
|
||||||
|
if (start.getTime() > today.getTime())
|
||||||
|
throw new Error('开始日期应早于现在');
|
||||||
|
|
||||||
|
let resetDate = new Date(startDate);
|
||||||
|
resetDate.setDate(resetDate.getDate() + cycleDays);
|
||||||
|
|
||||||
|
while (resetDate < today) {
|
||||||
|
resetDate.setDate(resetDate.getDate() + cycleDays);
|
||||||
|
}
|
||||||
|
|
||||||
|
const timeDiff = resetDate.getTime() - today.getTime();
|
||||||
|
const daysDiff = Math.ceil(timeDiff / (1000 * 3600 * 24));
|
||||||
|
|
||||||
|
return daysDiff;
|
||||||
|
} else {
|
||||||
|
if (!resetDay) throw new Error('未提供月重置日 resetDay');
|
||||||
|
resetDay = parseInt(resetDay);
|
||||||
|
if (isNaN(resetDay) || resetDay <= 0 || resetDay > 31)
|
||||||
|
throw new Error('月重置日应为 1-31 之间的整数');
|
||||||
|
let now = new Date();
|
||||||
|
let today = now.getDate();
|
||||||
|
let month = now.getMonth();
|
||||||
|
let year = now.getFullYear();
|
||||||
|
let daysInMonth;
|
||||||
|
|
||||||
|
if (resetDay > today) {
|
||||||
|
daysInMonth = 0;
|
||||||
|
} else {
|
||||||
|
daysInMonth = new Date(year, month + 1, 0).getDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
return daysInMonth - today + resetDay;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
$.error(`getRmainingDays failed: ${e.message ?? e}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return daysInMonth - today + resetDay;
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user