| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467 |
- 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);
- }
- })
|