代码如下,控制台只打印test1,不打印test2。并且这个页面中其他2个wx.request是正常工作的
import Dialog from 'miniprogram_npm/@vant/weapp/dialog/dialog';
import Toast from 'miniprogram_npm/@vant/weapp/toast/toast'
Page({
data: {
showPopup_start: false,
showUntilTime: false,
showPopup_until: false,
curTime: new Date().getTime(),
minDate: new Date().getTime(),
//预约时间只能在一周内(暂定所有场所均这样)
maxDate: new Date().getTime() + 1000 * 60 * 60 * 24 * 7,
//filter控制预约最小时间间隔(暂定所有场所均这样)
filter(type, options) {
//设置时间间隔为30分钟
if (type === 'minute') {
return options.filter(option => option % 30 === 0);
}
return options;
},
//formatter的功能是让时间选择器2022后面加个‘年’字,07后面加个‘月’字
formatter(type, value) {
if (type === 'year') {
return `${value}年`;
}
if (type === 'month') {
return `${value}月`;
}
return value;
},
//开始时间和结束时间
startTime: "",
startTime_str: "",
untilTime: new Date().getTime(),
untilTime_str: "",
//学号姓名和预约原因
stuNumber: "",
applicantName: "",
reason:'空',
//仪器信息
equipmentName: "",
equipmentPlace: "",
equipmentID: "",
equipmentManageer: "",
},
//点击预约时间展示popup(开始时间)
showTime1() {
this.setData({ showPopup_start: true })
},
//展示结束时间
showTime2() {
this.setData({ showPopup_until: true })
},
//点击其他地方关闭popup
onClose() {
this.setData({ showPopup_start: false, showPopup_until: false })
},
//让时间选择器选择预约时间更稳定
onInput(event) {
this.setData({
currentDate: event.detail,
});
},
//得到时间选择器选择的预约开始时间值
confirm1(event) {
// judgement是一个用来做判断的变量,如果时间是被预约过的话judgement=1
let judgement = 0
let that = this
// 获取数据库里相应仪器被预约过的时间段
wx.request({
url: 'http://192.168.15.135:5000/appointment/serch_by_equipment',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
equipment_id:that.data.equipmentID
},
success(res) {
console.log("confirm1",res)
let orders = res.data
let length = orders.length
let time = new Date().getTime()
for(let i = 0;i<length;++i){
let temp = orders[i].untilTime
// 预约开始时间不能在他人预约时间段中
if(temp-time>0 && event.detail>=orders[i].startTime && event.detail<=orders[i].untilTime){
judgement = 1
Toast.fail("该时间段已被预约")
}
}
if(judgement == 0){
let date = new Date(event.detail)
let startTime = date.getTime()
// 转换时间戳
let curDate = new Date(startTime)
let curYear = curDate.getFullYear()
let tempMonth = curDate.getMonth() + 1
let curMonth = tempMonth < 10 ? "0" + tempMonth : tempMonth
let curDay = curDate.getDate().toString().length == 1 ? ("0" + curDate.getDate()) : curDate.getDate()
let curHour = curDate.getHours().toString().length == 1 ? ("0" + curDate.getHours()) : curDate.getHours()
let curMinute = curDate.getMinutes().toString().length == 1 ? ("0" + curDate.getMinutes()) : curDate.getMinutes()
let startTime_str = curYear + "-" + curMonth + "-" + curDay + " " + curHour + ":" + curMinute
that.setData({
startTime: startTime,
startTime_str: startTime_str,
showPopup_start: false,
showUntilTime: true
})
console.log("startTime_str", that.data.startTime_str)
}
},
fail(err) {
console.log("获取订单信息失败", err)
}
})
},
//得到预约的结束时间
confirm2(event) {
let that = this
let date = new Date(event.detail)
let untilTime = date.getTime()
let judgement = 0
// 如果选择的时间小于当前时间,则提示用户
if (date.getTime() < that.data.curTime) {
Toast.fail("请选择当前时间以后的时间")
return
}
// 如果选择的时间小于开始时间,则提示用户
if (date.getTime() < that.data.startTime) {
Toast.fail("请选择开始时间以后的时间")
return
}
// POKE空间活动室的预约时间不能超过3小时
if (that.data.equipmentPlace == "POKE空间") {
if (untilTime - that.data.startTime > 10800000) {
Toast.fail("POKE空间预约时长不能超过3小时")
return
}
}
// 判断选择的结束时间是否符合规范(限制结束时间不能在别人的预约时间内,开始时间小于别人的开始时间但是结束时间大于别人的开始时间)
wx.request({
url: 'http://192.168.15.135:5000/appointment/serch_by_equipment',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
equipment_id:that.data.equipmentID
},
success(res) {
console.log("confirm2",res)
let orders = res.data
let length = orders.length
let time = new Date().getTime()
for(let i = 0;i<length;++i){
let temp = orders[i].untilTime
if((event.detail>=orders[i].startTime&&event.detail<=orders[i].untilTime)||(that.data.startTime<=orders[i].startTime&&event.detail>=orders[i].untilTime)){
judgement = 1
Toast.fail("该时间段内有他人在使用")
}
}
if(judgement == 0){
// 转换时间戳
let curDate = new Date(untilTime)
let curYear = curDate.getFullYear()
let tempMonth = curDate.getMonth() + 1
let curMonth = tempMonth < 10 ? "0" + tempMonth : tempMonth
let curDay = curDate.getDate().toString().length == 1 ? ("0" + curDate.getDate()) : curDate.getDate()
let curHour = curDate.getHours().toString().length == 1 ? ("0" + curDate.getHours()) : curDate.getHours()
let curMinute = curDate.getMinutes().toString().length == 1 ? ("0" + curDate.getMinutes()) : curDate.getMinutes()
let untilTime_str = curYear + "-" + curMonth + "-" + curDay + " " + curHour + ":" + curMinute
that.setData({
untilTime: untilTime,
untilTime_str: untilTime_str,
showPopup_until: false,
showUntilTime: true
})
console.log("untilTime", that.data.untilTime)
console.log("untilTime_str", that.data.untilTime_str)
}
},
fail(err) {
console.log("获取订单信息失败", err)
}
})
},
/*获取表单内容*/
//获取姓名
getInputName(e) {
this.setData({
applicantName: e.detail.value
})
},
//获取学号/工号
getInputNumber(e) {
this.setData({
stuNumber: e.detail.value
})
},
//获取预约原因
getReason(e){
console.log(e)
this.setData({
reason:e.detail.value
})
},
/* 二次确认订单(弹出框) */
submit() {
let that = this
//输入信息有空时,弹出toast提示
if (that.data.applicantName == '' || that.data.stuNumber == '' || that.data.startTime == '' || that.data.untilTime == '') {
Toast.fail("信息不完整")
} else {
//弹窗
Dialog.confirm({
title: "提示",
message: "预约空间后,如有特殊情况\n不使用空间需要及时取消预约 " + "\n3次预约不来者纳入黑名单" + "\n使用完空间后保持物品摆放整齐"
}).then(() => {
wx.request({
url: 'http://192.168.15.135:5000/appointment/add',
method: 'POST',
header: {
'content-type': 'application/json'
},
data: {
user_id: app.globalData.user_id,
equipment_id: that.data.equipmentID,
start_time: that.data.startTime_str,
end_time: that.data.untilTime_str
},
success(res) {
console.log("添加成功", res)
wx.navigateTo({
url: '/pages/finishedOrder/finishedOrder',
})
},
fail(err) {
console.log("添加失败", err)
}
})
}).catch(() => {
})
}
},
onLoad(options) {
let that = this;
console.log("传来的仪器信息", options)
that.setData({
equipmentName: (options.equipmentName ? options.equipmentName : ''),
equipmentPlace: (options.equipmentPlace ? options.equipmentPlace : ''),
equipmentID: (options.equipmentID ? options.equipmentID : ''),
})
},
})