123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- import { registrationRecord } from '../../api/other';
- import { uploadImage } from '../../utils/upload.js';
- Page({
- data: {
- cardList: [],
- page: 1,
- pageSize: 10,
- loadingMore: false,
- noMore: false,
- refreshing: false,
- activeItem:{},
- 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 registrationRecord({ 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(e) {
- // 选中的item数据
- this.setData({
- activeItem:e.currentTarget.dataset.item
- })
- 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({filePath:path,marathonId:this.data.activeItem.mv_id,competitionNo:this.data.activeItem.competition_no},'/api/upload/finish-certificate','file');
- 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]
- });
- },
- });
|