Commit 801c381e5c30781a12d28e811de29a5e5332ca87

Authored by fengwotao
1 parent f58186a6

perf(external/Componse): 重写信息下的图片

  1 +import { PublicConfigClass } from '@/packages/public'
  2 +import { CreateComponentType } from '@/packages/index.d'
  3 +import { OverrideImageConfig } from './index'
  4 +import cloneDeep from 'lodash/cloneDeep'
  5 +
  6 +export const option = {
  7 + // 图片路径
  8 + dataset: '',
  9 + // 适应方式
  10 + fit: 'fill',
  11 + // 圆角
  12 + borderRadius: 10
  13 +}
  14 +
  15 +export default class Config extends PublicConfigClass implements CreateComponentType {
  16 + public key = OverrideImageConfig.key
  17 + public chartConfig = cloneDeep(OverrideImageConfig)
  18 + public option = cloneDeep(option)
  19 +}
... ...
  1 +<template>
  2 + <collapse-item name="属性" :expanded="true">
  3 + <setting-item-box name="上传图片" :alone="true">
  4 + <setting-item>
  5 + <n-card class="upload-box">
  6 + <n-upload
  7 + :show-file-list="false"
  8 + v-model:file-list="uploadFileListRef"
  9 + :customRequest="customRequest"
  10 + :onBeforeUpload="beforeUploadHandle"
  11 + >
  12 + <n-upload-dragger>
  13 + <img v-if="optionData.dataset" class="upload-show" :src="optionData.dataset" alt="背景" />
  14 + <div class="upload-img" v-show="!optionData.dataset">
  15 + <img src="@/assets/images/canvas/noImage.png" />
  16 + <n-text class="upload-desc" depth="3">
  17 + 背景图需小于 {{ backgroundImageSize }}M ,格式为 png/jpg/gif 的文件
  18 + </n-text>
  19 + </div>
  20 + </n-upload-dragger>
  21 + </n-upload>
  22 + </n-card>
  23 + </setting-item>
  24 + </setting-item-box>
  25 + <setting-item-box name="样式">
  26 + <setting-item name="类型">
  27 + <n-select v-model:value="optionData.fit" size="small" :options="fitList"></n-select>
  28 + </setting-item>
  29 + <setting-item name="圆角">
  30 + <n-input-number
  31 + v-model:value="optionData.borderRadius"
  32 + size="small"
  33 + :min="0"
  34 + placeholder="圆角"
  35 + ></n-input-number>
  36 + </setting-item>
  37 + </setting-item-box>
  38 + </collapse-item>
  39 +</template>
  40 +
  41 +<script setup lang="ts">
  42 +import { PropType, ref, nextTick } from 'vue'
  43 +import { option } from './config'
  44 +import { CollapseItem, SettingItemBox, SettingItem } from '@/components/Pages/ChartItemSetting'
  45 +import { FileTypeEnum } from '@/enums/fileTypeEnum'
  46 +import { uploadFile } from '@/api/external/contentSave/content'
  47 +import { UploadCustomRequestOptions } from 'naive-ui'
  48 +import { backgroundImageSize } from '@/settings/designSetting'
  49 +import { fetchRouteParamsLocation } from '@/utils'
  50 +
  51 +const props = defineProps({
  52 + optionData: {
  53 + type: Object as PropType<typeof option>,
  54 + required: true
  55 + }
  56 +})
  57 +
  58 +const uploadFileListRef = ref()
  59 +
  60 +// 上传图片前置处理
  61 +//@ts-ignore
  62 +const beforeUploadHandle = async ({ file }) => {
  63 + uploadFileListRef.value = []
  64 + const type = file.file.type
  65 + const size = file.file.size
  66 +
  67 + if (size > 1024 * 1024 * backgroundImageSize) {
  68 + window['$message'].warning(`图片超出 ${backgroundImageSize}M 限制,请重新上传!`)
  69 + return false
  70 + }
  71 + if (type !== FileTypeEnum.PNG && type !== FileTypeEnum.JPEG && type !== FileTypeEnum.GIF) {
  72 + window['$message'].warning('文件格式不符合,请重新上传!')
  73 + return false
  74 + }
  75 + return true
  76 +}
  77 +
  78 +// 自定义上传操作
  79 +const customRequest = (options: UploadCustomRequestOptions) => {
  80 + const { file } = options
  81 + nextTick(async () => {
  82 + if (file.file) {
  83 + // 修改名称
  84 + const newNameFile = new File([file.file], `${fetchRouteParamsLocation()}_index_background.png`, {
  85 + type: file.file.type
  86 + })
  87 + let uploadParams = new FormData()
  88 + uploadParams.append('file', newNameFile)
  89 + const uploadRes = await uploadFile(uploadParams)
  90 + if (uploadRes) {
  91 + props.optionData.dataset = uploadRes?.fileStaticUri
  92 + window['$message'].success('添加图片成功!')
  93 + }
  94 + } else {
  95 + window['$message'].error('添加图片失败,请稍后重试!')
  96 + }
  97 + })
  98 +}
  99 +
  100 +// 适应类型
  101 +const fitList = [
  102 + {
  103 + value: 'fill',
  104 + label: 'fill'
  105 + },
  106 + {
  107 + value: 'contain',
  108 + label: 'contain'
  109 + },
  110 + {
  111 + value: 'cover',
  112 + label: 'cover'
  113 + },
  114 + {
  115 + value: 'scale-down',
  116 + label: 'scale-down'
  117 + },
  118 + {
  119 + value: 'none',
  120 + label: 'none'
  121 + }
  122 +]
  123 +</script>
  124 +<style lang="scss" scoped>
  125 +$uploadHeight: 193px;
  126 +.upload-box {
  127 + cursor: pointer;
  128 + margin-bottom: 20px;
  129 + @include deep() {
  130 + .n-card__content {
  131 + padding: 0;
  132 + overflow: hidden;
  133 + }
  134 + .n-upload-dragger {
  135 + padding: 5px;
  136 + }
  137 + }
  138 + .upload-show {
  139 + width: -webkit-fill-available;
  140 + height: $uploadHeight;
  141 + border-radius: 5px;
  142 + }
  143 + .upload-img {
  144 + display: flex;
  145 + flex-direction: column;
  146 + align-items: center;
  147 + img {
  148 + height: 150px;
  149 + }
  150 + .upload-desc {
  151 + padding: 10px 0;
  152 + }
  153 + }
  154 +}
  155 +</style>
... ...
  1 +import { ConfigType, PackagesCategoryEnum, ChartFrameEnum } from '@/packages/index.d'
  2 +import { ChatCategoryEnum, ChatCategoryEnumName } from '@/packages/components/Informations/index.d'
  3 +import { useWidgetKey } from '@/packages/external/useWidgetKey'
  4 +
  5 +const { key, conKey, chartKey } = useWidgetKey('OverrideImage', true)
  6 +
  7 +export const OverrideImageConfig: ConfigType = {
  8 + key,
  9 + chartKey,
  10 + conKey,
  11 + title: '图片2',
  12 + category: ChatCategoryEnum.MORE,
  13 + categoryName: ChatCategoryEnumName.MORE,
  14 + package: PackagesCategoryEnum.INFORMATIONS,
  15 + chartFrame: ChartFrameEnum.COMMON,
  16 + image: 'photo.png'
  17 +}
... ...
  1 +<template>
  2 + <div :style="getStyle(borderRadius)">
  3 + <n-image
  4 + :object-fit="fit"
  5 + preview-disabled
  6 + :src="!option.dataset ? logo : option.dataset"
  7 + :fallback-src="requireErrorImg()"
  8 + :width="w"
  9 + :height="h"
  10 + ></n-image>
  11 + </div>
  12 +</template>
  13 +
  14 +<script setup lang="ts">
  15 +import { PropType, shallowReactive, watch, toRefs } from 'vue'
  16 +import { requireErrorImg } from '@/utils'
  17 +import { useChartDataFetch } from '@/hooks'
  18 +import { CreateComponentType } from '@/packages/index.d'
  19 +import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'
  20 +import logo from '@/assets/logo.png'
  21 +
  22 +const props = defineProps({
  23 + chartConfig: {
  24 + type: Object as PropType<CreateComponentType>,
  25 + required: true
  26 + }
  27 +})
  28 +
  29 +const { w, h } = toRefs(props.chartConfig.attr)
  30 +const { dataset, fit, borderRadius } = toRefs(props.chartConfig.option)
  31 +
  32 +const option = shallowReactive({
  33 + dataset: ''
  34 +})
  35 +
  36 +const getStyle = (radius: number) => {
  37 + return {
  38 + borderRadius: `${radius}px`,
  39 + overflow: 'hidden'
  40 + }
  41 +}
  42 +
  43 +// 编辑更新
  44 +watch(
  45 + () => props.chartConfig.option.dataset,
  46 + (newData: any) => {
  47 + option.dataset = newData
  48 + },
  49 + {
  50 + immediate: true
  51 + }
  52 +)
  53 +
  54 +// 预览更新
  55 +useChartDataFetch(props.chartConfig, useChartEditStore, (newData: any) => {
  56 + option.dataset = newData
  57 +})
  58 +</script>
... ...
... ... @@ -2,12 +2,14 @@ import { EPackagesCategoryEnum, EPackagesType } from '@/packages/components/exte
2 2 import { ComposesList } from '@/packages/components/external/Composes'
3 3 import { ConfigType, PackagesCategoryEnum } from '@/packages/index.d'
4 4 import { ClockConfig } from '@/packages/components/external/Decorates/Mores/Icon'
  5 +import { OverrideImageConfig } from '@/packages/components/external/Informations/Mores/OverrideImage'
5 6
6 7 export function useInjectLib(packagesList: EPackagesType) {
7 8
8 9 packagesList[EPackagesCategoryEnum.COMPOSES] = ComposesList
9 10
10 11 addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.DECORATES, ClockConfig)
  12 + addWidgetToCategoryByCategoryName(packagesList, PackagesCategoryEnum.INFORMATIONS, OverrideImageConfig)
11 13 }
12 14
13 15 /**
... ...