mixin.js 6.01 KB
import echarts from '@/components/Echarts'
export default {
    props: {
        title: String,
        icon: String,
        ckey: String
    },
    inject: ['evtReport', 'getChartId', 'getChartName', 'getCardIds', 'getCardList', 'getChartList'],
    data() {
        return {
            chartId: "",
            chartData: {

            },
            loaded: false,
            params: {} //顶部公用请求参数
        }
    },
    mounted() {},
    components: {
        ...echarts
    },
    methods: {
        showLoading() {
            let chart = this.$refs.chart;
            if (chart && chart.chart) {
                chart.chart.showLoading("default", {
                    text: "",
                    color: "#409eff"
                });
                chart.chart.loading = true;
            }
        },
        hideLoading() {
            let chart = this.$refs.chart;
            if (chart && chart.chart) {
                chart.chart.hideLoading();
                chart.chart.loading = false;
            }
        },

        loadModuleData(params, force = false) {
            if (this.loaded && !force) return false;
            this.loaded = true;
            let org_name = params.orgType + 'Ids';
            /****************************************************/
            this.params = {
                orgType: params.orgType,
                orgIds: params[org_name] || params.mallIds,
                dateType: params.dateType,
                dataTypePlus: params.dataTypePlus?params.dataTypePlus:'',
                mallIds: params.mallIds,
                startDate: params.startDate,
                endDate: params.endDate,
                isStore:params.isStore,
                groupIds:params.groupIds
            };
            params[org_name] && (this.params[org_name] = params[org_name]);
            /*if(params.timeLevel){
                this.params.timeLevel = params.timeLevel
            }*/
            this.setChartConfig(params);
            this.loadChartData(this.params);
        },
        setChartConfig(params) {
            let chartKeys = this.getChartsConfig && this.getChartsConfig(params);
            if (!chartKeys || (!this.ckey && !chartKeys.default)) {
                this.configObj = {};
            } else if (!this.ckey && chartKeys.default) {
                this.configObj = chartKeys.default;
            } else {
                this.configObj = chartKeys[this.ckey] || chartKeys.default;
                this.configObj.otherConf || (this.configObj.otherConf = chartKeys.default.otherConf);
            }
            this.configObj && this.configObj.otherConf && (this.configObj.otherConf._color || (this.configObj.otherConf._color = this.getSeriesColor()));
        },
        resetLoaded() {
            this.loaded = false;
            this.chartData && (this.chartData.series = [])
        },
        bindIcon(icon) {
            if (this.icon == 'empty') return '';
            return require("@/assets/img/ipage/" + (this.icon || icon || 'shuju') + '.png');
        },
        /**
         * 获取随机颜色
         * */
        getRandomColors(length) {
            let colors = [],
                temp = [];
            while (colors.length < length) {
                temp.push(getColor());
                colors = [...new Set(temp)];
            }

            function getColor() {
                return (
                    "#" +
                    ("00000" + ((Math.random() * 0x1000000) << 0).toString(16)).slice(-6)
                );
            }
            return colors;
        },
        /**
         * 获取自定义颜色
         * */
        getSeriesColor() {
            return ['#4a90ff', '#55d9a9', '#f7cf98', '#ff9fc2', '#91e7f8', '#fd7041', '#597ef4', '#f8f02d', '#fa2d2e', '#b05cdd', '#ff877e', '#939893'];
        },
        ajaxModuleData(params) {
            if (!this.chartId) this.chartId = this.getChartId(this.ckey);
            if (!this.chartId) {
                console.error('无法获取' + this.ckey + '对应的chartId');
                return
            }
            /*缓存请求参数S*/
            if (Object.keys(this.params).length == 0 && params) {
                this.params = params;
            }
            if (!params) {
                params = this.params;
            }
            /*缓存请求参数E*/
            params.chartIds = this.chartId;
            this.tempParams = params;
            this.showLoading();
            return new Promise((resolve, reject) => {
                this.$api.ipageReport.bodyReport(params).then(res => {
                    let { data } = res.data;
                    if (!String(this.chartId).includes(',')) {
                        resolve(data[this.ckey]);
                    } else {
                        resolve(data);
                    }
                    this.hideLoading();
                }).catch(e => {
                    this.hideLoading();
                    resolve({});
                })
            });

        },
        /**
         * 获取全部店铺
         * */
        getZoneList() {
            return this.$api.base.zone({
                mallId: this.params.mallIds,
                status: 1,
                sortName: '"floor".name, "zone".name',
                // _t: Date.parse(new Date()) / 1000
            })
        },
        /**
         * 获取全部店铺
         * */
        getFloorList() {
            return this.$api.base.floor({
                mallId: this.params.mallIds,
                accountId: this.$cookie.get("accountId"),
                status: 1,
            })
        },
        getFloorZone(floorId, params) {
            return this.$api.base
                .getFloorZone(floorId, params);
        },
        /**
         * 获取全部业态
         * */
        getFormatList() {
            return this.$api.base.formatlist({
                //_t: Date.parse(new Date()) / 1000
            })
        },
        getMallList() {
            return this.$api.base.bases("/malls", { status: 1, accountId: this.$cookie.get('accountId') })
        }
    }
}