无人机管理后台前端(已迁走)
罗广辉
2025-10-11 02409bfbe15f22fc3b5dccadabfd860a660a49d9
src/views/resource/media.vue
@@ -4,9 +4,16 @@
      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)">删除
@@ -21,358 +28,331 @@
</template>
<script>
import { getMedia, deleteAllFile } from '@/api/media/media'; // 导入媒体相关的 API 方法
import { getAirportList } from '@/api/device/device';        // 导入设备相关的 API 方法
import { mapGetters } from 'vuex';                           // 从 Vuex 导入 mapGetters
import func from '@/utils/func';                             // 导入工具函数
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() {
  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: {},          // 搜索查询条件
      loading: true,      // 表格加载状态
      page: {             // 分页数据
        pageSize: 10,     // 每页显示数量
        currentPage: 1,   // 当前页码
        total: 0,         // 总记录数
      form: {},
      query: {
        daterange: [defaultStartDate, defaultEndDate]
      },
      taskBox: false,     // 控制弹窗显示状态
      selectionList: [],  // 选中的行数据列表
      selectedWorkspaceId: null, // 选中的工作区 ID
      option: {           // Avue CRUD 表格配置
        height: 'auto',   // 表格高度自动调整
        calcHeight: 32,   // 计算高度的偏移量
        tip: false,       // 不显示提示
        searchShow: true, // 显示搜索栏
        searchMenuSpan: 6,// 搜索栏宽度占比
        border: true,     // 显示边框
        index: true,      // 显示序号列
        viewBtn: true,    // 显示查看按钮
        selection: true,  // 允许多选
        dialogClickModal: false, // 对话框点击遮罩不关闭
        delBtn: false,    // 不显示默认删除按钮
        column: [         // 表格列配置
          {
            label: '机场选择',       // 列标题
            prop: 'airportNickname', // 数据字段
            type: 'select',         // 下拉选择类型
            search: true,           // 可搜索
            searchSpan: 5,          // 搜索栏宽度占比
            dicData: [],            // 下拉选项数据
            props: {                // 下拉选项映射
              label: 'nickname',
              value: 'nickname',
            },
            hide: true,             // 默认隐藏
      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: 'daterange',      // 数据字段
            type: 'daterange',      // 日期范围选择类型
            search: true,           // 可搜索
            searchRange: true,      // 支持范围搜索
            searchSpan: 6,          // 搜索栏宽度占比
            format: 'YYYY-MM-DD',   // 显示格式
            valueFormat: 'YYYY-MM-DD', // 值格式
            startPlaceholder: '开始时间', // 开始时间占位符
            endPlaceholder: '结束时间',   // 结束时间占位符
            viewDisplay: false,     // 查看时不显示
            hide: true,             // 默认隐藏
            value: [new Date(new Date().setMonth(new Date().getMonth() - 1)), new Date()], // 默认值:过去一个月至今
          },
          { label: '任务名称', prop: 'job_name' },    // 任务名称列
          { label: '文件名称', prop: 'file_name' },   // 文件名称列
          { label: '拍摄时间', prop: 'create_time' }, // 拍摄时间列
          { label: '文件路径', prop: 'object_key' },  // 文件路径列
          { label: '设备负载', prop: 'payload' },     // 设备负载列
          { 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: {              // HTTP 响应映射
              res: 'data',
            },
            action: '/blade-resource/oss/endpoint/put-file-task', // 上传接口地址
          },
      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: '',    // 媒体文件域名,从 API 获取
    };
      domainUrl: '',
      isDownloading: false, // 控制按钮加载状态
    }
  },
  computed: {
    func() {           // 计算属性,返回工具函数
      return func;
    },
    ...mapGetters(['permission']), // 从 Vuex 获取权限数据
    permissionList() { // 计算权限列表
    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), // 删除权限
      };
        addBtn: false,
        editBtn: false,
        viewBtn: this.validData(this.permission.attach_view, false),
        delBtn: this.validData(this.permission.attach_delete, false),
      }
    },
    ids() {            // 计算选中的行 ID 列表
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(',');
    },
    ids () { return this.selectionList.map(ele => ele.id).join(',') },
  },
  mounted() {         // 组件挂载时执行
    this.loadAirportList(); // 加载机场列表
    this.onLoad(this.page); // 加载初始数据
  mounted () {
    this.loadAirportList()
    this.$refs.crud.setSearch({ daterange: this.query.daterange })
    this.onLoad(this.page, this.query)
  },
  methods: {
    // 加载机场下拉列表数据
    loadAirportList() {
    loadAirportList () {
      getAirportList().then(res => {
        const airportData = res.data.data || [];
        const airportColumn = this.option.column.find(col => col.prop === 'airportNickname');
        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();               // 完成上传
    },
    // 删除单行数据
    handleRowDelete(row) {
      this.$confirm('确定删除此文件?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        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(() => {
          return deleteAllFile(row.id); // 调用删除 API
        })
        .then(() => {
          this.onLoad(this.page);       // 重新加载数据
          this.$message({
            type: 'success',
            message: '删除成功!',
          });
          this.onLoad(this.page)
          this.$message({ type: 'success', message: '删除成功!' })
        })
        .catch(error => {
          console.error('删除文件时出错:', error);
          this.$message({
            type: 'error',
            message: '删除失败,请稍后重试!',
          });
        });
    },
    // 删除行(由 avue-crud 触发)
    rowDel(row) {
      this.$confirm('确定将选择数据及对应的文件删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          return deleteAllFile(row.id); // 调用删除 API
          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: '操作成功!',
          });
        });
          this.onLoad(this.page)
          this.$message({ type: 'success', message: '操作成功!' })
        })
    },
    // 重置搜索条件
    searchReset() {
      this.query = {};
      this.selectedWorkspaceId = null;
      this.onLoad(this.page);
    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();
    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;
    selectionChange (list) {
      this.selectionList = list
    },
    // 清空选择
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    selectionClear () {
      this.selectionList = []
      this.$refs.crud.toggleSelection()
    },
    // 处理页码变化
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page, this.query);
    currentChange (currentPage) {
      this.page.currentPage = currentPage
      this.onLoad(this.page, this.query)
    },
    // 处理每页数量变化
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      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);
    refreshChange () {
      this.onLoad(this.page, this.query)
    },
    // 从 API 加载表格数据
    onLoad(page, params = {}) {
      this.loading = true;
      const { daterange } = params;
      let create_time = this.create_time;
      let end_time = this.end_time;
    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();
        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;
          create_time = startDate
          end_time = endDate
        }
      }
      let values = {
        ...params,
        ...this.query,
      };
      if (daterange) {
        values.daterange = null;
      }
      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);
      });
      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}`;
    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; // 拼接完整媒体 URL
      const mediaType = this.getMediaType(row.object_key); // 判断媒体类型
      if (mediaType === 'image') {
        window.open(mediaUrl, '_blank'); // 图片在新窗口打开
      } else if (mediaType === 'video') {
        // 临时测试:视频也在新窗口打开
        window.open(mediaUrl, '_blank');
        // this.showVideo(mediaUrl);        // 原逻辑:视频在弹窗中播放
    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'; // 是图片
      } else if (['mp4', 'avi', 'mov'].includes(extension)) {
        return 'video'; // 是视频
      }
      return 'unknown'; // 未知类型
    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,     // 设置视频 URL
      };
      this.taskOption = {  // 配置视频播放选项
    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, // 不自动播放
            },
          },
          { label: '视频播放', prop: 'videoUrl', type: 'video', span: 24, props: { controls: true, autoplay: false } },
        ],
      };
      }
    },
    // 检查权限有效性(假设这是定义在其他地方的工具方法)
    validData(val, def) {
      return typeof val !== 'undefined' ? val : def;
    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></style>
<style scoped lang="scss"></style>