|
|
@@ -76,6 +76,7 @@ function parseTimeString(timeStr) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+// 时长单位(毫秒):ms/s/m/h/d/w/mon/月/y;组合如 1y3mon、1h30m、2d
|
|
|
const DELAY_UNIT_MS = {
|
|
|
ms: 1,
|
|
|
s: 1000,
|
|
|
@@ -83,29 +84,37 @@ const DELAY_UNIT_MS = {
|
|
|
h: 60 * 60 * 1000,
|
|
|
d: 24 * 60 * 60 * 1000,
|
|
|
w: 7 * 24 * 60 * 60 * 1000,
|
|
|
+ mon: 30 * 24 * 60 * 60 * 1000,
|
|
|
'月': 30 * 24 * 60 * 60 * 1000,
|
|
|
y: 365 * 24 * 60 * 60 * 1000,
|
|
|
}
|
|
|
|
|
|
-/** 解析时长字符串,支持单位 ms/s/m/h/d/w/月,支持组合如 1h3m48s、2d30m */
|
|
|
+/** 解析时长字符串。单位:ms(毫秒) s(秒) m(分) h(时) d(天) w(周) mon/月(月) y(年),可组合如 1y3mon、7s、1h30m。纯数字按秒处理。 */
|
|
|
function parseDelayString(delayStr) {
|
|
|
- if (!delayStr || typeof delayStr !== 'string' || delayStr.trim() === '') return 0
|
|
|
+ if (delayStr == null) return 0
|
|
|
+ if (typeof delayStr === 'number' && !Number.isNaN(delayStr) && delayStr >= 0) return Math.round(delayStr * 1000)
|
|
|
+ if (typeof delayStr !== 'string' || delayStr.trim() === '') return 0
|
|
|
try {
|
|
|
- const trimmed = delayStr.trim()
|
|
|
- const re = /(\d+)\s*(ms|s|m|h|d|w|月|y)/g
|
|
|
+ const trimmed = String(delayStr).trim()
|
|
|
+ // 单位顺序:ms、mon 必须在 m、s 前,避免被拆成 m+s
|
|
|
+ const re = /(\d+)\s*(ms|mon|s|m|h|d|w|月|y)/gi
|
|
|
let total = 0
|
|
|
let hadMatch = false
|
|
|
let match
|
|
|
while ((match = re.exec(trimmed)) !== null) {
|
|
|
const value = parseInt(match[1], 10)
|
|
|
- const unit = match[2]
|
|
|
+ const unit = match[2].toLowerCase()
|
|
|
if (value < 0 || !DELAY_UNIT_MS[unit]) continue
|
|
|
total += value * DELAY_UNIT_MS[unit]
|
|
|
hadMatch = true
|
|
|
}
|
|
|
- return hadMatch ? total : null
|
|
|
+ if (hadMatch) return total
|
|
|
+ // 纯数字视为秒(如 "15" 或变量解析后只剩数字)
|
|
|
+ const bare = /^\d+$/.test(trimmed)
|
|
|
+ if (bare) return parseInt(trimmed, 10) * 1000
|
|
|
+ return 0
|
|
|
} catch (e) {
|
|
|
- return null
|
|
|
+ return 0
|
|
|
}
|
|
|
}
|
|
|
|