order.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { models, db, _ } from '../../utils/cloudbase.js'
  2. Page({
  3. data: {
  4. categoriesindex: 1,
  5. categories: [
  6. {
  7. title: '全部',
  8. type: 1,
  9. },
  10. {
  11. title: '待支付',
  12. type: 2,
  13. },
  14. {
  15. title: '待收货',
  16. type: 3,
  17. },
  18. {
  19. title: '已完成',
  20. type: 4,
  21. }
  22. ],
  23. orders: [],
  24. souimg: '',
  25. pageNumber: 1,
  26. pageSize: 10,
  27. hasMore: true, // 是否还有更多数据
  28. isLoading: false, // 防止多次触发
  29. },
  30. onLoad(options) {
  31. const type = Number(options.type) || 1;
  32. console.log('收到的 type 参数:', type);
  33. // 根据 type 加载数据
  34. this.setData({
  35. categoriesindex: type
  36. }, () => {
  37. this.getdatalist(); // 确保 this 指向正确
  38. });
  39. const fileIDs = [
  40. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  41. ];
  42. // 并发下载多个 fileID
  43. Promise.all(
  44. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  45. ).then(results => {
  46. // 每个 result 对应一个下载结果
  47. const tempFilePaths = results.map(r => r.tempFilePath);
  48. console.log('全部下载成功:', tempFilePaths);
  49. this.setData({
  50. souimg: tempFilePaths[0]
  51. });
  52. }).catch(err => {
  53. console.error('有文件下载失败:', err);
  54. });
  55. },
  56. onReachBottom() {
  57. // 上拉触底事件的处理函数
  58. this.loadMore();
  59. },
  60. loadMore() {
  61. // 加载更多数据的逻辑
  62. console.log('加载更多');
  63. this.getdatalist(true);
  64. },
  65. async getdatalist(isLoadMore = false) {
  66. if (this.data.isLoading || !this.data.hasMore) return;
  67. this.setData({ isLoading: true });
  68. const { pageNumber, pageSize } = this.data;
  69. try {
  70. // 根据 categoriesindex 的值动态设置 filter
  71. const filter = {};
  72. if (this.data.categoriesindex !== 1) {
  73. filter.where = {
  74. status: this.data.categoriesindex - 2, // 减去 2
  75. };
  76. }
  77. const { data } = await models.orders.list({
  78. filter, // 动态传递 filter
  79. pageSize,
  80. pageNumber,
  81. getCount: true, // 开启用来获取总数
  82. envType: 'prod', // 正式环境
  83. });
  84. // 返回查询到的数据列表 records 和 总数 total
  85. const collectList = data.records || [];
  86. if (collectList.length === 0) {
  87. this.setData({ hasMore: false, isLoading: false });
  88. return;
  89. }
  90. console.log(collectList, 'collectList');
  91. const fileManagerIds = collectList.map(item => item.merchandise_id);
  92. console.log(fileManagerIds, 'fileManagerIds');
  93. // 第二步:循环获取每个文件数据(单条查询)
  94. const fileDetailPromises = fileManagerIds.map(id => {
  95. return models.wx_merchandise.list({
  96. filter: {
  97. where: {
  98. _id: id
  99. }
  100. },
  101. envType: "prod"
  102. }).then(res => {
  103. const record = res.data?.records?.[0];
  104. if (!record) {
  105. console.warn(`未找到 _id 为 ${id} 的文件`);
  106. }
  107. return record || null;
  108. })
  109. .catch(err => {
  110. console.error(`获取文件 ${id} 失败`, err);
  111. return null;
  112. });
  113. });
  114. // 第三步:等待所有请求完成
  115. const fileDetails = await Promise.all(fileDetailPromises);
  116. console.log(fileDetails, 'fileDetails');
  117. // 第四步:过滤无效项,并添加 checked 字段
  118. const newFiles = fileDetails
  119. .filter(Boolean) // 去掉 null 或 undefined
  120. .map((file, index) => {
  121. const historyRecord = collectList[index]; // 获取原 download_history 的记录
  122. if (!file) return null;
  123. return {
  124. ...file,
  125. ...historyRecord
  126. };
  127. });
  128. this.setData({
  129. orders: isLoadMore ? this.data.orders.concat(newFiles) : newFiles,
  130. pageNumber: pageNumber + 1,
  131. hasMore: collectList.length === pageSize, // 如果返回的数量小于 pageSize,说明已经到底
  132. isLoading: false
  133. });
  134. } catch (err) {
  135. console.error('获取失败:', err);
  136. this.setData({ isLoading: false });
  137. }
  138. },
  139. tabcategories(e) {
  140. const type = e.currentTarget.dataset.type;
  141. console.log('type:', type);
  142. this.setData({
  143. categoriesindex: type,
  144. pageNumber: 1, // 重置页码
  145. hasMore: true, // 重置是否有更多数据
  146. orders: [], // 清空旧数据
  147. }, () => {
  148. console.log('categoriesindex:', this.data.categoriesindex);
  149. this.getdatalist(false);
  150. });
  151. },
  152. handleAction1(e) {
  153. console.log('查看详情', e);
  154. },
  155. handleAction2(e) {
  156. console.log('处理其他操作', e);
  157. },
  158. // 处理第一个按钮的点击事件
  159. handleAction1: function (event) {
  160. const index = event.currentTarget.dataset.index; // 获取点击的订单索引
  161. const status = this.data.orders[index].status; // 获取订单状态
  162. switch (status) {
  163. case 1:
  164. console.log("查看详情", index);
  165. // 执行查看详情的逻辑
  166. break;
  167. case 2:
  168. console.log("取消订单", index);
  169. // 执行取消订单的逻辑
  170. break;
  171. case 3:
  172. console.log("申请售后", index);
  173. // 执行申请售后的逻辑
  174. break;
  175. default:
  176. console.log("未知状态");
  177. }
  178. },
  179. // 处理第二个按钮的点击事件
  180. handleAction2: function (event) {
  181. const index = event.currentTarget.dataset.index; // 获取点击的订单索引
  182. const status = this.data.orders[index].status; // 获取订单状态
  183. switch (status) {
  184. case 1:
  185. console.log("确认收货", index);
  186. // 执行确认收货的逻辑
  187. break;
  188. case 2:
  189. console.log("立即支付", index);
  190. // 执行立即支付的逻辑
  191. break;
  192. case 3:
  193. console.log("再来一单", index);
  194. // 执行再来一单的逻辑
  195. break;
  196. default:
  197. console.log("未知状态");
  198. }
  199. },
  200. goToGoodsList (e) {
  201. const index = e.currentTarget.dataset.type;
  202. const item = this.data.orders[index];
  203. // console.log(e.currentTarget, 'item');
  204. wx.navigateTo({
  205. url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item))
  206. });
  207. }
  208. });