config.vue
4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<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>