index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // pages/add-inner-device/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. device_types: [],
  11. type: '',
  12. names: [],
  13. name: '',
  14. specs: [],
  15. spec: '',
  16. number: '',
  17. list: [],
  18. page: 1,
  19. touchBottom: false,
  20. showDate: false,
  21. default_dates: [],
  22. add_devices: [],
  23. showAdded: false
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载d
  27. */
  28. onLoad: function (options) {
  29. var that = this
  30. api.getByName(this, 'devices/getThreeLevel', 'device_types', {
  31. type: 'drop_menu'
  32. }, function () {
  33. that.updateNameSpec()
  34. });
  35. // api.getByName(this, 'inner-devices/get', 'names', {type: 'drop_menu'});
  36. // api.getByName(this, 'specs/get', 'specs', {type: 'drop_menu'});
  37. this.getList()
  38. },
  39. getItemById(items, id) {
  40. for (var i = 0; i < items.length; ++i) {
  41. if (items[i].value == id) return items[i]
  42. }
  43. return null
  44. },
  45. updateNameSpec: function () {
  46. var device_types = this.data.device_types
  47. var typeItem = this.getItemById(device_types, this.data.type)
  48. var names = typeItem.names;
  49. var nameItem = this.getItemById(names, this.data.name)
  50. // var specs = this.getItemById(nameItem.specs, this.data.spec)
  51. this.setData({
  52. names,
  53. specs: nameItem.specs
  54. })
  55. },
  56. switchShowDate: function (e) {
  57. this.setData({
  58. showDate: e.currentTarget.dataset.show,
  59. })
  60. },
  61. cancel: function () {
  62. this.setData({
  63. showAdded: false
  64. })
  65. },
  66. switchShowAdded: function (e) {
  67. this.complete()
  68. this.setData({
  69. showAdded: e.currentTarget.dataset.show
  70. })
  71. },
  72. complete: function (e) {
  73. wx.setStorageSync('sg-added-devices', this.data.add_devices)
  74. wx.navigateBack({
  75. delta: 0,
  76. })
  77. },
  78. confirmDate: function (e) {
  79. var ids = []
  80. var add_devices = this.data.add_devices
  81. for (var i = 0; i < add_devices.length; ++i) {
  82. ids.push(add_devices[i].id)
  83. }
  84. var list = this.data.list
  85. var [start_date, end_date] = e.detail;
  86. start_date = util.formatDate(start_date)
  87. end_date = util.formatDate(end_date)
  88. for (var i = 0; i < list.length; ++i) {
  89. if (ids.indexOf(list[i].id) == -1 && list[i].checked) {
  90. var device = list[i]
  91. device.start_date = start_date,
  92. device.end_date = end_date
  93. add_devices.push(device)
  94. }
  95. }
  96. this.switchShowDate(e)
  97. this.setData({
  98. add_devices: add_devices,
  99. showAdded: true
  100. })
  101. },
  102. switchSelect: function (e) {
  103. var list = this.data.list
  104. var index = e.detail.index
  105. list[index].checked = list[index].checked ? false : true
  106. this.setData({
  107. list
  108. })
  109. },
  110. onChange: function (e) {
  111. var name = e.currentTarget.dataset.name
  112. this.setData({
  113. [name]: e.detail
  114. })
  115. if (['type', 'name'].indexOf(name) != -1) {
  116. if (name == 'type') {
  117. this.setData({
  118. name: '',
  119. spec: ''
  120. })
  121. }
  122. if (name == 'name') {
  123. this.setData({
  124. spec: ''
  125. })
  126. }
  127. this.updateNameSpec()
  128. }
  129. this.search()
  130. },
  131. resetList: function () {
  132. this.setData({
  133. list: [],
  134. page: 1,
  135. touchBottom: false
  136. })
  137. },
  138. search: function () {
  139. this.resetList()
  140. this.getList()
  141. },
  142. getList: function () {
  143. var that = this
  144. http({
  145. url: 'inner-devices/search',
  146. data: {
  147. number: this.data.number,
  148. device_id: this.data.type,
  149. device_name_id: this.data.name,
  150. spec_id: this.data.spec,
  151. page: this.data.page,
  152. status: 'free'
  153. },
  154. success: function (res) {
  155. if (res.code == 0) {
  156. var list = that.data.list
  157. var touchBottom = that.data.touchBottom
  158. list = list.concat(res.data);
  159. if (res.data.length <= 0) {
  160. touchBottom = true;
  161. }
  162. that.setData({
  163. touchBottom,
  164. list
  165. })
  166. }
  167. }
  168. })
  169. },
  170. deleteDevice: function (e) {
  171. var index = e.currentTarget.dataset.index
  172. var add_devices = this.data.add_devices
  173. add_devices.splice(index, 1)
  174. this.setData({
  175. add_devices
  176. })
  177. },
  178. /**
  179. * 生命周期函数--监听页面初次渲染完成
  180. */
  181. onReady: function () {
  182. },
  183. /**
  184. * 生命周期函数--监听页面显示
  185. */
  186. onShow: function () {
  187. var add_devices = wx.getStorageSync('sg-added-devices')
  188. add_devices = add_devices ? add_devices : []
  189. this.setData({
  190. add_devices
  191. })
  192. },
  193. /**
  194. * 生命周期函数--监听页面隐藏
  195. */
  196. onHide: function () {
  197. },
  198. /**
  199. * 生命周期函数--监听页面卸载
  200. */
  201. onUnload: function () {
  202. },
  203. /**
  204. * 页面相关事件处理函数--监听用户下拉动作
  205. */
  206. onPullDownRefresh: function () {
  207. },
  208. /**
  209. * 页面上拉触底事件的处理函数
  210. */
  211. onReachBottom: function () {
  212. if (!this.data.touchBottom) {
  213. var page = this.data.page
  214. page = page + 1;
  215. this.setData({
  216. page
  217. })
  218. this.getList()
  219. }
  220. if (this.data.touchBottom) {
  221. util.error('没有更多数据了')
  222. }
  223. },
  224. /**
  225. * 用户点击右上角分享
  226. */
  227. onShareAppMessage: function () {
  228. }
  229. })