scene.js 19 KB

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