index.js 5.3 KB

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