hookehuyr

refactor: 优化文件上传逻辑并移除未使用的代码

重构文件上传组件,添加实时上传功能并显示进度
移除未使用的组件导入和类型声明
将保存按钮文案改为"提交"以更准确表达功能
...@@ -14,7 +14,6 @@ declare module 'vue' { ...@@ -14,7 +14,6 @@ declare module 'vue' {
14 NutActionSheet: typeof import('@nutui/nutui-taro')['ActionSheet'] 14 NutActionSheet: typeof import('@nutui/nutui-taro')['ActionSheet']
15 NutButton: typeof import('@nutui/nutui-taro')['Button'] 15 NutButton: typeof import('@nutui/nutui-taro')['Button']
16 NutDatePicker: typeof import('@nutui/nutui-taro')['DatePicker'] 16 NutDatePicker: typeof import('@nutui/nutui-taro')['DatePicker']
17 - NutDialog: typeof import('@nutui/nutui-taro')['Dialog']
18 NutImagePreview: typeof import('@nutui/nutui-taro')['ImagePreview'] 17 NutImagePreview: typeof import('@nutui/nutui-taro')['ImagePreview']
19 NutInput: typeof import('@nutui/nutui-taro')['Input'] 18 NutInput: typeof import('@nutui/nutui-taro')['Input']
20 NutPicker: typeof import('@nutui/nutui-taro')['Picker'] 19 NutPicker: typeof import('@nutui/nutui-taro')['Picker']
......
1 <template> 1 <template>
2 <view class="min-h-screen flex flex-col bg-white"> 2 <view class="min-h-screen flex flex-col bg-white">
3 - <!-- <AppHeader title="加入家庭" /> -->
4 <view class="flex-1 px-4 pt-3 pb-6 flex flex-col"> 3 <view class="flex-1 px-4 pt-3 pb-6 flex flex-col">
5 <!-- Title --> 4 <!-- Title -->
6 <h2 class="text-xl font-bold text-center mb-2"> 5 <h2 class="text-xl font-bold text-center mb-2">
...@@ -168,7 +167,6 @@ ...@@ -168,7 +167,6 @@
168 import { ref, computed, nextTick, onMounted, watch } from 'vue'; 167 import { ref, computed, nextTick, onMounted, watch } from 'vue';
169 import Taro from '@tarojs/taro'; 168 import Taro from '@tarojs/taro';
170 import { My, Check } from '@nutui/icons-vue-taro'; 169 import { My, Check } from '@nutui/icons-vue-taro';
171 -import AppHeader from '../../components/AppHeader.vue';
172 170
173 const mottoChars = ref(['', '', '', '']); 171 const mottoChars = ref(['', '', '', '']);
174 const selectedRole = ref(''); 172 const selectedRole = ref('');
......
...@@ -84,7 +84,7 @@ ...@@ -84,7 +84,7 @@
84 @tap="saveMedia" 84 @tap="saveMedia"
85 class="flex-1 bg-blue-500 text-white py-3 rounded-lg text-center" 85 class="flex-1 bg-blue-500 text-white py-3 rounded-lg text-center"
86 > 86 >
87 - 保存 87 + 提交
88 </view> 88 </view>
89 </view> 89 </view>
90 <view class="mt-6 text-sm text-gray-500"> 90 <view class="mt-6 text-sm text-gray-500">
...@@ -176,7 +176,7 @@ const chooseMedia = () => { ...@@ -176,7 +176,7 @@ const chooseMedia = () => {
176 maxDuration: 60, 176 maxDuration: 60,
177 sizeType: ['compressed'], 177 sizeType: ['compressed'],
178 camera: 'back', 178 camera: 'back',
179 - success: (res) => { 179 + success: async (res) => {
180 const file = res.tempFiles[0]; 180 const file = res.tempFiles[0];
181 181
182 // 检查文件大小(10MB限制) 182 // 检查文件大小(10MB限制)
...@@ -189,23 +189,48 @@ const chooseMedia = () => { ...@@ -189,23 +189,48 @@ const chooseMedia = () => {
189 return; 189 return;
190 } 190 }
191 191
192 - // 根据文件类型设置不同的信息 192 + // 显示上传进度
193 - if (file.fileType === 'image') { 193 + Taro.showLoading({ title: '上传中...' });
194 - uploadedFile.value = { 194 +
195 - type: 'image', 195 + try {
196 - url: file.tempFilePath, 196 + // 立即上传文件到服务器
197 - size: file.size, 197 + const serverUrl = await uploadFileToServer(file.tempFilePath);
198 - name: `image_${Date.now()}.jpg` 198 +
199 - }; 199 + // 根据文件类型设置不同的信息,包含服务器URL
200 - } else if (file.fileType === 'video') { 200 + if (file.fileType === 'image') {
201 - uploadedFile.value = { 201 + uploadedFile.value = {
202 - type: 'video', 202 + type: 'image',
203 - url: file.tempFilePath, 203 + url: file.tempFilePath,
204 - thumbnail: file.thumbTempFilePath, 204 + serverUrl: serverUrl,
205 - duration: Math.floor(file.duration), 205 + size: file.size,
206 - size: file.size, 206 + name: `image_${Date.now()}.jpg`
207 - name: `video_${Date.now()}.mp4` 207 + };
208 - }; 208 + } else if (file.fileType === 'video') {
209 + uploadedFile.value = {
210 + type: 'video',
211 + url: file.tempFilePath,
212 + serverUrl: serverUrl,
213 + thumbnail: file.thumbTempFilePath,
214 + duration: Math.floor(file.duration),
215 + size: file.size,
216 + name: `video_${Date.now()}.mp4`
217 + };
218 + }
219 +
220 + Taro.hideLoading();
221 + Taro.showToast({
222 + title: '上传成功',
223 + icon: 'success',
224 + duration: 1500
225 + });
226 + } catch (error) {
227 + console.error('上传失败:', error);
228 + Taro.hideLoading();
229 + Taro.showToast({
230 + title: '上传失败,请重试',
231 + icon: 'error',
232 + duration: 2000
233 + });
209 } 234 }
210 }, 235 },
211 fail: (err) => { 236 fail: (err) => {
...@@ -372,22 +397,38 @@ const saveMedia = async () => { ...@@ -372,22 +397,38 @@ const saveMedia = async () => {
372 return; 397 return;
373 } 398 }
374 399
400 + if (!uploadedFile.value.serverUrl) {
401 + Taro.showToast({
402 + title: '文件上传未完成,请重新选择',
403 + icon: 'error',
404 + duration: 2000
405 + });
406 + return;
407 + }
408 +
375 Taro.showLoading({ 409 Taro.showLoading({
376 - title: '上传中...', 410 + title: '保存中...',
377 mask: true 411 mask: true
378 }); 412 });
379 413
380 try { 414 try {
381 - // 上传文件到服务器 415 + // TODO: 这里预留给后续的接口调用
382 - const serverUrl = await uploadFileToServer(uploadedFile.value.url); 416 + // 调用后端接口保存媒体信息,传递 uploadedFile.value.serverUrl
383 - 417 + // const result = await saveMediaToBackend({
384 - // 更新文件信息,保存服务器返回的URL 418 + // type: uploadedFile.value.type,
385 - uploadedFile.value.serverUrl = serverUrl; 419 + // url: uploadedFile.value.serverUrl,
420 + // size: uploadedFile.value.size,
421 + // name: uploadedFile.value.name,
422 + // duration: uploadedFile.value.duration // 仅视频有此字段
423 + // });
424 +
425 + // 模拟接口调用
426 + await new Promise(resolve => setTimeout(resolve, 1000));
386 427
387 Taro.hideLoading(); 428 Taro.hideLoading();
388 Taro.showToast({ 429 Taro.showToast({
389 title: '保存成功,获得积分奖励!', 430 title: '保存成功,获得积分奖励!',
390 - icon: 'success', 431 + icon: 'none',
391 duration: 2000 432 duration: 2000
392 }); 433 });
393 434
...@@ -399,10 +440,10 @@ const saveMedia = async () => { ...@@ -399,10 +440,10 @@ const saveMedia = async () => {
399 // Taro.navigateBack(); 440 // Taro.navigateBack();
400 }, 2000); 441 }, 2000);
401 } catch (error) { 442 } catch (error) {
402 - console.error('上传失败:', error); 443 + console.error('保存失败:', error);
403 Taro.hideLoading(); 444 Taro.hideLoading();
404 Taro.showToast({ 445 Taro.showToast({
405 - title: '上传失败,请重试', 446 + title: '保存失败,请重试',
406 icon: 'error', 447 icon: 'error',
407 duration: 2000 448 duration: 2000
408 }); 449 });
......