index.js 4.2 KB

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