order.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. searchText: '', // 搜索关键词
  24. orders: [],
  25. souimg: '',
  26. pageNumber: 1,
  27. pageSize: 10,
  28. hasMore: true, // 是否还有更多数据
  29. isLoading: false, // 防止多次触发
  30. },
  31. onLoad(options) {
  32. const type = Number(options.type) || 1;
  33. console.log('收到的 type 参数:', type);
  34. // 根据 type 加载数据
  35. this.setData({
  36. categoriesindex: type
  37. }, () => {
  38. this.getdatalist(); // 确保 this 指向正确
  39. });
  40. const fileIDs = [
  41. 'cloud://cloud1-6g98iw7i28b01747.636c-cloud1-6g98iw7i28b01747-1367995226/images/icon/sou.png',
  42. ];
  43. // 并发下载多个 fileID
  44. Promise.all(
  45. fileIDs.map(fileID => wx.cloud.downloadFile({ fileID }))
  46. ).then(results => {
  47. // 每个 result 对应一个下载结果
  48. const tempFilePaths = results.map(r => r.tempFilePath);
  49. console.log('全部下载成功:', tempFilePaths);
  50. this.setData({
  51. souimg: tempFilePaths[0]
  52. });
  53. }).catch(err => {
  54. console.error('有文件下载失败:', err);
  55. });
  56. },
  57. onReachBottom() {
  58. // 上拉触底事件的处理函数
  59. this.loadMore();
  60. },
  61. loadMore() {
  62. // 加载更多数据的逻辑
  63. console.log('加载更多');
  64. this.getdatalist(true);
  65. },
  66. // 模糊搜索
  67. onSearchInput(e) {
  68. const value = e.detail.value.trim();
  69. this.setData({
  70. searchText: value,
  71. pageNumber: 1,
  72. hasMore: true,
  73. orders: []
  74. }, () => {
  75. this.getdatalist();
  76. });
  77. },
  78. async getdatalist(isLoadMore = false) {
  79. if (this.data.isLoading || !this.data.hasMore) return;
  80. this.setData({ isLoading: true });
  81. const { pageNumber, pageSize, categoriesindex, searchText, orders } = this.data;
  82. let dbRegExp = null;
  83. let jsRegExp = null;
  84. if (searchText) {
  85. dbRegExp = db.RegExp({
  86. regexp: searchText,
  87. options: 'i'
  88. });
  89. jsRegExp = new RegExp(searchText, 'i');
  90. }
  91. // 构造订单表的筛选条件
  92. // 获取本地存储的用户信息
  93. const userInfo = wx.getStorageSync('userInfo');
  94. const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
  95. const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID
  96. let orderWhere = {
  97. user_id: userId, // 每次必传
  98. school_id: schoolId
  99. };
  100. if (categoriesindex !== 1) {
  101. orderWhere.status = categoriesindex - 2;
  102. }
  103. if (dbRegExp) {
  104. orderWhere = _.and([
  105. orderWhere,
  106. { order_id: dbRegExp }
  107. ]);
  108. }
  109. try {
  110. // 第一步:查订单表
  111. const { data } = await models.orders.list({
  112. filter: { where: orderWhere },
  113. pageSize,
  114. pageNumber,
  115. getCount: true,
  116. envType: 'prod'
  117. });
  118. const orderList = data.records || [];
  119. if (orderList.length === 0) {
  120. this.setData({ hasMore: false, isLoading: false });
  121. return;
  122. }
  123. // 第二步:查商品表
  124. const fileManagerIds = orderList.map(item => item.merchandise_id);
  125. const fileDetailPromises = fileManagerIds.map(id => {
  126. return models.wx_merchandise.list({
  127. filter: { where: { _id: id } },
  128. envType: 'prod'
  129. }).then(res => res.data?.records?.[0] || null)
  130. .catch(err => {
  131. console.error(`商品获取失败: ${id}`, err);
  132. return null;
  133. });
  134. });
  135. const fileDetails = await Promise.all(fileDetailPromises);
  136. // 第三步:合并数据,并进行模糊搜索筛选(匹配一个就保留)
  137. const mergedOrders = [];
  138. for (let i = 0; i < orderList.length; i++) {
  139. const order = orderList[i];
  140. const merch = fileDetails[i];
  141. if (!merch) continue;
  142. if (jsRegExp) {
  143. const matchOrderId = jsRegExp.test(order._id || '');
  144. const matchName = jsRegExp.test(merch.name || '');
  145. if (!matchOrderId && !matchName) {
  146. continue; // 都没匹配上,跳过
  147. }
  148. }
  149. // 先把 _id 改名为 order_id
  150. const { _id, school_id, ...restOrder } = order;
  151. const orderRenamed = {
  152. ...restOrder,
  153. orders_id: _id,
  154. schools_id: school_id
  155. };
  156. mergedOrders.push({
  157. ...orderRenamed,
  158. ...merch
  159. });
  160. }
  161. // 第四步:更新数据
  162. this.setData({
  163. orders: isLoadMore ? orders.concat(mergedOrders) : mergedOrders,
  164. pageNumber: pageNumber + 1,
  165. hasMore: orderList.length === pageSize,
  166. isLoading: false
  167. });
  168. } catch (err) {
  169. console.error('获取数据失败:', err);
  170. this.setData({ isLoading: false });
  171. }
  172. },
  173. tabcategories(e) {
  174. const type = e.currentTarget.dataset.type;
  175. console.log('type:', type);
  176. this.setData({
  177. categoriesindex: type,
  178. searchText: '', // ✅ 清空搜索关键词
  179. pageNumber: 1, // 重置页码
  180. hasMore: true, // 重置是否有更多数据
  181. orders: [], // 清空旧数据
  182. }, () => {
  183. console.log('categoriesindex:', this.data.categoriesindex);
  184. this.getdatalist(false);
  185. });
  186. },
  187. // 处理第一个按钮的点击事件
  188. handleAction1: function (event) {
  189. const index = event.currentTarget.dataset.index; // 获取点击的订单索引
  190. const status = this.data.orders[index].status; // 获取订单状态
  191. switch (status) {
  192. case 1:
  193. console.log("查看详情", index);
  194. const item = this.data.orders[index];
  195. wx.navigateTo({
  196. url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item))
  197. });
  198. // 执行查看详情的逻辑
  199. break;
  200. case 0:
  201. console.log("取消订单", index);
  202. // 执行取消订单的逻辑
  203. break;
  204. case 2:
  205. console.log("申请售后", index);
  206. // 执行申请售后的逻辑
  207. const items = this.data.orders[index];
  208. console.log(items, 'itemsitemsitemsitems');
  209. wx.navigateTo({
  210. url: '/subpackages/afterservice/afterservice?data=' + encodeURIComponent(JSON.stringify(items))
  211. });
  212. break;
  213. default:
  214. console.log("未知状态");
  215. }
  216. },
  217. // 处理第二个按钮的点击事件
  218. handleAction2: function (event) {
  219. const index = event.currentTarget.dataset.index; // 获取点击的订单索引
  220. const status = this.data.orders[index].status; // 获取订单状态
  221. switch (status) {
  222. case 1:
  223. console.log("确认收货", index);
  224. this.getyesordels(this.data.orders[index])
  225. // 执行确认收货的逻辑
  226. break;
  227. case 0:
  228. console.log("立即支付", index);
  229. // 执行立即支付的逻辑
  230. break;
  231. case 2:
  232. console.log("再来一单", index);
  233. // 执行再来一单的逻辑
  234. break;
  235. default:
  236. console.log("未知状态");
  237. }
  238. },
  239. // 确认收货
  240. async getyesordels (list) {
  241. console.log(list, 'list');
  242. const userInfo = wx.getStorageSync('userInfo');
  243. const userId = userInfo && userInfo._id ? userInfo._id : ''; // 根据你的userInfo结构取ID
  244. const schoolId = userInfo && userInfo.school_id ? userInfo.school_id : ''; // 根据你的userInfo结构取ID
  245. const { data } = await models.orders.update({
  246. data: {
  247. status: 2, // 状态
  248. },
  249. filter: {
  250. where: {
  251. $and: [
  252. {
  253. merchandise_id: {
  254. $eq: list._id, // 推荐传入_id数据标识进行操作
  255. },
  256. },
  257. {
  258. user_id: {
  259. $eq: userId, // 推荐传入_id数据标识进行操作
  260. },
  261. },
  262. {
  263. school_id: {
  264. $eq: schoolId, // 推荐传入_id数据标识进行操作
  265. },
  266. },
  267. ]
  268. }
  269. },
  270. envType: "prod",
  271. });
  272. console.log(data.count, 'data.count');
  273. if (Number(data.count) > 0) {
  274. wx.showToast({
  275. title: '确认收货成功',
  276. icon: 'success',
  277. duration: 1500
  278. });
  279. // ✅ 重新获取数据
  280. this.setData({
  281. pageNumber: 1,
  282. hasMore: true,
  283. orders: []
  284. }, () => {
  285. this.getdatalist(false);
  286. });
  287. } else {
  288. wx.showToast({
  289. title: '确认收货失败',
  290. icon: 'error',
  291. duration: 1500
  292. });
  293. }
  294. },
  295. goToGoodsList (e) {
  296. const index = e.currentTarget.dataset.type;
  297. const item = this.data.orders[index];
  298. // console.log(e.currentTarget, 'item');
  299. wx.navigateTo({
  300. url: '/subpackages/orderdetails/orderdetails?data=' + encodeURIComponent(JSON.stringify(item))
  301. });
  302. },
  303. // async adds () {
  304. // const { data } = await models.orders.create({
  305. // data: {
  306. // adresses_id: "BWSLJAGVZA", // 地址id
  307. // sys_updateby_id: "", // 更新人
  308. // merchandise_id: "BULGAD2DA8", // 商品id
  309. // real_money: 223.12, // 实际付款金额
  310. // updat_adress: "", // 修改后地址
  311. // update_phone: "", // 修改后电话
  312. // delete: 0, // 逻辑删除
  313. // way: 1, // 收货方式
  314. // update_name: "", // 修改后收货人
  315. // school_id: "BU5N9CFX2Q", // 学校_id
  316. // user_id: "BWYRRKQVNY", // 用户_id
  317. // num_index: 1, // 商品数据
  318. // tracking_number: "物流单号888", // 物流单号
  319. // order_id: "订单号888", // 订单号
  320. // status: 2, // 状态
  321. // },
  322. // // envType: pre 体验环境, prod 正式环境
  323. // envType: "prod",
  324. // });
  325. // // 返回创建的数据 id
  326. // console.log(data);
  327. // }
  328. });