competitionRecords.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import { registrationRecord } from '../../api/other';
  2. import { uploadImage } from '../../utils/upload.js';
  3. Page({
  4. data: {
  5. cardList: [],
  6. page: 1,
  7. pageSize: 10,
  8. loadingMore: false,
  9. noMore: false,
  10. refreshing: false,
  11. activeItem:{},
  12. filePath: '', // 上传成功的文件路径
  13. fileType: '', // image / pdf
  14. },
  15. onLoad() {
  16. this.loadData(true);
  17. },
  18. // 请求数据
  19. async loadData(isRefresh = false) {
  20. if (this.data.loadingMore) return;
  21. let page = isRefresh ? 1 : this.data.page;
  22. this.setData({ loadingMore: true });
  23. try {
  24. const res = await registrationRecord({ page, pageSize: this.data.pageSize });
  25. const statusImgMap = {
  26. 0: '/static/image/register/wks.png',
  27. 1: '/static/image/register/yjs.png',
  28. 2: '/static/image/register/jxz.png'
  29. };
  30. const newList = res.data.list || [];
  31. let allList = isRefresh ? newList : [...this.data.cardList, ...newList];
  32. allList = allList.map(item => ({
  33. ...item,
  34. img: statusImgMap[item.status]
  35. }));
  36. this.setData({
  37. cardList: allList,
  38. loadingMore: false,
  39. page: page + 1,
  40. noMore: allList.length >= res.data.total, // 有数据时判断是否加载完
  41. refreshing: false
  42. });
  43. } catch (err) {
  44. console.error(err);
  45. wx.showToast({ title: '加载失败', icon: 'none' });
  46. this.setData({
  47. loadingMore: false,
  48. refreshing: false
  49. });
  50. }
  51. },
  52. // 上拉触底
  53. onReachBottom() {
  54. if (this.data.noMore) return;
  55. this.loadData();
  56. },
  57. // 下拉刷新
  58. onPullDownRefresh() {
  59. if (this.data.refreshing) return;
  60. this.setData({ refreshing: true, noMore: false, page: 1 });
  61. this.loadData(true);
  62. },
  63. // 点击上传区域
  64. chooseFile(e) {
  65. // 选中的item数据
  66. this.setData({
  67. activeItem:e.currentTarget.dataset.item
  68. })
  69. wx.showActionSheet({
  70. itemList: ['图片', 'PDF'],
  71. success: res => {
  72. if (res.tapIndex === 0) {
  73. this.chooseImage();
  74. } else {
  75. this.choosePdf();
  76. }
  77. }
  78. });
  79. },
  80. // 选择图片
  81. chooseImage() {
  82. wx.chooseImage({
  83. count: 1,
  84. sizeType: ['original', 'compressed'],
  85. sourceType: ['album', 'camera'],
  86. success: res => {
  87. const path = res.tempFilePaths[0];
  88. this.uploadFile(path, 'image');
  89. }
  90. });
  91. },
  92. // 选择 PDF
  93. choosePdf() {
  94. wx.chooseMessageFile({
  95. count: 1,
  96. type: 'file',
  97. success: res => {
  98. const file = res.tempFiles[0];
  99. if (file.name.endsWith('.pdf')) {
  100. this.uploadFile(file.path, 'pdf');
  101. } else {
  102. wx.showToast({ title: '请选择 PDF 文件', icon: 'none' });
  103. }
  104. }
  105. });
  106. },
  107. // 上传文件
  108. async uploadFile(path, type) {
  109. if (!path) {
  110. wx.showToast({ title: '文件路径错误', icon: 'none' });
  111. return;
  112. }
  113. try {
  114. const res = await uploadImage({filePath:path,marathonId:this.data.activeItem.mv_id,competitionNo:this.data.activeItem.competition_no},'/api/upload/finish-certificate','file');
  115. console.log('上传成功:', res);
  116. this.setData({
  117. filePath: res.url,
  118. fileType: type,
  119. "formData.competition_image": res.path
  120. });
  121. } catch (err) {
  122. console.error('上传失败:', err);
  123. }
  124. },
  125. // 删除文件
  126. removeFile() {
  127. this.setData({
  128. filePath: '',
  129. fileType: '',
  130. "formData.competition_image": ''
  131. });
  132. },
  133. //预览图片
  134. previewImage(e){
  135. const current = e.currentTarget.dataset.src;
  136. wx.previewImage({
  137. current,
  138. urls: [current]
  139. });
  140. },
  141. });