zjf
2023-03-08 51468f93275c2bcfcc7ad25bf05f3d6a079ff764
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="'导出'+crmTypeName"
    :append-to-body="true"
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :close-on-press-escape="false"
    width="550px"
    @close="closeView"
  >
    <div class="dialog-body">
      <p
        v-if="error"
        class="error"
        v-text="error" />
      <p
        v-if="done"
        class="done">
        <i class="el-icon-success"/>
        导出已完成
      </p>
      <p
        v-else-if="cancel"
        class="cancel">
        <i class="el-icon-warning"/>
        导出已取消
      </p>
      <div v-else>
        <i class="el-icon-loading" />
        导出中...
        {{ progress }}
      </div>
    </div>
    <span slot="footer" class="dialog-footer" />
  </el-dialog>
</template>
 
<script>
import { mapGetters } from 'vuex'
import {
  OutputCustomerToExcel,
  OutputContacterToExcel,
  OutputSalesChanceToExcel,
  OutputOtherSalesChanceToExcel,
  ExportSubscriptionsExcel
} from '@/api/common'
 
export default {
  name: 'CRMExport', // 文件导出
  components: {},
 
  props: {
    show: {
      type: Boolean,
      default: false
    },
    // CRM类型
    crmType: {
      type: String,
      default: ''
    },
    /** 是公海 */
    isSeas: {
      type: Boolean,
      default: false
    },
    // 搜索条件
    search: {
      type: String,
      default: ''
    },
    // 场景
    scene_id: {
      type: [Number, String],
      default: ''
    },
    // 高级搜索
    filterObj: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // 顶部搜索
    headObj: {
      type: Object,
      default: () => {
        return {}
      }
    },
    exportParams: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      showDialog: false,
      progress: '',
      error: '',
      // 导出已完成
      done: false,
      // 取消导出
      cancel: false,
      // 队列标识
      exportQueueIndex: '',
      // 临时数据
      tempData: {
        temp_file: ''
      },
      // 排序
      sortData: {}
    }
  },
  computed: {
    ...mapGetters(['userInfo']),
    crmTypeName () {
      return (
        {
          customer: '客户',
          leads: '线索',
          contacts: '联系人',
          product: '产品'
        }[this.crmType] || ''
      )
    }
  },
  watch: {
    show (val) {
      console.log(val)
      this.showDialog = val
      if (val) {
        this.cancel = false
        this.exportInfos()
        // window.onbeforeunload = (event) => {
        //   this.exportInfos({
        //     page: -1,
        //     temp_file: this.tempData.temp_file
        //   })
        //   return event
        // }
      } else {
        window.onbeforeunload = null
      }
    }
  },
  mounted () {
    this.$bus.off('getSortData')
    this.$bus.on('getSortData', (sortData) => {
      this.sortData = sortData
    })
  },
  methods: {
    // 关闭操作
    closeView () {
      this.error = ''
      this.done = false
      this.exportQueueIndex = ''
      this.progress = ''
      this.$emit('close')
      this.$emit('queryList')
    },
    // 点叉
    beforeClose (done) {
      if (this.error || this.done) {
        done()
        return
      }
      this.$confirm('此操作将终止导出, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.cancel = true
          done()
        })
        .catch(() => {
        })
    },
    // 导出操作
    exportInfos (data = {}) {
      console.log(this.headObj)
      let params
      if (this.crmType === 'customer') {
        params = {
          'PageIndex': 1,
          'PageSize': 999,
          'SelectOwnedType': this.headObj && this.headObj.list ? this.headObj.list.customer : 0,
          'BrachUserId': this.headObj && this.headObj.list ? this.headObj.list.BrachUserId : '',
          'Name': this.headObj && this.headObj.list ? this.headObj.list.user : '',
          'CreateStartTime': '',
          'CreateEndTime': '',
          'StartFollowTime': '',
          'EndFolowTime': ''
        }
      } else if (this.crmType === 'contacts') {
        params = {
          'PageIndex': 1,
          'PageSize': 999,
          'SelectOwnedType': this.headObj && this.headObj.list ? this.headObj.list.customer : 0,
          'BrachUserId': this.headObj && this.headObj.list ? this.headObj.list.BrachUserId : '',
          'Name': this.headObj && this.headObj.list ? this.headObj.list.user : ''
        }
      } else if (this.crmType === 'business') {
        params = {
          'PageIndex': 1,
          'PageSize': 999,
          'SalesChanceStage': this.headObj && this.headObj.list ? this.headObj.list.businessType : '',
          'SelectOwnedType': this.headObj && this.headObj.list ? this.headObj.list.customer : 0,
          'BrachUserId': this.headObj && this.headObj.list ? this.headObj.list.BrachUserId : '',
          'Name': this.headObj && this.headObj.list ? this.headObj.list.user : '',
          'SalesChanceType': this.headObj && this.headObj.list ? this.headObj.list.businessType : '',
          'ProvinceCode': this.headObj && this.headObj.list ? this.headObj.list.area : ''
        }
      } else if (this.crmType === 'businessChances') {
        params = {
          'PageIndex': 1,
          'PageSize': 999,
          'SalesChanceStage': this.headObj && this.headObj.list ? this.headObj.list.businessType : '',
          'SelectOwnedType': this.headObj && this.headObj.list ? this.headObj.list.customer : 0,
          'BrachUserId': this.headObj && this.headObj.list ? this.headObj.list.BrachUserId : '',
          'Name': this.headObj && this.headObj.list ? this.headObj.list.user : '',
          'SalesChanceType': this.headObj && this.headObj.list ? this.headObj.list.businessType : '',
          'ProvinceCode': this.headObj && this.headObj.list ? this.headObj.list.area : '',
          'CreateStartTime': '',
          'CreateEndTime': '',
          'ProjectProperty': '',
          'UploadStatusName': '',
          'ReportStatusName': ''
        }
      } else if (this.crmType === 'subscriptions') {
        params = {
          ...this.headObj
        }
      }
      let month = new Date().getMonth() + 1
      let timer = new Date().getFullYear() + '-' + month + '-' + new Date().getDate()
      let request
      request = {
        customer: OutputCustomerToExcel,
        contacts: OutputContacterToExcel,
        business: OutputSalesChanceToExcel,
        businessChances: OutputOtherSalesChanceToExcel,
        subscriptions: ExportSubscriptionsExcel
      }[this.crmType]
      console.log(request)
      request(params)
        .then(res => {
          if (this.crmType === 'subscriptions') {
            if (res.data.type === 'application/json') {
              // this.show = false
              this.closeView()
              let reader = new FileReader()
              reader.readAsText(res.data, 'utf-8')
              reader.onload = (e) => {
                let resData = JSON.parse(reader.result)
                this.$message.error(resData.Message)
              }
              return
            }
          }
          this.exportQueueIndex = ''
          this.done = true
          var blob
          if (res.data === 'subscriptions') {
            blob = new Blob([res.data], {
              type: 'application/octet-stream;charset=utf-8'
            })
          } else {
            blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel;charset=utf-8'
            })
          }
          // var blob = new Blob([res.data], {
          //   type: 'application/json'
          // })
          var downloadElement = document.createElement('a')
          var href = window.URL.createObjectURL(blob) // 创建下载的链接
          downloadElement.href = href
          // downloadElement.download =
          //     decodeURI(
          //       res.headers['content-disposition'].split('filename=')[1]
          //     ) || '' // 下载后文件名
          if (this.crmType === 'customer') {
            downloadElement.download = `客户列表` + timer + `.xlsx`
          } else if (this.crmType === 'business') {
            downloadElement.download = `商机列表` + timer + `.xlsx`
          } else if (this.crmType === 'contacts') {
            downloadElement.download = `客户联系人列表` + timer + `.xlsx`
          } else if (this.crmType === 'subscriptions') {
            downloadElement.download = `订阅列表` + timer + `.xlsx`
          } else {
            downloadElement.download = `列表` + timer + `.xlsx`
          }
          document.body.appendChild(downloadElement)
          downloadElement.click() // 点击下载
          document.body.removeChild(downloadElement) // 下载完成移除元素
          window.URL.revokeObjectURL(href) // 释放掉blob对象
          // }
        })
        .catch(() => {
        })
    }
  }
}
</script>
 
<style scoped lang="less">
 
.dialog-body {
  height: 150px;
  text-align: center;
  padding-top: 40px;
  color: #659DED;
  .el-icon-loading {
    margin-bottom: 10px;
    display: block;
  }
  p.error {
    color: #E6A23C;
  }
  p.done {
    color: #67C23A;
    .el-icon-success {
      font-size: 30px;
      display: block;
    }
  }
}
 
.cancel {
  color: #E6A23C;
  .el-icon-warning {
    font-size: 30px;
    display: block;
  }
}
 
.el-dialog__wrapper {
  /deep/ .el-icon-loading {
    font-size: 30px;
  }
 
  /deep/ .el-loading-text {
    font-size: 18px;
    margin-top: 10px;
  }
}
</style>