b_index.js 7.9 KB
var mta = require('mta-wechat-analysis')
var util = require('../../../utils/util.js')
var uri = require('../../../utils/uri.js')
var bookOrder = require('../../../utils/bookCommon.js')
import App from '../../../utils/api';

const {regeneratorRuntime} = global
Page({
  data: {
    isloading: false,
    pagex: 1,
    pagey: 10,
    sort: null,
    dateStr: '',
    calendarMaskLoad: false,
    calendarMask: false,
    dayStyle: [
      {month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5'},
      {month: 'current', day: new Date().getDate(), color: 'white', background: '#AAD4F5'}
    ],
    keywords: '',
    sort: '',
    form: {
      time: -1,
    },
    timePickerArray: [
      [],
      [],
      [],
    ],
    speciallist: [],
    ReservesCountMonth: [],
    ReservesTotal: 0,
    timeStrLabel: bookOrder.timeStrLabel
  },
  sellerid: 0,
  indexOpt: null,
  YYYY: 'YYYY',
  onLoad: async function (opt) {
    if (opt.rid) {
      this.reserveId = opt.rid
      delete opt.rid
    }
    let enter_params=util.cache('enter_params')
    if(enter_params && enter_params.hasNewLite){
      opt.hasNewLite=1
    }
    this.indexOpt = opt;
    mta.Page.init({
      "appID": "500667280",
      "eventID": "500667287",
      "statPullDownFresh": true,
      "statShareApp": true,
      "statReachBottom": true,
      "lauchOpts": opt, //渠道分析
    });
    let d = new Date() / 1000;
    let dateStr = util.unixtoTime(d, 'MM-DD DAY');
    var radio = 750 / util.getsystem().windowWidth;
    var contentHeight = util.getsystem().windowHeight - Math.ceil(120 / radio)
    if (opt.seller_id) {
      this.sellerid = opt.seller_id;
      util.cache('sellerid', opt.seller_id)
      util.cache('enter_params', opt)
      App.Reserve.SellerInfo(opt)
    } else {
      this.sellerid = util.cache('sellerid');
    }
    //this.sellerid=opt.seller_id ? opt.seller_id : util.cache('sellerid');
    let timePickerArray = await bookOrder.getDayTimeSpace();
    let speciallist = [
      {date: util.unixtoTime(d, 'YYYY-MM-DD'), background: '', text: '今 天'},
      {date: util.unixtoTime(d + 86400, 'YYYY-MM-DD'), background: '', text: '明 天'},
      {date: util.unixtoTime(d + 86400 * 2, 'YYYY-MM-DD'), background: '', text: '后 天'},
    ];
    this.setData({
      listHeight: contentHeight - Math.ceil(210 / radio),
      dateStr: dateStr,
      timePickerArray: timePickerArray,
      speciallist: speciallist,
    })
    //util.setBarTitle('预订本');
    this.indexOpt = opt;
    this.ReservesCountDay(util.unixtoTime(d, 'YYYY-MM-DD'));
    this.ReservesCountMonth(util.unixtoTime(d, 'YYYY-MM'));
  },
  onShow: function () {
    this.setData({
      isloading: false,
    })
    this.getbooklist();
    this.selectComponent('#nav').init();
    setTimeout(() => {
      this.setData({
        calendarMaskLoad: true,
      })
      let openid = getApp().openid;
      if (openid && this.indexOpt.seller_id) {
        App.Reserve.SellerForOpenid({openid: openid, sedata: JSON.stringify(this.indexOpt)})
        //客户经理=>人人都是客户经理
        //if(this.indexOpt.manager_id){
        App.Reserve.UpdateSellerUser({seat_openid: openid, kid: this.indexOpt.manager_id});
        //}
      } else {
        console.log('index-opt', openid, this.indexOpt);
      }

    }, 1000);
    if (!this.indexOpt.hasNewLite) {//没有登陆过新商户
      let url = './common/newSellerLite'
      return wx.redirectTo({
        url: url
      })
    }
    if (this.reserveId) {
      let url = './b_detail?id=' + this.reserveId
      this.reserveId = ''
      return wx.navigateTo({
        url: url
      })
    }
  },
  async getbooklist(isExport = false) {
    let time = 0;
    if (this.data.form.time < 0) {
      let sid = await bookOrder.getTimeSpaceIndexData();
      time = sid.timeIndex
    } else {
      time = this.data.form.time;
    }
    var formdata = {};
    if (this.data.sort != '') {
      formdata.sort = this.data.sort
    }
    if (this.data.keywords != '') {
      formdata.keywords = this.data.keywords
    }
    formdata.sid = this.sellerid;
    formdata.x = 1;
    formdata.y = 200;
    let rdate = await bookOrder.getTimeSpace(this.data.dateStr, time, null, null, this.YYYY);
    formdata.rdate = rdate;
    if (isExport) {
      this.__exportFile(formdata);
    } else {
      var that = this;
      App.Reserve.GetReserveList(formdata).then(function (json) {
        console.log(json)
        if (json.ret == 200) {
          that.setData({
            isloading: true,
            bookList: json.data.length == 0 ? null : json.data,
            form: {time: time},
          })
          that.boolists = that.selectAllComponents('.orderbook');
          that.boolists.forEach(function (v, i) {
            v.changeitem();
          })
        }
      }).catch(e => {
      })
    }
  },
  timeorder: function () {
    if (this.data.sort == '') {
      this.data.sort = 1;
    } else {
      this.data.sort = '';
    }

    this.setData({
      sort: this.data.sort
    })
    this.getbooklist();
  },
  searchinput: function (e) {
    this.data.keywords = e.detail.value;
    this.setData({
      keywords: this.data.keywords
    })
    if (e.detail.value == '') {
      this.getbooklist();
    }
  },
  calendarShow() {
    console.log('xxxxx')
    this.setData({
      calendarMask: true
    })
    console.log(this.data.calendarMask)
  },
  dateChange(event) {
    this.YYYY = event.detail.date.substr(0, 4);
    let cktime = util.timetouUnix(event.detail.date)
    console.log('cktime', cktime, this.YYYY)
    let clickDay = event.detail.date.substr(8, 2);
    let changeDay = `dayStyle[1].day`;
    let changeBg = `dayStyle[1].background`;
    this.setData({
      dateStr: util.unixtoTime(cktime, 'MM-DD `DAY/周WW`'),
      [changeDay]: clickDay,
      [changeBg]: "#84e7d0"
    })
    this.getbooklist();
    this.calendarClose();
    this.ReservesCountDay(event.detail.date);
  },
  calendarClose() {
    this.setData({
      calendarMask: false
    })
  },
  selectedValue: function (e) {
    this.setData({
      form: {time: e.currentTarget.dataset.value}
    })
    this.getbooklist();
  },
  searchbook: function (e) {
    console.log(this.data.form)
    this.getbooklist();
  },
  __exportFile(formdata) {
    App.Reserve.ExportReserve(formdata).then(res => {
      let url = res.data;
      if (!url) {
        return util.toast('没有预订数据', 'none')
      }
      wx.downloadFile({
        url: url,
        success: function (res) {
          const filePath = res.tempFilePath;
          console.log('filePath', filePath);
          wx.previewImage({
            current: filePath,
            urls: [filePath]
          })
          wx.saveImageToPhotosAlbum({
            filePath: filePath,
            fail(res) {
              util.toast('保存失败', 'none')
            }
          })
        }
      })
    });

  },
  exportFile() {
    this.getbooklist(true);
  },
  ReservesCountDay(day) {
    App.Reserve.ReservesCountDay({
      sid: this.sellerid,
      day: day
    }).then(res => {

      let ReservesTotal = 0;
      let ReservesCountDay = {};
      if (res.ret == 200) {
        ReservesCountDay = res.data;
        Object.keys(res.data).forEach(k => {
          if (res.data[k] != -1) {
            ReservesTotal += parseInt(res.data[k])
          }
        })
      }
      this.setData({
        ReservesTotal: ReservesTotal,
        ReservesCountDay: ReservesCountDay,
      })
    })
  },
  ReservesCountMonth(month) {
    App.Reserve.ReservesCountMonth({
      sid: this.sellerid,
      month: month
    }).then(res => {
      let ReservesCountMonth = [];
      if (res.ret == 200) {
        res.data.forEach(v => {
          ReservesCountMonth.push(v >= 1 ? 1 : 0)
        })
      }
      this.setData({
        ReservesCountMonth: ReservesCountMonth,
      })
    })
  },
  clickMonth(e) {
    this.ReservesCountMonth(e.detail.currentYear + '-' + e.detail.currentMonth);
  },
  gotoday(e) {
    this.ReservesCountMonth(e.detail.date.substr(0, 7));
  }
})