import { enrollRecord } from '../../api/other'; import { uploadImage } from '../../utils/upload.js'; Page({ data: { cardList: [], page: 1, pageSize: 10, loadingMore: false, noMore: false, refreshing: false, filePath: '', // 上传成功的文件路径 fileType: '', // image / pdf }, onLoad() { this.loadData(true); }, // 请求数据 async loadData(isRefresh = false) { if (this.data.loadingMore) return; let page = isRefresh ? 1 : this.data.page; this.setData({ loadingMore: true }); try { const res = await enrollRecord({ page, pageSize: this.data.pageSize }); const statusImgMap = { 0: '/static/image/register/wks.png', 1: '/static/image/register/yjs.png', 2: '/static/image/register/jxz.png' }; const newList = res.data.list || []; let allList = isRefresh ? newList : [...this.data.cardList, ...newList]; allList = allList.map(item => ({ ...item, img: statusImgMap[item.status] })); this.setData({ cardList: allList, loadingMore: false, page: page + 1, noMore: allList.length >= res.data.total, // 有数据时判断是否加载完 refreshing: false }); } catch (err) { console.error(err); wx.showToast({ title: '加载失败', icon: 'none' }); this.setData({ loadingMore: false, refreshing: false }); } }, // 上拉触底 onReachBottom() { if (this.data.noMore) return; this.loadData(); }, // 下拉刷新 onPullDownRefresh() { if (this.data.refreshing) return; this.setData({ refreshing: true, noMore: false, page: 1 }); this.loadData(true); }, // 点击上传区域 chooseFile() { wx.showActionSheet({ itemList: ['图片', 'PDF'], success: res => { if (res.tapIndex === 0) { this.chooseImage(); } else { this.choosePdf(); } } }); }, // 选择图片 chooseImage() { wx.chooseImage({ count: 1, sizeType: ['original', 'compressed'], sourceType: ['album', 'camera'], success: res => { const path = res.tempFilePaths[0]; this.uploadFile(path, 'image'); } }); }, // 选择 PDF choosePdf() { wx.chooseMessageFile({ count: 1, type: 'file', success: res => { const file = res.tempFiles[0]; if (file.name.endsWith('.pdf')) { this.uploadFile(file.path, 'pdf'); } else { wx.showToast({ title: '请选择 PDF 文件', icon: 'none' }); } } }); }, // 上传文件 async uploadFile(path, type) { if (!path) { wx.showToast({ title: '文件路径错误', icon: 'none' }); return; } try { const res = await uploadImage(path); console.log('上传成功:', res); this.setData({ filePath: res.url, fileType: type, "formData.competition_image": res.path }); } catch (err) { console.error('上传失败:', err); } }, // 删除文件 removeFile() { this.setData({ filePath: '', fileType: '', "formData.competition_image": '' }); }, //预览图片 previewImage(e){ const current = e.currentTarget.dataset.src; wx.previewImage({ current, urls: [current] }); }, });