scene.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. const { postGroup, getGroup, putGroup, delGroup } = require('../../api/group')
  2. const { postDevice, getDevice, putDevice, delDevice } = require('../../api/device')
  3. const { getBasic, getData, postData } = require('../../api/intell')
  4. // pages/scene/scene.js
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. remove: true, //删除工程
  11. Modify: true, //修改工程名称
  12. array: [], //分组
  13. brray: [],
  14. keyid: [],
  15. inputVal: [],
  16. getName: [], //发送往服务器的工程名称参数
  17. subscript: 0,
  18. Id: [],
  19. key: [],
  20. developline: [],
  21. status: 0,
  22. Status: "",
  23. phone: '',
  24. displaypicture: "https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/ling.png",
  25. display: []
  26. },
  27. //刷新列表
  28. Refreshlist: function() {
  29. wx.showToast({
  30. title: '获取数据中',
  31. icon: 'loading',
  32. duration: 2000
  33. })
  34. var that = this
  35. getGroup().then(res => {
  36. if (res.data.code == 0) {
  37. var data = res.data.data
  38. if (data != null) {
  39. for (let i = 0; i < data.length; i++) {
  40. if (data[i].devices != null) {
  41. for (let j = 0; j < data[i].devices.length; j++) {
  42. if (data[i].devices[j].module == 0) {
  43. let id = data[i].devices[j].deviceId
  44. let key = data[i].devices[j].deviceKey
  45. getBasic(id, key).then(ever => {
  46. if (ever.data.errno == 0) {
  47. if (ever.data.data.online == true) {
  48. data[i].devices[j].status = '在线'
  49. } else {
  50. data[i].devices[j].status = '离线'
  51. }
  52. } else {
  53. wx.showModal({
  54. title: '错误',
  55. content: ever.data.error,
  56. showCancel: false
  57. })
  58. }
  59. })
  60. } else {
  61. let status = data[i].devices[j].status
  62. switch (status) {
  63. case 0:
  64. data[i].devices[j].status = '离线'
  65. break;
  66. case 1:
  67. data[i].devices[j].status = '在线'
  68. break
  69. }
  70. }
  71. }
  72. } else {
  73. wx.setStorageSync('group', data)
  74. let group = wx.getStorageSync('group')
  75. for (let i = 0; i < group.length; i++) {
  76. if (group[i].groupName.length > 10) {
  77. group[i].groupName = group[i].groupName.slice(0, 9) + '....'
  78. }
  79. }
  80. that.setData({ array: group })
  81. }
  82. }
  83. }
  84. setTimeout(function() {
  85. wx.setStorageSync('group', data)
  86. let tgroups = wx.getStorageSync('group')
  87. that.nameRestriction(tgroups)
  88. }, 1500)
  89. } else {
  90. wx.showModal({
  91. title: '错误',
  92. content: res.data.msg,
  93. showCancel: false
  94. })
  95. }
  96. })
  97. },
  98. //添加分组
  99. apption: function() {
  100. var that = this
  101. wx.showModal({
  102. title: '添加工程',
  103. editable: true,
  104. placeholderText: '请输入名称',
  105. success: function(ever) {
  106. if (ever.confirm) {
  107. if (ever.content == '') {
  108. wx.showModal({
  109. title: '提示',
  110. content: '请输入工程名',
  111. showCancel: false
  112. })
  113. } else if (ever.content.length > 15) {
  114. wx.showModal({
  115. title: '提示',
  116. content: '分组名称不能大于15个字符',
  117. showCancel: false
  118. })
  119. } else {
  120. postGroup({ groupName: ever.content }).then((res) => {
  121. if (res.data.code == 0) {
  122. if (wx.getStorageSync('group') == '') {
  123. wx.setStorageSync('group', [])
  124. var a = wx.getStorageSync('group')
  125. a.push({
  126. id: res.data.data.id,
  127. groupName: ever
  128. .content,
  129. devices: []
  130. })
  131. wx.setStorageSync('group', a)
  132. } else {
  133. var b = wx.getStorageSync('group')
  134. b.push({
  135. id: res.data.data.id,
  136. groupName: ever
  137. .content,
  138. devices: []
  139. })
  140. wx.setStorageSync('group', b)
  141. }
  142. var group = wx.getStorageSync('group')
  143. that.nameRestriction(group)
  144. } else {
  145. wx.showModal({
  146. title: '错误',
  147. content: res.data.msg,
  148. showCancel: false
  149. })
  150. }
  151. })
  152. }
  153. }
  154. }
  155. })
  156. },
  157. //删除分组
  158. removeapption: function(e) {
  159. var that = this
  160. wx.showModal({
  161. title: '提示',
  162. content: '确定删除该工程',
  163. success: function(res) {
  164. if (res.confirm) {
  165. var index = e.currentTarget.dataset.indexdel //获取对应下标
  166. var Idindex = that.data.array[index].id
  167. var groupName = that.data.array[index].groupName
  168. delGroup(Idindex).then((res) => {
  169. if (res.data.code == 0) {
  170. if (groupName == wx.getStorageSync('groupName')) {
  171. wx.setStorageSync('groupName', '')
  172. wx.setStorageSync('deviceName', '')
  173. }
  174. var deleteid = wx.getStorageSync('group')
  175. deleteid.splice(index, 1)
  176. wx.setStorageSync('group', deleteid)
  177. var Group = wx.getStorageSync('group')
  178. that.nameRestriction(Group)
  179. } else {
  180. wx.showModal({
  181. title: '提示',
  182. content: res.data.msg,
  183. showCancel: false
  184. })
  185. }
  186. })
  187. }
  188. }
  189. })
  190. },
  191. //修改分组名称
  192. ShowChanges: function(e) {
  193. var that = this
  194. wx.showModal({
  195. title: '修改工程名称',
  196. editable: true,
  197. placeholderText: '请输入名称',
  198. success: res => {
  199. if (res.confirm) {
  200. var index = e.currentTarget.dataset.indexdel //获取对应下标
  201. var Idindex = that.data.array[index].id //获取对应下标的id值
  202. var groupName = that.data.array[index].name
  203. if (res.content == '') {
  204. wx.showModal({
  205. title: '提示',
  206. content: '请输入工程名',
  207. showCancel: false
  208. })
  209. } else if (res.content.length > 15) {
  210. wx.showModal({
  211. title: '提示',
  212. content: '分组名称不能大于15个字符',
  213. showCancel: false
  214. })
  215. } else {
  216. putGroup({ id: Idindex, groupName: res.content }).then(ever => {
  217. if (ever.data.code == 0) {
  218. if (groupName == wx.getStorageSync('groupName')) {
  219. wx.setStorageSync('groupName', res.content)
  220. }
  221. let group = wx.getStorageSync('group')
  222. group[index].groupName = res.content
  223. wx.setStorageSync('group', group)
  224. var Group = wx.getStorageSync('group')
  225. that.nameRestriction(Group)
  226. } else {
  227. wx.showModal({
  228. title: '提示',
  229. content: ever.data.msg,
  230. showCancel: false
  231. })
  232. }
  233. })
  234. }
  235. }
  236. }
  237. })
  238. },
  239. //扫码(添加设备)
  240. scanCodeEvent: function(e) {
  241. var that = this
  242. var index = e.currentTarget.dataset.indexdel //获取对应下标
  243. var Idindex = that.data.array[index].id //分组Id
  244. Idindex = parseInt(Idindex)
  245. var group = wx.getStorageSync('group')
  246. //获取设备
  247. wx.scanCode({
  248. success(res) {
  249. if (res.result.substring(0, 1) == '{') {
  250. let parameter = res.result.match(/\d+(\.\d+)?/g)
  251. let ID = parameter[0]
  252. let SN = parameter[1]
  253. //添加设备
  254. postDevice({
  255. groupId: Idindex,
  256. deviceSn: SN,
  257. deviceId: ID,
  258. module: 1,
  259. deviceName: SN
  260. }).then(add => {
  261. if (add.data.code == 0) {
  262. //查询设备
  263. getDevice(add.data.data.id, Idindex).then(get => {
  264. var Get = get.data.data
  265. switch (Get.status) {
  266. case 0:
  267. Get.status = '离线'
  268. break
  269. case 1:
  270. Get.status = '在线'
  271. break
  272. }
  273. if (group[index].devices != null) {
  274. group[index].devices.push(Get)
  275. } else {
  276. group[index].devices = []
  277. group[index].devices.push(Get)
  278. }
  279. wx.setStorageSync('group', group)
  280. var Group = wx.getStorageSync('group')
  281. that.nameRestriction(Group)
  282. })
  283. } else {
  284. wx.showModal({
  285. title: '提示',
  286. content: add.data.msg,
  287. showCancel: false
  288. })
  289. }
  290. })
  291. }
  292. //dsx添加的设备
  293. else if (res.result.substring(0, 2) == 86) {
  294. let ID = res.result
  295. let SN = res.result
  296. //添加设备
  297. postDevice({
  298. groupId: Idindex,
  299. deviceSn: SN,
  300. deviceId: ID,
  301. module: 1,
  302. deviceName: SN
  303. }).then(add => {
  304. if (add.data.code == 0) {
  305. //查询设备
  306. getDevice(add.data.data.id, Idindex).then(get => {
  307. var Get = get.data.data
  308. switch (Get.status) {
  309. case 0:
  310. Get.status = '离线'
  311. break
  312. case 1:
  313. Get.status = '在线'
  314. break
  315. }
  316. if (group[index].devices != null) {
  317. group[index].devices.push(Get)
  318. } else {
  319. group[index].devices = []
  320. group[index].devices.push(Get)
  321. }
  322. wx.setStorageSync('group', group)
  323. var Group = wx.getStorageSync('group')
  324. that.nameRestriction(Group)
  325. })
  326. } else {
  327. wx.showModal({
  328. title: '提示',
  329. content: add.data.msg,
  330. showCancel: false
  331. })
  332. }
  333. })
  334. } else {
  335. var arr = res.result.split(' ');
  336. var a = arr[0] //设备id
  337. var b = arr[1] //设备key
  338. //获取该设备的在线状态和编码
  339. getBasic(a, b).then(ever => {
  340. if (ever.data.errno == 0) {
  341. var online = ever.data.data.online //设备在线状态
  342. switch (online) {
  343. case true:
  344. that.setData({ status: 1 })
  345. that.setData({ Status: "在线" })
  346. break;
  347. default:
  348. that.setData({ status: 0 })
  349. that.setData({ Status: "离线" })
  350. break;
  351. }
  352. var title = ever.data.data.title //设备编码
  353. getData(a, b).then(gu => {
  354. if (gu.data.errno == 0) {
  355. let trys = gu.data.data
  356. for (let w = 0; w < trys.length; w++) {
  357. if (trys[w].id == "RelayCount") {
  358. var num = trys[w].current_value
  359. num = parseInt(num)
  360. }
  361. }
  362. var numName = ''
  363. for (let p = 0; p < num; p++) {
  364. numName = numName + '回路' + (p + 1) + ','
  365. }
  366. numName = numName.slice(0, numName.length - 1)
  367. //添加设备
  368. postDevice({
  369. groupId: Idindex,
  370. deviceSn: title,
  371. deviceName: title,
  372. deviceId: a,
  373. deviceKey: b,
  374. status: that.data.status,
  375. circuitNum: num,
  376. circuits: numName,
  377. module: 0
  378. })
  379. .then(su => {
  380. if (su.data.code == 0) {
  381. //将设备的信息储存至本地
  382. if (group[index].devices !=
  383. null) {
  384. group[index].devices.push({
  385. id: su.data.data.id,
  386. deviceSn: title,
  387. deviceName: title,
  388. status: that.data.Status,
  389. deviceId: a,
  390. deviceKey: b,
  391. circuitNum: num,
  392. circuits: numName,
  393. module: 0
  394. })
  395. } else {
  396. group[index].devices = []
  397. group[index].devices.push({
  398. id: su.data.data
  399. .id,
  400. deviceSn: title,
  401. deviceName: title,
  402. status: that
  403. .data
  404. .Status,
  405. deviceId: a,
  406. deviceKey: b,
  407. circuitNum: num,
  408. circuits: numName,
  409. module: 0
  410. })
  411. }
  412. wx.setStorageSync('group',
  413. group)
  414. var Group = wx.getStorageSync(
  415. 'group')
  416. that.nameRestriction(Group)
  417. } else {
  418. wx.showModal({
  419. title: '提示',
  420. content: su.data
  421. .msg,
  422. showCancel: false
  423. })
  424. }
  425. })
  426. } else {
  427. wx.showModal({
  428. title: '错误',
  429. content: gu.data.error,
  430. showCancel: false
  431. })
  432. }
  433. })
  434. } else {
  435. wx.showModal({
  436. title: '错误',
  437. content: ever.data.error,
  438. showCancel: false
  439. })
  440. }
  441. })
  442. }
  443. }
  444. })
  445. },
  446. //修改设备名称
  447. modifyequipmentname: function(e) {
  448. var that = this
  449. var key = e.currentTarget.dataset.indexdel
  450. var group = wx.getStorageSync('group')
  451. var deviceId = group[key[0]].devices[key[1]].id
  452. var groupName = group[key[0]].groupName //获取当前的分组名称
  453. var groupId = group[key[0]].id
  454. var deviceName = group[key[0]].devices[key[1]].deviceName //获取当前的设备名称
  455. wx.showModal({
  456. title: '修改设备名称',
  457. editable: true,
  458. placeholderText: '请输入名称',
  459. success: res => {
  460. if (res.confirm) {
  461. if (res.content == '') {
  462. wx.showModal({
  463. title: '提示',
  464. showCancel: false,
  465. content: '请输入名称'
  466. })
  467. } else if (res.content.length > 10) {
  468. wx.showModal({
  469. title: '提示',
  470. showCancel: false,
  471. content: '设备名称不能大于10个字符'
  472. })
  473. } else {
  474. putDevice({ deviceName: res.content, id: deviceId, groupId: groupId })
  475. .then(ever => {
  476. if (ever.data.code == 0) {
  477. if (groupName == wx.getStorageSync('groupName') &&
  478. deviceName == wx.getStorageSync('deviceName')) {
  479. wx.setStorageSync('deviceName', res.content)
  480. }
  481. group[key[0]].devices[key[1]].deviceName = res.content
  482. wx.setStorageSync('group', group)
  483. var Group = wx.getStorageSync('group')
  484. that.nameRestriction(Group)
  485. } else {
  486. wx.showModal({
  487. title: '错误',
  488. content: ever.data.msg,
  489. showCancel: false
  490. })
  491. }
  492. })
  493. }
  494. }
  495. }
  496. })
  497. },
  498. //删除设备
  499. Deletedevice: function(e) {
  500. var that = this
  501. wx.showModal({
  502. title: '删除设备',
  503. content: '确定删除该设备吗?',
  504. success: function(res) {
  505. if (res.confirm) {
  506. let key = e.currentTarget.dataset.indexdel //获得分组和设备的下标值([分组下标,设备下标])
  507. let group = wx.getStorageSync('group')
  508. let subscript = group[key[0]].devices[key[1]].id //设备对应的id
  509. var groupName = group[key[0]].groupName
  510. var deviceName = group[key[0]].devices[key[1]].deviceName
  511. delDevice(subscript).then(nrg => {
  512. if (nrg.data.code == 0) {
  513. group[key[0]].devices.splice(key[1], 1)
  514. wx.setStorageSync('group', group)
  515. var Group = wx.getStorageSync('group')
  516. that.nameRestriction(Group)
  517. if (groupName == wx.getStorageSync('groupName') &&
  518. deviceName == wx.getStorageSync('deviceName')) {
  519. wx.setStorageSync('deviceName', '')
  520. }
  521. } else {
  522. wx.showModal({
  523. title: '提示',
  524. content: nrg.data.msg,
  525. showCancel: false
  526. })
  527. }
  528. })
  529. }
  530. }
  531. })
  532. },
  533. //设备的显示隐藏
  534. Develop: function(e) {
  535. var that = this
  536. let array = that.data.array
  537. var pd = e.currentTarget.dataset.indexdel
  538. for (let item = 0; item < array.length; item++) {
  539. if (array[item].id == array[pd].id) {
  540. //判断当前对象中的insert是否为true(true为显示,其他为隐藏) insert是新增的一个值然后进行判断
  541. if (array[item].insert == "" || array[item].insert == undefined) {
  542. array[item].insert = "true"
  543. } else {
  544. array[item].insert = ""
  545. }
  546. }
  547. }
  548. that.setData({
  549. array: array
  550. })
  551. },
  552. //分组和设备名称的显示限制
  553. nameRestriction: function(Group) {
  554. var that = this
  555. for (let i = 0; i < Group.length; i++) {
  556. if (Group[i].groupName.length > 10) {
  557. Group[i].groupName = Group[i].groupName.slice(0, 9) + '....'
  558. }
  559. if (Group[i].devices != null) {
  560. for (let j = 0; j < Group[i].devices.length; j++) {
  561. if (Group[i].devices[j].deviceName.length > 10) {
  562. Group[i].devices[j].deviceName = Group[i].devices[j].deviceName.substr(-4)
  563. }
  564. if (Group[i].devices[j].deviceSn.length > 10) {
  565. Group[i].devices[j].deviceSn = Group[i].devices[j].deviceSn.substr(-4)
  566. }
  567. }
  568. }
  569. }
  570. that.setData({ array: Group })
  571. },
  572. /**
  573. * 生命周期函数--监听页面加载
  574. */
  575. onLoad(options) {
  576. this.Refreshlist()
  577. },
  578. /**
  579. * 生命周期函数--监听页面初次渲染完成
  580. */
  581. onReady() {},
  582. /**
  583. * 生命周期函数--监听页面显示
  584. */
  585. onShow() {
  586. var that = this
  587. setTimeout(function() {
  588. if (that.data.array.length == 0) {
  589. that.Refreshlist()
  590. }
  591. }, 1000)
  592. },
  593. /**
  594. * 生命周期函数--监听页面隐藏
  595. */
  596. onHide() {
  597. },
  598. /**
  599. * 生命周期函数--监听页面卸载
  600. */
  601. onUnload() {
  602. },
  603. /**
  604. * 页面相关事件处理函数--监听用户下拉动作
  605. */
  606. onPullDownRefresh() {
  607. },
  608. /**
  609. * 页面上拉触底事件的处理函数
  610. */
  611. onReachBottom() {
  612. },
  613. /**
  614. * 用户点击右上角分享
  615. */
  616. onShareAppMessage() {
  617. }
  618. })