config.vue 6.69 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-radio-group @change="handleChecked" v-model:value="optionData.sourceType" name="radiogroup">
          <n-space>
            <n-radio v-for="(item, index) in sourceTypes" :key="item.value" :value="item.value">
              {{ item.label }}
            </n-radio>
          </n-space>
        </n-radio-group>
      </setting-item>
    </setting-item-box>
    <setting-item-box v-if="optionData.sourceType === sourceTypeEnum.CUSTOM" 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 v-if="optionData.sourceType === sourceTypeEnum.PLATFORM" name="组织" :alone="true">
      <setting-item>
        <n-tree-select
          placement="top-start"
          label-field="name"
          v-model:value="optionData.organization"
          key-field="id"
          children-field="children"
          :options="originationOption"
          @update:value="handleUpdateTreeValue"
        />
      </setting-item>
    </setting-item-box>
    <setting-item-box v-if="optionData.sourceType === sourceTypeEnum.PLATFORM" name="视频" :alone="true">
      <setting-item>
        <n-select @update:value="handleSelect" v-model:value="optionData.dataset" :options="videoOptions" />
      </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, onMounted } from 'vue'
import { option, sourceTypeEnum } from './config'
import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
import { FileTypeEnum } from '@/enums/fileTypeEnum'
import { uploadFile } from '@/api/external/contentSave/content'
import { UploadCustomRequestOptions, NTreeSelect } from 'naive-ui'
import { backgroundImageSize } from '@/settings/designSetting'
import { fetchRouteParamsLocation } from '@/utils'
import { getOrganizationList, getVideoList, getVideoUrl } from '@/api/external/common/index'

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

const uploadFileListRef = ref()

const sourceTypes = [
  {
    value: 'custom',
    label: '自定义'
  },
  {
    value: 'platform',
    label: '平台获取'
  }
]

const originationOption = ref([])

const videoOptions = ref([])

const getOriginationList = async () => {
  const res = await getOrganizationList()
  originationOption.value = res
}

const handleUpdateTreeValue = (value: string) => {
  props.optionData.dataset = ''
  getVideoLists(value)
}

const getVideoLists = async (organizationId: string) => {
  const res = await getVideoList({ organizationId })
  videoOptions.value = res?.data?.map((item: any) => ({
    label: item.name,
    value: item.accessMode === 1 ? item.id : item.videoUrl,
    id: item.id,
    accessMode: item.accessMode
  }))
}

const getVideoUrlById = async (id: string) => {
  const res = await getVideoUrl(id)
  if (!res) return
  const { url } = res.data
  props.optionData.url = url
}

const handleChecked = ({ target }: any) => {
  props.optionData.dataset = ''
  const { value } = target
  if (value === sourceTypeEnum.PLATFORM) {
    getOriginationList()
  }
}

const handleSelect = (value: string, e: any) => {
  const { accessMode, id } = e
  if (accessMode === 1) {
    getVideoUrlById(id)
  } else {
    props.optionData.url = ''
  }
}

onMounted(() => {
  if (props.optionData.sourceType === sourceTypeEnum.PLATFORM) {
    getOriginationList()
    if (props.optionData.organization) {
      getVideoLists(props.optionData.organization)
    }
  }
})

// 上传图片前置处理
// 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>