dateChooseDialog.vue 9.37 KB
<template>
  <div>
    <baseDialog :is-show.sync="dialogShow"
                :config="dialogConfig.setting"
                :dialog-config="dialogConfig.config"
                class="detail-content-warn">
      <div>
        <div class="form_div">
          <div class="tips">
            默认为当前月份到昨日数据,可自定义周期
            <el-button type="text"
                       class="scBtn"
                       @click="handleRefresh">刷新
            </el-button>
            (注:下载按钮无法点击时请刷新)
          </div>
          <el-form label-width="100px"
                   :inline="true"
                   :rules="rules"
                   :model="form"
                   ref="form">
            <el-form-item label="周期:"
                          prop="dateRange">
              <el-date-picker v-model="form.dateRange"
                              type="datetimerange"
                              range-separator="至"
                              start-placeholder="开始时间"
                              end-placeholder="结束时间"
                              value-format="yyyy-MM-dd HH:mm"
                              format="yyyy-MM-dd HH:mm"
                              :default-time="['00:00:00', '23:59:59']"
                              :picker-options="pickerOptions"
                              @change="handleDateRangeChange" />
              <el-button type="text"
                         class="scBtn"
                         @click="handleGenerate">生成</el-button>
              <div v-if="filename">
                <span class="fileTips"
                      :title="filename">{{ filename ? (filename.length > 40 ? (filename.slice(0, 40) + '...') : filename) : '' }}</span>
                <i class="el-icon-delete delBtn"
                   @click="handleDelete"></i>
              </div>
            </el-form-item>
          </el-form>
        </div>
        <div class="footer">
          <el-button @click="handleCancel">取消</el-button>
          <el-button type="primary"
                     :disabled="btDis"
                     @click="handleDownload">下载</el-button>
        </div>
      </div>
    </basedialog>
  </div>
</template>

<script>
import {
  remake,
  refresh,
  reportExport_download,
  isexistdata
} from "@api/downloadReportManage"
import { Util, deepClone } from "@/common/util"
export default {
  name: "DateChooseDialog",
  components: {
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    paramsObj: {
      type: Object,
      default: {}
    }
  },
  computed: {
    dialogShow: {
      get() {
        return this.visible
      },
      set(val) {
        this.$emit('dialogClose')
      }
    }
  },
  mounted() {
    this.setDefaultDateRange()
  },
  data() {
    return {
      userInfo: this.$store.state.d2admin.user.info,
      dialogConfig: {
        setting: {
          showClose: true,
          appendToBody: true,
          btnList: []
        },
        config: {
          title: "选择报表周期并下载",
          width: "600px"
        }
      },
      detailInfo: {},
      loading: true,
      btDis: true,
      form: {
        dateRange: ''
      },
      rules: {
        dateRange: [
          { required: true, message: "请选择周期", trigger: "change" }
        ]
      },
      pickerOptions: {
        disabledDate(time) {
          const now = new Date()
          const threeMonthsAgo = new Date()
          threeMonthsAgo.setMonth(now.getMonth() - 3)
          const nowDay = new Date(now)
          // nowDay.setDate(yesterday.getDate() - 1)
          nowDay.setHours(23, 59, 59, 0)

          // 只允许选择:从3个月前 到 今天之间的时间
          return time.getTime() > nowDay.getTime() || time.getTime() < threeMonthsAgo.getTime()
        },
        shortcuts: [{
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            end.setDate(end.getDate()) // 今天
            end.setHours(23, 59, 59)
            const start = new Date()
            start.setMonth(start.getMonth() - 3)
            start.setDate(1)
            start.setHours(0, 0, 0)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      filename: '',
      downLoadKey: ''
    }
  },
  methods: {
    setDefaultDateRange() {
      const now = new Date() // 今天,例如 2025-08-01

      // 1. 计算 "昨天"
      const yesterday = new Date(now)
      yesterday.setDate(yesterday.getDate() - 1) // yesterday = 2025-07-31

      // 2. 计算 "昨天所在月份的第一天"
      // 使用 yesterday 的年份和月份,日期设为 1
      const firstDayOfYesterdayMonth = new Date(yesterday.getFullYear(), yesterday.getMonth(), 1)
      firstDayOfYesterdayMonth.setHours(0, 0, 0, 0) // 确保时间是 00:00:00

      // 3. 设置 "昨天" 的时间到 23:59
      yesterday.setHours(23, 59, 0, 0) // 设置为 23:59:00 (秒=0, 毫秒=0)

      // 4. 格式化并赋值 (关键!必须是字符串数组)
      this.form.dateRange = [
        firstDayOfYesterdayMonth, // "2025-07-01 00:00"
        yesterday // "2025-07-31 23:59"
      ]
    },

    handleDateRangeChange(value) {
      if (!Array.isArray(value) || value.length !== 2) return

      const [startDateStr, endDateStr] = value
      const startDate = new Date(startDateStr)
      const endDate = new Date(endDateStr)

      const diffMinutes = (endDate - startDate) / (1000 * 60)

      if (diffMinutes < 30) {
        this.$message.error('时间范围不能小于30分钟')
        this.form.dateRange = [] // 清空选择
      }
    },

    handleCancel() {
      this.dialogShow = false
    },

    handleGenerate() {
      this.$refs.form.validate(valid => {
        if (valid) {
          var params = {
            accountName: this.userInfo.loginAccount.userName,
            businessType: this.paramsObj.business_type,
            reportTypeName: this.paramsObj.report_type,
            key: this.paramsObj.key,
            startTime: Util.dateFormate(this.form.dateRange[0], 'YYYY-MM-DD HH:mm'),
            endTime: Util.dateFormate(this.form.dateRange[1], 'YYYY-MM-DD HH:mm')
          }
          // return console.log('params', params)
          isexistdata(params).then(res => {
            if (res.isexistdata) {
              remake(params).then(res => {
                this.notice.okTips('操作成功')
              })
            } else {
              // this.notice.warningTips(
              //   "周期内无有效数据"
              // )
              this.$message.error('周期内无有效数据')
            }
          })
        }
      })
    },

    handleRefresh() {
      this.$refs.form.validate(valid => {
        if (valid) {
          var params = {
            businessType: this.paramsObj.business_type,
            reportTypeName: this.paramsObj.report_type,
            key: this.paramsObj.key,
            startTime: Util.dateFormate(this.form.dateRange[0], 'YYYY-MM-DD HH:mm'),
            endTime: Util.dateFormate(this.form.dateRange[1], 'YYYY-MM-DD HH:mm')
          }
          refresh(params).then(res => {
            this.notice.okTips('操作成功')
            this.filename = res.filename
            if (res.code === 0) {
              this.btDis = false
              this.downLoadKey = res.key
            }
          })
        }
      })
    },

    handleDelete() {
      this.filename = ''
      this.btDis = true // 删除文件后禁用下载按钮
    },

    handleDownload() {
      reportExport_download({ key: this.downLoadKey }).then((result) => {
        console.log(result, "result")
        var url = result.list[0].url + '&accountName=' + this.userInfo.loginAccount.userName
        result && this.downLoadFile(url)
        this.btDis = true // 只能下载一次,下载后禁用按钮
      })
    },

    /**
     * url下载文件
     *
     */
    downLoadFile: function (url) {
      const Temp = document.createElement("a")
      Temp.href = url
      document.body.append(Temp)
      Temp.click()
      document.body.removeChild(Temp)
    }
  }
}
</script>

<style lang="scss" scoped>
.detail-content-warn {
  .form_div {
    color: #333333;

    .el-form-item__content {
      display: inline-block;
      width: calc(100% - 150px);
      line-height: 24px;
    }

    .el-form-item__label {
      padding: 0;
      line-height: 24px;
    }
    .el-form-item {
      width: 100%;
      margin-bottom: 20px;
    }

    .edit-sub-block {
      // padding: 0 10px;

      .detail-txt {
        display: inline-block;
        width: 200px;
        vertical-align: bottom;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
      ::v-deep .el-form-item__content {
        max-width: calc(100% - 110px);
        word-break: break-all;
      }
      .itemDiv {
        display: flex;
        .halfItem {
          width: 50%;
        }
      }
      .onLineItem {
        ::v-deep .el-form-item__content {
          max-width: 80%;
        }
      }
    }
    .imgCon {
      text-align: center;
      .imgSize {
        width: 120px;
        height: 120px;
      }
    }
    .tips {
      text-align: left;
      margin: 10px 0 20px 0;
      color: #333333;
    }
    .scBtn {
      padding-left: 10px;
    }
    .fileTips {
      font-size: 12px;
    }
    .delBtn {
      padding-left: 10px;
      cursor: pointer;
    }
  }
  .footer {
    text-align: right;
    margin-top: 50px;
  }
}
</style>