lrj
昨天 dc643ba44fd2a426263015491268a0f0d6b4671d
wx/pages/registration/registration.js
@@ -9,6 +9,7 @@
    activityId: '',
    activity: null,
    loading: false,
    userInfo: null, // 当前用户信息
    
    // 表单数据
    formData: {
@@ -102,12 +103,15 @@
      console.log('❌ 没有接收到activityId参数')
    }
    
    // 加载用户信息
    this.loadUserInfo()
    // 从全局数据获取用户信息预填充
    this.prefillUserInfo()
    
    // 加载区域数据
    this.loadRegions()
  },
  }
  // 加载活动信息
  async loadActivityInfo() {
@@ -210,15 +214,80 @@
    }
  },
  // 加载用户信息
  loadUserInfo() {
    const userInfo = app.globalData.userInfo
    console.log('🔍 加载用户信息用于显示:', userInfo)
    if (userInfo) {
      let displayUserInfo = {
        name: userInfo.name || '',
        phone: userInfo.phone || '',
        avatarUrl: userInfo.avatarUrl || '',
        gender: null,
        education: ''
      }
      // 如果用户有Player信息,优先使用Player的详细信息
      if (userInfo.player) {
        console.log('📋 使用Player信息显示:', userInfo.player)
        displayUserInfo.name = userInfo.player.name || userInfo.name || ''
        displayUserInfo.phone = userInfo.player.phone || userInfo.phone || ''
        displayUserInfo.avatarUrl = userInfo.player.avatarUrl || userInfo.avatarUrl || ''
        // 处理性别信息:0=男,1=女
        if (userInfo.player.gender !== undefined && userInfo.player.gender !== null) {
          displayUserInfo.gender = parseInt(userInfo.player.gender)
        }
        // 处理学历信息
        if (userInfo.player.education) {
          displayUserInfo.education = userInfo.player.education
        }
      }
      console.log('✅ 设置显示用户信息:', displayUserInfo)
      this.setData({
        userInfo: displayUserInfo
      })
    } else {
      console.log('⚠️ 未找到用户信息')
    }
  },
  // 预填充用户信息
  prefillUserInfo() {
    const userInfo = app.globalData.userInfo
    console.log('🔍 预填充用户信息:', userInfo)
    if (userInfo) {
      this.setData({
      const updateData = {
        'formData.name': userInfo.name || '',
        'formData.phone': userInfo.phone || '',
        'formData.email': userInfo.email || ''
      })
      }
      // 如果用户有Player信息,优先使用Player的详细信息
      if (userInfo.player) {
        console.log('📋 使用Player信息预填充:', userInfo.player)
        updateData['formData.name'] = userInfo.player.name || userInfo.name || ''
        updateData['formData.phone'] = userInfo.player.phone || userInfo.phone || ''
        // 处理性别信息:0=男,1=女
        if (userInfo.player.gender !== undefined && userInfo.player.gender !== null) {
          const genderIndex = parseInt(userInfo.player.gender)
          if (genderIndex === 0 || genderIndex === 1) {
            updateData['formData.gender'] = genderIndex
            updateData['genderIndex'] = genderIndex
            console.log('👤 设置性别:', genderIndex === 0 ? '男' : '女')
          }
        }
      }
      console.log('✅ 预填充数据:', updateData)
      this.setData(updateData)
    } else {
      console.log('⚠️ 未找到用户信息,无法预填充')
    }
  },
@@ -649,18 +718,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 +821,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: '正在打开...',
          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: '正在打开...',
          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 +1111,15 @@
      })
      // 上传到COS
      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 +1301,23 @@
      })
      // 第一步:上传到COS
      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 +1342,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 {
          // 第一步:上传到COS
          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 +1387,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 +1400,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 +1410,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 // 默认图片