competitionRecords.js 3.8 KB

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