purchasehistory.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/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.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. createdAt: this.formatTime(file.createdAt) // 这里格式化
  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. formatTime(ts) {
  125. const date = new Date(ts);
  126. const y = date.getFullYear();
  127. const m = String(date.getMonth() + 1).padStart(2, '0');
  128. const d = String(date.getDate()).padStart(2, '0');
  129. const hh = String(date.getHours()).padStart(2, '0');
  130. const mm = String(date.getMinutes()).padStart(2, '0');
  131. const ss = String(date.getSeconds()).padStart(2, '0');
  132. return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
  133. },
  134. // 收藏管理是否编辑
  135. toggleManageMode() {
  136. const { isManaging, historyList } = this.data;
  137. if (isManaging) {
  138. if (historyList && historyList.length > 0) {
  139. historyList.forEach(item => item.checked = false);
  140. }
  141. this.setData({ isManaging: false });
  142. } else {
  143. this.setData({ isManaging: true });
  144. }
  145. },
  146. // 全选按钮
  147. selectAll() {
  148. const { historyList } = this.data;
  149. if (!historyList || historyList.length === 0) return;
  150. const allChecked = historyList.every(item => item.checked);
  151. const updatedList = historyList.map(item => ({
  152. ...item,
  153. checked: !allChecked
  154. }));
  155. this.setData({ historyList: updatedList });
  156. },
  157. // 单选
  158. onCheckboxGroupChange(e) {
  159. const selectedIds = e.detail.value;
  160. const updatedList = this.data.historyList.map(item => ({
  161. ...item,
  162. checked: selectedIds.includes(item.download_history_id),
  163. }));
  164. this.setData({ historyList: updatedList });
  165. },
  166. // 删除
  167. async deleteItems() {
  168. const { historyList } = this.data;
  169. const selectedItems = historyList.filter(item => item.checked);
  170. const fileids = selectedItems.map(item => item.download_history_id);
  171. const userInfo = wx.getStorageSync('userInfo')
  172. if (fileids.length === 0) {
  173. wx.showToast({ title: '请先选择要删除的数据', icon: 'none' });
  174. return;
  175. }
  176. console.log(userInfo._id, 'userInfo._id');
  177. const { data } = await models.download_history.deleteMany({
  178. filter: {
  179. where: {
  180. where: {
  181. user_id: _.eq(userInfo._id)
  182. }
  183. }
  184. },
  185. // envType: pre 体验环境, prod 正式环境
  186. envType: "prod",
  187. });
  188. // 更新页面数据
  189. const updatedList = historyList.filter(item => !fileids.includes(item.download_history_id));
  190. this.setData({
  191. historyList: updatedList,
  192. isManaging: false
  193. }, () => {
  194. // 回调中触发插入逻辑
  195. this.insertNewItems();
  196. });
  197. },
  198. async insertNewItems() {
  199. try {
  200. const userInfo = wx.getStorageSync('userInfo');
  201. const { historyList } = this.data;
  202. if (!historyList.length) {
  203. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  204. return;
  205. }
  206. const newItems = historyList.map(item => ({
  207. file_manage_id: item._id,
  208. user_id: userInfo._id
  209. }));
  210. const { data } = await models.download_history.createMany({
  211. data: newItems,
  212. envType: 'prod'
  213. });
  214. console.log('插入成功:', data);
  215. } catch (err) {
  216. console.error('插入失败:', err);
  217. wx.showToast({ title: '插入失败', icon: 'none' });
  218. }
  219. }
  220. });