scene.js 22 KB

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