order-list.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, {
  3. getTickleContext,
  4. } from "../../../../core/behavior/tickle.behavior";
  5. import {
  6. orderListMethod,
  7. orderCancelMethod,
  8. orderPayMethod,
  9. } from "../../request";
  10. // module/article/pages/order-list/order-list.ts
  11. Page({
  12. behaviors: [PageContainerBehavior, tickleBehavior],
  13. properties: {},
  14. data: {
  15. showConfirm: false,
  16. currentTab: "all", // 当前选中的标签
  17. orders: [],
  18. filteredOrders: [], // 用于存储过滤后的订单
  19. patientId: 0,
  20. id: "",
  21. statusObj: {
  22. 0: "待付款",
  23. 6: "待收货",
  24. 345: "交易成功",
  25. 2: "交易关闭",
  26. },
  27. statusClassObj: {
  28. 0: "status-pending",
  29. 6: "status-received",
  30. 345: "status-completed",
  31. 2: "status-closed",
  32. },
  33. selectedAddress: null,
  34. paying: false,
  35. },
  36. computed: {},
  37. onLoad(options: any) {
  38. // 读取 tab 参数,默认为 all
  39. console.log(options, "options");
  40. const tab = options?.tab || "all";
  41. this.setData({ currentTab: tab });
  42. // this.filterOrdersList(tab);
  43. },
  44. onShow() {
  45. this.filterOrdersList(this.data.currentTab);
  46. // 读取地址
  47. // if (JSON.stringify(this.data.selectedAddress) !== "{}") {
  48. // this.setData({
  49. // name: this.data.selectedAddress?.liaison,
  50. // phone: this.data.selectedAddress?.phone,
  51. // address: this.data.selectedAddress?.fullAddress,
  52. // });
  53. // }
  54. },
  55. // 切换tab
  56. onTabChange(event: any) {
  57. const type = event.detail.value;
  58. console.log(type, "传来的type");
  59. this.setData({ currentTab: type });
  60. this.filterOrdersList(type);
  61. },
  62. // 获取列表数据
  63. async getOrderList(progress: string) {
  64. const patientId = wx.getStorageSync("patientId");
  65. if (!patientId) {
  66. getTickleContext.call(this).showWarnMessage("请先登录");
  67. return;
  68. }
  69. const res = await orderListMethod(patientId, progress);
  70. if (res && res.data) {
  71. res.data.forEach((item: any) => {
  72. item.address = `${item.provinceName}${item.cityName}${item.areaName}${item.detailAddress}`;
  73. item.liaison =
  74. item.liaison !== null && item.liaison !== undefined
  75. ? item.liaison
  76. : "";
  77. item.phone =
  78. item.phone !== null && item.phone !== undefined ? item.phone : "";
  79. });
  80. this.setData({ orders: res.data });
  81. }
  82. },
  83. // 过滤订单l列表
  84. filterOrdersList(type: any) {
  85. // 根据当前选中的标签过滤订单
  86. console.log(type, "根据传来的type获取不同状态下的列表");
  87. switch (type) {
  88. // 全部订单
  89. case "all":
  90. this.getOrderList("");
  91. break;
  92. // 待付款订单
  93. case "pending":
  94. this.getOrderList("0");
  95. break;
  96. // 待收货订单
  97. case "received":
  98. this.getOrderList("6");
  99. break;
  100. // 交易完成订单
  101. case "completed":
  102. this.getOrderList("345");
  103. break;
  104. default:
  105. break;
  106. }
  107. },
  108. // 订单支付
  109. async onPay(event: any) {
  110. if (this.data.paying) return; // 防重复
  111. this.setData({ paying: true });
  112. try {
  113. const orderId = event.currentTarget.dataset.id;
  114. const res = await orderPayMethod(orderId);
  115. wx.navigateTo({
  116. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  117. });
  118. } catch (error: any) {
  119. this.setData({ paying: false });
  120. getTickleContext.call(this).showWarnMessage(error.errMsg);
  121. }
  122. },
  123. // 查看物流
  124. onSeeLogistics(e: any) {
  125. console.log(e, "查看物流");
  126. wx.showToast({
  127. title: "暂未开通",
  128. icon: "none",
  129. });
  130. // const id = e.currentTarget.dataset.id;
  131. // wx.navigateTo({
  132. // url: `/module/article/pages/see-logistics/see-logistics?id=${id}`,
  133. // });
  134. },
  135. // 切换地址
  136. changeAddress(e: any) {
  137. const orderStatus = e.currentTarget.dataset.status;
  138. const id = e.currentTarget.dataset.id;
  139. console.log(orderStatus, "切换地址");
  140. // 根据订单状态判断是否可以切换地址. 待支付状态下可以切换地址
  141. if (orderStatus === "0") {
  142. wx.navigateTo({
  143. url:
  144. "/module/article/pages/manage-address/manage-address?type=orderList&orderId=" +
  145. id,
  146. });
  147. }
  148. },
  149. // 打开取消订单弹窗
  150. async onCancel(event: any) {
  151. const orderId = event.currentTarget.dataset.id;
  152. // 处理订单取消逻辑
  153. this.setData({ id: orderId });
  154. this.setData({ showConfirm: true });
  155. },
  156. // 取消订单
  157. async cancelOrder(id: string) {
  158. this.setData({ showConfirm: true });
  159. try {
  160. await orderCancelMethod(id);
  161. /* 取消订单逻辑 */
  162. this.setData({ showConfirm: false });
  163. wx.navigateTo({
  164. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  165. });
  166. } catch (error: any) {
  167. getTickleContext.call(this).showWarnMessage(error.errMsg);
  168. }
  169. },
  170. // 确认取消订单
  171. confirmCancelDialog() {
  172. this.cancelOrder(this.data.id);
  173. },
  174. // 关闭取消订单弹窗
  175. closeDialog() {
  176. this.setData({ showConfirm: false });
  177. },
  178. // 确认收货
  179. onConfirmReceiving(e: any) {
  180. console.log(e, "确认收货");
  181. const orderId = e.currentTarget.dataset.id;
  182. wx.navigateTo({
  183. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  184. });
  185. },
  186. // 切换地址
  187. onChangeAddress(e: any) {
  188. const orderId = e.currentTarget.dataset.id;
  189. // 打开地址选择器,选择后更新对应订单的地址
  190. // 伪代码
  191. wx.chooseAddress({
  192. success: (res) => {
  193. // 假设你有 orders 数组
  194. const idx = this.data.orders.findIndex((o) => o.orderId === orderId);
  195. if (idx !== -1) {
  196. this.setData({
  197. [`orders[${idx}].address`]: res.detailInfo,
  198. });
  199. }
  200. },
  201. });
  202. },
  203. // 订单详情
  204. onOrderDetail(e: any) {
  205. const id = e.currentTarget.dataset.id;
  206. // const status = e.currentTarget.dataset.status;
  207. wx.navigateTo({
  208. url: `/module/article/pages/order-detail/order-detail?id=${id}`,
  209. });
  210. },
  211. });