order-list.ts 6.4 KB

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