order-list.ts 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. payingId: '',
  36. showAddress: false,
  37. // 节流控制
  38. throttleTimers: {} as Record<string, boolean>, // 存储各个按钮的节流定时器
  39. },
  40. computed: {},
  41. // 节流函数 - 防止短时间内重复点击
  42. throttle(func: Function, delay: number = 1000, key: string = "default") {
  43. return (...args: any[]) => {
  44. // 如果该按钮正在节流中,直接返回
  45. if (this.data.throttleTimers[key]) {
  46. console.log(`按钮 ${key} 正在节流中,忽略点击`);
  47. return;
  48. }
  49. // 设置节流定时器
  50. this.setData({
  51. [`throttleTimers.${key}`]: true,
  52. });
  53. // 执行函数
  54. const result = func.apply(this, args);
  55. // 延迟清除节流状态
  56. setTimeout(() => {
  57. this.setData({
  58. [`throttleTimers.${key}`]: false,
  59. });
  60. }, delay);
  61. return result;
  62. };
  63. },
  64. onLoad(options: any) {
  65. // 读取 tab 参数,默认为 all
  66. console.log(options, "options");
  67. const tab = options?.tab || "all";
  68. this.setData({ currentTab: tab });
  69. // this.filterOrdersList(tab);
  70. },
  71. onShow() {
  72. this.filterOrdersList(this.data.currentTab);
  73. // 读取地址
  74. // if (JSON.stringify(this.data.selectedAddress) !== "{}") {
  75. // this.setData({
  76. // name: this.data.selectedAddress?.liaison,
  77. // phone: this.data.selectedAddress?.phone,
  78. // address: this.data.selectedAddress?.fullAddress,
  79. // });
  80. // }
  81. },
  82. // 切换tab
  83. onTabChange(event: any) {
  84. const type = event.detail.value;
  85. console.log(type, "传来的type");
  86. this.setData({ currentTab: type });
  87. this.filterOrdersList(type);
  88. },
  89. // 获取列表数据
  90. async getOrderList(progress: string) {
  91. const patientId = wx.getStorageSync("patientId");
  92. if (!patientId) {
  93. getTickleContext.call(this).showWarnMessage("请先登录");
  94. return;
  95. }
  96. const res = await orderListMethod(patientId, progress);
  97. if (res && res.data) {
  98. res.data.forEach((item: any) => {
  99. item.address = `${item.provinceName}${item.cityName}${item.areaName}${item.detailAddress}`;
  100. item.liaison =
  101. item.liaison !== null && item.liaison !== undefined
  102. ? item.liaison
  103. : "";
  104. item.phone =
  105. item.phone !== null && item.phone !== undefined ? item.phone : "";
  106. if (
  107. !item.provinceName ||
  108. !item.cityName ||
  109. !item.areaName ||
  110. !item.detailAddress ||
  111. !item.liaison ||
  112. !item.phone
  113. ) {
  114. item.showAddress = false;
  115. } else {
  116. item.showAddress = true;
  117. }
  118. });
  119. this.setData({ orders: res.data });
  120. }
  121. },
  122. // 过滤订单l列表
  123. filterOrdersList(type: any) {
  124. // 根据当前选中的标签过滤订单
  125. console.log(type, "根据传来的type获取不同状态下的列表");
  126. switch (type) {
  127. // 全部订单
  128. case "all":
  129. this.getOrderList("");
  130. break;
  131. // 待付款订单
  132. case "pending":
  133. this.getOrderList("0");
  134. break;
  135. // 待收货订单
  136. case "received":
  137. this.getOrderList("6");
  138. break;
  139. // 交易完成订单
  140. case "completed":
  141. this.getOrderList("345");
  142. break;
  143. default:
  144. break;
  145. }
  146. },
  147. // 订单支付
  148. onPay: function (event: any) {
  149. return this.throttle(
  150. async (e: any) => {
  151. const orderId = e.currentTarget.dataset.id;
  152. if (this.data.payingId) return; // 防重复
  153. this.setData({ paying: true, payingId: orderId });
  154. try {
  155. await orderPayMethod(orderId);
  156. wx.navigateTo({
  157. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  158. });
  159. } catch (error: any) {
  160. this.setData({ paying: false, payingId: '' });
  161. // getTickleContext.call(this).showWarnMessage(error.errMsg);
  162. wx.showToast({
  163. title: error.errMsg,
  164. icon: "none",
  165. });
  166. } finally {
  167. this.setData({ paying: false, payingId: '' });
  168. }
  169. },
  170. 2000,
  171. "pay"
  172. )(event);
  173. },
  174. // 查看物流
  175. onSeeLogistics: function (e: any) {
  176. return this.throttle(
  177. (event: any) => {
  178. console.log(event, "查看物流");
  179. wx.showToast({
  180. title: "暂未开通",
  181. icon: "none",
  182. });
  183. // const id = event.currentTarget.dataset.id;
  184. // wx.navigateTo({
  185. // url: `/module/article/pages/see-logistics/see-logistics?id=${id}`,
  186. // });
  187. },
  188. 2000,
  189. "logistics"
  190. )(e);
  191. },
  192. // 切换地址
  193. changeAddress: function (e: any) {
  194. return this.throttle(
  195. (event: any) => {
  196. const orderStatus = event.currentTarget.dataset.status;
  197. const id = event.currentTarget.dataset.id;
  198. console.log(orderStatus, "切换地址");
  199. // 根据订单状态判断是否可以切换地址. 待支付状态下可以切换地址
  200. if (orderStatus === "0") {
  201. wx.navigateTo({
  202. url:
  203. "/module/article/pages/manage-address/manage-address?type=orderList&orderId=" +
  204. id,
  205. });
  206. }
  207. },
  208. 2000,
  209. "changeAddress"
  210. )(e);
  211. },
  212. // 打开取消订单弹窗
  213. onCancel: function (event: any) {
  214. return this.throttle(
  215. (e: any) => {
  216. const orderId = e.currentTarget.dataset.id;
  217. // 处理订单取消逻辑
  218. this.setData({ id: orderId });
  219. this.setData({ showConfirm: true });
  220. },
  221. 2000,
  222. "cancel"
  223. )(event);
  224. },
  225. // 取消订单
  226. async cancelOrder(id: string) {
  227. this.setData({ showConfirm: true });
  228. try {
  229. await orderCancelMethod(id);
  230. /* 取消订单逻辑 */
  231. this.setData({ showConfirm: false });
  232. wx.navigateTo({
  233. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  234. });
  235. } catch (error: any) {
  236. getTickleContext.call(this).showWarnMessage(error.errMsg);
  237. }
  238. },
  239. // 确认取消订单
  240. confirmCancelDialog() {
  241. this.cancelOrder(this.data.id);
  242. },
  243. // 关闭取消订单弹窗
  244. closeDialog() {
  245. this.setData({ showConfirm: false });
  246. },
  247. // 确认收货
  248. onConfirmReceiving: function (e: any) {
  249. return this.throttle(
  250. (event: any) => {
  251. console.log(event, "确认收货");
  252. const orderId = event.currentTarget.dataset.id;
  253. wx.navigateTo({
  254. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  255. });
  256. },
  257. 2000,
  258. "confirmReceiving"
  259. )(e);
  260. },
  261. // 切换地址
  262. onChangeAddress(e: any) {
  263. const orderId = e.currentTarget.dataset.id;
  264. // 打开地址选择器,选择后更新对应订单的地址
  265. // 伪代码
  266. wx.chooseAddress({
  267. success: (res) => {
  268. // 假设你有 orders 数组
  269. const idx = this.data.orders.findIndex(
  270. (o: any) => o.orderId === orderId
  271. );
  272. if (idx !== -1) {
  273. this.setData({
  274. [`orders[${idx}].address`]: res.detailInfo,
  275. });
  276. }
  277. },
  278. });
  279. },
  280. // 订单详情
  281. onOrderDetail(e: any) {
  282. const id = e.currentTarget.dataset.id;
  283. // const status = e.currentTarget.dataset.status;
  284. wx.navigateTo({
  285. url: `/module/article/pages/order-detail/order-detail?id=${id}`,
  286. });
  287. },
  288. //回到我的页面
  289. goMine() {
  290. console.log("goMine");
  291. wx.redirectTo({
  292. url: "/pages/mine/mine",
  293. });
  294. }
  295. });