config.vue 4.14 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 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 { NInputNumber, NSelect } from 'naive-ui'
import { uploadFile } from '@/api/external/contentSave/content'
import { UploadCustomRequestOptions } from 'naive-ui'

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

const defaultFileList = ref([])

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

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

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

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

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


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
        window['$message'].success('上传文件成功!')
      }
    } else {
      window['$message'].error('上传文件失败,请稍后重试!')
    }
  })
}
</script>

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