index.js 4.0 KB

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