无人机管理后台前端(已迁走)
张含笑
2025-09-01 2ca94de8ede18ac07ccfd8dec7b6f6a707adde9b
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
<template>
  <basic-container>
    <avue-crud :option="option" :table-loading="loading" :data="data" v-model:page="page" :permission="permissionList"
      v-model="form" ref="crud" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @on-load="onLoad">
      <template #menu-left>
        <el-button type="primary" icon="el-icon-download" plain :loading="isDownloading" @click="handleZipDownload">{{
          isDownloading ? '压缩下载中...' : '压缩下载' }}</el-button>
      </template>
      <template #menu="scope">
        <el-button type="primary" text icon="el-icon-view" v-if="permission.attach_delete"
          @click="handleRowView(scope.row)">查看
        </el-button>
        <el-button type="success" text icon="el-icon-download" v-if="permission.attach_delete"
          @click="handleRowDownload(scope.row)">下载
        </el-button>
        <el-button type="danger" text icon="el-icon-delete" v-if="permission.attach_delete"
          @click="handleRowDelete(scope.row)">删除
        </el-button>
      </template>
    </avue-crud>
    <el-dialog title="媒体文件" append-to-body v-model="taskBox" width="555px">
      <avue-form ref="form" :option="taskOption" v-model="taskForm" :upload-after="uploadAfter">
      </avue-form>
    </el-dialog>
  </basic-container>
</template>
 
<script>
import JSZip from 'jszip'
import { getMedia, deleteAllFile } from '@/api/resource/media'
import { getAirportList } from '@/api/device/device'
import { mapGetters } from 'vuex'
import func from '@/utils/func'
 
export default {
  data () {
    const endDate = new Date()
    const startDate = new Date()
    startDate.setMonth(startDate.getMonth() - 1)
    const defaultStartDate = `${startDate.getFullYear()}-${String(startDate.getMonth() + 1).padStart(2, '0')}-${String(startDate.getDate()).padStart(2, '0')}`
    const defaultEndDate = `${endDate.getFullYear()}-${String(endDate.getMonth() + 1).padStart(2, '0')}-${String(endDate.getDate()).padStart(2, '0')}`
 
    return {
      form: {},
      query: {
        daterange: [defaultStartDate, defaultEndDate]
      },
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      taskBox: false,
      selectionList: [],
      selectedWorkspaceId: null,
      option: {
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        dialogClickModal: false,
        delBtn: false,
 
        height: 'auto',
        calcHeight: 20,
        column: [
          { label: '机场选择', prop: 'airportNickname', type: 'select', search: true, searchSpan: 5, dicData: [], props: { label: 'nickname', value: 'nickname' }, hide: true, change: (value) => {
               this.searchChange({ ...this.query,airportNickname:value.value}, () => {});
          } },
          { label: '创建日期', prop: 'daterange', type: 'daterange', search: true, searchRange: true, searchSpan: 6, format: 'YYYY-MM-DD', valueFormat: 'YYYY-MM-DD', startPlaceholder: '开始时间', endPlaceholder: '结束时间', viewDisplay: false, hide: true, searchValue: [defaultStartDate, defaultEndDate] 
           , change: (value) => {
               this.searchChange({ ...this.query,daterange:value.value}, () => {});
          }
          },
          { label: '任务名称', prop: 'job_name' },
          { label: '文件名称', prop: 'file_name' },
          { label: '拍摄时间', prop: 'create_time' },
          { label: '文件路径', prop: 'object_key' },
          { label: '设备负载', prop: 'payload' },
        ],
      },
      data: [],
      taskForm: {},
      taskOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          { label: '任务上传', prop: 'taskFile', type: 'upload', drag: true, loadText: '任务上传中,请稍等', span: 24, propsHttp: { res: 'data' }, action: '/blade-resource/oss/endpoint/put-file-task' },
        ],
      },
      domainUrl: '',
      isDownloading: false, // 控制按钮加载状态
    }
  },
  computed: {
    func () { return func },
    ...mapGetters(['permission']),
    permissionList () {
      return {
        addBtn: false,
        editBtn: false,
        viewBtn: this.validData(this.permission.attach_view, false),
        delBtn: this.validData(this.permission.attach_delete, false),
      }
    },
    ids () { return this.selectionList.map(ele => ele.id).join(',') },
  },
  mounted () {
    this.loadAirportList()
    this.$refs.crud.setSearch({ daterange: this.query.daterange })
    this.onLoad(this.page, this.query)
  },
  methods: {
    loadAirportList () {
      getAirportList().then(res => {
        const airportData = res.data.data || []
        const airportColumn = this.option.column.find(col => col.prop === 'airportNickname')
        if (airportColumn) {
          airportColumn.dicData = airportData.map(item => ({
            nickname: item.nickname,
            workspace_id: item.workspace_id,
          }))
        }
      }).catch(error => {
        console.error('加载机场列表时出错:', error)
      })
    },
    handleUpload () {
      this.taskBox = true
    },
    uploadAfter (res, done) {
      this.taskBox = false
      this.refreshChange()
      done()
    },
    async handleRowDownload (row) {
      const mediaUrl = this.domainUrl + row.object_key
      try {
        const response = await fetch(mediaUrl)
        if (!response.ok) throw new Error('文件下载失败')
        const blob = await response.blob()
        const url = window.URL.createObjectURL(blob)
        const link = document.createElement('a')
        link.href = url
        link.download = row.file_name || 'download'
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(url)
        this.$message({ type: 'success', message: '文件下载成功: ' + row.file_name })
      } catch (error) {
        console.error('下载文件时出错:', error)
        this.$message({ type: 'error', message: '下载失败: ' + error.message })
      }
    },
    handleRowDelete (row) {
      this.$confirm('确定删除此文件?', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
        .then(() => deleteAllFile(row.id))
        .then(() => {
          this.onLoad(this.page)
          this.$message({ type: 'success', message: '删除成功!' })
        })
        .catch(error => {
          console.error('删除文件时出错:', error)
          this.$message({ type: 'error', message: '删除失败,请稍后重试!' })
        })
    },
    rowDel (row) {
      this.$confirm('确定将选择数据及对应的文件删除?', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' })
        .then(() => deleteAllFile(row.id))
        .then(() => {
          this.onLoad(this.page)
          this.$message({ type: 'success', message: '操作成功!' })
        })
    },
    searchReset () {
      const endDate = new Date()
      const startDate = new Date()
      startDate.setMonth(startDate.getMonth() - 1)
      const defaultStartDate = `${startDate.getFullYear()}-${String(startDate.getMonth() + 1).padStart(2, '0')}-${String(startDate.getDate()).padStart(2, '0')}`
      const defaultEndDate = `${endDate.getFullYear()}-${String(endDate.getMonth() + 1).padStart(2, '0')}-${String(endDate.getDate()).padStart(2, '0')}`
      this.query = { daterange: [defaultStartDate, defaultEndDate] }
      this.selectedWorkspaceId = null
      this.$refs.crud.setSearch({ daterange: this.query.daterange })
      this.onLoad(this.page, this.query)
    },
    searchChange (params, done) {
      this.query = params
      this.page.currentPage = 1
      const airportColumn = this.option.column.find(col => col.prop === 'airportNickname')
      const selectedAirport = airportColumn.dicData.find(item => item.nickname === params.airportNickname)
      this.selectedWorkspaceId = selectedAirport ? selectedAirport.workspace_id : null
      this.onLoad(this.page, params)
      done()
    },
    selectionChange (list) {
      this.selectionList = list
    },
    selectionClear () {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage
      this.onLoad(this.page, this.query)
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize
      this.onLoad(this.page, this.query)
    },
    refreshChange () {
      this.onLoad(this.page, this.query)
    },
    onLoad (page, params = {}) {
      this.loading = true
      const { daterange } = params
      let create_time = null
      let end_time = null
 
      if (daterange && Array.isArray(daterange) && daterange.length === 2) {
        const startStr = daterange[0]
        const endStr = daterange[1]
        const startDate = new Date(startStr + ' 00:00:00').getTime()
        const endDate = new Date(endStr + ' 23:59:59').getTime()
        if (!isNaN(startDate) && !isNaN(endDate)) {
          create_time = startDate
          end_time = endDate
        }
      }
 
      let values = { ...params, ...this.query }
      if (daterange) values.daterange = null
 
      getMedia(page.currentPage, page.pageSize, this.selectedWorkspaceId || null, create_time, end_time)
        .then(res => {
          const data = res.data
          if (data && data.data && data.data.list) {
            this.page.total = data.data.pagination.total
            this.data = data.data.list.map(item => ({
              job_name: item.job_name,
              file_name: item.file_name,
              create_time: this.formatCreateTime(item.create_time),
              object_key: item.object_key,
              payload: item.payload,
              airportNickname: item.airportNickname,
              id: item.id,
            }))
            this.domainUrl = data.data.list.length > 0 ? data.data.list[0].domain_url : 'http://localhost:2888/manage/resource'
          } else {
            this.data = []
          }
          this.loading = false
          this.selectionClear()
          this.$refs.crud.refreshTable()
        })
        .catch(error => {
          this.loading = false
          console.error('加载媒体数据时出错:', error)
        })
    },
    formatCreateTime (createTime) {
      if (typeof createTime !== 'number' || isNaN(createTime)) return ''
      const timestamp = createTime < 10000000000 ? createTime * 1000 : createTime
      const date = new Date(timestamp)
      if (isNaN(date.getTime())) return ''
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      const seconds = String(date.getSeconds()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
    },
    handleRowView (row) {
      const mediaUrl = this.domainUrl + row.object_key
      const mediaType = this.getMediaType(row.object_key)
      if (mediaType === 'image' || mediaType === 'video') {
        window.open(mediaUrl, '_blank')
      }
    },
    getMediaType (objectKey) {
      const extension = objectKey.split('.').pop().toLowerCase()
      if (['jpg', 'jpeg', 'png', 'gif'].includes(extension)) return 'image'
      if (['mp4', 'avi', 'mov'].includes(extension)) return 'video'
      return 'unknown'
    },
    showVideo (url) {
      this.taskBox = true
      this.taskForm = { videoUrl: url }
      this.taskOption = {
        submitBtn: false,
        emptyBtn: false,
        column: [
          { label: '视频播放', prop: 'videoUrl', type: 'video', span: 24, props: { controls: true, autoplay: false } },
        ],
      }
    },
    validData (val, def) {
      return typeof val !== 'undefined' ? val : def
    },
    async handleZipDownload () {
      if (this.selectionList.length === 0) {
        this.$message({ type: 'warning', message: '请先选择需要下载的文件' })
        return
      }
 
      this.isDownloading = true
 
      try {
        const zip = new JSZip()
        const files = this.selectionList.map(item => ({
          url: this.domainUrl + item.object_key,
          name: item.file_name
        }))
 
        const fetchPromises = files.map(async file => {
          const response = await fetch(file.url, { cache: 'force-cache' })
          if (!response.ok) throw new Error(`下载文件 ${file.name} 失败`)
          const blob = await response.blob()
          zip.file(file.name, blob)
        })
 
        await Promise.all(fetchPromises)
 
        const content = await zip.generateAsync({
          type: 'blob',
          compression: 'DEFLATE',
          compressionOptions: { level: 1 }
        })
 
        const url = window.URL.createObjectURL(content)
        const link = document.createElement('a')
        link.href = url
        link.download = `media_files_${new Date().toISOString().slice(0, 10)}_${new Date().getTime()}.zip`
        document.body.appendChild(link)
        link.click()
        document.body.removeChild(link)
        window.URL.revokeObjectURL(url)
 
        this.$message({ type: 'success', message: `成功下载 ${this.selectionList.length} 个文件` })
      } catch (error) {
        console.error('压缩下载失败:', error)
        this.$message({ type: 'error', message: '压缩下载失败: ' + error.message })
      } finally {
        this.isDownloading = false
      }
    },
  },
}
</script>
 
<style scoped lang="scss"></style>