purchasehistory.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // import { models, db, _ } from '../../utils/cloudbase.js'
  2. import { getDB, getModels, getCommand, getTempFileURLs } from '../../utils/cloudbase.js'
  3. Page({
  4. data: {
  5. role: "teacher",
  6. historyList: [],
  7. isManaging: false,
  8. xiazi: '',
  9. pageNumber: 1,
  10. pageSize: 10,
  11. hasMore: true, // 是否还有更多数据
  12. isLoading: false, // 防止多次触发
  13. },
  14. async onLoad(options) {
  15. // 列表数据
  16. this.setData({
  17. pageNumber: 1,
  18. hasMore: true,
  19. historyList: []
  20. }, () => {
  21. this.getcollect();
  22. });
  23. // 页面加载时的初始化操作
  24. const fileIDs = [
  25. 'cloud://honghgaier-5guiffgcf17a2eea.686f-honghgaier-5guiffgcf17a2eea-1373037829/images/icon/xiazi.png',
  26. ];
  27. const fileList = await getTempFileURLs(fileIDs)
  28. this.setData({
  29. xiazi: fileList[0].tempFileURL,
  30. })
  31. // // 并发下载多个 fileID
  32. // Promise.all(
  33. // fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  34. // ).then(results => {
  35. // // 每个 result 对应一个下载结果
  36. // const tempFilePaths = results.map(r => r.tempFilePath);
  37. // console.log('全部下载成功:', tempFilePaths);
  38. // this.setData({
  39. // xiazi: tempFilePaths[0],
  40. // });
  41. // }).catch(err => {
  42. // console.error('有文件下载失败:', err);
  43. // });
  44. },
  45. onReachBottom() {
  46. // 上拉触底事件的处理函数
  47. this.loadMore();
  48. },
  49. loadMore() {
  50. // 加载更多数据的逻辑
  51. console.log('加载更多');
  52. // this.getcollect(true);
  53. if (this.data.isLoading || !this.data.hasMore) return;
  54. this.setData({ isLoading: true });
  55. this.getcollect(true);
  56. },
  57. // 收藏列表
  58. async getcollect(isLoadMore = false) {
  59. const models = await getModels();
  60. if (this.data.isLoading || !this.data.hasMore) return;
  61. this.setData({ isLoading: true });
  62. const userInfo = wx.getStorageSync('userInfo');
  63. const { pageNumber, pageSize } = this.data;
  64. try {
  65. const { data } = await models.parent_download_history.list({
  66. filter: {
  67. where: { wx_user_id: userInfo._id }
  68. },
  69. pageSize,
  70. pageNumber,
  71. getCount: true,
  72. envType: "prod",
  73. });
  74. const collectList = data.records || [];
  75. if (collectList.length === 0) {
  76. this.setData({ hasMore: false, isLoading: false });
  77. return;
  78. }
  79. const fileManagerIds = collectList.map(item => item.file_manage_id);
  80. // 查询每个文件详情
  81. const fileDetailPromises = fileManagerIds.map(id => {
  82. return models.file_manage.list({
  83. filter: { where: { _id: id } },
  84. envType: "prod"
  85. })
  86. .then(async res => {
  87. const record = res.data?.records?.[0];
  88. if (!record) {
  89. console.warn(`未找到 _id 为 ${id} 的文件`);
  90. return null;
  91. }
  92. // ✅ 如果 cover 是 cloud://,获取临时链接
  93. if (record.cover && record.cover.startsWith('cloud://')) {
  94. try {
  95. const fileList = await getTempFileURLs([record.cover]);
  96. if (fileList && fileList.length > 0) {
  97. record.coverTemp = fileList[0].tempFileURL;
  98. }
  99. } catch (err) {
  100. console.error(`获取文件 ${id} 封面临时链接失败`, err);
  101. }
  102. } else {
  103. record.coverTemp = record.cover; // 已经是 http 链接
  104. }
  105. return record;
  106. })
  107. .catch(err => {
  108. console.error(`获取文件 ${id} 失败`, err);
  109. return null;
  110. });
  111. });
  112. const fileDetails = await Promise.all(fileDetailPromises);
  113. // 过滤无效项,并添加 checked、download_history_id 和格式化时间
  114. const newFiles = fileDetails
  115. .filter(Boolean)
  116. .map((file, index) => {
  117. const historyRecord = collectList[index];
  118. if (!file) return null;
  119. return {
  120. ...file,
  121. checked: false,
  122. download_history_id: historyRecord._id,
  123. createdAt: this.formatTime(historyRecord.createdAt)
  124. };
  125. });
  126. this.setData({
  127. historyList: isLoadMore ? this.data.historyList.concat(newFiles) : newFiles,
  128. pageNumber: pageNumber + 1,
  129. hasMore: collectList.length === pageSize,
  130. isLoading: false
  131. });
  132. } catch (err) {
  133. console.error('获取收藏文件失败:', err);
  134. this.setData({ isLoading: false });
  135. }
  136. },
  137. formatTime(ts) {
  138. if (!ts) return '';
  139. const date = new Date(ts);
  140. const y = date.getFullYear();
  141. const m = String(date.getMonth() + 1).padStart(2, '0');
  142. const d = String(date.getDate()).padStart(2, '0');
  143. const hh = String(date.getHours()).padStart(2, '0');
  144. const mm = String(date.getMinutes()).padStart(2, '0');
  145. const ss = String(date.getSeconds()).padStart(2, '0');
  146. return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
  147. },
  148. // 收藏管理是否编辑
  149. toggleManageMode() {
  150. const { isManaging, historyList } = this.data;
  151. if (isManaging) {
  152. if (historyList && historyList.length > 0) {
  153. historyList.forEach(item => item.checked = false);
  154. }
  155. this.setData({ isManaging: false });
  156. } else {
  157. this.setData({ isManaging: true });
  158. }
  159. },
  160. // 全选按钮
  161. selectAll() {
  162. const { historyList } = this.data;
  163. if (!historyList || historyList.length === 0) return;
  164. const allChecked = historyList.every(item => item.checked);
  165. const updatedList = historyList.map(item => ({
  166. ...item,
  167. checked: !allChecked
  168. }));
  169. this.setData({ historyList: updatedList });
  170. },
  171. // 单选
  172. onCheckboxGroupChange(e) {
  173. const selectedIds = e.detail.value;
  174. const updatedList = this.data.historyList.map(item => ({
  175. ...item,
  176. checked: selectedIds.includes(item.download_history_id),
  177. }));
  178. this.setData({ historyList: updatedList });
  179. },
  180. // 删除
  181. async deleteItems() {
  182. const models = await getModels()
  183. const _ = await getCommand()
  184. const { historyList } = this.data;
  185. const selectedItems = historyList.filter(item => item.checked);
  186. const fileids = selectedItems.map(item => item.download_history_id);
  187. const userInfo = wx.getStorageSync('userInfo')
  188. if (fileids.length === 0) {
  189. wx.showToast({ title: '请先选择要删除的数据', icon: 'none' });
  190. return;
  191. }
  192. console.log(userInfo._id, 'userInfo._id');
  193. const { data } = await models.parent_download_history.deleteMany({
  194. filter: {
  195. where: {
  196. where: {
  197. wx_user_id: _.eq(userInfo._id),
  198. file_manage_id: _.eq(userInfo._id),
  199. }
  200. }
  201. },
  202. // envType: pre 体验环境, prod 正式环境
  203. envType: "prod",
  204. });
  205. // 更新页面数据
  206. const updatedList = historyList.filter(item => !fileids.includes(item.download_history_id));
  207. this.setData({
  208. historyList: updatedList,
  209. isManaging: false
  210. }, () => {
  211. // 回调中触发插入逻辑
  212. this.insertNewItems();
  213. });
  214. },
  215. async insertNewItems() {
  216. try {
  217. const userInfo = wx.getStorageSync('userInfo');
  218. const { historyList } = this.data;
  219. if (!historyList.length) {
  220. // wx.showToast({ title: '没有收藏的记录数据', icon: 'none' });
  221. return;
  222. }
  223. const newItems = historyList.map(item => ({
  224. file_manage_id: item._id,
  225. wx_user_id: userInfo._id
  226. }));
  227. const models = await getModels()
  228. const { data } = await models.parent_download_history.createMany({
  229. data: newItems,
  230. envType: 'prod'
  231. });
  232. console.log('插入成功:', data);
  233. } catch (err) {
  234. console.error('插入失败:', err);
  235. wx.showToast({ title: '插入失败', icon: 'none' });
  236. }
  237. }
  238. });