config.vue 3.91 KB
<template>
  <CollapseItem name="播放器配置" :expanded="true">
    <setting-item-box name="上传图片" :alone="true">
      <setting-item>
        <n-card class="upload-box">
          <n-upload
            :show-file-list="false"
            v-model:file-list="uploadFileListRef"
            :customRequest="customRequest"
            :onBeforeUpload="beforeUploadHandle"
          >
            <n-upload-dragger>
              <img v-if="optionData.poster" class="upload-show" :src="optionData.poster" alt="背景" />
              <div class="upload-img" v-show="!optionData.poster">
                <img src="@/assets/images/canvas/noImage.png" />
                <n-text class="upload-desc" depth="3">
                  图片需小于 {{ backgroundImageSize }}M ,格式为 png/jpg/gif 的文件
                </n-text>
              </div>
            </n-upload-dragger>
          </n-upload>
        </n-card>
      </setting-item>
    </setting-item-box>
    <setting-item-box name="源地址" :alone="true">
      <setting-item>
        <n-input-group>
          <n-input v-model:value="optionData.dataset" size="small" placeholder="请输入源地址"></n-input>
        </n-input-group>
      </setting-item>
    </setting-item-box>
    <setting-item-box name="自动播放">
      <setting-item>
        <n-switch v-model:value="optionData.autoplay" size="small" />
      </setting-item>
    </setting-item-box>
  </CollapseItem>
</template>

<script setup lang="ts">
import { PropType, ref, nextTick } from 'vue'
import { option } from './config'
import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
import { FileTypeEnum } from '@/enums/fileTypeEnum'
import { uploadFile } from '@/api/external/contentSave/content'
import { UploadCustomRequestOptions } from 'naive-ui'
import { backgroundImageSize } from '@/settings/designSetting'
import { fetchRouteParamsLocation } from '@/utils'

const props = defineProps({
  optionData: {
    type: Object as PropType<typeof option>,
    required: true
  }
})

const uploadFileListRef = ref()

// 上传图片前置处理
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const beforeUploadHandle = async ({ file }) => {
  uploadFileListRef.value = []
  const type = file.file.type
  const size = file.file.size

  if (size > 1024 * 1024 * backgroundImageSize) {
    window['$message'].warning(`图片超出 ${backgroundImageSize}M 限制,请重新上传!`)
    return false
  }
  if (type !== FileTypeEnum.PNG && type !== FileTypeEnum.JPEG && type !== FileTypeEnum.GIF) {
    window['$message'].warning('文件格式不符合,请重新上传!')
    return false
  }
  return true
}

// 自定义上传操作
const customRequest = (options: UploadCustomRequestOptions) => {
  const { file } = options
  nextTick(async () => {
    if (file.file) {
      // 修改名称
      const newNameFile = new File([file.file], `${fetchRouteParamsLocation()}_index_background.png`, {
        type: file.file.type
      })
      let uploadParams = new FormData()
      uploadParams.append('file', newNameFile)
      const uploadRes = await uploadFile(uploadParams)
      if (uploadRes) {
        props.optionData.poster = uploadRes?.fileStaticUri
        window['$message'].success('添加图片成功!')
      }
    } else {
      window['$message'].error('添加图片失败,请稍后重试!')
    }
  })
}
</script>
<style lang="scss" scoped>
$uploadHeight: 193px;
.upload-box {
  cursor: pointer;
  margin-bottom: 20px;
  @include deep() {
    .n-card__content {
      padding: 0;
      overflow: hidden;
    }
    .n-upload-dragger {
      padding: 5px;
    }
  }
  .upload-show {
    width: -webkit-fill-available;
    height: $uploadHeight;
    border-radius: 5px;
  }
  .upload-img {
    display: flex;
    flex-direction: column;
    align-items: center;
    img {
      height: 150px;
    }
    .upload-desc {
      padding: 10px 0;
    }
  }
}
</style>