index.js 6.38 KB
// pages/index/account/index.js
const App = getApp();
const customBehavior = require('../../../extends/custom.behavior.js');
import _ from 'underscore';
import * as echarts from '../../../ec-canvas/echarts';
import {
    echartInfo
} from '../../../utils/common';
import chartData from '../../../data';
import {
    getReportCharts,
    getIndexInfo
} from "../../../api/index.js";
import {
    getCharts
} from "../../../api/report.js";


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

    /**
     * 组件的初始数据
     */
    data: {
        picUrl:App.globalData.picUrl,
        accountName:'',
        windowWidth: App.globalData.windowWidth,
        navHeight: getApp().globalData.navHeight,
        deploymentType:wx.getStorageSync('deploymentType'),
        trendVal: 'account_passenger_flow',
        trendName:'客流量',
        rankVal: 'total_passenger',
        rankData: {},
        belongTo: 'NewAccount',
        params: {
            orgType: 'account',
            dateType: 'day',
            mallIds: '',
            startDate: '',
            endDate: '',
            chartIds: ''
        },
        dateStr: '昨日',
        trendData: {},
        cardData: {}
        /********************************************/

    },

    lifetimes: {
        attached: function () {
            this.getCharts({
                accountId: this.data.accountId,
            }).then(res => {
                this.changeTimerFn({
                    ...this.data.time,
                    dateType: this.data.timeType
                });
            })
            this.setData({
                accountName:wx.getStorageSync('accountName')
            })
        }
    },

    /**
     * 组件的方法列表
     */
    methods: {
        mallBtn(){
           this.triggerEvent('showMall')
        },
        // 获取报表chartIds
        getReportCharts: function () {
            if(this.data.chartList.length==0)return;
            wx.showLoading({
                title: '数据加载中',
                mask: true
            });
            this.loadHeadData();
            this.loaRankData();
            this.loaTrendData();
        },
        loadHeadData() {
            let orgIds = this.data.accountId;
            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);
                    cardData[key] = item;
                });
                this.setData({
                    cardData
                })
            })
        },
        loaRankData() {
            let orgIds = this.data.accountId;
            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
            } = e.detail;
            this.setData({
                rankVal: name
            }, () => {
                this.loaRankData();
            })
        },
        loaTrendData() {
            let orgIds = this.data.accountId;
            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;
                }
                //trendData.series[0].title=this.data.trendName;
                this.setData({
                    trendData: trendData
                }, () => {
                    wx.hideLoading();
                })
            });
        },
        changeAccountFn: function (value) {
            this.getReportCharts();
            // this.getCharts({accountId:value}).then(res=>{
            //     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();
            })
        },
    }
})