index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // pages/filter/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. active: 0,
  11. devices: [],
  12. device_ids: [''],
  13. names: [],
  14. device_name_ids: [''],
  15. specs: [],
  16. spec_ids: [''],
  17. rent_types: [],
  18. rent_type_ids: ['']
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. },
  25. onChange: function(e) {
  26. var name = e.currentTarget.dataset.name
  27. this.setData({
  28. [name]: e.detail
  29. })
  30. if(['device_id', 'device_name_id'].indexOf(name) != -1) {
  31. if(name == 'device_id') {
  32. this.setData({
  33. device_name_id: '',
  34. spec_id: ''
  35. })
  36. }
  37. if(name == 'device_name_id') {
  38. this.setData({
  39. spec_id: ''
  40. })
  41. }
  42. this.updateNameSpec()
  43. }
  44. },
  45. confirm: function() {
  46. this.saveFilter()
  47. wx.navigateBack()
  48. },
  49. /**
  50. * 生命周期函数--监听页面初次渲染完成
  51. */
  52. onReady: function () {
  53. },
  54. delete: function(e) {
  55. var name = e.currentTarget.dataset.name
  56. var index = e.currentTarget.dataset.index
  57. var items = this.data[name]
  58. if(index == 0) return false;
  59. items.splice(index, 1)
  60. this.setData({
  61. [name]: items
  62. })
  63. },
  64. add: function(e) {
  65. var name = e.currentTarget.dataset.name
  66. var items = this.data[name]
  67. if(!items[items.length - 1]) {
  68. util.error('请先选择');
  69. return false
  70. }
  71. items.push('')
  72. this.setData({
  73. [name]: items
  74. })
  75. },
  76. onDropChange: function(e) {
  77. var index = e.currentTarget.dataset.index
  78. var name = e.currentTarget.dataset.name
  79. var val = e.detail
  80. var items = this.data[name]
  81. items[index] = val
  82. this.setData({
  83. [name]: items
  84. })
  85. if(name == 'device_ids') {
  86. this.updateDeviceNames()
  87. }
  88. if(name == 'device_name_ids') {
  89. this.updateSpecs()
  90. }
  91. },
  92. updateDeviceNames() {
  93. var that = this;
  94. that.setData({
  95. device_name_ids: ['']
  96. })
  97. api.getByName(this, 'device-names/get', 'names', {type: 'drop_menu', device_ids: this.data.device_ids}, function() {
  98. that.updateSpecs();
  99. });
  100. },
  101. updateSpecs() {
  102. var that = this;
  103. that.setData({
  104. spec_ids: ['']
  105. })
  106. api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu', device_name_ids: this.data.device_name_ids});
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. var data = wx.getStorageSync('sg-data-filters')
  113. this.setData(data)
  114. api.getByName(this, 'devices/get', 'devices', {type: 'drop_menu'});
  115. api.getByName(this, 'device-names/get', 'names', {type: 'drop_menu', device_ids: this.data.device_ids});
  116. api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu', device_name_ids: this.data.device_name_ids});
  117. api.getByName(this, 'rent-types/get', 'rent_types', {type: 'drop_menu'});
  118. },
  119. /**
  120. * 生命周期函数--监听页面隐藏
  121. */
  122. onHide: function () {
  123. this.saveFilter()
  124. },
  125. saveFilter() {
  126. var data = {
  127. devices: this.data.devices,
  128. device_ids: this.data.device_ids,
  129. names: this.data.names,
  130. device_name_ids: this.data.device_name_ids,
  131. specs: this.data.specs,
  132. spec_ids: this.data.spec_ids,
  133. rent_types: this.data.rent_types,
  134. rent_type_ids: this.data.rent_type_ids
  135. }
  136. wx.setStorageSync('sg-data-filters', data)
  137. },
  138. /**
  139. * 生命周期函数--监听页面卸载
  140. */
  141. onUnload: function () {
  142. },
  143. /**
  144. * 页面相关事件处理函数--监听用户下拉动作
  145. */
  146. onPullDownRefresh: function () {
  147. },
  148. /**
  149. * 页面上拉触底事件的处理函数
  150. */
  151. onReachBottom: function () {
  152. },
  153. /**
  154. * 用户点击右上角分享
  155. */
  156. onShareAppMessage: function () {
  157. }
  158. })