collect.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. role: "teacher",
  5. historyList: [],
  6. isManaging: false,
  7. souchangs: '',
  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/soucangs.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. this.setData({
  33. souchangs: tempFilePaths[0],
  34. });
  35. }).catch(err => {
  36. console.error('有文件下载失败:', err);
  37. });
  38. },
  39. onReachBottom() {
  40. // 上拉触底事件的处理函数
  41. this.loadMore();
  42. },
  43. loadMore() {
  44. // 加载更多数据的逻辑
  45. console.log('加载更多');
  46. this.getcollect(true);
  47. },
  48. // 收藏列表
  49. async getcollect(isLoadMore = false) {
  50. if (this.data.isLoading || !this.data.hasMore) return;
  51. this.setData({ isLoading: true });
  52. const userInfo = wx.getStorageSync('userInfo');
  53. const { pageNumber, pageSize } = this.data;
  54. try {
  55. const { data } = await models.wx_collect.list({
  56. filter: {
  57. where: {
  58. wx_user_id: userInfo._id
  59. }
  60. },
  61. pageSize,
  62. pageNumber,
  63. getCount: true,
  64. envType: "prod",
  65. });
  66. const collectList = data.records || [];
  67. if (collectList.length === 0) {
  68. this.setData({ hasMore: false, isLoading: false });
  69. return;
  70. }
  71. const fileManagerIds = collectList.map(item => item.file_manager_id);
  72. const fileRes = await models.file_manage.list({
  73. filter: {
  74. where: {
  75. _id: { $in: fileManagerIds }
  76. }
  77. },
  78. envType: "prod",
  79. });
  80. const newFiles = fileRes.data.records.map(item => ({
  81. ...item,
  82. checked: false
  83. }));
  84. this.setData({
  85. historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
  86. pageNumber: pageNumber + 1,
  87. hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
  88. isLoading: false
  89. });
  90. } catch (err) {
  91. console.error('获取收藏文件失败:', err);
  92. this.setData({ isLoading: false });
  93. }
  94. },
  95. // 收藏管理是否编辑
  96. toggleManageMode() {
  97. const { isManaging, historyList } = this.data;
  98. if (isManaging) {
  99. if (historyList && historyList.length > 0) {
  100. historyList.forEach(item => item.checked = false);
  101. }
  102. this.setData({ isManaging: false });
  103. } else {
  104. this.setData({ isManaging: true });
  105. }
  106. },
  107. // 全选按钮
  108. selectAll() {
  109. const { historyList } = this.data;
  110. if (!historyList || historyList.length === 0) return;
  111. const allChecked = historyList.every(item => item.checked);
  112. const updatedList = historyList.map(item => ({
  113. ...item,
  114. checked: !allChecked
  115. }));
  116. this.setData({ historyList: updatedList });
  117. },
  118. // 单选
  119. onCheckboxGroupChange(e) {
  120. const selectedIds = e.detail.value;
  121. const updatedList = this.data.historyList.map(item => ({
  122. ...item,
  123. checked: selectedIds.includes(item._id),
  124. }));
  125. this.setData({ historyList: updatedList });
  126. },
  127. // 删除
  128. async deleteItems() {
  129. const { historyList } = this.data;
  130. const selectedItems = historyList.filter(item => item.checked);
  131. const fileids = selectedItems.map(item => item._id);
  132. const userInfo = wx.getStorageSync('userInfo')
  133. if (fileids.length === 0) {
  134. wx.showToast({ title: '请先选择要取消收藏的记录', icon: 'none' });
  135. return;
  136. }
  137. console.log(userInfo._id, 'userInfo._id');
  138. const { data } = await models.wx_collect.deleteMany({
  139. filter: {
  140. where: {
  141. where: {
  142. wx_user_id: _.eq(userInfo._id)
  143. }
  144. }
  145. },
  146. // envType: pre 体验环境, prod 正式环境
  147. envType: "prod",
  148. });
  149. // 更新页面数据
  150. const updatedList = historyList.filter(item => !fileids.includes(item._id));
  151. this.setData({
  152. historyList: updatedList,
  153. isManaging: false
  154. }, () => {
  155. // 回调中触发插入逻辑
  156. this.insertNewItems();
  157. });
  158. },
  159. async insertNewItems() {
  160. try {
  161. const userInfo = wx.getStorageSync('userInfo');
  162. const { historyList } = this.data;
  163. if (!historyList.length) {
  164. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  165. return;
  166. }
  167. const newItems = historyList.map(item => ({
  168. file_manager_id: item._id,
  169. wx_user_id: userInfo._id
  170. }));
  171. const { data } = await models.wx_collect.createMany({
  172. data: newItems,
  173. envType: 'prod'
  174. });
  175. console.log('插入成功:', data);
  176. } catch (err) {
  177. console.error('插入失败:', err);
  178. wx.showToast({ title: '插入失败', icon: 'none' });
  179. }
  180. }
  181. });