Crunchy
2025-06-14 7e460156de73171f9660ce48f80703e79f8b478d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<template>
  <div class="table-search">
    <el-form
      ref="queryForm"
      v-model="queryParams"
      size="small"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="日期:">
        <el-date-picker
          v-model="queryParams.date"
          value-format="yyyy-MM-dd HH:mm"
          format="yyyy-MM-dd HH:mm"
          type="datetimerange"
          :picker-options="pickerOptions"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          align="right"
        />
      </el-form-item>
      <el-form-item v-show="show" :label="searchParams.hasOwnProperty('productModel')? '规格型号':'客户名称'">
        <el-select v-model="queryParams.type" :placeholder="searchParams.hasOwnProperty('productModel')? '请选择规格型号':'请选择客户名称'" clearable style="width: 240px">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="search"
        >搜索</el-button>
        <el-button :style="{color:'#409EFF',border:' 1px solid #409EFF'}" icon="iconfont icon-wenjiandaochu" size="mini" @click="exportExcel">导出</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
 
<script>
 
export default {
  props: [
    'searchData',
    'searchParams',
    'options',
    'getList',
    'file',
    'show',
    'excelName'
  ],
  data() {
    return {
      queryParams: {
        date: [],
        type: ''
      },
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  methods: {
    search() {
      // "查询条件:"时间需要格式化成yyyy-mm-dd hh-mm-ss
      // console.log(this.queryParams)
      let { date, type } = this.queryParams
      date = date == null ? [] : date
      let params = { ...this.searchParams, startTime: date[0], endTime: date[1] }
      params = this.searchParams.hasOwnProperty('productModel') ? { ...params, productModel: type } : { ...params, customerName: type }
      const obj = {}
      const arr = Object.keys(params).filter(key => params[key])
      arr.forEach(item => obj[item] = params[item])
      console.log(obj)// 查看所有请求参数
      this.getList(obj).then(res => {
        this.searchData({ ...res.data, startTime: date[0], endTime: date[1] })
      })
    },
    exportExcel() {
      let { date, type } = this.queryParams
      date = date == null ? [] : date
      let params = { startTime: date[0], endTime: date[1] }
      params = this.searchParams.hasOwnProperty('productModel') ? { ...params, productModel: type } : { ...params, customerName: type }
      const obj = {}
      const arr = Object.keys(params).filter(key => params[key])
      arr.forEach(item => obj[item] = params[item])
      console.log(obj)// 查看所有请求参数
      this.file(obj).then(response => {
        const blob = new Blob([response.data], { type: 'application/vnd.ms-excel;charset=utf-8' })
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `${this.excelName}.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        this.loadingBtn = false
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      }).catch(message => {
        this.$message.error({
          message: '抱歉下载错误!错误原因:' + message,
          type: 'error'
        })
      })
    }
  }
}
</script>
 
<style lang="scss">
.table-search{
   .el-form-item{
      margin-bottom: 0;
      margin: 12px;
    }
}
</style>