Commit 4d64538776859eba2c3f397ea524241b05989ede

Authored by fengwotao
1 parent 8b700968

pref:优化企业定制关于上传图片的重复代码

... ... @@ -6,52 +6,21 @@
6 6 <template #logoUpload>
7 7 <ContentUploadText>
8 8 <template #uploadImg>
9   - <Upload
10   - name="avatar"
11   - list-type="picture-card"
12   - class="avatar-uploader"
13   - :show-upload-list="false"
14   - @preview="handlePreview"
15   - :customRequest="customUploadLogoPic"
16   - :before-upload="beforeUploadLogoPic"
17   - >
18   - <img v-if="logoPic" class="fill-img" :src="logoPic" alt="avatar" />
19   - <div v-else>
20   - <Spin v-if="loading" tip="正在上传中..." />
21   - <PlusOutlined v-else />
22   - <div class="ant-upload-text">上传</div>
23   - </div>
24   - </Upload>
  9 + <CustomUploadComp :imgUrl="logoPic" @setImg="handleSetLogoImgUrl" />
25 10 </template>
26 11 <template #uploadText>
27   - <div class="box-outline">
28   - 支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过500KB
29   - </div>
  12 + <div class="box-outline"> 支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过5M </div>
30 13 </template>
31 14 </ContentUploadText>
32 15 </template>
33 16 <template #bgUpload>
34 17 <ContentUploadText>
35 18 <template #uploadImg>
36   - <Upload
37   - name="avatar"
38   - list-type="picture-card"
39   - class="avatar-uploader"
40   - :show-upload-list="false"
41   - :customRequest="customUploadBgPic"
42   - :before-upload="beforeUploadBgPic"
43   - >
44   - <img v-if="bgPic" class="fill-img" :src="bgPic" alt="avatar" />
45   - <div v-else>
46   - <Spin v-if="loading1" tip="正在上传中..." />
47   - <PlusOutlined v-else />
48   - <div class="ant-upload-text">上传</div>
49   - </div>
50   - </Upload>
  19 + <CustomUploadComp :imgUrl="bgPic" @setImg="handleSetBgImgUrl" />
51 20 </template>
52 21 <template #uploadText>
53 22 <div class="box-outline">
54   - 支持.PNG、.JPG格式,建议尺寸为1920*1080px,大小不超过2M
  23 + 支持.PNG、.JPG格式,建议尺寸为1920*1080px,大小不超过5M
55 24 </div>
56 25 </template>
57 26 </ContentUploadText>
... ... @@ -83,7 +52,7 @@
83 52 </template>
84 53 <template #uploadText>
85 54 <div class="box-outline">
86   - 支持.PNG、.JPG格式,建议尺寸为800*600px,大小不超过3M
  55 + 支持.PNG、.JPG格式,建议尺寸为800*600px,大小不超过5M
87 56 </div>
88 57 </template>
89 58 </ContentUploadText>
... ... @@ -108,15 +77,16 @@
108 77 import { defineComponent, ref, unref, onMounted } from 'vue';
109 78 import { BasicForm, useForm } from '/@/components/Form/index';
110 79 import { Loading } from '/@/components/Loading/index';
111   - import { Card, Upload, Input, Modal, Spin } from 'ant-design-vue';
  80 + import { Card, Upload, Input, Modal } from 'ant-design-vue';
112 81 import { PlusOutlined } from '@ant-design/icons-vue';
113 82 import { schemas } from '../config/AppDraw.config';
114 83 import { FileItem, FileInfo } from '../types/index';
115   - import { logoUpload, bgUpload, resetAppInfo } from '/@/api/oem/index';
  84 + import { bgUpload, resetAppInfo } from '/@/api/oem/index';
116 85 import { useMessage } from '/@/hooks/web/useMessage';
117 86 import { getAppDesign, updateAppDesign } from '/@/api/oem/index';
118 87 import { Authority } from '/@/components/Authority';
119 88 import ContentUploadText from './ContentUploadText.vue';
  89 + import { CustomUploadComp } from './customUplaod/index';
120 90
121 91 export default defineComponent({
122 92 components: {
... ... @@ -129,7 +99,7 @@
129 99 Modal,
130 100 Authority,
131 101 ContentUploadText,
132   - Spin,
  102 + CustomUploadComp,
133 103 },
134 104 setup() {
135 105 const loading = ref(false);
... ... @@ -170,59 +140,20 @@
170 140 previewVisible.value = true;
171 141 };
172 142
  143 + const handleSetBgImgUrl = (d) => {
  144 + bgPic.value = d;
  145 + };
  146 +
  147 + const handleSetLogoImgUrl = (d) => {
  148 + logoPic.value = d;
  149 + };
  150 +
173 151 // logo图片上传
174 152 const logoPic = ref();
175   - async function customUploadLogoPic({ file }) {
176   - if (beforeUploadLogoPic(file)) {
177   - logoPic.value = '';
178   - loading.value = true;
179   - const formData = new FormData();
180   - formData.append('file', file);
181   - const response = await logoUpload(formData);
182   - if (response.fileStaticUri) {
183   - logoPic.value = response.fileStaticUri;
184   - loading.value = false;
185   - }
186   - }
187   - }
188   - const beforeUploadLogoPic = (file) => {
189   - const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
190   - if (!isJpgOrPng) {
191   - createMessage.error('只能上传图片文件!');
192   - }
193   - const isLt2M = (file.size as number) / 1024 < 500;
194   - if (!isLt2M) {
195   - createMessage.error('图片大小不能超过500KB!');
196   - }
197   - return isJpgOrPng && isLt2M;
198   - };
199 153
200 154 // 登录页背景上传
201 155 const bgPic = ref();
202   - async function customUploadBgPic({ file }) {
203   - if (beforeUploadBgPic(file)) {
204   - bgPic.value = '';
205   - loading1.value = true;
206   - const formData = new FormData();
207   - formData.append('file', file);
208   - const response = await bgUpload(formData);
209   - if (response.fileStaticUri) {
210   - bgPic.value = response.fileStaticUri;
211   - loading1.value = false;
212   - }
213   - }
214   - }
215   - const beforeUploadBgPic = (file) => {
216   - const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
217   - if (!isJpgOrPng) {
218   - createMessage.error('只能上传图片文件!');
219   - }
220   - const isLt2M = (file.size as number) / 1024 / 1024 < 2;
221   - if (!isLt2M) {
222   - createMessage.error('图片大小不能超过2MB!');
223   - }
224   - return isJpgOrPng && isLt2M;
225   - };
  156 +
226 157 // 首页轮播图
227 158 const fileList = ref<FileItem[]>([]);
228 159 async function customUploadHomeSwiper({ file }) {
... ... @@ -337,10 +268,6 @@
337 268 handleUpdateInfo,
338 269 handleCancel,
339 270 handlePreview,
340   - customUploadLogoPic,
341   - beforeUploadLogoPic,
342   - customUploadBgPic,
343   - beforeUploadBgPic,
344 271 customUploadHomeSwiper,
345 272 beforeUploadHomeSwiper,
346 273 handleChange,
... ... @@ -351,6 +278,8 @@
351 278 loading,
352 279 loading1,
353 280 handleResetInfo,
  281 + handleSetBgImgUrl,
  282 + handleSetLogoImgUrl,
354 283 };
355 284 },
356 285 });
... ... @@ -371,9 +300,4 @@
371 300 border: 1px dashed #d9d9d9;
372 301 text-align: left;
373 302 }
374   -
375   - .fill-img {
376   - width: 100%;
377   - height: 100%;
378   - }
379 303 </style>
... ...
... ... @@ -6,26 +6,10 @@
6 6 <template #logoUpload>
7 7 <ContentUploadText>
8 8 <template #uploadImg>
9   - <Upload
10   - name="avatar"
11   - list-type="picture-card"
12   - class="avatar-uploader"
13   - :show-upload-list="false"
14   - :customRequest="customUploadLogoPic"
15   - :before-upload="beforeUploadLogoPic"
16   - >
17   - <img class="fill-img" v-if="logoPic" :src="logoPic" alt="avatar" />
18   - <div v-else>
19   - <Spin v-if="loading" tip="正在上传中..." />
20   - <PlusOutlined v-else />
21   - <div class="ant-upload-text">上传</div>
22   - </div>
23   - </Upload>
  9 + <CustomUploadComp :imgUrl="logoPic" @setImg="handleSetLogoImgUrl" />
24 10 </template>
25 11 <template #uploadText>
26   - <div class="box-outline">
27   - 支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过500KB
28   - </div>
  12 + <div class="box-outline"> 支持.PNG、.JPG格式,建议尺寸为32*32px,大小不超过5M </div>
29 13 </template>
30 14 </ContentUploadText>
31 15 </template>
... ... @@ -61,23 +45,7 @@
61 45 <template #bgUpload>
62 46 <ContentUploadText>
63 47 <template #uploadImg>
64   - <Upload
65   - name="avatar"
66   - list-type="picture-card"
67   - class="avatar-uploader"
68   - :show-upload-list="false"
69   - :customRequest="customUploadBgPic"
70   - :before-upload="beforeUploadBgPic"
71   - >
72   - <img class="fill-img" v-if="bgPic" :src="bgPic" alt="avatar" />
73   - <div v-else>
74   - <div>
75   - <Spin v-if="loading2" tip="正在上传中..." />
76   - <PlusOutlined v-else />
77   - <div class="ant-upload-text">上传</div>
78   - </div>
79   - </div></Upload
80   - >
  48 + <CustomUploadComp :imgUrl="bgPic" @setImg="handleSetBgImgUrl" />
81 49 </template>
82 50 <template #uploadText>
83 51 <div class="box-outline">
... ... @@ -112,19 +80,13 @@
112 80 import { Loading } from '/@/components/Loading/index';
113 81 import { useMessage } from '/@/hooks/web/useMessage';
114 82 import type { FileItem } from '/@/components/Upload/src/typing';
115   - import {
116   - logoUpload,
117   - iconUpload,
118   - bgUpload,
119   - getPlatForm,
120   - updatePlatForm,
121   - resetPlateInfo,
122   - } from '/@/api/oem/index';
  83 + import { iconUpload, getPlatForm, updatePlatForm, resetPlateInfo } from '/@/api/oem/index';
123 84 import { PlusOutlined } from '@ant-design/icons-vue';
124 85 import { useUserStore } from '/@/store/modules/user';
125 86 import { createLocalStorage } from '/@/utils/cache/index';
126 87 import { Authority } from '/@/components/Authority';
127 88 import ContentUploadText from './ContentUploadText.vue';
  89 + import { CustomUploadComp } from './customUplaod/index';
128 90
129 91 export default defineComponent({
130 92 components: {
... ... @@ -137,6 +99,7 @@
137 99 Authority,
138 100 ContentUploadText,
139 101 Spin,
  102 + CustomUploadComp,
140 103 },
141 104 setup() {
142 105 const loading = ref(false);
... ... @@ -163,30 +126,13 @@
163 126 const logoPic = ref();
164 127 const iconPic = ref();
165 128 const bgPic = ref();
166   - // logo图片上传
167   - async function customUploadLogoPic({ file }) {
168   - if (beforeUploadLogoPic(file)) {
169   - logoPic.value = '';
170   - loading.value = true;
171   - const formData = new FormData();
172   - formData.append('file', file);
173   - const response = await logoUpload(formData);
174   - if (response.fileStaticUri) {
175   - logoPic.value = response.fileStaticUri;
176   - loading.value = false;
177   - }
178   - }
179   - }
180   - const beforeUploadLogoPic = (file: FileItem) => {
181   - const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
182   - if (!isJpgOrPng) {
183   - createMessage.error('只能上传图片文件!');
184   - }
185   - const isLt2M = (file.size as number) / 1024 < 500;
186   - if (!isLt2M) {
187   - createMessage.error('图片大小不能超过500KB!');
188   - }
189   - return isJpgOrPng && isLt2M;
  129 +
  130 + const handleSetBgImgUrl = (d) => {
  131 + bgPic.value = d;
  132 + };
  133 +
  134 + const handleSetLogoImgUrl = (d) => {
  135 + logoPic.value = d;
190 136 };
191 137 // Icon上传
192 138 async function customUploadIconPic({ file }) {
... ... @@ -213,32 +159,6 @@
213 159 }
214 160 return isJpgOrPng && isLt2M;
215 161 };
216   - // 登录页背景上传
217   - async function customUploadBgPic({ file }) {
218   - if (beforeUploadBgPic(file)) {
219   - bgPic.value = '';
220   - loading2.value = true;
221   - const formData = new FormData();
222   - formData.append('file', file);
223   - const response = await bgUpload(formData);
224   - if (response.fileStaticUri) {
225   - bgPic.value = response.fileStaticUri;
226   - loading2.value = false;
227   - }
228   - }
229   - }
230   - const beforeUploadBgPic = (file: FileItem) => {
231   - const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
232   - if (!isJpgOrPng) {
233   - createMessage.error('只能上传图片文件!');
234   - }
235   - const isLt2M = (file.size as number) / 1024 / 1024 < 5;
236   - if (!isLt2M) {
237   - createMessage.error('图片大小不能超过5MB!');
238   - }
239   - return isJpgOrPng && isLt2M;
240   - };
241   -
242 162 // 更新
243 163 const handleUpdateInfo = async () => {
244 164 try {
... ... @@ -296,18 +216,16 @@
296 216 logoPic,
297 217 iconPic,
298 218 bgPic,
299   - customUploadLogoPic,
300   - beforeUploadLogoPic,
301 219 customUploadIconPic,
302 220 beforeUploadIconPic,
303   - customUploadBgPic,
304   - beforeUploadBgPic,
305 221 compState,
306 222 handleUpdateInfo,
307 223 loading,
308 224 loading1,
309 225 loading2,
310 226 handleResetInfo,
  227 + handleSetBgImgUrl,
  228 + handleSetLogoImgUrl,
311 229 };
312 230 },
313 231 });
... ...
... ... @@ -5,21 +5,7 @@
5 5 <template #qrcode>
6 6 <ContentUploadText>
7 7 <template #uploadImg>
8   - <Upload
9   - name="avatar"
10   - list-type="picture-card"
11   - class="avatar-uploader"
12   - :show-upload-list="false"
13   - :customRequest="customUploadqrcodePic"
14   - :before-upload="beforeUploadqrcodePic"
15   - >
16   - <img class="fill-img" v-if="qrcodePic" :src="qrcodePic" alt="avatar" />
17   - <div v-else>
18   - <Spin v-if="loading" tip="正在上传中..." />
19   - <PlusOutlined v-else />
20   - <div class="ant-upload-text">上传</div>
21   - </div></Upload
22   - >
  8 + <CustomUploadComp :imgUrl="qrcodePic" @setImg="handleSetCodeImgUrl" />
23 9 </template>
24 10 <template #uploadText>
25 11 <div class="box-outline"> 支持.PNG、.JPG格式,建议尺寸为300*300px,大小不超过5M </div>
... ... @@ -46,7 +32,7 @@
46 32
47 33 <script lang="ts">
48 34 import { defineComponent, onMounted, ref, computed } from 'vue';
49   - import { Card, Upload, Spin } from 'ant-design-vue';
  35 + import { Card } from 'ant-design-vue';
50 36 import { BasicForm, useForm } from '/@/components/Form/index';
51 37 import { schemas, provSchemas } from '../config/enterPriseInfo.config';
52 38 import { getAreaList, getEnterPriseDetail, updateEnterPriseDetail } from '/@/api/oem/index';
... ... @@ -54,25 +40,21 @@
54 40 import { useMessage } from '/@/hooks/web/useMessage';
55 41 import { useUserStore } from '/@/store/modules/user';
56 42 import { createLocalStorage } from '/@/utils/cache';
57   - import { PlusOutlined } from '@ant-design/icons-vue';
58   - import { qrcodeUpload } from '/@/api/oem/index';
59   - import type { FileItem } from '/@/components/Upload/src/typing';
60 43 import type { CityItem, Code } from '../types';
61 44 import { Authority } from '/@/components/Authority';
62 45 import { USER_INFO_KEY } from '/@/enums/cacheEnum';
63 46 import { getAuthCache } from '/@/utils/auth';
64 47 import ContentUploadText from './ContentUploadText.vue';
  48 + import { CustomUploadComp } from './customUplaod/index';
65 49
66 50 export default defineComponent({
67 51 components: {
68 52 Card,
69 53 BasicForm,
70 54 Loading,
71   - Upload,
72   - PlusOutlined,
73 55 Authority,
74 56 ContentUploadText,
75   - Spin,
  57 + CustomUploadComp,
76 58 },
77 59 setup() {
78 60 const userInfo: any = getAuthCache(USER_INFO_KEY);
... ... @@ -119,30 +101,8 @@
119 101 const { createMessage } = useMessage();
120 102
121 103 const qrcodePic = ref();
122   - const customUploadqrcodePic = async ({ file }) => {
123   - clearValidate('qrcode');
124   - if (beforeUploadqrcodePic(file)) {
125   - qrcodePic.value = '';
126   - loading.value = true;
127   - const formData = new FormData();
128   - formData.append('file', file);
129   - const response = await qrcodeUpload(formData);
130   - if (response.fileStaticUri) {
131   - qrcodePic.value = response.fileStaticUri;
132   - loading.value = false;
133   - }
134   - }
135   - };
136   - const beforeUploadqrcodePic = (file: FileItem) => {
137   - const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
138   - if (!isJpgOrPng) {
139   - createMessage.error('只能上传图片文件!');
140   - }
141   - const isLt2M = (file.size as number) / 1024 / 1024 < 5;
142   - if (!isLt2M) {
143   - createMessage.error('图片大小不能超过5MB!');
144   - }
145   - return isJpgOrPng && isLt2M;
  104 + const handleSetCodeImgUrl = (d) => {
  105 + qrcodePic.value = d;
146 106 };
147 107 // 更新
148 108 const handleUpdateInfo = async () => {
... ... @@ -372,11 +332,10 @@
372 332 compState,
373 333 qrcodePic,
374 334 handleUpdateInfo,
375   - customUploadqrcodePic,
376   - beforeUploadqrcodePic,
377 335 registerCustomForm,
378 336 loading,
379 337 isWhereAdmin,
  338 + handleSetCodeImgUrl,
380 339 };
381 340 },
382 341 });
... ...
  1 +<script setup name="CustomUploadComp" lang="ts">
  2 + import { PlusOutlined } from '@ant-design/icons-vue';
  3 + import { Upload, Spin } from 'ant-design-vue';
  4 + import { ref, watchEffect } from 'vue';
  5 + import { useUpload } from './hooks/useUploadFile.hook';
  6 + import { logoUpload } from '/@/api/oem';
  7 +
  8 + const props = withDefaults(
  9 + defineProps<{
  10 + imgUrl: string;
  11 + }>(),
  12 + {
  13 + imgUrl: '',
  14 + }
  15 + );
  16 + const emit = defineEmits(['setImg']);
  17 +
  18 + const { beforeUploadVerify } = useUpload();
  19 +
  20 + const uploadLoading = ref(false);
  21 +
  22 + const uploadImgUrl = ref('');
  23 +
  24 + //上传图片接口
  25 + const customUpload = async ({ file }) => {
  26 + try {
  27 + if (beforeUploadVerify(file)) {
  28 + uploadImgUrl.value = '';
  29 + uploadLoading.value = true;
  30 + const formData = new FormData();
  31 + formData.append('file', file);
  32 + const response = await logoUpload(formData);
  33 + if (response.fileStaticUri) {
  34 + uploadImgUrl.value = response.fileStaticUri;
  35 + emit('setImg', uploadImgUrl.value);
  36 + }
  37 + }
  38 + } finally {
  39 + uploadLoading.value = false;
  40 + }
  41 + };
  42 +
  43 + watchEffect(() => {
  44 + init();
  45 + });
  46 + function init() {
  47 + if (!props.imgUrl) uploadImgUrl.value = '';
  48 + uploadImgUrl.value = props.imgUrl;
  49 + }
  50 +</script>
  51 +
  52 +<template>
  53 + <div>
  54 + <Upload
  55 + name="avatar"
  56 + list-type="picture-card"
  57 + class="avatar-uploader"
  58 + :show-upload-list="false"
  59 + :customRequest="customUpload"
  60 + :before-upload="beforeUploadVerify"
  61 + >
  62 + <img v-if="uploadImgUrl" class="fill-img" :src="uploadImgUrl" alt="avatar" />
  63 + <div v-else>
  64 + <Spin v-if="uploadLoading" tip="正在上传中..." />
  65 + <PlusOutlined v-else />
  66 + <div class="ant-upload-text">上传</div>
  67 + </div>
  68 + </Upload>
  69 + </div>
  70 +</template>
  71 +
  72 +<style lang="less" scoped>
  73 + .fill-img {
  74 + width: 100%;
  75 + height: 100%;
  76 + }
  77 +</style>
... ...
  1 +import { useMessage } from '/@/hooks/web/useMessage';
  2 +
  3 +const { createMessage } = useMessage();
  4 +
  5 +export const useUpload = () => {
  6 + //验证图片类型和大小
  7 + const beforeUploadVerify = ({ type, size }) => {
  8 + const isJpgOrPng = type === 'image/jpeg' || type === 'image/png';
  9 + if (!isJpgOrPng) {
  10 + createMessage.error('只能上传图片文件!');
  11 + }
  12 + const limitImgSize = (size as number) / 1024 / 1024 < 5;
  13 + if (!limitImgSize) {
  14 + createMessage.error('图片大小不能超过5MB!');
  15 + }
  16 + return isJpgOrPng && limitImgSize;
  17 + };
  18 + return {
  19 + beforeUploadVerify,
  20 + };
  21 +};
... ...
  1 +import CustomUploadComp from './CustomUploadComp.vue';
  2 +export { CustomUploadComp };
... ...