index.js 8.02 KB
// pages/index/account/index.js
const App = getApp();
const customBehavior = require('../../../extends/custom.behavior.js');
import _ from 'underscore';
import cardConfig from '../../../utils/card'

Component({
    behaviors: [customBehavior],
    /**
     * 组件的属性列表
     */
    properties: {
        timeType: String,
        time: Object,
        isMall:Boolean,
        mallId: String,
        orgName:String
    },

    /**
     * 组件的初始数据
     */
    data: {
        picUrl:App.globalData.picUrl,
        windowWidth: App.globalData.windowWidth,
        navHeight: getApp().globalData.navHeight,
        deploymentType: wx.getStorageSync('deploymentType'),
        trendVal: '',
        trendName: '',
        rankVal: 'total_passenger',
        rankData: {},
        belongTo: 'NewMall',
        params: {
            orgType: 'mall',
            dateType: 'day',
            startDate: '',
            endDate: '',
            chartIds: ''
        },
        dateStr: '昨日',
        trendData: {},
        hbarData: {},
        cardData: {},
        isOnce:true,
        cardsList:[],
        showTab:true
        /********************************************/

    },

    lifetimes: {
        attached: function () {
            this.data.isOnce = false
            this.getCharts().then(res => {
                this.data.cardsList = res.data.cards
                this.data.isOnce = true
                this.changeTimerFn({
                    ...this.data.time,
                    dateType: this.data.timeType
                });
            })
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        mallBtn(){
            if(!this.data.isMall){
                this.triggerEvent('showMall');
            }
        },
        // 获取报表chartIds
        getReportCharts: function (res) {
            if (this.data.chartList.length == 0) return;
            wx.showLoading({
                title: '数据加载中',
                mask: true
            });
            this.loadHeadData();
           
            this.loadBarData();
            this.loaRankData();
        },
        loadHeadData() {
            let orgIds = this.data.mallId;
            let chartIds = this.getCardIds();
            this.getHeadReport({
                chartIds,
                orgIds
            }).then(res => {
                let cardData = [];
                Object.keys(res.data).forEach(key => {
                    let item = res.data[key];
                    if (this.data.params.dateType == 'day') {
                        item.value = item.today;
                        item.ratio = item.yesterdayRatio;
                    } else if (this.data.params.dateType == 'month') {
                        item.value = item.month;
                        item.ratio = item.lastMonthDayRatio;
                    } else if (this.data.params.dateType == 'year') {
                        item.value = item.year;
                        item.ratio = item.lastYearRatio;
                    }
                    item.value = String(item.value && this.toThousands(item.value)).replace('分','');
                    item.rstatus = (item.ratio && item.ratio.includes('-')) ? 1 : 2;
                    item.name = this.getChartName(key);
                    item.key = key;
                    item.dateType = this.data.params.dateType
                    cardData.push(item)
                });
                let newData = []
                this.data.cardsList.forEach(item=>{
                    cardData.forEach(card=>{
                        if(item.chartKey==card.key){
                            newData.push(card)
                        }
                    })
                })
                let name = '',title = ''
                this.data.trendVal = ''
                Object.keys(cardConfig).forEach(key=>{
                    newData.forEach(card=>{
                        if(key==card.key){
                            card.unit = cardConfig[key].unit;
                            card.hasTrend = cardConfig[key][card.dateType].level && cardConfig[key][card.dateType].level.length>0
                            if(!name && card.hasTrend){
                                name = key
                                title = card.name
                            }
                        }
                    })
                })
                this.data.showTab = true
                this.setData({
                    trendVal: name,
                    trendName: title
                }, () => {
                    this.loaTrendData();
                })
                this.setData({
                    cardData:newData
                })
            })
        },
        loaRankData() {
            let orgIds = this.data.mallId;
            let chartIds = this.getChartIds('mall_ranking');
            this.getBodyReport({
                chartIds,
                sortType: 'desc',
                sortKey:this.data.rankVal,
                orgIds
            }).then(res => {
                let rankData = res.data.mall_ranking;
                rankData.key = this.data.rankVal;
                this.setData({
                    rankData
                }, () => {
                    wx.hideLoading();
                })
            });
        },
        changeTrend(e) {
            let {
                name,title
            } = e.detail;
            this.setData({
                trendVal: name,
                trendName: title
            }, () => {
                this.loaTrendData();
            })
        },
        changeRank(e) {
            let {
                name,title
            } = e.detail;
            this.setData({
                rankVal: name
            }, () => {
                this.loaRankData();
            })
        },
        loaTrendData() {
            let orgIds = this.data.mallId;
            let chartIds = this.getChartIds(this.data.trendVal);
            this.getBodyReport({
                chartIds,
                orgIds,
                timeLevel: this.data.params.dateType == 'day' ? 'HOUR' : 'DAY'
            }).then(res => {
                let trendData = res.data[this.data.trendVal];
                if (trendData.series && trendData.series[0]) {
                    trendData.series[0].name = this.data.trendName;
                }
                this.setData({
                    trendData: trendData
                }, () => {
                    wx.hideLoading();
                })
            });
        },
        loadBarData() {
            let orgIds = this.data.mallId;
            let chartKey = 'stay_time_statistics';
            let chartIds = this.getChartIds(chartKey);
            this.getBodyReport({
                chartIds,
                orgIds,
            }).then(res => {
                let hbarData = res.data[chartKey];
                this.setData({
                    hbarData
                }, () => {
                    wx.hideLoading();
                })
            });
        },
        changeAccountFn: function (value) {
            // this.getReportCharts();
            if(!this.data.isOnce) return
            this.getCharts({mallId:value}).then(res=>{
                this.data.cardsList = res.data.cards
                this.getReportCharts();
            })
        },
        changeTimerFn: async function (value) {
            let {
                startDate,
                dateType,
                endDate
            } = value;
            let reportType, dateStr;
            if (dateType == 0) {
                reportType = 'day';
                dateStr = '昨日';
            } else if (dateType === 1) {
                reportType = 'month';
                dateStr = '上月';
            } else {
                reportType = 'year';
                dateStr = '去年';
            }
            this.setData({
                dateStr,
                'params.dateType': reportType,
                'params.startDate': startDate,
                'params.endDate': endDate
            }, () => {
                this.getReportCharts();
            })
        },
    }
})