Commit aff15756d4a5231f8aea2dc4dbc1737b5b73110d

Authored by xp.Huang
2 parents 2f3df181 10a6ff0e

Merge branch 'ww' into 'main_dev'

fix(backgrounImage): 修复背景图片不能上传

See merge request yunteng/thingskit-view!37
1 import { defHttp } from "@/utils/external/http/axios"; 1 import { defHttp } from "@/utils/external/http/axios";
2 -import { DictItem } from "./model"; 2 +import { DictItem, UploadResponse } from "./model";
3 3
4 enum Api { 4 enum Api {
5 - GET_DICT = '/dict_item' 5 + GET_DICT = '/dict_item',
  6 + UPLOAD = '/oss/upload'
6 } 7 }
7 8
8 export const getDictItemByCode = (value: string) => { 9 export const getDictItemByCode = (value: string) => {
@@ -13,3 +14,10 @@ export const getDictItemByCode = (value: string) => { @@ -13,3 +14,10 @@ export const getDictItemByCode = (value: string) => {
13 } 14 }
14 }) 15 })
15 } 16 }
  17 +
  18 +export const upload = (file: FormData) => {
  19 + return defHttp.post<UploadResponse>({
  20 + url: Api.UPLOAD,
  21 + params: file
  22 + })
  23 +}
@@ -8,3 +8,11 @@ export interface DictItem { @@ -8,3 +8,11 @@ export interface DictItem {
8 createTime: string; 8 createTime: string;
9 updateTime: string; 9 updateTime: string;
10 } 10 }
  11 +
  12 +export interface UploadResponse {
  13 + fileName: string
  14 + fileDownloadUri: string
  15 + fileType: string
  16 + size: number
  17 + fileStaticUri: string
  18 +}
  1 +import { SelectOption } from "naive-ui"
  2 +import { computed } from "vue"
  3 +
  4 +const isHref = (url: string) => {
  5 + try {
  6 + new URL(url)
  7 + return true
  8 + } catch (error) {
  9 + return false
  10 + }
  11 +}
  12 +
  13 +export const useBackgroundSelect = () => {
  14 +
  15 +
  16 + const getAllBackgroundImageList = computed(() => {
  17 + const pathList = import.meta.glob('../../assets/external/background/*')
  18 + return Object.keys(pathList).map(item => {
  19 + const imgName = item.split('/').at(-1)
  20 + return {
  21 + label: imgName,
  22 + value: imgName
  23 + } as SelectOption
  24 + })
  25 + })
  26 +
  27 + const getBackgroundImagePath = (name: string) => {
  28 + return isHref(name) ? name : new URL(`../../assets/external/background/${name}`, import.meta.url).href
  29 + }
  30 +
  31 + return {
  32 + getAllBackgroundImageList,
  33 + getBackgroundImagePath
  34 + }
  35 +
  36 +}
  37 +
1 -import { useGlobSetting } from "@/hooks/external/setting"  
2 -import { SelectOption } from "naive-ui"  
3 -import { ProjectRuntimeEnvEnum } from "../../../build/external/envEnum"  
4 -  
5 -export const useSyncAssetsFile = () => {  
6 - const { publicPath } = useGlobSetting()  
7 - const staticDir = 'static'  
8 - const isDev = [ProjectRuntimeEnvEnum.DEV, ProjectRuntimeEnvEnum.DEV_ALONE].includes(import.meta.env.MODE as ProjectRuntimeEnvEnum)  
9 - const jpgModules = import.meta.glob('../../assets/**/*.jpg')  
10 - const pngModules = import.meta.glob('../../assets/**/*.png')  
11 - const imagesModules = { ...jpgModules, ...pngModules }  
12 -  
13 - const getFileInfo = (filePath: string) => {  
14 - const filePathArr = filePath.split('/')  
15 - const fileName = filePathArr.at(-1)  
16 - const fileExt = fileName?.split('.').at(-1)  
17 - return { fileName, fileExt }  
18 - }  
19 -  
20 - const buildFilePath = (fileExt: string, fileName: string) => {  
21 - return `${publicPath}${staticDir}/${fileExt}/${fileName}`  
22 - }  
23 -  
24 - const setPath = (filePath: string) => {  
25 - if (isDev && filePath) {  
26 - const { fileExt, fileName } = getFileInfo(filePath)  
27 - return buildFilePath(fileExt!, fileName!)  
28 - }  
29 - return filePath  
30 - }  
31 -  
32 - const getPath = (filePath: string) => {  
33 - if (isDev && filePath) {  
34 - const { fileName } = getFileInfo(filePath)  
35 - const imagesKeys = Object.keys(imagesModules)  
36 - const path = imagesKeys.find(key => key.includes(fileName!))  
37 - return `/src/${path?.replaceAll('../', '')}`  
38 - }  
39 -  
40 - return filePath ?? ''  
41 - }  
42 -  
43 - const transformPath = (filesPath: string[]) => {  
44 - const _filesPath: SelectOption[] = []  
45 -  
46 - for (const file of filesPath) {  
47 - const { fileExt, fileName } = getFileInfo(file)  
48 - _filesPath.push({  
49 - label: fileName,  
50 - value: buildFilePath(fileExt!, fileName!)  
51 - })  
52 - }  
53 -  
54 - return _filesPath  
55 - }  
56 -  
57 - return { setPath, getPath, transformPath }  
58 -}  
@@ -2,27 +2,23 @@ @@ -2,27 +2,23 @@
2 import { EditCanvasConfigEnum } from '@/store/modules/chartEditStore/chartEditStore.d'; 2 import { EditCanvasConfigEnum } from '@/store/modules/chartEditStore/chartEditStore.d';
3 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore'; 3 import { useChartEditStore } from '@/store/modules/chartEditStore/chartEditStore';
4 import { NEllipsis, NImage, NSelect, NSpace, NText, SelectOption } from 'naive-ui'; 4 import { NEllipsis, NImage, NSelect, NSpace, NText, SelectOption } from 'naive-ui';
5 -import { computed, h } from 'vue';  
6 -import { useSyncAssetsFile } from '@/utils/external/useSyncAssetsFile'; 5 +import { computed, h, unref } from 'vue';
  6 +import { useBackgroundSelect } from '@/utils/external/useBackgroundImageSelect';
7 7
8 const chartEditStore = useChartEditStore() 8 const chartEditStore = useChartEditStore()
9 const canvasConfig = chartEditStore.getEditCanvasConfig 9 const canvasConfig = chartEditStore.getEditCanvasConfig
10 10
11 -const { transformPath, setPath, getPath } = useSyncAssetsFile()  
12 -const getSelectBgOptions = computed(() => {  
13 - const imagesModules = import.meta.globEager('@/assets/external/background/*')  
14 - return transformPath(Object.keys(imagesModules))  
15 -}) 11 +const { getAllBackgroundImageList, getBackgroundImagePath } = useBackgroundSelect()
16 12
17 const renderOption = (option: SelectOption) => { 13 const renderOption = (option: SelectOption) => {
18 return h(NSpace, { justify: 'space-between', style: 'padding: 0 15px; height: 28px; line-height: 28px;' }, () => [ 14 return h(NSpace, { justify: 'space-between', style: 'padding: 0 15px; height: 28px; line-height: 28px;' }, () => [
19 - h(NImage, { width: 50, src: getPath(option.value as string), previewDisabled: true, class: 'select-image' } as Recordable), 15 + h(NImage, { width: 50, src: getBackgroundImagePath(option.value as string), previewDisabled: true, style: { height: '28px' } } as Recordable),
20 h(NEllipsis, null, () => option.label) 16 h(NEllipsis, null, () => option.label)
21 ]) 17 ])
22 } 18 }
23 19
24 const handleUpdateValue = (value: string, _option: SelectOption) => { 20 const handleUpdateValue = (value: string, _option: SelectOption) => {
25 - chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.BACKGROUND_IMAGE, setPath(value)) 21 + chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.BACKGROUND_IMAGE, value)
26 } 22 }
27 23
28 </script> 24 </script>
@@ -32,7 +28,7 @@ const handleUpdateValue = (value: string, _option: SelectOption) => { @@ -32,7 +28,7 @@ const handleUpdateValue = (value: string, _option: SelectOption) => {
32 <NSpace> 28 <NSpace>
33 <NText>背景选择</NText> 29 <NText>背景选择</NText>
34 <NSelect size="small" placeholder="请选择您要使用的背景" clearable style="width: 250px" :value="canvasConfig.backgroundImage" 30 <NSelect size="small" placeholder="请选择您要使用的背景" clearable style="width: 250px" :value="canvasConfig.backgroundImage"
35 - :options="getSelectBgOptions" :render-label="renderOption" @update-value="handleUpdateValue" /> 31 + :options="getAllBackgroundImageList" :render-label="renderOption" @update-value="handleUpdateValue" />
36 </NSpace> 32 </NSpace>
37 </template> 33 </template>
38 34
  1 +import { upload } from "@/api/external/common"
  2 +import { EditCanvasConfigEnum } from "@/store/modules/chartEditStore/chartEditStore.d"
  3 +import { useChartEditStore } from "@/store/modules/chartEditStore/chartEditStore"
  4 +
  5 +export const useUploadBackgroundImg = async (file: File) => {
  6 + const formData = new FormData()
  7 + const chartEditStore = useChartEditStore()
  8 + formData.set('file', file)
  9 + const { fileStaticUri } = await upload(formData)
  10 + chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.BACKGROUND_IMAGE, fileStaticUri)
  11 + chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, false)
  12 +}
@@ -30,7 +30,7 @@ @@ -30,7 +30,7 @@
30 :onBeforeUpload="beforeUploadHandle" 30 :onBeforeUpload="beforeUploadHandle"
31 > 31 >
32 <n-upload-dragger> 32 <n-upload-dragger>
33 - <img v-if="canvasConfig.backgroundImage" class="upload-show" :src="canvasConfig.backgroundImage" alt="背景" /> 33 + <img v-if="canvasConfig.backgroundImage" class="upload-show" :src="getBackgroundImagePath(canvasConfig.backgroundImage)" alt="背景" />
34 <div class="upload-img" v-show="!canvasConfig.backgroundImage"> 34 <div class="upload-img" v-show="!canvasConfig.backgroundImage">
35 <img src="@/assets/images/canvas/noImage.png" /> 35 <img src="@/assets/images/canvas/noImage.png" />
36 <n-text class="upload-desc" depth="3"> 36 <n-text class="upload-desc" depth="3">
@@ -142,8 +142,9 @@ import { icon } from '@/plugins' @@ -142,8 +142,9 @@ import { icon } from '@/plugins'
142 142
143 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致 143 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致
144 import SelectBackgroundImage from './external/SelectBackgroundImage.vue' 144 import SelectBackgroundImage from './external/SelectBackgroundImage.vue'
145 -import { useSyncAssetsFile } from '@/utils/external/useSyncAssetsFile'  
146 -const { getPath } = useSyncAssetsFile() 145 +import { useBackgroundSelect } from '@/utils/external/useBackgroundImageSelect'
  146 +import { useUploadBackgroundImg } from './external/useUploadBackground'
  147 +const { getBackgroundImagePath } = useBackgroundSelect()
147 148
148 const { ColorPaletteIcon } = icon.ionicons5 149 const { ColorPaletteIcon } = icon.ionicons5
149 const { ScaleIcon, FitToScreenIcon, FitToHeightIcon, FitToWidthIcon } = icon.carbon 150 const { ScaleIcon, FitToScreenIcon, FitToHeightIcon, FitToWidthIcon } = icon.carbon
@@ -275,9 +276,11 @@ const customRequest = (options: UploadCustomRequestOptions) => { @@ -275,9 +276,11 @@ const customRequest = (options: UploadCustomRequestOptions) => {
275 const { file } = options 276 const { file } = options
276 nextTick(() => { 277 nextTick(() => {
277 if (file.file) { 278 if (file.file) {
278 - const ImageUrl = fileToUrl(file.file)  
279 - chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.BACKGROUND_IMAGE, ImageUrl)  
280 - chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, false) 279 + // THINGS_KIT 上传图片逻辑修改
  280 + useUploadBackgroundImg(file.file)
  281 + // const ImageUrl = fileToUrl(file.file)
  282 + // chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.BACKGROUND_IMAGE, ImageUrl)
  283 + // chartEditStore.setEditCanvasConfig(EditCanvasConfigEnum.SELECT_COLOR, false)
281 } else { 284 } else {
282 window['$message'].error('添加图片失败,请稍后重试!') 285 window['$message'].error('添加图片失败,请稍后重试!')
283 } 286 }
@@ -106,7 +106,7 @@ import { EditRule } from './components/EditRule' @@ -106,7 +106,7 @@ import { EditRule } from './components/EditRule'
106 import { EditBottom } from './components/EditBottom' 106 import { EditBottom } from './components/EditBottom'
107 import { EditShapeBox } from './components/EditShapeBox' 107 import { EditShapeBox } from './components/EditShapeBox'
108 import { EditTools } from './components/EditTools' 108 import { EditTools } from './components/EditTools'
109 -import { useSyncAssetsFile } from '@/utils/external/useSyncAssetsFile' 109 +import { useBackgroundSelect } from '@/utils/external/useBackgroundImageSelect'
110 110
111 const chartEditStore = useChartEditStore() 111 const chartEditStore = useChartEditStore()
112 const { handleContextMenu } = useContextMenu() 112 const { handleContextMenu } = useContextMenu()
@@ -164,7 +164,7 @@ const filterShow = computed(() => { @@ -164,7 +164,7 @@ const filterShow = computed(() => {
164 164
165 // 背景 165 // 背景
166 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致 166 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致
167 -const { getPath } = useSyncAssetsFile() 167 +const { getBackgroundImagePath } = useBackgroundSelect()
168 const rangeStyle = computed(() => { 168 const rangeStyle = computed(() => {
169 // 设置背景色和图片背景 169 // 设置背景色和图片背景
170 const background = chartEditStore.getEditCanvasConfig.background 170 const background = chartEditStore.getEditCanvasConfig.background
@@ -176,8 +176,7 @@ const rangeStyle = computed(() => { @@ -176,8 +176,7 @@ const rangeStyle = computed(() => {
176 ? { background: backgroundColor } 176 ? { background: backgroundColor }
177 // : { background: `url(${backgroundImage}) no-repeat center center / cover !important` } 177 // : { background: `url(${backgroundImage}) no-repeat center center / cover !important` }
178 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致 178 // THINGS_KIT 路径转换,同步生产环境与开发环境的保存的静态资源文件路径不一致
179 - : { background: `url(${getPath(backgroundImage!)}) no-repeat center center / cover !important` }  
180 - // @ts-ignore 179 + : { background: `url(${getBackgroundImagePath(backgroundImage!)}) no-repeat center center / cover !important` }
181 return { 180 return {
182 ...computedBackground, 181 ...computedBackground,
183 width: 'inherit', 182 width: 'inherit',