diff --git a/pages/home/home.js b/pages/home/home.js index 4079948..b54c9ad 100644 --- a/pages/home/home.js +++ b/pages/home/home.js @@ -1,6 +1,7 @@ const { API } = require('../../utils/constants'); const { request } = require('../../utils/http'); const { formatDateTime, eventStatusText, eventStatusForPill, eventFormatText } = require('../../utils/format'); +const auth = require('../../utils/auth'); /** 统一后台轮播字段(camelCase / PascalCase、枚举数字)供 wxml 绑定 */ function normalizeBannerItem(raw) { @@ -234,36 +235,97 @@ Page({ openUploadPicker({ kind, registrationId }) { const title = kind === 'voucher' ? '上传付款凭证' : '上传素材'; - wx.showActionSheet({ - itemList: ['从相册选择', '拍照'], - success: (r) => { - if (!r || typeof r.tapIndex !== 'number') return; - const sourceType = r.tapIndex === 1 ? ['camera'] : ['album']; - wx.chooseMedia({ - count: 1, - mediaType: ['image'], - sourceType, - success: (res) => { - const tempFilePath = - res && res.tempFiles && res.tempFiles[0] ? res.tempFiles[0].tempFilePath : ''; - if (!tempFilePath) return; - wx.showModal({ - title, - content: '确认上传该图片?', - cancelText: '取消', - confirmText: '确定', - success: (mr) => { - if (!mr || !mr.confirm) return; - // TODO: 接入后端上传接口(素材/付款凭证) - // kind: material | voucher - // registrationId: 报名记录 id - // tempFilePath: 本地图片路径 - wx.showToast({ title: '已选择', icon: 'success' }); + if (kind === 'material') { + wx.showActionSheet({ + itemList: ['从相册选择图片/视频', '拍照/录视频', '选择音乐文件'], + success: (r) => { + if (!r || typeof r.tapIndex !== 'number') return; + if (r.tapIndex === 2) { + wx.chooseMessageFile({ + count: 1, + type: 'file', + extension: ['mp3', 'wav', 'aac', 'flac', 'm4a', 'ogg'], + success: (res) => { + const file = res && res.tempFiles && res.tempFiles[0]; + const tempFilePath = file ? file.path : ''; + if (!tempFilePath) return; + this._confirmAndUploadMaterial(title, tempFilePath, registrationId); + } + }); + } else { + const sourceType = r.tapIndex === 1 ? ['camera'] : ['album']; + wx.chooseMedia({ + count: 1, + mediaType: ['image', 'video'], + sourceType, + success: (res) => { + const tempFilePath = + res && res.tempFiles && res.tempFiles[0] ? res.tempFiles[0].tempFilePath : ''; + if (!tempFilePath) return; + this._confirmAndUploadMaterial(title, tempFilePath, registrationId); } }); } - }); + } + }); + } else { + wx.showActionSheet({ + itemList: ['从相册选择', '拍照'], + success: (r) => { + if (!r || typeof r.tapIndex !== 'number') return; + const sourceType = r.tapIndex === 1 ? ['camera'] : ['album']; + wx.chooseMedia({ + count: 1, + mediaType: ['image'], + sourceType, + success: (res) => { + const tempFilePath = + res && res.tempFiles && res.tempFiles[0] ? res.tempFiles[0].tempFilePath : ''; + if (!tempFilePath) return; + wx.showModal({ + title, + content: '确认上传该图片?', + cancelText: '取消', + confirmText: '确定', + success: (mr) => { + if (!mr || !mr.confirm) return; + // TODO: 接入付款凭证上传接口 + wx.showToast({ title: '已选择', icon: 'success' }); + } + }); + } + }); + } + }); + } + }, + + _confirmAndUploadMaterial(title, tempFilePath, registrationId) { + wx.showModal({ + title, + content: '确认上传该文件?', + cancelText: '取消', + confirmText: '确定', + success: (mr) => { + console.log('用户选择:', mr); + if (!mr || !mr.confirm) return; + wx.showLoading({ title: '上传中...', mask: true }); + auth.uploadFile(tempFilePath) + .then((url) => { + return request({ + path: API.uploadMaterial, + method: 'POST', + needAuth: true, + data: { registrationId, materialUrl: url } + }); + }) + .then(() => { + wx.showToast({ title: '上传成功', icon: 'success' }); + }) + .catch((err) => { + wx.showToast({ title: (err && err.message) || '上传失败', icon: 'none' }); + }); } }); }, diff --git a/utils/constants.js b/utils/constants.js index 73c8aff..a24ecac 100644 --- a/utils/constants.js +++ b/utils/constants.js @@ -32,6 +32,7 @@ const API = { myTeams: '/api/my/teams', myCertificates: '/api/my/certificates', fileUpload: '/api/file/upload', + uploadMaterial: '/api/eventclient/upload-material', ocrIdCard: '/api/ocr/recognize/idcard', ocrPassport: '/api/ocr/recognize/passport' };