purchasehistory.js 7.1 KB

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