From 93eb6b470773bc49ea6e1a9d4cbd914eb95d525b Mon Sep 17 00:00:00 2001
From: lrj <owen.stl@gmail.com>
Date: 星期二, 30 九月 2025 17:38:04 +0800
Subject: [PATCH] feat: 完善比赛晋级功能并清理测试文件

---
 wx/pages/registration/registration.js |  230 ++++++++++++++++++++++++++++++++++++++++++++++++---------
 1 files changed, 194 insertions(+), 36 deletions(-)

diff --git a/wx/pages/registration/registration.js b/wx/pages/registration/registration.js
index 6de900a..e797579 100644
--- a/wx/pages/registration/registration.js
+++ b/wx/pages/registration/registration.js
@@ -649,18 +649,21 @@
         this.setData({ attachments: updatedAttachments })
         
         try {
-          const uploadResult = await cosUtil.uploadFile(attachment.path, {
-            onProgress: (progress) => {
+          const uploadResult = await cosUtil.uploadFile(
+            attachment.path,
+            'attachment',
+            attachment.name || 'attachment',
+            (percent) => {
               // 鏇存柊涓婁紶杩涘害
               const progressAttachments = this.data.attachments.map(item => {
                 if (item.id === attachment.id) {
-                  return { ...item, progress: Math.round(progress.percent) }
+                  return { ...item, progress: Math.round(percent) }
                 }
                 return item
               })
               this.setData({ attachments: progressAttachments })
             }
-          })
+          )
           
           // 涓婁紶鎴愬姛
           const successAttachments = this.data.attachments.map(item => {
@@ -749,6 +752,141 @@
       wx.hideLoading()
       wx.showToast({
         title: '鍒犻櫎澶辫触',
+        icon: 'error'
+      })
+    }
+  },
+
+  // 棰勮闄勪欢
+  async onPreviewAttachment(e) {
+    try {
+      const index = e.currentTarget.dataset.index
+      const attachment = this.data.attachments[index]
+
+      if (!attachment.uploaded || !attachment.url) {
+        wx.showToast({
+          title: '鏂囦欢鏈笂浼犲畬鎴�',
+          icon: 'none'
+        })
+        return
+      }
+
+      const fileType = attachment.type
+
+      if (fileType === 'image') {
+        // 棰勮鍥剧墖 (media_type = 1)
+        wx.previewImage({
+          current: attachment.url,
+          urls: [attachment.url]
+        })
+      } else if (fileType === 'video') {
+        // 鎾斁瑙嗛 (media_type = 2) - 璺宠浆鍒拌棰戞挱鏀鹃〉闈�
+        wx.navigateTo({
+          url: `/pages/video/video?url=${encodeURIComponent(attachment.url)}&title=${encodeURIComponent(attachment.name)}`
+        })
+      } else if (fileType === 'pdf') {
+        // PDF鏂囦欢 (media_type = 4) - 浣跨敤灏忕▼搴忓唴缃殑鏂囨。棰勮
+        wx.showLoading({
+          title: '姝e湪鎵撳紑...',
+          mask: true
+        })
+        
+        wx.downloadFile({
+          url: attachment.url,
+          success: (res) => {
+            wx.hideLoading()
+            if (res.statusCode === 200) {
+              wx.openDocument({
+                filePath: res.tempFilePath,
+                fileType: 'pdf',
+                success: () => {
+                  console.log('PDF鎵撳紑鎴愬姛')
+                },
+                fail: (err) => {
+                  console.error('PDF鎵撳紑澶辫触:', err)
+                  wx.showToast({
+                    title: 'PDF鎵撳紑澶辫触',
+                    icon: 'none'
+                  })
+                }
+              })
+            } else {
+              wx.showToast({
+                title: '鏂囦欢涓嬭浇澶辫触',
+                icon: 'none'
+              })
+            }
+          },
+          fail: (err) => {
+            wx.hideLoading()
+            console.error('PDF涓嬭浇澶辫触:', err)
+            wx.showToast({
+              title: '鏂囦欢涓嬭浇澶辫触',
+              icon: 'none'
+            })
+          }
+        })
+      } else if (fileType === 'word' || fileType === 'excel' || fileType === 'ppt') {
+        // Office鏂囨。 (media_type = 4) - 浣跨敤灏忕▼搴忓唴缃殑鏂囨。棰勮
+        wx.showLoading({
+          title: '姝e湪鎵撳紑...',
+          mask: true
+        })
+        
+        wx.downloadFile({
+          url: attachment.url,
+          success: (res) => {
+            wx.hideLoading()
+            if (res.statusCode === 200) {
+              const fileTypeMap = {
+                'word': 'doc',
+                'excel': 'xls',
+                'ppt': 'ppt'
+              }
+              
+              wx.openDocument({
+                filePath: res.tempFilePath,
+                fileType: fileTypeMap[fileType] || 'doc',
+                success: () => {
+                  console.log('鏂囨。鎵撳紑鎴愬姛')
+                },
+                fail: (err) => {
+                  console.error('鏂囨。鎵撳紑澶辫触:', err)
+                  wx.showToast({
+                    title: '鏂囨。鎵撳紑澶辫触',
+                    icon: 'none'
+                  })
+                }
+              })
+            } else {
+              wx.showToast({
+                title: '鏂囦欢涓嬭浇澶辫触',
+                icon: 'none'
+              })
+            }
+          },
+          fail: (err) => {
+            wx.hideLoading()
+            console.error('鏂囨。涓嬭浇澶辫触:', err)
+            wx.showToast({
+              title: '鏂囦欢涓嬭浇澶辫触',
+              icon: 'none'
+            })
+          }
+        })
+      } else {
+        // 鍏朵粬鏂囦欢绫诲瀷 (media_type = 5) - 鎻愮ず鐢ㄦ埛
+        wx.showModal({
+          title: '鏂囦欢棰勮',
+          content: `鏆備笉鏀寔棰勮${fileType}绫诲瀷鐨勬枃浠讹紝璇蜂笅杞藉悗鏌ョ湅`,
+          showCancel: false,
+          confirmText: '鐭ラ亾浜�'
+        })
+      }
+    } catch (error) {
+      console.error('棰勮闄勪欢澶辫触:', error)
+      wx.showToast({
+        title: '棰勮澶辫触',
         icon: 'error'
       })
     }
@@ -904,13 +1042,15 @@
       })
 
       // 涓婁紶鍒癈OS
-      const uploadResult = await cosUtil.uploadAvatar(this.data.localAvatarPath, {
-        onProgress: (progress) => {
-          this.setData({
-            avatarUploadProgress: Math.round(progress.percent)
-          })
-        }
-      })
+      const uploadResult = await cosUtil.uploadAvatar(
+         this.data.localAvatarPath,
+         'avatar.jpg',
+         (percent) => {
+           this.setData({
+             avatarUploadProgress: Math.round(percent)
+           })
+         }
+       )
 
       // 涓婁紶鎴愬姛锛屾洿鏂拌〃鍗曟暟鎹�
       this.setData({
@@ -1092,18 +1232,23 @@
       })
 
       // 绗竴姝ワ細涓婁紶鍒癈OS
-      const uploadResult = await cosUtil.uploadAvatar(this.data.localAvatarPath, 'avatar.jpg', (progress) => {
-        this.setData({
-          avatarUploadProgress: Math.round(progress.percent)
-        })
-      })
+      const uploadResult = await cosUtil.uploadAvatar(
+         this.data.localAvatarPath,
+         'avatar.jpg',
+         (percent) => {
+           this.setData({
+             avatarUploadProgress: Math.round(percent)
+           })
+         }
+       )
 
       // 绗簩姝ワ細淇濆瓨濯掍綋璁板綍鍒版暟鎹簱
       await this.saveMediaRecord({
-        targetType: 7, // USER_AVATAR
+        targetType: 'player', // V2 浣跨敤瀛楃涓诧細player 琛ㄧず鐢ㄦ埛澶村儚锛屽搴� DB 鐨� 7 (USER_AVATAR)
         targetId: idInfo.userId,
-        url: uploadResult.url,
+        path: uploadResult.key,
         fileName: uploadResult.fileName || 'avatar.jpg',
+        fileExt: this.getFileExtension(uploadResult.fileName || 'avatar.jpg'),
         fileSize: uploadResult.fileSize,
         mediaType: 1 // 鍥剧墖
       })
@@ -1128,21 +1273,27 @@
   async uploadAttachmentsWithRegistrationId(idInfo) {
     for (let i = 0; i < this.data.attachments.length; i++) {
       const attachment = this.data.attachments[i]
-      if (!attachment.uploaded && attachment.localPath) {
+      if (!attachment.uploaded && attachment.path) {
         try {
           // 绗竴姝ワ細涓婁紶鍒癈OS
-          const uploadResult = await cosUtil.uploadFile(attachment.localPath, 'attachment', attachment.name || 'attachment', (progress) => {
-            this.setData({
-              [`attachments[${i}].uploadProgress`]: Math.round(progress.percent)
-            })
-          })
+          const uploadResult = await cosUtil.uploadFile(
+            attachment.path,
+            'attachment',
+            attachment.name || 'attachment',
+            (percent) => {
+              this.setData({
+                [`attachments[${i}].uploadProgress`]: Math.round(percent)
+              })
+            }
+          )
 
           // 绗簩姝ワ細淇濆瓨濯掍綋璁板綍鍒版暟鎹簱
           await this.saveMediaRecord({
-            targetType: 5, // ACTIVITY_PLAYER_SUBMISSION
+            targetType: 'activity_player', // V2 浣跨敤瀛楃涓诧細activity_player 琛ㄧず鎶ュ悕闄勪欢锛屽搴� DB 鐨� 5
             targetId: idInfo.activityPlayerId,
-            url: uploadResult.url,
+            path: uploadResult.key,
             fileName: uploadResult.fileName || attachment.name,
+            fileExt: this.getFileExtension(uploadResult.fileName || attachment.name),
             fileSize: uploadResult.fileSize,
             mediaType: this.getMediaType(attachment.name) // 鏍规嵁鏂囦欢鎵╁睍鍚嶅垽鏂被鍨�
           })
@@ -1167,11 +1318,11 @@
   // 淇濆瓨濯掍綋璁板綍鍒版暟鎹簱
   async saveMediaRecord(mediaData) {
     const mutation = `
-      mutation SaveMedia($input: MediaInput!) {
-        saveMedia(input: $input) {
-          id
-          url
-          fileName
+      mutation SaveMediaV2($input: MediaSaveInput!) {
+        saveMediaV2(input: $input) {
+          success
+          message
+          mediaId
         }
       }
     `
@@ -1180,8 +1331,9 @@
       input: {
         targetType: mediaData.targetType,
         targetId: mediaData.targetId,
-        url: mediaData.url,
-        fileName: mediaData.fileName,
+        path: mediaData.path,
+        fileName: mediaData.fileName || mediaData.name, // V2 瀛楁涓� fileName锛屽吋瀹规棫瀛楁 name
+        fileExt: mediaData.fileExt,
         fileSize: mediaData.fileSize,
         mediaType: mediaData.mediaType
       }
@@ -1189,14 +1341,20 @@
     
     try {
       const result = await app.graphqlRequest(mutation, variables)
-      console.log('濯掍綋璁板綍淇濆瓨鎴愬姛:', result.saveMedia)
-      return result.saveMedia
+      console.log('濯掍綋璁板綍淇濆瓨鎴愬姛(V2):', result.saveMediaV2)
+      return result.saveMediaV2
     } catch (error) {
-      console.error('濯掍綋璁板綍淇濆瓨澶辫触:', error)
+      console.error('濯掍綋璁板綍淇濆瓨澶辫触(V2):', error)
       throw error
     }
   },
 
+  // 鑾峰彇鏂囦欢鎵╁睍鍚�
+  getFileExtension(fileName) {
+    if (!fileName) return ''
+    return fileName.split('.').pop().toLowerCase()
+  },
+
   // 鏍规嵁鏂囦欢鍚嶈幏鍙栧獟浣撶被鍨�
   getMediaType(fileName) {
     if (!fileName) return 1 // 榛樿鍥剧墖

--
Gitblit v1.8.0