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. console.log(items)
  83. this.setData({
  84. [name]: items
  85. })
  86. if(name == 'device_ids') {
  87. this.updateDeviceNames()
  88. }
  89. if(name == 'device_name_ids') {
  90. this.updateSpecs()
  91. }
  92. },
  93. updateDeviceNames() {
  94. var that = this;
  95. that.setData({
  96. device_name_ids: ['']
  97. })
  98. api.getByName(this, 'device-names/get', 'names', {type: 'drop_menu', device_ids: this.data.device_ids}, function() {
  99. that.updateSpecs();
  100. });
  101. },
  102. updateSpecs() {
  103. var that = this;
  104. that.setData({
  105. spec_ids: ['']
  106. })
  107. api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu', device_name_ids: this.data.device_name_ids});
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: function () {
  113. var data = wx.getStorageSync('sg-data-filters')
  114. this.setData(data)
  115. api.getByName(this, 'devices/get', 'devices', {type: 'drop_menu'});
  116. api.getByName(this, 'device-names/get', 'names', {type: 'drop_menu', device_ids: this.data.device_ids});
  117. api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu', device_name_ids: this.data.device_name_ids});
  118. api.getByName(this, 'rent-types/get', 'rent_types', {type: 'drop_menu'});
  119. },
  120. /**
  121. * 生命周期函数--监听页面隐藏
  122. */
  123. onHide: function () {
  124. this.saveFilter()
  125. },
  126. saveFilter() {
  127. var data = {
  128. devices: this.data.devices,
  129. device_ids: this.data.device_ids,
  130. names: this.data.names,
  131. device_name_ids: this.data.device_name_ids,
  132. specs: this.data.specs,
  133. spec_ids: this.data.spec_ids,
  134. rent_types: this.data.rent_types,
  135. rent_type_ids: this.data.rent_type_ids
  136. }
  137. wx.setStorageSync('sg-data-filters', data)
  138. },
  139. /**
  140. * 生命周期函数--监听页面卸载
  141. */
  142. onUnload: function () {
  143. },
  144. /**
  145. * 页面相关事件处理函数--监听用户下拉动作
  146. */
  147. onPullDownRefresh: function () {
  148. },
  149. /**
  150. * 页面上拉触底事件的处理函数
  151. */
  152. onReachBottom: function () {
  153. },
  154. /**
  155. * 用户点击右上角分享
  156. */
  157. onShareAppMessage: function () {
  158. }
  159. })