123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- const {postGroup,getGroup,putGroup,delGroup} = require('../../api/group')
- const {postDevice,getDevice,putDevice,delDevice} = require('../../api/device')
- const {getBasic,getData,postData} = require('../../api/intell')
- // pages/scene/scene.js
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- remove:true,//删除工程
- Modify:true,//修改工程名称
- array:[],//分组
- brray:[],
- keyid:[],
- inputVal:[],
- getName:[],//发送往服务器的工程名称参数
- subscript:0,
- Id:[],
- key:[],
- developline:[],
- status:0,
- Status:"",
- phone:'',
- displaypicture:"https://cloud.long-chi.com/resource/miniprogram-lightcontrol/image/ling.png",
- display:[]
- },
- //刷新列表
- Refreshlist:function(){
- wx.showToast({
- title:'刷新中',
- icon:'loading',
- duration: 2000
- })
- var that = this
- getGroup().then(res => {
- if(res.data.code == 0){
- var data = res.data.data
- if(data != null){
- for(let i = 0;i <data.length;i++){
- if(data[i].devices != null){
- for(let j = 0;j < data[i].devices.length;j++){
- if(data[i].devices[j].module == 0){
- let id = data[i].devices[j].deviceId
- let key = data[i].devices[j].deviceKey
- getBasic(id,key).then(ever =>{
- if(ever.data.errno == 0){
- if(ever.data.data.online == true){
- data[i].devices[j].status = '在线'
- }
- else{
- data[i].devices[j].status = '离线'
- }
- wx.setStorageSync('group',data)
- let tgroups = wx.getStorageSync('group')
- for(let i = 0;i < tgroups.length;i++){
- if(tgroups[i].groupName.length > 10){
- tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:tgroups})
- }
- else{
- wx.showModal({
- title:'错误',
- content:ever.data.error,
- showCancel:false
- })
- }
- })
- }
- else{
- let status = data[i].devices[j].status
- switch(status){
- case 0:
- data[i].devices[j].status = '离线'
- break;
- case 1:
- data[i].devices[j].status = '在线'
- break
- }
- wx.setStorageSync('group',data)
- let tgroups = wx.getStorageSync('group')
- for(let i = 0;i < tgroups.length;i++){
- if(tgroups[i].groupName.length > 10){
- tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:tgroups})
- }
- }
- }
- else{
- wx.setStorageSync('group',data)
- let group = wx.getStorageSync('group')
- for(let i = 0;i < group.length;i++){
- if(group[i].groupName.length > 10){
- group[i].groupName = group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:group})
- }
- }
- }
- }
- else{
- wx.showModal({
- title:'错误',
- content:res.data.msg,
- showCancel:false
- })
- }
- })
- },
- //添加分组
- apption:function(){
- var that = this
- wx.showModal({
- title:'添加工程',
- editable:true,
- placeholderText:'请输入名称',
- success:function(ever){
- if(ever.confirm){
- if (ever.content == '') {
- wx.showModal({
- title:'提示',
- content:'请输入工程名',
- showCancel:false
- })
- }
- else if(ever.content.length > 15){
- wx.showModal({
- title:'提示',
- content:'分组名称不能大于15个字符',
- showCancel:false
- })
- }
- else{
- postGroup({groupName:ever.content}).then((res)=>{
- if(res.data.code == 0){
- if(wx.getStorageSync('group') == ''){
- wx.setStorageSync('group', [])
- var a = wx.getStorageSync('group')
- a.push({id:res.data.data.id,groupName:ever.content,devices:[]})
- wx.setStorageSync('group', a)
- }
- else{
- var b = wx.getStorageSync('group')
- b.push({id:res.data.data.id,groupName:ever.content,devices:[]})
- wx.setStorageSync('group', b)
- }
- var group = wx.getStorageSync('group')
- for(let i = 0;i < group.length;i++){
- if(group[i].groupName.length > 10){
- group[i].groupName = group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:group})
- }
- else{
- wx.showModal({
- title:'错误',
- content:res.data.msg,
- showCancel:false
- })
- }
- })
- }
- }
- }
- })
- },
- //删除分组
- removeapption:function(e){
- var that = this
- wx.showModal({
- title:'提示',
- content:'确定删除该工程',
- success:function(res){
- if (res.confirm) {
- var index = e.currentTarget.dataset.indexdel//获取对应下标
- var Idindex = that.data.array[index].id
- var groupName = that.data.array[index].groupName
- delGroup(Idindex).then((res)=>{
- if(res.data.code == 0){
- if(groupName == wx.getStorageSync('groupName')){
- wx.setStorageSync('groupName', '')
- wx.setStorageSync('deviceName', '')
- }
- var deleteid = wx.getStorageSync('group')
- deleteid.splice(index,1)
- wx.setStorageSync('group', deleteid)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array: Group})
- }
- else{
- wx.showModal({
- title:'提示',
- content:res.data.msg,
- showCancel:false
- })
- }
- })
- }
- }
- })
- },
- //修改分组名称
- ShowChanges:function(e){
- var that = this
- wx.showModal({
- title:'修改工程名称',
- editable:true,
- placeholderText:'请输入名称',
- success:res =>{
- if (res.confirm) {
- var index = e.currentTarget.dataset.indexdel//获取对应下标
- var Idindex = that.data.array[index].id//获取对应下标的id值
- var groupName = that.data.array[index].name
- if (res.content == '') {
- wx.showModal({
- title:'提示',
- content:'请输入工程名',
- showCancel:false
- })
- }
- else if(res.content.length > 15){
- wx.showModal({
- title:'提示',
- content:'分组名称不能大于15个字符',
- showCancel:false
- })
- }
- else{
- putGroup({id:Idindex,groupName:res.content}).then(ever=>{
- if (ever.data.code == 0) {
- if(groupName == wx.getStorageSync('groupName')){
- wx.setStorageSync('groupName',res.content)
- }
- let group = wx.getStorageSync('group')
- group[index].groupName = res.content
- wx.setStorageSync('group',group)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:Group})
- }
- else{
- wx.showModal({
- title:'提示',
- content:ever.data.msg,
- showCancel:false
- })
- }
- })
- }
- }
- }
- })
- },
- //扫码(添加设备)
- scanCodeEvent: function(e){
- var that = this
- var index = e.currentTarget.dataset.indexdel//获取对应下标
- var Idindex = that.data.array[index].id//分组Id
- Idindex = parseInt(Idindex)
- var group = wx.getStorageSync('group')
- //获取设备
- wx.scanCode({
- success(res){
- if(res.result.substring(0,1) == '{'){
- let parameter = res.result.match(/\d+(\.\d+)?/g)
- let ID = parameter[0]
- let SN = parameter[1]
- //添加设备
- postDevice({groupId:Idindex,deviceSn:SN,deviceId:ID,module:1,deviceName:SN}).then(add =>{
- if(add.data.code == 0){
- //查询设备
- getDevice(add.data.data.id,Idindex).then(get =>{
- var Get = get.data.data
- switch(Get[0].status){
- case 0:
- Get[0].status = '离线'
- break
- case 1:
- Get[0].status = '在线'
- break
- }
- if(group[index].devices != null){
- group[index].devices.push(Get[0])
- }
- else{
- group[index].devices = []
- group[index].devices.push(Get[0])
- }
- wx.setStorageSync('group', group)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array: Group})
- })
- }
- else{
- wx.showModal({
- title:'提示',
- content:add.data.msg,
- showCancel:false
- })
- }
- })
- }
- else{
- var arr = res.result.split(' ');
- var a = arr[0]//设备id
- var b = arr[1]//设备key
- //获取该设备的在线状态和编码
- getBasic(a,b).then(ever=>{
- var online = ever.data.data.online//设备在线状态
- switch (online) {
- case true:
- that.setData({status:1})
- that.setData({Status:"在线"})
- break;
- default:
- that.setData({status:0})
- that.setData({Status:"离线"})
- break;
- }
- var title = ever.data.data.title//设备编码
- getData(a,b).then(gu =>{
- let trys = gu.data.data
- for(let w = 0;w < trys.length;w++){
- if(trys[w].id == "RelayCount"){
- var num = trys[w].current_value
- num = parseInt(num)
- }
- }
- var numName = ''
- for(let p = 0;p < num;p++){
- numName = numName + '回路' + (p + 1) + ','
- }
- numName = numName.slice(0,numName.length - 1)
- //添加设备
- postDevice({groupId:Idindex,deviceSn:title,deviceName:title,deviceId:a,deviceKey:b,status:that.data.status,circuitNum:num,circuits:numName,module:0})
- .then(su => {
- if (su.data.code == 0){
- //将设备的信息储存至本地
- if(group[index].devices != null){
- group[index].devices.push({
- id:su.data.data.id,
- deviceSn:title,
- deviceName:title,
- status:that.data.Status,
- deviceId:a,
- deviceKey:b,
- circuitNum:num,
- circuits:numName,
- module:0
- })
- }
- else{
- group[index].devices = []
- group[index].devices.push({
- id:su.data.data.id,
- deviceSn:title,
- deviceName:title,
- status:that.data.Status,
- deviceId:a,
- deviceKey:b,
- circuitNum:num,
- circuits:numName,
- module:0
- })
- }
- wx.setStorageSync('group', group)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array: Group})
- }
- else{
- wx.showModal({
- title:'提示',
- content:su.data.msg,
- showCancel:false
- })
- }
- })
- })
- })
- }
- }
- })
- },
- //修改设备名称
- modifyequipmentname:function(e){
- var that = this
- var key = e.currentTarget.dataset.indexdel
- var group = wx.getStorageSync('group')
- var deviceId = group[key[0]].devices[key[1]].id
- var groupName = group[key[0]].groupName//获取当前的分组名称
- var groupId = group[key[0]].id
- var deviceName = group[key[0]].devices[key[1]].deviceName//获取当前的设备名称
- wx.showModal({
- title:'修改设备名称',
- editable:true,
- placeholderText:'请输入名称',
- success:res =>{
- if (res.confirm) {
- if (res.content == '') {
- wx.showModal({
- title:'提示',
- showCancel:false,
- content:'请输入名称'
- })
- }
- else if(res.content.length > 10){
- wx.showModal({
- title:'提示',
- showCancel:false,
- content:'设备名称不能大于10个字符'
- })
- }
- else{
- putDevice({deviceName:res.content,id:deviceId,groupId:groupId}).then(ever=>{
- if(ever.data.code == 0){
- if(groupName == wx.getStorageSync('groupName') && deviceName == wx.getStorageSync('deviceName')){
- wx.setStorageSync('deviceName',res.content)
- }
- group[key[0]].devices[key[1]].deviceName = res.content
- wx.setStorageSync('group', group)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array: Group})
- }
- else{
- wx.showModal({
- title:'错误',
- content:ever.data.msg,
- showCancel:false
- })
- }
- })
- }
- }
- }
- })
- },
- //删除设备
- Deletedevice:function(e){
- var that = this
- wx.showModal({
- title:'删除设备',
- content:'确定删除该设备吗?',
- success:function(res){
- if(res.confirm){
- let key = e.currentTarget.dataset.indexdel//获得分组和设备的下标值([分组下标,设备下标])
- let group = wx.getStorageSync('group')
- let subscript = group[key[0]].devices[key[1]].id//设备对应的id
- var groupName = group[key[0]].groupName
- var deviceName = group[key[0]].devices[key[1]].deviceName
- delDevice(subscript).then(nrg=>{
- if (nrg.data.code == 0) {
- group[key[0]].devices.splice(key[1],1)
- wx.setStorageSync('group', group)
- var Group = wx.getStorageSync('group')
- for(let i = 0;i < Group.length;i++){
- if(Group[i].groupName.length > 10){
- Group[i].groupName = Group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array: Group})
- if(groupName == wx.getStorageSync('groupName') && deviceName == wx.getStorageSync('deviceName')){
- wx.setStorageSync('deviceName','')
- }
- }
- else{
- wx.showModal({
- title:'提示',
- content:nrg.data.msg,
- showCancel:false
- })
- }
- })
- }
- }
- })
- },
- //设备的显示隐藏
- Develop:function(e){
- var that = this
- let array = that.data.array
- var pd = e.currentTarget.dataset.indexdel
- for(let item =0;item < array.length;item++){
- if (array[item].id == array[pd].id) {
- //判断当前对象中的insert是否为true(true为显示,其他为隐藏) insert是新增的一个值然后进行判断
- if (array[item].insert == "" || array[item].insert == undefined) {
- array[item].insert = "true"
- } else {
- array[item].insert = ""
- }
- }
- }
- that.setData({
- array:array
- })
- },
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad(options) {
- wx.showToast({
- title:'获取数据中',
- icon:'loading',
- duration: 2000
- })
- var that = this
- getGroup().then(res => {
- if(res.data.code == 0){
- var data = res.data.data
- if(data != null){
- for(let i = 0;i <data.length;i++){
- if(data[i].devices != null){
- for(let j = 0;j < data[i].devices.length;j++){
- if(data[i].devices[j].module == 0){
- let id = data[i].devices[j].deviceId
- let key = data[i].devices[j].deviceKey
- getBasic(id,key).then(ever =>{
- if(ever.data.errno == 0){
- if(ever.data.data.online == true){
- data[i].devices[j].status = '在线'
- }
- else{
- data[i].devices[j].status = '离线'
- }
- wx.setStorageSync('group',data)
- let tgroups = wx.getStorageSync('group')
- for(let i = 0;i < tgroups.length;i++){
- if(tgroups[i].groupName.length > 10){
- tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:tgroups})
- }
- else{
- wx.showModal({
- title:'错误',
- content:ever.data.error,
- showCancel:false
- })
- }
- })
- }
- else{
- let status = data[i].devices[j].status
- switch(status){
- case 0:
- data[i].devices[j].status = '离线'
- break;
- case 1:
- data[i].devices[j].status = '在线'
- break
- }
- wx.setStorageSync('group',data)
- let tgroups = wx.getStorageSync('group')
- for(let i = 0;i < tgroups.length;i++){
- if(tgroups[i].groupName.length > 10){
- tgroups[i].groupName = tgroups[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:tgroups})
- }
- }
- }
- else{
- wx.setStorageSync('group',data)
- let group = wx.getStorageSync('group')
- for(let i = 0;i < group.length;i++){
- if(group[i].groupName.length > 10){
- group[i].groupName = group[i].groupName.slice(0,9) + '....'
- }
- }
- that.setData({array:group})
- }
- }
- }
- }
- else{
- wx.showModal({
- title:'错误',
- content:res.data.msg,
- showCancel:false
- })
- }
- })
- },
- /**
- * 生命周期函数--监听页面初次渲染完成
- */
- onReady() {
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- },
- /**
- * 生命周期函数--监听页面隐藏
- */
- onHide() {
- },
- /**
- * 生命周期函数--监听页面卸载
- */
- onUnload() {
- },
- /**
- * 页面相关事件处理函数--监听用户下拉动作
- */
- onPullDownRefresh() {
- },
- /**
- * 页面上拉触底事件的处理函数
- */
- onReachBottom() {
- },
- /**
- * 用户点击右上角分享
- */
- onShareAppMessage() {
- }
- })
|