purchasehistory.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. console.log(collectList, 'collectList');
  69. if (collectList.length === 0) {
  70. this.setData({ hasMore: false, isLoading: false });
  71. return;
  72. }
  73. console.log(collectList, 'collectList');
  74. const fileManagerIds = collectList.map(item => item.file_manage_id);
  75. console.log(fileManagerIds, 'fileManagerIds');
  76. // 第二步:循环获取每个文件数据(单条查询)
  77. const fileDetailPromises = fileManagerIds.map(id => {
  78. return models.file_manage.list({
  79. filter: {
  80. where: {
  81. _id: id
  82. }
  83. },
  84. envType: "prod"
  85. }).then(res => {
  86. const record = res.data?.records?.[0];
  87. if (!record) {
  88. console.warn(`未找到 _id 为 ${id} 的文件`);
  89. }
  90. return record || null;
  91. })
  92. .catch(err => {
  93. console.error(`获取文件 ${id} 失败`, err);
  94. return null;
  95. });
  96. });
  97. // 第三步:等待所有请求完成
  98. const fileDetails = await Promise.all(fileDetailPromises);
  99. console.log(fileDetails, 'fileDetails');
  100. // 第四步:过滤无效项,并添加 checked 字段
  101. const newFiles = fileDetails
  102. .filter(Boolean) // 去掉 null 或 undefined
  103. .map((file, index) => {
  104. const historyRecord = collectList[index]; // 获取原 download_history 的记录
  105. if (!file) return null;
  106. return {
  107. ...file,
  108. checked: false,
  109. download_history_id: historyRecord._id // 关键:存 download_history 的 id
  110. };
  111. });
  112. this.setData({
  113. historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
  114. pageNumber: pageNumber + 1,
  115. hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
  116. isLoading: false
  117. });
  118. } catch (err) {
  119. console.error('获取收藏文件失败:', err);
  120. this.setData({ isLoading: false });
  121. }
  122. },
  123. // 收藏管理是否编辑
  124. toggleManageMode() {
  125. const { isManaging, historyList } = this.data;
  126. if (isManaging) {
  127. if (historyList && historyList.length > 0) {
  128. historyList.forEach(item => item.checked = false);
  129. }
  130. this.setData({ isManaging: false });
  131. } else {
  132. this.setData({ isManaging: true });
  133. }
  134. },
  135. // 全选按钮
  136. selectAll() {
  137. const { historyList } = this.data;
  138. if (!historyList || historyList.length === 0) return;
  139. const allChecked = historyList.every(item => item.checked);
  140. const updatedList = historyList.map(item => ({
  141. ...item,
  142. checked: !allChecked
  143. }));
  144. this.setData({ historyList: updatedList });
  145. },
  146. // 单选
  147. onCheckboxGroupChange(e) {
  148. const selectedIds = e.detail.value;
  149. const updatedList = this.data.historyList.map(item => ({
  150. ...item,
  151. checked: selectedIds.includes(item.download_history_id),
  152. }));
  153. this.setData({ historyList: updatedList });
  154. },
  155. // 删除
  156. async deleteItems() {
  157. const { historyList } = this.data;
  158. const selectedItems = historyList.filter(item => item.checked);
  159. const fileids = selectedItems.map(item => item.download_history_id);
  160. const userInfo = wx.getStorageSync('userInfo')
  161. if (fileids.length === 0) {
  162. wx.showToast({ title: '请先选择要删除的数据', icon: 'none' });
  163. return;
  164. }
  165. console.log(userInfo._id, 'userInfo._id');
  166. const { data } = await models.parent_download_history.deleteMany({
  167. filter: {
  168. where: {
  169. where: {
  170. user_id: _.eq(userInfo._id)
  171. }
  172. }
  173. },
  174. // envType: pre 体验环境, prod 正式环境
  175. envType: "prod",
  176. });
  177. // 更新页面数据
  178. const updatedList = historyList.filter(item => !fileids.includes(item.download_history_id));
  179. this.setData({
  180. historyList: updatedList,
  181. isManaging: false
  182. }, () => {
  183. // 回调中触发插入逻辑
  184. this.insertNewItems();
  185. });
  186. },
  187. async insertNewItems() {
  188. try {
  189. const userInfo = wx.getStorageSync('userInfo');
  190. const { historyList } = this.data;
  191. if (!historyList.length) {
  192. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  193. return;
  194. }
  195. const newItems = historyList.map(item => ({
  196. file_manage_id: item._id,
  197. user_id: userInfo._id
  198. }));
  199. const { data } = await models.parent_download_history.createMany({
  200. data: newItems,
  201. envType: 'prod'
  202. });
  203. console.log('插入成功:', data);
  204. } catch (err) {
  205. console.error('插入失败:', err);
  206. wx.showToast({ title: '插入失败', icon: 'none' });
  207. }
  208. }
  209. });