registrationRecordList.js 3.3 KB

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