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"
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"
5863</template >
5964
6065<script setup lang="ts">
61- import { ref , watch , defineProps , defineEmits } from " vue" ;
66+ import { ref , watch } from " vue" ;
6267import { useRequest } from " vue-hooks-plus" ;
6368import { saveImgAPI } from " @/apis" ;
6469import { 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
87101const localTitle = ref <string >(props .title || " " );
@@ -93,28 +107,41 @@ const localOptions = ref(props.options);
93107const localMax = ref (props .maximum_option );
94108const 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
115142const 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 }
0 commit comments