index.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. switchShowAdded: function (e) {
  62. this.complete()
  63. this.setData({
  64. showAdded: e.currentTarget.dataset.show
  65. })
  66. },
  67. complete: function (e) {
  68. wx.setStorageSync('sg-added-devices', this.data.add_devices)
  69. wx.navigateBack({
  70. delta: 0,
  71. })
  72. },
  73. confirmDate: function (e) {
  74. var ids = []
  75. var add_devices = this.data.add_devices
  76. for (var i = 0; i < add_devices.length; ++i) {
  77. ids.push(add_devices[i].id)
  78. }
  79. var list = this.data.list
  80. var [start_date, end_date] = e.detail;
  81. start_date = util.formatDate(start_date)
  82. end_date = util.formatDate(end_date)
  83. for (var i = 0; i < list.length; ++i) {
  84. if (ids.indexOf(list[i].id) == -1 && list[i].checked) {
  85. var device = list[i]
  86. device.start_date = start_date,
  87. device.end_date = end_date
  88. add_devices.push(device)
  89. }
  90. }
  91. this.switchShowDate(e)
  92. this.setData({
  93. add_devices: add_devices,
  94. showAdded: true
  95. })
  96. },
  97. switchSelect: function (e) {
  98. var list = this.data.list
  99. var index = e.detail.index
  100. list[index].checked = list[index].checked ? false : true
  101. this.setData({
  102. list
  103. })
  104. },
  105. onChange: function (e) {
  106. var name = e.currentTarget.dataset.name
  107. this.setData({
  108. [name]: e.detail
  109. })
  110. if (['type', 'name'].indexOf(name) != -1) {
  111. if (name == 'type') {
  112. this.setData({
  113. name: '',
  114. spec: ''
  115. })
  116. }
  117. if (name == 'name') {
  118. this.setData({
  119. spec: ''
  120. })
  121. }
  122. this.updateNameSpec()
  123. }
  124. this.search()
  125. },
  126. resetList: function () {
  127. this.setData({
  128. list: [],
  129. page: 1,
  130. touchBottom: false
  131. })
  132. },
  133. search: function () {
  134. this.resetList()
  135. this.getList()
  136. },
  137. getList: function () {
  138. var that = this
  139. http({
  140. url: 'inner-devices/search',
  141. data: {
  142. number: this.data.number,
  143. device_id: this.data.type,
  144. device_name_id: this.data.name,
  145. spec_id: this.data.spec,
  146. page: this.data.page,
  147. free: true
  148. },
  149. success: function (res) {
  150. if (res.code == 0) {
  151. var list = that.data.list
  152. var touchBottom = that.data.touchBottom
  153. list = list.concat(res.data);
  154. if (res.data.length <= 0) {
  155. touchBottom = true;
  156. }
  157. that.setData({
  158. touchBottom,
  159. list
  160. })
  161. }
  162. }
  163. })
  164. },
  165. deleteDevice: function (e) {
  166. var index = e.currentTarget.dataset.index
  167. var add_devices = this.data.add_devices
  168. add_devices.splice(index, 1)
  169. this.setData({
  170. add_devices
  171. })
  172. },
  173. /**
  174. * 生命周期函数--监听页面初次渲染完成
  175. */
  176. onReady: function () {
  177. },
  178. /**
  179. * 生命周期函数--监听页面显示
  180. */
  181. onShow: function () {
  182. var add_devices = wx.getStorageSync('sg-added-devices')
  183. add_devices = add_devices ? add_devices : []
  184. this.setData({
  185. add_devices
  186. })
  187. },
  188. /**
  189. * 生命周期函数--监听页面隐藏
  190. */
  191. onHide: function () {
  192. },
  193. /**
  194. * 生命周期函数--监听页面卸载
  195. */
  196. onUnload: function () {
  197. },
  198. /**
  199. * 页面相关事件处理函数--监听用户下拉动作
  200. */
  201. onPullDownRefresh: function () {
  202. },
  203. /**
  204. * 页面上拉触底事件的处理函数
  205. */
  206. onReachBottom: function () {
  207. if (!this.data.touchBottom) {
  208. var page = this.data.page
  209. page = page + 1;
  210. this.setData({
  211. page
  212. })
  213. this.getList()
  214. }
  215. if (this.data.touchBottom) {
  216. util.error('没有更多数据了')
  217. }
  218. },
  219. /**
  220. * 用户点击右上角分享
  221. */
  222. onShareAppMessage: function () {
  223. }
  224. })