index.js 5.7 KB

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