需求每天多个时间段里可认领,在可认领时间段的倒计时一小时内,显示倒计时;在可认领时间段内,按钮为亮色;不在可认领时间段内,按钮为灰色
解决:
const timeSolts = res.data.data.limitTime
// const timeSolts = '10:40-11:00;12:00-02:00'
let obj = []
if (!timeSolts) {
obj = {
'handleValue': 'canClaim',
'beginArr': [],
'overArr': [],
}
} else {
obj = await con.handleTime(timeSolts, diff)
}
//处理可认领时间段数据
handleTime: async (timeSolts, diff) => {
let timeArrays = timeSolts.split(';')
let curArray = []
let obj = {}
let val = ''
let beginArr = []
let overArr = []
if (!timeArrays) {
val = 'canClaim'
} else {
timeArrays.forEach((item) => {
if (!item) {
return
}
const limitBegin = item.split('-')[0]
const limitOver = item.split('-')[1]
const today = moment().format('YYYY-MM-DD')
//开抢时间
const timeBeginToday = moment(today + ' ' + limitBegin).unix()
const timeOverToday = moment(today + ' ' + limitOver).unix()
beginArr.push(timeBeginToday - 3600)
overArr.push(timeOverToday)
//现在时间
const nowTime = moment().unix() + parseInt(diff)
let handleValue = ''
if (nowTime >= timeBeginToday && nowTime < timeOverToday) {
handleValue = 'canClaim'
} else if (nowTime > timeOverToday) {
handleValue = 'cannotCliaim'
} else if (nowTime < timeBeginToday && (timeBeginToday - nowTime) > 3600) {
handleValue = 'cannotCliaim'
} else if (nowTime < timeBeginToday && (timeBeginToday - nowTime) <= 3600) {
handleValue = (timeBeginToday - nowTime)
}
curArray.push(handleValue)
})
let curSet = new Set(curArray)
if (curSet.has('canClaim')) {
val = 'canClaim'
} else {
let newArray = []
for (let v of curSet) {
if (typeof (v) === 'number') {
newArray.push(v)
}
}
if (newArray.length === 0 && !curSet.has('canClaim')) {
val = 'cannotCliaim'
} else if (newArray.length) {
val = Math.min(...newArray)
}
}
}
obj = {
val,
beginArr,
overArr
}
return obj
},