optionMall.vue 11.9 KB
<template>
    <div class="ipage-option-wrapper">
        <ul class="condition-box">
            <li class="condition-item">
                <span class="condition-item-text">{{ ($Project == 'mall' ? $t('pholder.mallS') : $t('pholder.shopS')) + ':'}}</span>
                <el-select v-model="mallVal" filterable :placeholder="$t('pholder.shopSelect')" class="mall-sel-box"
                    @change="mallChange">
                    <el-option v-for="item in mallData" :key="item.id" :label="item.name" :value="item.id">
                    </el-option>
                </el-select>
            </li>
            <!-- 选择日期 S -->
            <li class="condition-item">
              <div class="condition-item-option">
                <div class="time-wrapper newTime">
                  <el-radio-group v-model="dateType" size="small">
                    <el-radio-button v-for="item in analyasisTime" :key="item.value" :label="item.value" >{{i18nFormatter(item.label)}}</el-radio-button>
                    </el-radio-group>
                </div>
              </div>
            </li>
            <li class="condition-item">
                <!-- <span class="condition-item-text">{{ $t('iPage.selectdate') }}:</span> -->
                <div class="condition-item-option">
                    <el-date-picker class="date-input" v-show="dateType == 'day'" size="mini" v-model="dayTime"
                        value-format="yyyy-MM-dd" format="yyyy-MM-dd" align="right" type="date"
                        :placeholder="$t('pholder.date')" :picker-options="pickerOption"></el-date-picker>
                    <el-date-picker class="date-input analysis-date-input date-week-box" v-show="dateType === 'week'" v-model="weekTime" align="right" type="week" :format="datePickerFormatter('week')" size="mini" :placeholder="$t('pholder.week')" :picker-options="weekPickerOpt">
                        </el-date-picker>
                    <el-date-picker class="date-input" v-show="dateType == 'month'" size="mini" v-model="monthTime"
                        align="right" type="month" value-format="yyyy-MM" format="yyyy-MM"
                        :placeholder="$t('pholder.month')" :picker-options="pickerOption"></el-date-picker>
                    <el-date-picker class="date-input" v-show="dateType == 'year'" size="mini" v-model="yearTime"
                        align="right" type="year" value-format="yyyy" format="yyyy" :placeholder="$t('pholder.year')"
                        :picker-options="pickerOption"></el-date-picker>
                    <el-date-picker class="date-input analysis-date-input times-box" v-model="customTime" value-format="yyyy-MM-dd" align="right" type="daterange" size="mini" :picker-options="pickerOption" range-separator="-" v-show="dateType === 'custom'" :start-placeholder="$t('table.startTime')" :end-placeholder="$t('table.endTime')">
                    </el-date-picker>
                </div>
            </li>
            <!-- 选择日期 E -->
            <!-- 操作按钮区域 S -->
            <li class="condition-item">
                <el-button type="primary" class="primary-btn" size="small"
                    :disabled="(dateType == 'day' && !dayTime) || (dateType == 'week' && !weekTime) || (dateType == 'month' && !monthTime) || (dateType == 'year' && !yearTime)|| (dateType == 'custom' && !customTime)"
                    @click="confirmCondition(true)">{{ $t('button.confirm') }}</el-button>
                <!-- <el-button class="reset-btn" size="small" @click="resetRefresh">{{ $t('button.reset') }}</el-button> -->
            </li>
            <!-- 操作按钮区域 E -->
        </ul>
        <!-- 统计方式区域 S -->
        <ul class="condition-box">
            <el-button style="margin-left:10px;" class="reset-btn collapse-btn" size="small" icon="el-icon-download"
                :loading="downloadStatus" @click="download"></el-button>
        </ul>
        <!-- 统计方式区域 E -->
    </div>
</template>
<script>
let hasInit = false; //是否完成初始化绘图
let optionData;
import _ from 'underscore';
import { mixin } from './download'
import html2canvas from "html2canvas";
import jsPDF from "jspdf";
const analyasisTime = ['day', "week","month", "year", 'custom'].map(item => ({
    label: item,
    value: item
}))
export default {
    mixin: [mixin],
    // props: {
    //     pVal: {
    //         type: String,
    //         required: true,
    //     },
    // },
    data() {
        return {
            downloadStatus: false,
            accountVal: "",
            mallVal: "",
            analyasisTime,
            mallData: [],
            orgType: "mall", //当前页面所属的层级【'account','mall','floor',...】
            dateType: "day", //报告类型【'day','month',...】
            radioVal: "", //单选【统计、画像】
            params: {},
            dayTime: "", //日-期
            weekTime:'',//周
            monthTime: "", //月
            yearTime: "", //年
            customTime: '',//自定义
            pickerOption: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
            },
            weekPickerOpt: {
              firstDayOfWeek: window._vionConfig.firstDayOfWeek ? window._vionConfig.firstDayOfWeek : 1
            },
        };
    },
    watch: {
        radioVal(val) {
            // this.confirmCondition();
        },
        dayTime(val) {
            this.sessionDate("dayDate", val);
        },
        weekTime(val) {
            this.sessionDate("weekDate", val);
        },
        monthTime(val) {
            this.sessionDate("monthDate", val);
        },
        yearTime(val) {
            this.sessionDate("yearDate", val);
        },
        customTime(val) {
          this.sessionDate('customDate', val);
        }
    },
    mounted() {
        hasInit = false;
        let { dayDate, weekDate, monthDate, yearDate, customDate } = this.createDate();
        this.dayTime = dayDate;
        this.weekTime = weekDate;
        this.monthTime = monthDate;
        this.yearTime = yearDate;
        this.customTime = customDate;
        // this.initOption();
    },
    methods: {
        download() {
            this.downloadStatus = true;
            let mparts = document.querySelector('.mparts');
            let { width, height } = mparts.getBoundingClientRect();
            const ops = {
                scale: window.devicePixelRatio,
                width,
                height,
                useCORS: true,
                allowTaint: false,
            }
            let contentWidth = width;
            let contentHeight = height;
            html2canvas(mparts, ops).then(canvas => {
                // jspdf.js 插件对单页面的最大宽高限制 为 14400
                let limit = 14400;
                let pdf;
                let pageData = canvas.toDataURL("image/jpeg", 1.0); // 清晰度 0 - 1
                if (contentHeight > limit) {
                    let contentScale = limit / contentHeight;
                    contentHeight = limit;
                    contentWidth = contentScale * contentWidth;
                }
                let orientation = "p";
                // 在 jspdf 源码里,如果是 orientation = 'p' 且 width > height 时, 会把 width 和 height 值交换,
                // 类似于 把 orientation 的值修改为 'l' , 反之亦同。
                if (contentWidth > contentHeight) {
                    orientation = "l";
                }
                // orientation Possible values are "portrait" or "landscape" (or shortcuts "p" or "l")
                pdf = new jsPDF(orientation, "pt", [contentWidth, contentHeight]); // 下载尺寸 a4 纸 比例
                // pdf.addImage(pageData, 'JPEG', 左,上,宽度,高度)设置
                pdf.addImage(pageData, "JPEG", 0, 0, contentWidth, contentHeight);
                this.$emit('downloadSuccess', true);
                let dateVal = this.dateType == "day" ? this.dayTime : this.dateType == "month" ? this.monthTime : this.dateType == "year" ? this.yearTime : "";
                let timeObj = this.newDateFormat(this.dateType, new Date(dateVal));
                let title2 = timeObj.startTime.slice(0, this.dateType == "day" ? 11 : this.dateType == "month" ? 7 : 4);
                let title = this.$t(`asisTab.${this.orgType}`) + this.$t(`asisTab.${this.dateType + 'Report'}`);
                pdf.save(title + '【' + title2 + '】' + '.pdf');
                this.downloadStatus = false;
            })
        },
        i18nFormatter(label) {
            let langeLabel = 'asisTab.' + label;
            return this.$t(langeLabel)
        },
        mallChange() {
            this.initDraw();
            this.$cookie.set('mallId', this.mallVal);
        },
        setOption(data) {
            switch (data.orgType) {
                case "mall":
                    this.mallVal = data.id;
                    this.radioVal = "count";
                    this.$cookie.set('mallId', data.id);
                    this.$router.push('/ipage/ipagemall' + this.dateType);
                    break;
            }
        },
        initOption(objData,mallData) {
            this.mallVal = objData.mallId;
            this.mallData = mallData;
            let emitData = {
              mallIds: this.mallVal,
              orgType: this.orgType,
              dateType: this.dateType,
              dateVal:
                  this.dateType == "day"
                      ? this.dayTime
                      : this.dateType == "week"
                          ? this.weekTime
                      : this.dateType == "month"
                          ? this.monthTime
                          : this.dateType == "year"
                              ? this.yearTime
                              : this.dateType == "custom"
                                ? this.customTime
                                : "",
            }
            let timeObj = this.newDateFormat(
                this.dateType,
                new Date(emitData.dateVal)
            );
            emitData.startDate = timeObj.startTime;
            emitData.endDate = timeObj.endTime;
            this.$emit('initData', emitData);
        },
        confirmCondition(force) {
            const emitData = {
                mallIds: this.mallVal,
                orgType: this.orgType,
                dateType: this.dateType,
                dateVal:
                    this.dateType == "day"
                        ? this.dayTime
                        : this.dateType == "week"
                            ? this.weekTime
                        : this.dateType == "month"
                            ? this.monthTime
                            : this.dateType == "year"
                                ? this.yearTime
                                : this.dateType == "custom"
                                  ? this.customTime
                                  : "",
            };
            if(this.dateType == "custom"){
              emitData.startDate = this.customTime[0];
              emitData.endDate = this.customTime[1];
            }else{
              let timeObj = this.newDateFormat(
                  this.dateType,
                  new Date(emitData.dateVal)
              );
              emitData.startDate = timeObj.startTime;
              emitData.endDate = timeObj.endTime;
            }
            this.$emit("reportData", emitData);
        },
        resetRefresh() {
            this.dayTime = dateUnit.dateFormat(new Date(), "yyyy-MM-dd");
            this.weekTime = new Date();  // 周时间
            this.monthTime = new Date(); // 月时间
            this.yearTime = new Date(); // 年时间
            this.customTime = [this.dayReduce(new Date(), 15), dateUnit.dateFormat(new Date(), 'yyyy-MM-dd')];
        },
        initDraw() {
            if (hasInit) return false;
            hasInit = true;
            this.confirmCondition();
        },
    },

};
</script>
<style scoped>
@import url(../css/public.css);
.newTime{
  width: auto;
}
</style>