// pages/add-inner-device/index.js import 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) { api.getByName(this, 'devices/get', 'device_types', {type: 'drop_menu'}); api.getByName(this, 'inner-devices/get', 'names', {type: 'drop_menu'}); api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu'}); this.getList() }, switchShowDate: function(e) { this.setData({ showDate: e.currentTarget.dataset.show }) }, switchShowAdded: function(e) { 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.setData({ add_devices: add_devices }) this.switchShowDate(e) }, switchSelect: function(e) { var list = this.data.list var index = e.currentTarget.dataset.index var can = e.currentTarget.dataset.can if(can) { list[index].checked = list[index].checked ? false : true this.setData({ list }) } else { util.error('该设备暂不能借用') } }, onChange: function(e) { var name = e.currentTarget.dataset.name this.setData({ [name]: e.detail }) 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, name: this.data.name, spec_id: this.data.spec, page: this.data.page }, 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 () { } })