config.vue 5.89 KB
<template>
  <collapse-item name="三维配置" :expanded="true">
    <setting-item-box name="文件上传" :alone="true">
      <setting-item :name="fileSizeMsg">
        <n-upload
          :max="1"
          :customRequest="customRequest"
          :onBeforeUpload="beforeUploadHandle"
          :default-file-list="defaultFileList"
        >
          <n-button> 上传文件</n-button>
        </n-upload>
      </setting-item>
    </setting-item-box>
    <setting-item-box name="静态路径">
      <setting-item>
        <n-select
          @change="handlePathChange"
          style="width: 12vw"
          v-model:value="optionData.dataset"
          size="small"
          :options="pathList"
          :render-label="renderOption"
        ></n-select>
      </setting-item>
    </setting-item-box>
    <setting-item-box name="属性配置">
      <setting-item name="场景色(需要这种格式HEX #000000,否则失效)">
        <n-color-picker size="small" :show-alpha="false" v-model:value="optionData.backgroundColor"></n-color-picker>
      </setting-item>
      <SettingItem name="场景透明度">
        <n-input-number :min="0" :max="1" v-model:value="optionData.backgroundAlpha" />
      </SettingItem>
      <SettingItem name="启用阻尼">
        <n-switch v-model:value="optionData.enableDamping" size="small" />
      </SettingItem>
      <SettingItem v-if="optionData.enableDamping" name="阻尼值">
        <n-input-number v-model:value="optionData.dampingFactor" :min="0" :max="1" size="small"></n-input-number>
      </SettingItem>
      <SettingItem name="启用动画">
        <n-switch v-model:value="optionData.autoPlay" size="small" />
      </SettingItem>
      <SettingItem name="输出编码">
        <n-select v-model:value="optionData.outputEncoding" size="small" :options="encodinghList"></n-select>
      </SettingItem>
      <SettingItem name="是否清空场景">
        <n-switch @change="handleChange" v-model:value="optionData.clearScene" size="small" />
      </SettingItem>
    </setting-item-box>
  </collapse-item>
</template>

<script setup lang="ts">
import { PropType, h, ref, nextTick } from 'vue'
import { option } from './config'
import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
import { NEllipsis, NInputNumber, NSelect, NSpace, SelectOption, NCard } from 'naive-ui'
import { uploadFile } from '@/api/external/contentSave/content'
import { downloadFile } from '@/api/external/common'
import { UploadCustomRequestOptions } from 'naive-ui'

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

const defaultFileList = ref([])

//文件限制大小
const fileSizeConst = ref(15)

const handleChange = (e: boolean) => {
  if (e) props.optionData.dataset = ''
}

const handlePathChange = () => {
  props.optionData.clearScene = false
}

const encodinghList = [
  { label: 'linear', value: 'linear' },
  { label: 'sRGB ', value: 'sRGB ' }
]

const pathList = [
  {
    label: '猫模型',
    value: 'src/assets/external/models/fbx/cat.fbx',
    type: 'fbx'
  },
  {
    label: '枪拖把模型',
    value: 'src/assets/external/models/gltf/Sniper_Stand.glb',
    type: 'gltf'
  },
  {
    label: '飞机模型',
    value: 'src/assets/external/models/obj/11803_Airplane_v1_l1.obj',
    type: 'obj'
  },
  {
    label: '踏月桥模型',
    value: 'src/assets/external/models/stl/踏月桥.stl',
    type: 'stl'
  },
  {
    label: '龙模型',
    value: 'src/assets/external/models/collada/Dragon 2.5_dae.dae',
    type: 'dae'
  },
  {
    label: '战斗机模型',
    value: 'src/assets/external/models/ply/Intergalactic_Spaceship-(Ply).ply',
    type: 'ply'
  },
  {
    label: '正方体模型',
    value: 'src/assets/external/models/json/lightmap.json',
    type: 'json'
  }
]

/**
 * 三维文件格式,vue-3d-loader是这个插件支持的格式,其他格式待解决
 */
const threeSupportFileFormat = ['fbx', 'obj', 'gltf', 'stl', 'dae', 'glb', 'ply', 'json']

const renderOption = (option: SelectOption) => {
  const threeSupportType = threeSupportFileFormat.find(item => item === option.type)
  return h(NSpace, { justify: 'space-between', style: 'padding: 0 15px; height: 28px; line-height: 28px;' }, () => [
    h(NEllipsis, null, () => threeSupportType),
    h(NEllipsis, null, () => option.label)
  ])
}

const fileSizeMsg = ref(`文件需小于 ${fileSizeConst.value}M ,格式为${threeSupportFileFormat.join(',')}的文件`)

const extname = (filename: string) => {
  if (!filename || typeof filename != 'string') {
    return false
  }
  let a = filename.split('').reverse().join('')
  let b = a.substring(0, a.search(/\./)).split('').reverse().join('')
  return b
}

// 上传图片前置处理
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
//@ts-ignore
const beforeUploadHandle = async ({ file }) => {
  defaultFileList.value = []
  const type = extname(file.file.name) as string
  const size = file.file.size
  if (size / (1024 * 1024) > fileSizeConst.value) {
    window['$message'].warning(`文件超出 ${fileSizeConst.value}M限制,请重新上传!`)
    return false
  }
  if (!threeSupportFileFormat.includes(type)) {
    window['$message'].warning('文件格式不符合,请重新上传!')
    return false
  }
  return true
}

// 自定义上传操作
const customRequest = (options: UploadCustomRequestOptions) => {
  const { file } = options
  nextTick(async () => {
    if (file.file) {
      const formData = new FormData()
      formData.append('file', file.file)
      const uploadRes = await uploadFile(formData)
      if (uploadRes) {
        props.optionData.dataset = uploadRes?.fileStaticUri
        const res = await downloadFile(uploadRes?.fileName)
        console.log(res)
        window['$message'].success('上传文件成功!')
      }
    } else {
      window['$message'].error('上传文件失败,请稍后重试!')
    }
  })
}
</script>

<style lang="scss" scoped></style>