home.js 6.69 KB
const sliderWidth = 96; // 需要设置slider的宽度,用于计算中间位置
const WXRequest = require('../../utils/wxrequest.js');
const moment = require('./../../utils/moment.min.js'); 
Page({
    data: {
        tabs: ["报表统计", "防拆报警", "围栏报警", "离线设备", "低电量"],
        activeIndex: 0,
        sliderOffset: 0,
        sliderLeft: 0,
        activeWeekTime: moment().format('YYYY-MM-DD 00:00'),
        offlineWeekTime: moment().format('YYYY-MM-DD 00:00'),
        deviceCount: '',
        vehicleCount: '',
        activeToday: '',
        onlineRateToday: '',
        powerOffAlarm: '',
        fenceAlarm: '',
        lightAlarm: '',
        timeoutId: undefined,
        scrollHeight: 0,
        scrollTop: 0,

        preventAlarmList: [],//防拆报警
        preventAlarmListInit: false,
        preventAlarmPage: 1,
        preventAlarmTotalPage: 1,
        fenceAlarmList: [],//围栏报警
        fenceAlarmListInit: false,
        fenceAlarmPage: 1,
        fenceAlarmTotalPage: 1,
        offlineList: [],//离线
        offlineListInit: false,
        offlinePage: 1,
        offlineTotalPage: 1,
        lowBatteryList: [],//低电量
        lowBatteryListInit: false,
        lowBatteryPage: 1,
        lowBatteryTotalPage: 1,
        blankHidden: true
    },
    onLoad: function () {
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
                console.log()
                wx.createSelectorQuery().select('#pageTop').boundingClientRect((rect)=>{
                    that.setData({
                        scrollHeight: res.windowHeight-rect.height-52-15
                    })
                }).exec();
            }
        });
        that.isLoading = true;

        //WXRequest.login().then(()=>{
            that.loadData()
        //});
        //that.loadData()
    },
    onShow(){
        if(this.data.deviceCount === '' && !this.isLoading){
            this.loadData()
        }
    },
    loadData(){
        var that = this;
        const userStatus = wx.getStorageSync('userStatus');
        if(userStatus == 0){
            that.setData({
                blankHidden: true
            });
            WXRequest.get('home/getDeviceCount').then((res)=>{
                if(res.returnCode == '0'){
                    that.setData({
                        ...res.data
                    });
                    that.isLoading = false;
                }
            });
        }else{
            that.setData({
                blankHidden: false
            });
            that.isLoading = false;
        }
        
    },
    tabClick: function (e) {
        const id = e.currentTarget.id;

        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: id
        });
        if(id === '1' && !this.data.preventAlarmListInit){
            this.loadPreventAlarmList();
            this.setData({
                preventAlarmListInit: true
            });
        }else if(id === '2' && !this.data.fenceAlarmListInit){
            this.loadFenceAlarmList();
            this.setData({
                fenceAlarmListInit: true
            });
        }else if(id === '3' && !this.data.offlineListInit){
            this.loadOfflineList();
            this.setData({
                offlineListInit: true
            });
        }else if(id === '4' && !this.data.lowBatteryListInit){
            this.loadLowBatteryList();
            this.setData({
                lowBatteryListInit: true
            });
        }

    },
    loadPreventAlarmList(){
        var that = this;
        if(that.data.preventAlarmPage>that.data.preventAlarmTotalPage) return;

        WXRequest.get('alarm/getTodayAlarmDevice', {
            currentPage: that.data.preventAlarmPage,
            pageSize: 5
        }).then((res)=>{
            that.setData({
                preventAlarmTotalPage: Math.ceil(res.recordsTotal/5),
                preventAlarmList: that.data.preventAlarmList.concat(res.data)
            });
        });
    },
    loadFenceAlarmList(){
        var that = this;
        if(that.data.fenceAlarmPage>that.data.fenceAlarmTotalPage) return;

        WXRequest.get('report/getFenceAlarmList', {
            currentPage: that.data.fenceAlarmPage,
            pageSize: 5
        }).then((res)=>{
            that.setData({
                fenceAlarmTotalPage: Math.ceil(res.recordsTotal/5),
                fenceAlarmList: that.data.fenceAlarmList.concat(res.data)
            });
        });
    },
    loadLowBatteryList(){
        var that = this;
        if(that.data.lowBatteryPage>that.data.lowBatteryTotalPage) return;

        WXRequest.get('report/getLowBatteryList', {
            currentPage: that.data.lowBatteryPage,
            pageSize: 5
        }).then((res)=>{
            that.setData({
                lowBatteryTotalPage: Math.ceil(res.recordsTotal/5),
                lowBatteryList: that.data.lowBatteryList.concat(res.data)
            });
        });
    },
    loadOfflineList(){
        var that = this;
        if(that.data.offlinePage>that.data.offlineTotalPage) return;

        WXRequest.get('report/getDeviceLineList', {
            currentPage: that.data.offlinePage,
            pageSize: 5
        }).then((res)=>{
            that.setData({
                offlineTotalPage: Math.ceil(res.recordsTotal/5),
                offlineList: that.data.offlineList.concat(res.data)
            });
        });
    },
    bindPreventAlarmDownLoad:function(){   
        this.setData({
            preventAlarmPage: this.data.preventAlarmPage+1
        })
        this.loadPreventAlarmList()
    },
    bindfenceAlarmDownLoad:function(){   
        this.setData({
            fenceAlarmPage: this.data.fenceAlarmPage+1
        })
        this.loadFenceAlarmList()
    },
    bindOfflineDownLoad:function(){   
        this.setData({
            offlinePage: this.data.offlinePage+1
        })
        this.loadOfflineList()
    },
    bindLowBatteryDownLoad:function(){   
        this.setData({
            lowBatteryPage: this.data.lowBatteryPage+1
        })
        this.loadLowBatteryList()
    },
    redirectToFindcarTap(e){
        const dataset = e.currentTarget.dataset;
        wx.setStorageSync('carInfo', {
          name: dataset.name,
          standno: dataset.standno,
          sns: dataset.sns,
          vehicleid: dataset.vehicleid
        });
        wx.setStorageSync('initFindCar', true);
        
        wx.switchTab({
          url: '../../pages/findCar/findCar'
        });
    }
});