index.js 5.2 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', {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.setData({
  61. showAdded: e.currentTarget.dataset.show
  62. })
  63. },
  64. complete: function(e) {
  65. wx.setStorageSync('sg-added-devices', this.data.add_devices)
  66. wx.navigateBack({
  67. delta: 0,
  68. })
  69. },
  70. confirmDate: function(e) {
  71. var ids = []
  72. var add_devices = this.data.add_devices
  73. for(var i = 0; i < add_devices.length; ++i) {
  74. ids.push(add_devices[i].id)
  75. }
  76. var list = this.data.list
  77. var [start_date, end_date] = e.detail;
  78. start_date = util.formatDate(start_date)
  79. end_date = util.formatDate(end_date)
  80. for(var i = 0; i < list.length; ++i) {
  81. if(ids.indexOf(list[i].id) == -1 && list[i].checked) {
  82. var device = list[i]
  83. device.start_date = start_date,
  84. device.end_date = end_date
  85. add_devices.push(device)
  86. }
  87. }
  88. this.setData({
  89. add_devices: add_devices
  90. })
  91. this.switchShowDate(e)
  92. },
  93. switchSelect: function(e) {
  94. var list = this.data.list
  95. var index = e.currentTarget.dataset.index
  96. var can = e.currentTarget.dataset.can
  97. if(can) {
  98. list[index].checked = list[index].checked ? false : true
  99. this.setData({
  100. list
  101. })
  102. } else {
  103. util.error('该设备暂不能借用')
  104. }
  105. },
  106. onChange: function(e) {
  107. var name = e.currentTarget.dataset.name
  108. this.setData({
  109. [name]: e.detail
  110. })
  111. if(['type', 'name'].indexOf(name) != -1) {
  112. if(name == 'type') {
  113. this.setData({
  114. name: '',
  115. spec: ''
  116. })
  117. }
  118. if(name == 'name') {
  119. this.setData({
  120. spec: ''
  121. })
  122. }
  123. this.updateNameSpec()
  124. }
  125. this.search()
  126. },
  127. resetList: function() {
  128. this.setData({
  129. list: [],
  130. page: 1,
  131. touchBottom: false
  132. })
  133. },
  134. search: function() {
  135. this.resetList()
  136. this.getList()
  137. },
  138. getList: function() {
  139. var that = this
  140. http({
  141. url: 'inner-devices/search',
  142. data: {
  143. number: this.data.number,
  144. device_id: this.data.type,
  145. device_name_id: this.data.name,
  146. spec_id: this.data.spec,
  147. page: this.data.page
  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. })