guoshilong
2023-11-13 54849757852f6ab40eb17afbd03d1d839b60a38d
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
<template>
    <!--  <div class="header">媒体文件</div>-->
    <!--搜索栏-->
    <div class="search-panel-wrapper">
        <div class="search-part">
            <a-range-picker :size="searchPanelOptions.size" :format="searchPanelOptions.dateFormat"
                            :valueFormat="searchPanelOptions.valueFormat" v-model:value="timeRangeArr.data"
                            @change="dateChange" style="width: 300px"/>
        </div>
 
        <div class="search-part">
            <a-select v-model:value="subFileTypeArr" allowClear @change="subFileTypeChange"
                      :options="subFileTypeOptions"
                      :maxTagCount="searchPanelOptions.maxTagCount" mode="multiple"
                      :size="searchPanelOptions.size" placeholder="所有类型" style="width: 300px">
            </a-select>
        </div>
 
        <div class="search-part">
            <a-select v-model:value="payloadArr" allowClear @change="payloadChange" :options="payloadOptions"
                      :maxTagCount="searchPanelOptions.maxTagCount" mode="multiple"
                      :size="searchPanelOptions.size" placeholder="所有负载" style="width: 300px">
            </a-select>
        </div>
 
        <div class="search-part">
            <a-input-search :size="searchPanelOptions.size" v-model:value="searchQuery.name" @change="inputChange"
                            placeholder="按文件名称搜索" style="width: 300px"/>
        </div>
 
    </div>
 
    <div class="button-wrapper">
        <a-button type="primary" @click="compress">压缩打包</a-button>
    </div>
 
    <a-spin :spinning="loading" :delay="1000" tip="加载中" size="large">
        <div class="media-panel-wrapper">
            <a-table class="media-table" :columns="columns" :data-source="mediaData.data" row-key="fingerprint"
                     rowKey="file_id" :row-selection="rowSelection"
                     :pagination="paginationProp" :scroll="{ x: '100%', y: 600 }" @change="refreshData">
                <template v-for="col in ['name']" #[col]="{text, record}" :key="col">
 
                    <div>
                        <a-input
                                v-if="editableData[record.file_id]"
                                v-model:value="editableData[record.file_id]['file_name']"
                                style="margin: -5px 0"
                        />
                        <template v-else>
                            <a-tooltip :title="text">
                                <a @click="viewFile(record.object_key)">{{ text }}</a>
                            </a-tooltip>
                        </template>
                    </div>
 
                </template>
                <template #original="{ text }">
                    {{ text }}
                </template>
                <template #action="{ record }">
 
                    <div class="editable-row-operations">
                        <!-- 编辑态操作 -->
                        <div v-if="editableData[record.file_id]">
                            <a-tooltip title="保存">
                                <span @click="save(record)" style="color: #28d445;"><CheckOutlined/></span>
                            </a-tooltip>
                            <a-tooltip title="取消">
                                <span @click="() => delete editableData[record.file_id]" style="color: #e70102;"><CloseOutlined/></span>
                            </a-tooltip>
                        </div>
                        <!-- 非编辑态操作 -->
                        <div v-else class="flex-align-center flex-row" style="color: #2d8cf0">
                            <a-tooltip title="下载">
                                <a class="fz18" @click="downloadMedia(record)">
                                    <DownloadOutlined/>
                                </a>
                            </a-tooltip>
 
                            <a-tooltip title="编辑">
                                <a class="fz18" @click="edit(record)">
                                    <EditOutlined/>
                                </a>
                            </a-tooltip>
                        </div>
                    </div>
 
                </template>
            </a-table>
        </div>
    </a-spin>
 
    <div @click.self="showVideo = false" v-if="showVideo" class="modal">
        <video class="video-js" :id="videoPlayerId"></video>
    </div>
 
</template>
 
<script setup lang="ts">
import { ref } from '@vue/reactivity'
import { TableState } from 'ant-design-vue/lib/table/interface'
import { onMounted, reactive, UnwrapRef } from 'vue'
import { IPage } from '../api/http/type'
import { ELocalStorageKey } from '../types/enums'
import { downloadFile } from '../utils/common'
import { downloadMediaFile, getMediaFiles, MediaQueryParam, updateMediaFile } from '/@/api/media'
import { DownloadOutlined, EditOutlined, CheckOutlined, CloseOutlined } from '@ant-design/icons-vue'
import { message, Pagination } from 'ant-design-vue'
import { ColumnProps } from 'ant-design-vue/es/table/interface'
import JSZip from 'jszip'
import { saveAs } from 'file-saver'
import * as VueViewer from 'v-viewer' // 引入js
import 'viewerjs/dist/viewer.css' // 引入css
import { api as viewerApi } from 'v-viewer'
import axios from 'axios'
import videojs from 'video.js'
 
type Key = ColumnProps['key'];
 
const workspaceId = localStorage.getItem(ELocalStorageKey.WorkspaceId)!
const loading = ref(false)
const showVideo = ref(false)
const videoPlayerId = ref('videoPlayerId')
// 文件前缀
const prefix = 'https://dev.jxpskj.com:8026/cloud-bucket'
// 搜索栏配置项
const searchPanelOptions = reactive({
  size: 'large',
  maxTagCount: 2,
  dateFormat: 'YYYY-MM-DD',
  valueFormat: 'YYYY-MM-DD'
})
 
interface MediaFile {
    fingerprint: string,
    drone: string,
    payload: string,
    is_original: string,
    file_name: string,
    file_path: string,
    create_time: string,
    file_id: string,
    object_key: string
}
 
const subFileTypeOptions = [
  // { value: 0, label: '普通图片' },
  // { value: 1, label: '全景图' },
]
 
const payloadOptions = [
  { value: 'M30T Camera', label: 'M30T Camera' },
]
 
const timeRangeArr = reactive({
  data: [] as string[]
})
const searchQuery = reactive<MediaQueryParam>({})
const subFileTypeArr = reactive([])
const payloadArr = reactive([])
const editableData: UnwrapRef<Record<string, MediaFile>> = reactive({})
const columns = [
  {
    title: '文件名称',
    dataIndex: 'file_name',
    ellipsis: true,
    slots: { customRender: 'name' }
  },
  // {
  //   title: '文件路径',
  //   dataIndex: 'file_path',
  //   ellipsis: true,
  //   slots: { customRender: 'path' }
  // },
  {
    title: '拍摄负载',
    dataIndex: 'payload'
  },
  // {
  //   title: '大小',
  //   dataIndex: 'size',
  // },
  {
    title: '创建时间',
    dataIndex: 'create_time'
  },
  {
    title: '操作',
    slots: { customRender: 'action' }
  }
]
const body: IPage = {
  page: 1,
  total: 0,
  page_size: 50
}
const paginationProp = reactive({
  pageSizeOptions: ['20', '50', '100'],
  showQuickJumper: true,
  showSizeChanger: true,
  pageSize: 50,
  current: 1,
  total: 0
})
 
const selectedRow = reactive({
  list: [] as MediaFile[]
})
 
type Pagination = TableState['pagination']
 
const mediaData = reactive({
  data: [] as MediaFile[]
})
 
onMounted(() => {
  getFiles()
})
 
const rowSelection = {
  onChange: (selectedRowKeys: Key[], selectedRows: any[]) => {
    selectedRow.list = selectedRows
  },
}
 
function edit (record: MediaFile) {
  editableData[record.file_id] = record
}
 
// 保存
function save (record: MediaFile) {
  delete editableData[record.file_id]
  // updateDevice({ nickname: record.nickname }, workspaceId.value, record.device_sn)
  updateMediaFile(workspaceId, { fileName: record.file_name, fileId: record.file_id }).then(res => {
    if (res.code === 0) {
      message.success('修改成功')
    } else {
      message.error('修改失败')
    }
  }).catch(err => {
    console.log(err)
    message.error('修改失败')
  })
}
 
function viewFile (objectKey: string) {
  const ext = objectKey.split('.')[1]
  const fileType = getFileType(ext)
  let url = ''
  if (!objectKey.startsWith('/')) {
    url = prefix + '/' + objectKey
  } else {
    url = prefix + objectKey
  }
  console.log('文件地址:', url)
 
  if (fileType === 'image') {
    viewImage(url)
  } else if (fileType === 'video') {
    viewVideo(url)
  }
}
 
function getFileType (ext: string) {
  // 获取类型结果
  let result = ''
  // fileName无后缀返回 false
  if (!ext) {
    return -1
  }
  ext = ext.toLocaleLowerCase()
  // 图片格式
  const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif']
  // 进行图片匹配
  result = imglist.find(item => item === ext)
  if (result) {
    return 'image'
  }
  // 匹配txt
  const txtlist = ['txt']
  result = txtlist.find(item => item === ext)
  if (result) {
    return 'txt'
  }
  // 匹配 excel
  const excelist = ['xls', 'xlsx']
  result = excelist.find(item => item === ext)
  if (result) {
    return 'excel'
  }
  // 匹配 word
  const wordlist = ['doc', 'docx']
  result = wordlist.find(item => item === ext)
  if (result) {
    return 'word'
  }
  // 匹配 pdf
  const pdflist = ['pdf']
  result = pdflist.find(item => item === ext)
  if (result) {
    return 'pdf'
  }
  // 匹配 ppt
  const pptlist = ['ppt', 'pptx']
  result = pptlist.find(item => item === ext)
  if (result) {
    return 'ppt'
  }
  // 匹配 视频
  const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v', 'ogv']
  result = videolist.find(item => item === ext)
  if (result) {
    return 'video'
  }
  // 匹配 音频
  const radiolist = ['mp3', 'wav', 'wmv']
  result = radiolist.find(item => item === ext)
  if (result) {
    return 'radio'
  }
  // 其他 文件类型
  return 'other'
}
 
function viewImage (url: string) {
  viewerApi({
    images: [url]
  })
}
 
async function viewVideo (url: string) {
  showVideo.value = true
  await nextTick()
  const player = videojs(videoPlayerId.value, {
    autoplay: true, // 自动播放
    controls: true, // 控件 设置为true,控件才会显示
    fullscreenToggle: true, // 是否显示全屏按钮
    playToggle: true, // 是否显示播放按钮
    progressControl: true, // 是否显示进度条。除了boolean,还可以设置一个ProgressControlOptions对象,更详细的配置进度条。
    volumePanel: true, // 是否显示音量。除了boolean,还可以设置一个VolumePanelOptions对象,更详细的配置音量组件。
    pictureInPictureToggle: false, // 是否显示画中画按钮
    remainingTimeDisplay: true, // 是否显示时长
 
  })
 
  player.src(url)
  player.on('ended', () => {
    showVideo.value = false
  })
 
  player.on('error', () => {
    const error = player.error()
    console.log('video error:' + error.code + '-' + error.message)
  })
}
 
// 打包选中项
function compress () {
  if (selectedRow.list.length === 0) {
    message.warning('请先选择一条数据')
  } else {
    // http://dev.jxpskj.com:9000/cloud-bucket/a03c34be-1e4d-45c7-8e4b-0b4c14dbb334/DJI_202309251910_002_a03c34be-1e4d-45c7-8e4b-0b4c14dbb334/DJI_20230925191305_0006_W.jpeg
    const zip = new JSZip()
    const promiseList = [] as any
    selectedRow.list.forEach(e => {
      const url = prefix + e.object_key
      const promise = getFile(url, e.file_name)
      promiseList.push(promise)
    })
 
    Promise.all(promiseList).then(res => {
      console.log(res, '+++++++++++')
      res.forEach(e => {
        zip.file(e.name, e.data, { base64: true })
      })
      zip.generateAsync({ type: 'blob' })
        .then(function (content) {
          // see FileSaver.js
          saveAs(content, '打包文件.zip')
        })
    })
  }
}
 
// 获取文件的blob
function getFile (url: string, name: string) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      responseType: 'blob',
    })
      .then((response) => {
        const ext = url.split('.')[1]
        // 若文件名不以后缀结尾,则手动添加后缀
        resolve({
          name: name,
          data: response.data
        })
      })
      .catch((error) => {
        reject(error.toString())
      })
  })
}
 
function dateChange (value: any) {
  searchQuery.startTime = value[0]
  searchQuery.endTime = value[1]
  getFiles()
}
 
function subFileTypeChange (value: any) {
  searchQuery.subFileType = value.join(',')
  getFiles()
}
 
function payloadChange (value: any) {
  searchQuery.payload = value.join(',')
  getFiles()
}
 
function inputChange (searchValue: string) {
  getFiles()
}
 
function getFiles () {
  getMediaFiles(workspaceId, body, searchQuery).then(res => {
    mediaData.data = res.data.list
    paginationProp.total = res.data.pagination.total
    paginationProp.current = res.data.pagination.page
  })
}
 
function refreshData (page: Pagination) {
  body.page = page?.current!
  body.page_size = page?.pageSize!
  getFiles()
}
 
function downloadMedia (media: MediaFile) {
  loading.value = true
  const url = prefix + '/' + media.object_key
  getFile(url, media.file_name).then(res => {
    downloadFile(res.data, res.name)
    loading.value = false
  })
 
  // downloadMediaFile(workspaceId, media.file_id).then(res => {
  //     if (!res) {
  //         return
  //     }
  //
  //     const data = new Blob([url])
  //     downloadFile(data, media.file_name)
  //     loading.value = false
  // }).finally(() => {
  //     loading.value = false
  // })
}
 
</script>
 
<style lang="scss" scoped>
.media-panel-wrapper {
    width: 100%;
    padding: 16px;
 
    .media-table {
        background: #fff;
        margin-top: 10px;
    }
 
    .action-area {
        color: $primary;
        cursor: pointer;
    }
}
 
.header {
    width: 100%;
    height: 60px;
    background: #fff;
    padding: 16px;
    font-size: 20px;
    font-weight: bold;
    text-align: start;
    color: #000;
}
 
.search-panel-wrapper {
    height: 85px;
    background: #ffffff;
    padding: 0 20px;
    display: flex;
    align-items: center;
 
    .search-part {
        margin-right: 10px;
    }
 
}
 
.button-wrapper {
    background: #ffffff;
    padding-left: 20px;
}
 
.editable-row-operations {
    div > span {
        margin-right: 10px;
    }
}
 
.modal {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
 
    #videoPlayerId {
        width: 70%;
        height: 80%;
    }
}
</style>