config.vue 4.1 KB
<template>
  <collapse-item 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.dataset" class="upload-show" :src="optionData.dataset" alt="背景" />
              <div class="upload-img" v-show="!optionData.dataset">
                <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="样式">
      <setting-item name="类型">
        <n-select v-model:value="optionData.fit" size="small" :options="fitList"></n-select>
      </setting-item>
      <setting-item name="圆角">
        <n-input-number
          v-model:value="optionData.borderRadius"
          size="small"
          :min="0"
          placeholder="圆角"
        ></n-input-number>
      </setting-item>
    </setting-item-box>
  </collapse-item>
</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()

// 上传图片前置处理
//@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.dataset = uploadRes?.fileStaticUri
        window['$message'].success('添加图片成功!')
      }
    } else {
      window['$message'].error('添加图片失败,请稍后重试!')
    }
  })
}

// 适应类型
const fitList = [
  {
    value: 'fill',
    label: 'fill'
  },
  {
    value: 'contain',
    label: 'contain'
  },
  {
    value: 'cover',
    label: 'cover'
  },
  {
    value: 'scale-down',
    label: 'scale-down'
  },
  {
    value: 'none',
    label: 'none'
  }
]
</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>