var today = new Date()
var y = today.getFullYear()
var m = today.getMonth() + 1
var d = today.getDate()
var beginTime = `${y}-${m}-${d} 08:00:01`; // 开抢时间
var orderDate = `${y}-${m}-${d+6}`
var orderTimes = [
"10:00-10:30",
"10:30-11:00",
"11:00-11:30",
"14:30-15:00",
"09:30-10:00",
"09:00-09:30",
"15:00-15:30",
"15:30-16:00",
"16:00-16:30",
"16:30-17:00",
"08:30-09:00"
]
var basicInfo = {
equipmentId: 'c689602c8595d61d6e06d05e5d719034',
equipmentName: '流式分析仪',
phone: "15927056970",
}
const params = orderTimes.map(t => ({
orderTime: t,
orderDate: orderDate,
...basicInfo
}))
var delay = 40; // 倒计时精度:20毫秒
var repeat = 20; // 抢几次
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
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();
await getUser()
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) return
else sendData()
count++
}
async function sendAjax(data) {
try {
let t = await add(data)
if ("A00000" == t.code) {
rootVue.$message.success("申请成功")
}
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 >0 ? delay:0
} catch (e) {
console.log(e)
return 0
}
}
run()
console