Skip to content

Commit cbd4e90

Browse files
authored
Merge pull request #193 from Yi-Ran6/fix-upload-bug
fix:图片上传修改
2 parents cedd4eb + c54b960 commit cbd4e90

File tree

3 files changed

+190
-66
lines changed

3 files changed

+190
-66
lines changed

src/pages/DetailInfo/question/checkbox.vue

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
</div>
1919
</div>
2020
<div class="divider" />
21-
<div ref="scrollContainer" class="flex-col p-5 overflow-y-auto h-180 mt-10" style="scroll-behavior: smooth;">
21+
<div class="flex-col p-5 overflow-y-auto h-180 mt-10" style="scroll-behavior: smooth;">
2222
<div v-for="item in localOptions" :key="item.serialNum" class="my-5">
2323
<div class="flex items-center gap-10">
24-
<input type="checkbox" :name="item.serialNum" class="checkbox-sm my-5">
24+
<input type="checkbox" :name="`${item.serialNum}`" class="checkbox-sm my-5">
2525
<input
2626
v-model="item.content"
2727
type="text"
@@ -30,7 +30,12 @@
3030
>
3131
<div class="ml-10 flex items-center gap-20">
3232
<div v-if="item.img" class="mt-4">
33-
<img :src="item.img" alt="Preview" style="max-width: 50px; max-height: 50px;">
33+
<img
34+
:key="item.img"
35+
:src="item.img"
36+
alt="Preview"
37+
style="max-width: 50px; max-height: 50px;"
38+
>
3439
</div>
3540
<input
3641
v-if="isActive"
@@ -58,7 +63,7 @@
5863
</template>
5964

6065
<script setup lang="ts">
61-
import { ref, watch, defineProps, defineEmits } from "vue";
66+
import { ref, watch } from "vue";
6267
import { useRequest } from "vue-hooks-plus";
6368
import { saveImgAPI } from "@/apis";
6469
import { ElNotification } from "element-plus";
@@ -80,8 +85,17 @@ const props = defineProps<{
8085
}[]
8186
}>();
8287
83-
const scrollContainer = ref<HTMLDivElement>();
84-
const emits = defineEmits(["update:unique", "on-click", "update:otherOption", "update:optionChoose", "update:title", "update:options", "update:describe"]);
88+
const emits = defineEmits([
89+
"update:unique",
90+
"on-click",
91+
"update:otherOption",
92+
"update:optionChoose",
93+
"update:title",
94+
"update:options",
95+
"update:describe",
96+
"update:minimum_option",
97+
"update:maximum_option"
98+
]);
8599
86100
// Local copies of props to maintain reactivity
87101
const localTitle = ref<string>(props.title || "");
@@ -93,28 +107,41 @@ const localOptions = ref(props.options);
93107
const localMax = ref(props.maximum_option);
94108
const localMin = ref(props.minimum_option);
95109
96-
const handleFileChange = async (event, serialNum: number) => {
97-
const file = event.target.files[0];
110+
const handleFileChange = async (event: Event, serialNum: number) => {
111+
const input = event.target as HTMLInputElement;
112+
const file = input.files?.[0];
98113
if (!file) return;
114+
99115
const formData = new FormData();
100116
formData.append("img", file);
117+
118+
const currentOption = localOptions.value?.find(item => item.serialNum === serialNum);
119+
if (!currentOption) return;
120+
121+
const originalImg = currentOption.img || "";
122+
101123
useRequest(() => saveImgAPI(formData), {
102-
onSuccess(res) {
103-
const option = localOptions.value.find(item => item.serialNum === serialNum);
104-
if (option) {
105-
option.img = res.data;
124+
onSuccess(res: any) {
125+
if (res.code === 200) {
126+
127+
currentOption.img = res.data;
128+
ElNotification.success("上传图片成功");
129+
} else {
130+
throw new Error(res.msg || "上传失败");
106131
}
107-
ElNotification.success("上传图片成功");
108132
},
109-
onError(error) {
110-
ElNotification.error("上传图片失败" + error);
133+
onError(error: any) {
134+
135+
currentOption.img = originalImg;
136+
input.value = "";
137+
ElNotification.error("上传图片失败:" + (error.message || error));
111138
}
112139
});
113140
};
114141
115142
const deleteOption = (serialNum: number) => {
116-
localOptions.value = localOptions.value.filter(item => item.serialNum !== serialNum);
117-
localOptions.value.forEach((item) => {
143+
localOptions.value = localOptions.value?.filter(item => item.serialNum !== serialNum);
144+
localOptions.value?.forEach((item) => {
118145
if (item.serialNum > serialNum) {
119146
item.serialNum -= 1;
120147
}

src/pages/DetailInfo/question/radio.vue

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
</div>
1919
</div>
2020
<div class="divider" />
21-
<div ref="scrollContainer" class="flex-col p-5 overflow-y-auto h-180 mt-10" style="scroll-behavior: smooth;">
21+
<div class="flex-col p-5 overflow-y-auto h-180 mt-10" style="scroll-behavior: smooth;">
2222
<div v-for="item in localOptions" :key="item.serialNum" class="flex items-center gap-10 my-5">
23-
<input type="radio" :name="props.serialNum" class="radio-sm my-5">
23+
<input type="radio" :name="`${props.serialNum}`" class="radio-sm my-5">
2424
<input
2525
v-model="item.content"
2626
type="text"
@@ -29,7 +29,12 @@
2929
>
3030
<div class="ml-10 flex items-center gap-20">
3131
<div v-if="item.img" class="mt-4">
32-
<img :src="item.img" alt="Preview" style="max-width: 50px; max-height: 50px;">
32+
<img
33+
:key="item.img"
34+
:src="item.img"
35+
alt="Preview"
36+
style="max-width: 50px; max-height: 50px;"
37+
>
3338
</div>
3439
<input
3540
v-if="isActive"
@@ -56,7 +61,7 @@
5661
</template>
5762

5863
<script setup lang="ts">
59-
import { ref, watch, defineProps, defineEmits } from "vue";
64+
import { ref, watch } from "vue";
6065
import { useRequest } from "vue-hooks-plus";
6166
import { saveImgAPI } from "@/apis";
6267
import { ElNotification } from "element-plus";
@@ -78,29 +83,42 @@ const props = defineProps<{
7883
7984
const emits = defineEmits(["update:unique", "on-click", "update:otherOption", "update:optionChoose", "update:title", "update:options", "update:describe"]);
8085
81-
const scrollContainer = ref<HTMLDivElement>();
8286
const localTitle = ref<string>(props.title || "");
8387
const localDescribe = ref<string>(props.describe || "");
8488
const localOptionChoose = ref<boolean>(props.optionChoose);
8589
const localUnique = ref<boolean>(props.unique);
8690
const localOtherOption = ref<boolean>(props.otherOption);
8791
const localOptions = ref(props.options ? [...props.options] : []);
8892
89-
const handleFileChange = async (event, serialNum: number) => {
90-
const file = event.target.files[0];
93+
const handleFileChange = async (event: Event, serialNum: number) => {
94+
const input = event.target as HTMLInputElement;
95+
const file = input.files?.[0];
9196
if (!file) return;
97+
9298
const formData = new FormData();
9399
formData.append("img", file);
100+
101+
const currentOption = localOptions.value.find(item => item.serialNum === serialNum);
102+
if (!currentOption) return;
103+
104+
const originalImg = currentOption.img || "";
105+
94106
useRequest(() => saveImgAPI(formData), {
95-
onSuccess(res) {
96-
const option = localOptions.value.find(item => item.serialNum === serialNum);
97-
if (option) {
98-
option.img = res.data;
107+
onSuccess(res: any) {
108+
if (res.code === 200) {
109+
110+
currentOption.img = res.data;
111+
ElNotification.success("上传图片成功");
112+
} else {
113+
114+
throw new Error(res.msg || "上传失败");
99115
}
100-
ElNotification.success("上传图片成功");
101116
},
102-
onError(error) {
103-
ElNotification.error("上传图片失败" + error);
117+
onError(error: any) {
118+
119+
currentOption.img = originalImg;
120+
input.value = "";
121+
ElNotification.error("上传图片失败:" + (error.message || error));
104122
}
105123
});
106124
};

0 commit comments

Comments
 (0)