collect.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/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. if (this.data.isLoading || !this.data.hasMore) return;
  48. this.setData({ isLoading: true });
  49. this.getcollect(true);
  50. },
  51. // 收藏列表
  52. async getcollect(isLoadMore = false) {
  53. if (this.data.isLoading || !this.data.hasMore) return;
  54. this.setData({ isLoading: true });
  55. const userInfo = wx.getStorageSync('userInfo');
  56. const { pageNumber, pageSize } = this.data;
  57. try {
  58. const { data } = await models.wx_collect.list({
  59. filter: {
  60. where: {
  61. wx_user_id: userInfo._id
  62. }
  63. },
  64. pageSize,
  65. pageNumber,
  66. getCount: true,
  67. envType: "prod",
  68. });
  69. const collectList = data.records || [];
  70. if (collectList.length === 0) {
  71. this.setData({ hasMore: false, isLoading: false });
  72. return;
  73. }
  74. const fileManagerIds = collectList.map(item => item.file_manager_id);
  75. const fileRes = await models.file_manage.list({
  76. filter: {
  77. where: {
  78. _id: { $in: fileManagerIds }
  79. }
  80. },
  81. envType: "prod",
  82. });
  83. const newFiles = fileRes.data.records.map(item => ({
  84. ...item,
  85. checked: false,
  86. createdAt: this.formatTime(item.createdAt) // 这里格式化
  87. }));
  88. this.setData({
  89. historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
  90. pageNumber: pageNumber + 1,
  91. hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
  92. isLoading: false
  93. });
  94. } catch (err) {
  95. console.error('获取收藏文件失败:', err);
  96. this.setData({ isLoading: false });
  97. }
  98. },
  99. // 格式化时间函数
  100. formatTime(ts) {
  101. const date = new Date(ts);
  102. const y = date.getFullYear();
  103. const m = String(date.getMonth() + 1).padStart(2, '0');
  104. const d = String(date.getDate()).padStart(2, '0');
  105. const hh = String(date.getHours()).padStart(2, '0');
  106. const mm = String(date.getMinutes()).padStart(2, '0');
  107. const ss = String(date.getSeconds()).padStart(2, '0');
  108. return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
  109. },
  110. // 收藏管理是否编辑
  111. toggleManageMode() {
  112. const { isManaging, historyList } = this.data;
  113. if (isManaging) {
  114. if (historyList && historyList.length > 0) {
  115. historyList.forEach(item => item.checked = false);
  116. }
  117. this.setData({ isManaging: false });
  118. } else {
  119. this.setData({ isManaging: true });
  120. }
  121. },
  122. // 全选按钮
  123. selectAll() {
  124. const { historyList } = this.data;
  125. if (!historyList || historyList.length === 0) return;
  126. const allChecked = historyList.every(item => item.checked);
  127. const updatedList = historyList.map(item => ({
  128. ...item,
  129. checked: !allChecked
  130. }));
  131. this.setData({ historyList: updatedList });
  132. },
  133. // 单选
  134. onCheckboxGroupChange(e) {
  135. const selectedIds = e.detail.value;
  136. const updatedList = this.data.historyList.map(item => ({
  137. ...item,
  138. checked: selectedIds.includes(item._id),
  139. }));
  140. this.setData({ historyList: updatedList });
  141. },
  142. // 删除
  143. async deleteItems() {
  144. const { historyList } = this.data;
  145. const selectedItems = historyList.filter(item => item.checked);
  146. const fileids = selectedItems.map(item => item._id);
  147. const userInfo = wx.getStorageSync('userInfo')
  148. if (fileids.length === 0) {
  149. wx.showToast({ title: '请先选择要取消收藏的记录', icon: 'none' });
  150. return;
  151. }
  152. console.log(userInfo._id, 'userInfo._id');
  153. const { data } = await models.wx_collect.deleteMany({
  154. filter: {
  155. where: {
  156. where: {
  157. wx_user_id: _.eq(userInfo._id)
  158. }
  159. }
  160. },
  161. // envType: pre 体验环境, prod 正式环境
  162. envType: "prod",
  163. });
  164. // 更新页面数据
  165. const updatedList = historyList.filter(item => !fileids.includes(item._id));
  166. this.setData({
  167. historyList: updatedList,
  168. isManaging: false
  169. }, () => {
  170. // 回调中触发插入逻辑
  171. this.insertNewItems();
  172. });
  173. },
  174. async insertNewItems() {
  175. try {
  176. const userInfo = wx.getStorageSync('userInfo');
  177. const { historyList } = this.data;
  178. if (!historyList.length) {
  179. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  180. return;
  181. }
  182. const newItems = historyList.map(item => ({
  183. file_manager_id: item._id,
  184. wx_user_id: userInfo._id
  185. }));
  186. const { data } = await models.wx_collect.createMany({
  187. data: newItems,
  188. envType: 'prod'
  189. });
  190. console.log('插入成功:', data);
  191. } catch (err) {
  192. console.error('插入失败:', err);
  193. wx.showToast({ title: '插入失败', icon: 'none' });
  194. }
  195. }
  196. });