purchasehistory.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. role: "teacher",
  5. historyList: [],
  6. isManaging: false,
  7. xiazi: '',
  8. pageNumber: 1,
  9. pageSize: 10,
  10. hasMore: true, // 是否还有更多数据
  11. isLoading: false, // 防止多次触发
  12. },
  13. onLoad(options) {
  14. // 列表数据
  15. this.setData({
  16. pageNumber: 1,
  17. hasMore: true,
  18. historyList: []
  19. }, () => {
  20. this.getcollect();
  21. });
  22. // 页面加载时的初始化操作
  23. const fileIDs = [
  24. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/xiazi.png',
  25. ];
  26. // 并发下载多个 fileID
  27. Promise.all(
  28. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  29. ).then(results => {
  30. // 每个 result 对应一个下载结果
  31. const tempFilePaths = results.map(r => r.tempFilePath);
  32. console.log('全部下载成功:', tempFilePaths);
  33. this.setData({
  34. xiazi: tempFilePaths[0],
  35. });
  36. }).catch(err => {
  37. console.error('有文件下载失败:', err);
  38. });
  39. },
  40. onReachBottom() {
  41. // 上拉触底事件的处理函数
  42. this.loadMore();
  43. },
  44. loadMore() {
  45. // 加载更多数据的逻辑
  46. console.log('加载更多');
  47. this.getcollect(true);
  48. },
  49. // 收藏列表
  50. async getcollect(isLoadMore = false) {
  51. if (this.data.isLoading || !this.data.hasMore) return;
  52. this.setData({ isLoading: true });
  53. const userInfo = wx.getStorageSync('userInfo');
  54. const { pageNumber, pageSize } = this.data;
  55. try {
  56. const { data } = await models.parent_download_history.list({
  57. filter: {
  58. where: {
  59. wx_user_id: userInfo._id
  60. }
  61. },
  62. pageSize,
  63. pageNumber,
  64. getCount: true,
  65. envType: "prod",
  66. });
  67. const collectList = data.records || [];
  68. if (collectList.length === 0) {
  69. this.setData({ hasMore: false, isLoading: false });
  70. return;
  71. }
  72. console.log(collectList, 'collectList');
  73. const fileManagerIds = collectList.map(item => item.file_manage_id);
  74. console.log(fileManagerIds, 'fileManagerIds');
  75. // 第二步:循环获取每个文件数据(单条查询)
  76. const fileDetailPromises = fileManagerIds.map(id => {
  77. return models.file_manage.list({
  78. filter: {
  79. where: {
  80. _id: id
  81. }
  82. },
  83. envType: "prod"
  84. }).then(res => {
  85. const record = res.data?.records?.[0];
  86. if (!record) {
  87. console.warn(`未找到 _id 为 ${id} 的文件`);
  88. }
  89. return record || null;
  90. })
  91. .catch(err => {
  92. console.error(`获取文件 ${id} 失败`, err);
  93. return null;
  94. });
  95. });
  96. // 第三步:等待所有请求完成
  97. const fileDetails = await Promise.all(fileDetailPromises);
  98. console.log(fileDetails, 'fileDetails');
  99. // 第四步:过滤无效项,并添加 checked 字段
  100. const newFiles = fileDetails
  101. .filter(Boolean) // 去掉 null 或 undefined
  102. .map((file, index) => {
  103. const historyRecord = collectList[index]; // 获取原 download_history 的记录
  104. if (!file) return null;
  105. return {
  106. ...file,
  107. checked: false,
  108. download_history_id: historyRecord._id // 关键:存 download_history 的 id
  109. };
  110. });
  111. this.setData({
  112. historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
  113. pageNumber: pageNumber + 1,
  114. hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
  115. isLoading: false
  116. });
  117. } catch (err) {
  118. console.error('获取收藏文件失败:', err);
  119. this.setData({ isLoading: false });
  120. }
  121. },
  122. // 收藏管理是否编辑
  123. toggleManageMode() {
  124. const { isManaging, historyList } = this.data;
  125. if (isManaging) {
  126. if (historyList && historyList.length > 0) {
  127. historyList.forEach(item => item.checked = false);
  128. }
  129. this.setData({ isManaging: false });
  130. } else {
  131. this.setData({ isManaging: true });
  132. }
  133. },
  134. // 全选按钮
  135. selectAll() {
  136. const { historyList } = this.data;
  137. if (!historyList || historyList.length === 0) return;
  138. const allChecked = historyList.every(item => item.checked);
  139. const updatedList = historyList.map(item => ({
  140. ...item,
  141. checked: !allChecked
  142. }));
  143. this.setData({ historyList: updatedList });
  144. },
  145. // 单选
  146. onCheckboxGroupChange(e) {
  147. const selectedIds = e.detail.value;
  148. const updatedList = this.data.historyList.map(item => ({
  149. ...item,
  150. checked: selectedIds.includes(item.download_history_id),
  151. }));
  152. this.setData({ historyList: updatedList });
  153. },
  154. // 删除
  155. async deleteItems() {
  156. const { historyList } = this.data;
  157. const selectedItems = historyList.filter(item => item.checked);
  158. const fileids = selectedItems.map(item => item.download_history_id);
  159. const userInfo = wx.getStorageSync('userInfo')
  160. if (fileids.length === 0) {
  161. wx.showToast({ title: '请先选择要删除的数据', icon: 'none' });
  162. return;
  163. }
  164. console.log(userInfo._id, 'userInfo._id');
  165. const { data } = await models.parent_download_history.deleteMany({
  166. filter: {
  167. where: {
  168. where: {
  169. user_id: _.eq(userInfo._id)
  170. }
  171. }
  172. },
  173. // envType: pre 体验环境, prod 正式环境
  174. envType: "prod",
  175. });
  176. // 更新页面数据
  177. const updatedList = historyList.filter(item => !fileids.includes(item.download_history_id));
  178. this.setData({
  179. historyList: updatedList,
  180. isManaging: false
  181. }, () => {
  182. // 回调中触发插入逻辑
  183. this.insertNewItems();
  184. });
  185. },
  186. async insertNewItems() {
  187. try {
  188. const userInfo = wx.getStorageSync('userInfo');
  189. const { historyList } = this.data;
  190. if (!historyList.length) {
  191. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  192. return;
  193. }
  194. const newItems = historyList.map(item => ({
  195. file_manage_id: item._id,
  196. user_id: userInfo._id
  197. }));
  198. const { data } = await models.parent_download_history.createMany({
  199. data: newItems,
  200. envType: 'prod'
  201. });
  202. console.log('插入成功:', data);
  203. } catch (err) {
  204. console.error('插入失败:', err);
  205. wx.showToast({ title: '插入失败', icon: 'none' });
  206. }
  207. }
  208. });