index.js 4.2 KB

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