| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 | // pages/add-inner-device/index.jsimport http from '../../utils/http'import util from '../../utils/util'import api from '../../utils/api'Page({  /**   * 页面的初始数据   */  data: {    device_types: [],    type: '',    names: [],    name: '',    specs: [],    spec: '',    number: '',    list: [],    page: 1,    touchBottom: false,    showDate: false,    default_dates: [],    add_devices: [],    showAdded: false  },  /**   * 生命周期函数--监听页面加载d   */  onLoad: function (options) {    var that = this    api.getByName(this, 'devices/getThreeLevel', 'device_types', {      type: 'drop_menu'    }, function () {      that.updateNameSpec()    });    // api.getByName(this, 'inner-devices/get', 'names', {type: 'drop_menu'});    // api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu'});    this.getList()  },  getItemById(items, id) {    for (var i = 0; i < items.length; ++i) {      if (items[i].value == id) return items[i]    }    return null  },  updateNameSpec: function () {    var device_types = this.data.device_types    var typeItem = this.getItemById(device_types, this.data.type)    var names = typeItem.names;    var nameItem = this.getItemById(names, this.data.name)    // var specs = this.getItemById(nameItem.specs, this.data.spec)    this.setData({      names,      specs: nameItem.specs    })  },  switchShowDate: function (e) {    this.setData({      showDate: e.currentTarget.dataset.show,    })  },  switchShowAdded: function (e) {    this.complete()    this.setData({      showAdded: e.currentTarget.dataset.show    })  },  complete: function (e) {    wx.setStorageSync('sg-added-devices', this.data.add_devices)    wx.navigateBack({      delta: 0,    })  },  confirmDate: function (e) {    var ids = []    var add_devices = this.data.add_devices    for (var i = 0; i < add_devices.length; ++i) {      ids.push(add_devices[i].id)    }    var list = this.data.list    var [start_date, end_date] = e.detail;    start_date = util.formatDate(start_date)    end_date = util.formatDate(end_date)    for (var i = 0; i < list.length; ++i) {      if (ids.indexOf(list[i].id) == -1 && list[i].checked) {        var device = list[i]        device.start_date = start_date,          device.end_date = end_date        add_devices.push(device)      }    }    this.switchShowDate(e)    this.setData({      add_devices: add_devices,      showAdded: true    })  },  switchSelect: function (e) {    var list = this.data.list    var index = e.detail.index    list[index].checked = list[index].checked ? false : true    this.setData({      list    })  },  onChange: function (e) {    var name = e.currentTarget.dataset.name    this.setData({      [name]: e.detail    })    if (['type', 'name'].indexOf(name) != -1) {      if (name == 'type') {        this.setData({          name: '',          spec: ''        })      }      if (name == 'name') {        this.setData({          spec: ''        })      }      this.updateNameSpec()    }    this.search()  },  resetList: function () {    this.setData({      list: [],      page: 1,      touchBottom: false    })  },  search: function () {    this.resetList()    this.getList()  },  getList: function () {    var that = this    http({      url: 'inner-devices/search',      data: {        number: this.data.number,        device_id: this.data.type,        device_name_id: this.data.name,        spec_id: this.data.spec,        page: this.data.page,        free: true      },      success: function (res) {        if (res.code == 0) {          var list = that.data.list          var touchBottom = that.data.touchBottom          list = list.concat(res.data);          if (res.data.length <= 0) {            touchBottom = true;          }          that.setData({            touchBottom,            list          })        }      }    })  },  deleteDevice: function (e) {    var index = e.currentTarget.dataset.index    var add_devices = this.data.add_devices    add_devices.splice(index, 1)    this.setData({      add_devices    })  },  /**   * 生命周期函数--监听页面初次渲染完成   */  onReady: function () {  },  /**   * 生命周期函数--监听页面显示   */  onShow: function () {    var add_devices = wx.getStorageSync('sg-added-devices')    add_devices = add_devices ? add_devices : []    this.setData({      add_devices    })  },  /**   * 生命周期函数--监听页面隐藏   */  onHide: function () {  },  /**   * 生命周期函数--监听页面卸载   */  onUnload: function () {  },  /**   * 页面相关事件处理函数--监听用户下拉动作   */  onPullDownRefresh: function () {  },  /**   * 页面上拉触底事件的处理函数   */  onReachBottom: function () {    if (!this.data.touchBottom) {      var page = this.data.page      page = page + 1;      this.setData({        page      })      this.getList()    }    if (this.data.touchBottom) {      util.error('没有更多数据了')    }  },  /**   * 用户点击右上角分享   */  onShareAppMessage: function () {  }})
 |