var _request = require('../../../utils/request.js') var rid; Page({ data: { detail: {}, ordertype:1, today:"", date1:"", date3:"", startdate1:"", enddate1:"", date2:"", startdate2:"", enddate2:"", checkbox:[], num1:1, num2:1, num3:1, orderList:[], orderListMul:[], total:0, checkbox_un:[], preOrderList:[], historyOrderList:[], balance: 0, cashTotal: 0, orderprice:[4,10,10],//单点价格早,中,晚 ordersubprice:[2,4,4],//订餐价格早,中,晚 checkAll:false,//全选 loading:false,//是否执行中 can1:true,//单点时早餐是否可选 can2:true,//单点时中餐是否可选 can3:true//单点时晚餐是否可选 }, onLoad: function (options) { console.log('onload onLoad == canteen',options) // this.getRadish() this.getBalanceInfo(); this.getOrdrList(); // 将当前日期加7天 const currentDate = new Date(); const futureDate = new Date(currentDate); futureDate.setDate(futureDate.getDate() + 8); const formattedDate = this.formatDate(futureDate); this.setData({ today: this.getDateString(0), startdate1: this.getDateString(0), enddate1:this.getDateString(1), startdate2: this.getDateString(0), enddate2:this.getDateString(1), date1:this.getDateString(0), date2:formattedDate, }) }, getBalanceInfo () { var that = this var url = 'v1/user/cash/balance/info' var params = { } var success = function (res) { var result = res.data.available that.setData({ balance: result, cashTotal: res.data }) } _request.$get(url, params, success) }, getList () { var that = this var url = 'v1/user/balances' var params = { } var success = function (res) { var data = res.data that.setData({ detail: data }) } _request.$get(url, params, success) }, getDateString(addnum){ let year = new Date().getFullYear() + addnum; let month = new Date().getMonth() +1; let day = new Date().getDate(); if(month<10) month = '0'+month; if(day<10) day = '0'+day; let times = year +'-'+ month +'-'+ day; return times; }, formatDate(date) { const year = date.getFullYear(); const month = String(date.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed const day = String(date.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }, bindDateChange: function(e) { console.log('picker1发送选择改变,携带值为', e.detail.value) this.setData({ date1: e.detail.value, startdate2: e.detail.value }) this.getSingleRule(); this.calcOrder(); }, bindDateChange2: function(e) { console.log('picker2发送选择改变,携带值为', e.detail.value) this.setData({ date2: e.detail.value }) this.calcOrder(); }, radioChange:function(e){ console.log('radio发生change事件,携带value值为:', e.detail.value); if(e.detail.value == 0){ this.setData({ checkbox:[] }); this.getSingleRule(); } this.setData({ ordertype:e.detail.value, total:0 }); this.calcOrder(); }, getSingleRule() { var that = this var url = 'newapi:food/check-date?date='+this.data.date1; var params = { } var success = function (res) { console.log('res',res) var data = res.data.data; that.setData({ can1:data['1'], can2:data['2'], can3:data['3'], }) } _request.$get(url, params, success) }, getOrdrList:function(e){ var that = this var url = 'newapi:food/get-info' var params = { } var success = function (res) { var data = res.data.data.list; var hisOrderList = []; for (let item in data) { let thedate = data[item]['food_date']; let xingqi; if(new Date(thedate).getDay() == 0) xingqi = '星期日'; if(new Date(thedate).getDay() == 1) xingqi = '星期一'; if(new Date(thedate).getDay() == 2) xingqi = '星期二'; if(new Date(thedate).getDay() == 3) xingqi = '星期三'; if(new Date(thedate).getDay() == 4) xingqi = '星期四'; if(new Date(thedate).getDay() == 5) xingqi = '星期五'; if(new Date(thedate).getDay() == 6) xingqi = '星期六'; data[item]['food_day'] = xingqi; data[item]['can_break_fast'] = data[item]['break_fast'] ? false : data[item]['can_break_fast']; data[item]['can_lunch'] = data[item]['lunch'] ? false : data[item]['can_lunch']; data[item]['can_dinner'] = data[item]['dinner'] ? false : data[item]['can_dinner']; if(data[item]['break_fast']) hisOrderList.push(`${data[item]['food_date']},1,1`); if(data[item]['lunch']) hisOrderList.push(`${data[item]['food_date']},1,2`); if(data[item]['dinner']) hisOrderList.push(`${data[item]['food_date']},1,3`); } that.setData({ preOrderList: data, date1:data[0]['food_date'], date3:data[0]['food_date'], date2:data[data.length - 1]['food_date'], historyOrderList:hisOrderList }) console.log('data',data); } _request.$get(url, params, success) }, checkAllfunc: function(e){ console.log('checkAll');//checkAll let nowcheckAll = !this.data.checkAll; let checkarr = []; let preOrderList = this.data.preOrderList; for(let i in preOrderList){ if(preOrderList[i]['can_break_fast']){ preOrderList[i]['break_fast'] = !preOrderList[i]['break_fast']; checkarr.push(`${preOrderList[i]['food_date']},1,1`); } if(preOrderList[i]['can_lunch']){ preOrderList[i]['lunch'] = !preOrderList[i]['lunch']; checkarr.push(`${preOrderList[i]['food_date']},1,2`); } if(preOrderList[i]['can_dinner']){ preOrderList[i]['dinner'] = !preOrderList[i]['dinner']; checkarr.push(`${preOrderList[i]['food_date']},1,3`); } } if(!nowcheckAll) checkarr = []; let a = checkarr; let b = this.data.historyOrderList; // 从数组a中筛选出不包含在数组b中的值 var resultA = a.filter(itemA => !b.includes(itemA)); console.log("数组a中与数组b不同的值: ",resultA); this.setData({ checkAll:nowcheckAll, orderListMul:resultA, preOrderList:preOrderList }); this.calcOrder(); }, checkboxChangeList(e){ console.log('checkboxChangeList checkbox发生change事件,携带value值为:', e.detail.value); console.log('historylist:', this.data.historyOrderList); let a = e.detail.value; let b = this.data.historyOrderList; // 从数组a中筛选出不包含在数组b中的值 var resultA = a.filter(itemA => !b.includes(itemA)); console.log("数组a中与数组b不同的值: ",resultA); this.setData({ orderListMul:resultA }); this.calcOrder(); }, checkboxChange(e) { console.log('checkboxChange checkbox发生change事件,携带value值为:', e.detail.value); this.setData({ checkbox:[e.detail.value] }); this.calcOrder(); }, checkboxChangeUn(e) { console.log('checkboxUn发生change事件,携带value值为:', e.detail.value); this.setData({ checkbox_un:e.detail.value }); this.calcOrder(); }, calcOrder(){ console.log('calcOrder',this.data.ordertype,this.data.date1,this.data.checkbox); if(this.data.ordertype == 0){ let orderl = []; if(this.data.date1 && this.data.checkbox.length>0){ let total = 0; for(let i in this.data.checkbox){ let val = this.data.checkbox[i]; let num; let price; if(val==1){ num = this.data.num1; }else if(val==2){ num = this.data.num2; }else{ num = this.data.num3; } price = this.data.orderprice[val-1] * num; orderl.push([this.data.date1,val,num,price]); total += price; } this.setData({ orderList:orderl, total:total }) console.log('orderl',orderl); }else{ this.setData({ orderList:[] }) } }else{ if(this.data.orderListMul.length>0){ let total=0; for(let i in this.data.orderListMul){ let val = this.data.orderListMul[i].split(',')[2]; let price; price = this.data.ordersubprice[val-1] * 1; total += price; } this.setData({ total:total }) }else{ this.setData({ orderList:[], total:0 }) } } }, getDatesInRange(startDate, endDate) { const dates = []; let currentDate = new Date(startDate); while (currentDate <= endDate) { console.log('1',this.data.checkbox_un.includes("1")); console.log('2',this.data.checkbox_un.includes("2")); if(this.data.checkbox_un.includes("1") && this.data.checkbox_un.includes("2")){ if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) { dates.push(new Date(currentDate)); } }else if(this.data.checkbox_un.includes("1") && !this.data.checkbox_un.includes("2")){//剔除周六 if (currentDate.getDay() !== 6) { dates.push(new Date(currentDate)); } }else if(this.data.checkbox_un.includes("2") && !this.data.checkbox_un.includes("1")){//剔除周日 if (currentDate.getDay() !== 0) { dates.push(new Date(currentDate)); } }else{ dates.push(new Date(currentDate)); } currentDate.setDate(currentDate.getDate() + 1); } return dates; }, add: function (e) { var type = e.target.dataset.type; if(type == 1){ this.setData({ num1:this.data.num1+1 }) }else if(type == 2){ this.setData({ num2:this.data.num2+1 }) }else{ this.setData({ num3:this.data.num3+1 }) } this.calcOrder(); }, mius: function (e) { var type = e.target.dataset.type; if(type == 1){ let num = this.data.num1 - 1 <1?1:this.data.num1-1; this.setData({ num1:num }) }else if(type == 2){ let num = this.data.num2 - 1 <1?1:this.data.num2-1; this.setData({ num2:num }) }else{ let num = this.data.num3 - 1 <1?1:this.data.num3-1; this.setData({ num3:num }) } this.calcOrder(); }, changeCount: function (val) { var count = val.detail.value var type = val.target.dataset.type; if(type == 1){ this.setData({ num1:count }) }else if(type == 2){ this.setData({ num2:count }) }else{ this.setData({ num3:count }) } this.calcOrder(); }, toOrder: function(){ let paramorder = { "foodInfoElement": [ // { // "date": "string", // "nums": 0, // "type": 0 // } ], "single": this.data.ordertype == 0 ? true:false } if(this.data.ordertype == 0){ console.log('order',this.data.orderList); for(let i in this.data.orderList){ let thepar = { "date": this.data.orderList[i][0], "nums": parseInt(this.data.orderList[i][2]), "type": parseInt(this.data.orderList[i][1]) } paramorder['foodInfoElement'].push(thepar); } }else{ console.log('order',this.data.orderListMul); for(let i in this.data.orderListMul){ let thepar = { "date": this.data.orderListMul[i].split(',')[0], "nums": parseInt(this.data.orderListMul[i].split(',')[1]), "type": parseInt(this.data.orderListMul[i].split(',')[2]) } paramorder['foodInfoElement'].push(thepar); } } console.log('paramorder',paramorder); if(paramorder['foodInfoElement'].length == 0){ wx.showToast({ title: '请先选餐', icon: 'error', duration: 2000 }) return; } this.setData({ loading:true }) var that = this var url = 'newapi:food/order-food' var success = function (res) { console.log('res',res); if(res.data.code == 0){ wx.showToast({ title: '已成功下单', icon: 'success', duration: 2000 }) setTimeout(function(){ wx.navigateBack() },1000); }else{ wx.showToast({ title: res.data.msg, icon: 'none', duration: 2000 }) setTimeout(function(){ that.setData({ loading:false }) },500) } } _request.$post(url, JSON.stringify(paramorder), success); } })