var today = new Date()
var y = today.getFullYear()
var m = today.getMonth() + 1
var d = today.getDate()
var beginTime = `${y}-${m}-${d} 08:00:00`; // 开抢时间
var next = new Date(Date.now()+6*24*60*60*1000) // 预约下周
var orderDate = `${next.getFullYear()}-${format(next.getMonth()+1)}-${format(next.getDate())}`
console.log(beginTime)
console.log(orderDate)
function format(v){
return v>=10 ? v :`0${v}`
}
var orderTimes = [
"12:30-14:30",
"11:30-12:30",
"16:30-17:30",
"15:30-16:30",
"10:30-11:30",
"14:30-15:30",
"08:00-09:30",
"09:30-10:30",
"17:30-18:30",
"18:30-19:30",
"19:30-21:00"
]
var basicInfo = {
equipmentId: '7a20fea6d6e22799ad1f0251424279eb',
equipmentName: '激光共聚焦',
phone: "15927056970",
}
const params = orderTimes.map(t => ({
orderTime: t,
orderDate: orderDate,
...basicInfo
}))
var delay = 40; // 倒计时精度:20毫秒
var repeat = 30; // 抢几次
var count = 0;
var done = false; // 是否抢到
var rootVue = document.getElementById('app').__vue__;
var add = rootVue.$api.reserveManagement.orderRecord.add
var queryEquipmentAndOrderRecordsByIdEx = rootVue.$api.laboratoryEquipmentManagement.equipment.queryEquipmentAndOrderRecordsByIdEx
var getUser = rootVue.$api.basicDataManagement.login.getLoginInfo
var done = false
async function run() {
let delayTime = await getDelayTime();
let beginTimestamp = new Date(beginTime).getTime() + delayTime
console.log("开始时间:",new Date(beginTimestamp))
const countdownTimer = setInterval(() => {
let startTime = (Date.now() - beginTimestamp) / 1000;
// 还没到开抢时间
if (startTime < 0) {
console.log(`倒计时 ${-parseInt(startTime)} 秒`)
return
}
// 到了开抢时间
else {
sendData()
clearInterval(countdownTimer)
}
}, delay)
}
// 提交信息
async function sendData() {
let orderTimes = await getOrderTimes();
let data = params.find(v => !orderTimes.includes(v.orderTime))
if (data) {
await sendAjax(data)
} else {
// 已经被抢完了
rootVue.$message.error("已经被抢光了")
return
}
console.log("------- 等待0.5秒 ---------")
await sleep(500)
if (count > repeat || done) return
else sendData()
count++
}
async function sendAjax(data) {
try {
let t = await add(data)
if ("A00000" == t.code) {
rootVue.$message.success("申请成功")
done = true
}
return t
} catch (e) {
console.log(e)
}
}
async function getOrderTimes() {
try {
let { data: { orderTimes } } = await queryEquipmentAndOrderRecordsByIdEx(basicInfo.equipmentId, orderDate)
return orderTimes
} catch (e) {
console.log(e)
}
}
const sleep = delay => new Promise(res => setTimeout(res, delay));
async function getDelayTime() {
try {
let { date } = await queryEquipmentAndOrderRecordsByIdEx(basicInfo.equipmentId, orderDate)
let delay = Date.now() - new Date(date)
console.log("服务器时间:",date)
console.log("浏览器时间:",new Date(Date.now()))
console.log("延迟时间:",delay)
return delay
} catch (e) {
console.log(e)
return 0
}
}
run()
console