CVIDraw.vue
10.2 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
<template>
<div class="card">
<Card :bordered="false" class="card">
<BasicForm @register="registerForm">
<template #logoUpload>
<ContentUploadText>
<template #uploadImg>
<Upload
name="avatar"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
:customRequest="customUploadLogoPic"
:before-upload="beforeUploadLogoPic"
>
<img v-if="logoPic" :src="logoPic" alt="avatar" />
<div v-else>
<Spin v-if="loading" tip="正在上传中..." />
<PlusOutlined v-else style="font-size: 2.5rem" /> </div
></Upload>
</template>
<template #uploadText>
<div class="flex justify-center items-center">
支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过500KB
</div>
</template>
</ContentUploadText>
</template>
<template #iconUpload>
<ContentUploadText>
<template #uploadImg>
<Upload
name="avatar"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
:customRequest="customUploadIconPic"
:before-upload="beforeUploadIconPic"
>
<div v-if="iconPic">
<img :src="iconPic" class="m-auto" />
<div style="background-color: #ccc; margin-top: 20px">重新上传</div>
</div>
<div v-else>
<div>
<Spin v-if="loading1" tip="正在上传中..." />
<PlusOutlined v-else style="font-size: 2.5rem" />
</div> </div
></Upload>
</template>
<template #uploadText>
<div class="flex justify-center items-center"> 支持.ICON格式,建议尺寸为16*16px </div>
</template>
</ContentUploadText>
</template>
<template #bgUpload>
<ContentUploadText>
<template #uploadImg>
<Upload
name="avatar"
list-type="picture-card"
class="avatar-uploader"
:show-upload-list="false"
:customRequest="customUploadBgPic"
:before-upload="beforeUploadBgPic"
>
<img v-if="bgPic" :src="bgPic" alt="avatar" />
<div v-else>
<div>
<Spin v-if="loading2" tip="正在上传中..." />
<PlusOutlined v-else style="font-size: 2.5rem" />
</div> </div
></Upload>
</template>
<template #uploadText>
<div class="flex justify-center items-center">
支持.PNG、.JPG格式,建议尺寸为1920*1080px以上,大小不超过5M
</div>
</template>
</ContentUploadText>
</template>
<template #colorInput="{ model, field }"
><Input disabled v-model:value="model[field]">
<template #prefix> <input type="color" v-model="model[field]" /> </template
></Input>
</template>
</BasicForm>
</Card>
<Loading v-bind="compState" />
<Authority value="api:yt:platform:update:update">
<a-button @click="handleUpdateInfo" type="primary" class="mt-4">保存信息</a-button>
</Authority>
<Authority value="api:yt:platform:data_reset:reset">
<a-button @click="handleResetInfo" type="primary" class="ml-4">恢复默认设置</a-button>
</Authority>
</div>
</template>
<script lang="ts">
import { defineComponent, ref, onMounted, unref } from 'vue';
import { Card, Upload, Input, Spin } from 'ant-design-vue';
import { BasicForm, useForm } from '/@/components/Form/index';
import { schemas } from '../config/CVIDraw.config';
import { Loading } from '/@/components/Loading/index';
import { useMessage } from '/@/hooks/web/useMessage';
import type { FileItem } from '/@/components/Upload/src/typing';
import {
logoUpload,
iconUpload,
bgUpload,
getPlatForm,
updatePlatForm,
resetPlateInfo,
} from '/@/api/oem/index';
import { PlusOutlined } from '@ant-design/icons-vue';
import { useUserStore } from '/@/store/modules/user';
import { createLocalStorage } from '/@/utils/cache/index';
import { Authority } from '/@/components/Authority';
import ContentUploadText from './ContentUploadText.vue';
export default defineComponent({
components: {
BasicForm,
Card,
Loading,
Upload,
Input,
PlusOutlined,
Authority,
ContentUploadText,
Spin,
},
setup() {
const loading = ref(false);
const loading1 = ref(false);
const loading2 = ref(false);
const compState = ref({
absolute: false,
loading: false,
tip: '拼命加载中...',
});
const { createMessage } = useMessage();
const userStore = useUserStore();
const storage = createLocalStorage();
const [registerForm, { getFieldsValue, setFieldsValue, resetFields }] = useForm({
schemas,
showSubmitButton: false,
showResetButton: false,
labelWidth: 150,
wrapperCol: {
span: 10,
},
});
const logoPic = ref();
const iconPic = ref();
const bgPic = ref();
// logo图片上传
async function customUploadLogoPic({ file }) {
if (beforeUploadLogoPic(file)) {
logoPic.value = '';
loading.value = true;
const formData = new FormData();
formData.append('file', file);
const response = await logoUpload(formData);
if (response.fileStaticUri) {
logoPic.value = response.fileStaticUri;
loading.value = false;
}
}
}
const beforeUploadLogoPic = (file: FileItem) => {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
createMessage.error('只能上传图片文件!');
}
const isLt2M = (file.size as number) / 1024 < 500;
if (!isLt2M) {
createMessage.error('图片大小不能超过500KB!');
}
return isJpgOrPng && isLt2M;
};
// Icon上传
async function customUploadIconPic({ file }) {
if (beforeUploadIconPic(file)) {
iconPic.value = '';
loading1.value = true;
const formData = new FormData();
formData.append('file', file);
const response = await iconUpload(formData);
if (response.fileStaticUri) {
iconPic.value = response.fileStaticUri;
loading1.value = false;
}
}
}
const beforeUploadIconPic = (file: FileItem) => {
const isJpgOrPng = file.type === 'image/x-icon';
if (!isJpgOrPng) {
createMessage.error('只能上传.icon图片文件!');
}
const isLt2M = (file.size as number) / 1024 < 500;
if (!isLt2M) {
createMessage.error('图片大小不能超过500KB!');
}
return isJpgOrPng && isLt2M;
};
// 登录页背景上传
async function customUploadBgPic({ file }) {
if (beforeUploadBgPic(file)) {
bgPic.value = '';
loading2.value = true;
const formData = new FormData();
formData.append('file', file);
const response = await bgUpload(formData);
if (response.fileStaticUri) {
bgPic.value = response.fileStaticUri;
loading2.value = false;
}
}
}
const beforeUploadBgPic = (file: FileItem) => {
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
if (!isJpgOrPng) {
createMessage.error('只能上传图片文件!');
}
const isLt2M = (file.size as number) / 1024 / 1024 < 5;
if (!isLt2M) {
createMessage.error('图片大小不能超过5MB!');
}
return isJpgOrPng && isLt2M;
};
// 更新
const handleUpdateInfo = async () => {
try {
const fieldValue = getFieldsValue();
compState.value.loading = true;
const newFieldValue = {
...fieldValue,
logo: unref(logoPic),
icon: unref(iconPic),
background: unref(bgPic),
};
await updatePlatForm(newFieldValue);
compState.value.loading = false;
createMessage.success('保存信息成功');
setPlatFormInfo(newFieldValue);
} catch (e) {
createMessage.error('保存信息失败');
}
};
// 设置平台信息
function setPlatFormInfo(newFieldValue) {
// 保存store
userStore.setPlatInfo(newFieldValue);
// 保存本地缓存
storage.set('platformInfo', newFieldValue);
}
onMounted(async () => {
const res = await getPlatForm();
setFieldsValue(res);
logoPic.value = res.logo;
iconPic.value = res.icon;
bgPic.value = res.background;
});
const handleResetInfo = async () => {
try {
compState.value.loading = true;
await resetPlateInfo();
compState.value.loading = false;
createMessage.success('恢复出厂设置成功');
resetFields();
const res = await getPlatForm();
setFieldsValue(res);
logoPic.value = res.logo;
iconPic.value = res.icon;
bgPic.value = res.background;
} catch (e) {
compState.value.loading = false;
createMessage.error('恢复出厂设置失败');
}
};
return {
registerForm,
logoPic,
iconPic,
bgPic,
customUploadLogoPic,
beforeUploadLogoPic,
customUploadIconPic,
beforeUploadIconPic,
customUploadBgPic,
beforeUploadBgPic,
compState,
handleUpdateInfo,
loading,
loading1,
loading2,
handleResetInfo,
};
},
});
</script>
<style lang="less" scoped></style>