order-list.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, {
  3. getTickleContext,
  4. } from "../../../../core/behavior/tickle.behavior";
  5. import { handleWeChatPayment } from "../../../../utils/util";
  6. import {
  7. orderListMethod,
  8. orderCancelMethod,
  9. orderPayMethod,
  10. } from "../../request";
  11. import I18nBehavior from "../../../../i18n/behavior";
  12. // module/article/pages/order-list/order-list.ts
  13. const i18n = {
  14. orderText: {
  15. mineOrder: "我的服务",
  16. paying: "待确认",
  17. paid: "已确认",
  18. paySuccess: "已完成",
  19. },
  20. };
  21. Page({
  22. behaviors: [PageContainerBehavior, tickleBehavior,I18nBehavior],
  23. properties: {},
  24. data: {
  25. i18n,
  26. showConfirm: false,
  27. currentTab: "all", // 当前选中的标签
  28. orders: [],
  29. filteredOrders: [], // 用于存储过滤后的订单
  30. patientId: 0,
  31. id: "",
  32. statusObj: {
  33. 0: "待付款",
  34. // 6: "待收货",
  35. 6: "已付款",
  36. 345: "交易成功",
  37. 2: "交易关闭",
  38. },
  39. statusClassObj: {
  40. 0: "status-pending",
  41. 6: "status-received",
  42. 345: "status-completed",
  43. 2: "status-closed",
  44. },
  45. selectedAddress: null,
  46. paying: false,
  47. payingId: '',
  48. showAddress: false,
  49. // 节流控制
  50. throttleTimers: {} as Record<string, boolean>,
  51. expandedItems: {} as Record<string, boolean>,
  52. },
  53. computed: {},
  54. // 节流 - 防止短时间内重复点击
  55. throttle(func: Function, delay: number = 1000, key: string = "default") {
  56. return (...args: any[]) => {
  57. // 如果该按钮正在节流中,直接返回
  58. if (this.data.throttleTimers[key]) {
  59. return;
  60. }
  61. // 设置节流定时器
  62. this.setData({
  63. [`throttleTimers.${key}`]: true,
  64. });
  65. // 执行函数
  66. const result = func.apply(this, args);
  67. // 延迟清除节流状态
  68. setTimeout(() => {
  69. this.setData({
  70. [`throttleTimers.${key}`]: false,
  71. });
  72. }, delay);
  73. return result;
  74. };
  75. },
  76. onLoad(options: any) {
  77. // 读取 tab 参数,默认为 all
  78. const tab = options?.tab || "all";
  79. this.setData({ currentTab: tab });
  80. },
  81. onShow() {
  82. this.filterOrdersList(this.data.currentTab);
  83. },
  84. // 切换tab
  85. onTabChange(event: any) {
  86. const type = event.detail.value;
  87. this.setData({ currentTab: type });
  88. this.filterOrdersList(type);
  89. },
  90. // 获取列表数据
  91. async getOrderList(progress: string) {
  92. const patientId = wx.getStorageSync("patientId");
  93. if (!patientId) {
  94. getTickleContext.call(this).showWarnMessage("请先登录");
  95. return;
  96. }
  97. const res = await orderListMethod(patientId, progress);
  98. if (res && res.data) {
  99. res.data.forEach((item: any) => {
  100. item.address = `${item.provinceName}${item.cityName}${item.areaName}${item.detailAddress}`;
  101. item.liaison =
  102. item.liaison !== null && item.liaison !== undefined
  103. ? item.liaison
  104. : "";
  105. item.phone =
  106. item.phone !== null && item.phone !== undefined ? item.phone : "";
  107. if (
  108. !item.provinceName ||
  109. !item.cityName ||
  110. !item.areaName ||
  111. !item.detailAddress ||
  112. !item.liaison ||
  113. !item.phone
  114. ) {
  115. item.showAddress = false;
  116. } else {
  117. item.showAddress = true;
  118. }
  119. if (item.items && Array.isArray(item.items)) {
  120. item.items = item.items.map((subItem: any) => {
  121. return {
  122. id: subItem.id || '',
  123. name: subItem.conditioningProgramName || '',
  124. description: (() => {
  125. const dose = subItem?.convertDose ?? '1';
  126. const unit = subItem?.convertUnit ?? '次';
  127. return `${dose} ${unit}`;
  128. })(),
  129. photo: subItem.conditioningProgramPhoto || '',
  130. price: subItem.unitPrice || 0,
  131. quantity: subItem?.totalMeasure || 0,
  132. }
  133. });
  134. }
  135. });
  136. // 批量初始化展开状态为false(默认折叠)
  137. const expandedItems: Record<string, boolean> = {};
  138. res.data.forEach((item: any) => {
  139. if (!this.data.expandedItems[item.id]) {
  140. expandedItems[item.id] = false;
  141. }
  142. });
  143. if (Object.keys(expandedItems).length > 0) {
  144. this.setData({ expandedItems: { ...this.data.expandedItems, ...expandedItems } });
  145. }
  146. this.setData({ orders: res.data });
  147. }
  148. },
  149. // 过滤订单l列表
  150. filterOrdersList(type: any) {
  151. // 根据当前选中的标签过滤订单
  152. switch (type) {
  153. // 全部订单
  154. case "all":
  155. this.getOrderList("");
  156. break;
  157. // 待付款订单
  158. case "pending":
  159. this.getOrderList("0");
  160. break;
  161. // 已付款订单
  162. case "paid":
  163. this.getOrderList("6");
  164. break;
  165. // 交易完成订单
  166. case "completed":
  167. this.getOrderList("345");
  168. break;
  169. // 交易关闭订单
  170. case "closed":
  171. this.getOrderList("2");
  172. break;
  173. default:
  174. break;
  175. }
  176. },
  177. // 订单支付
  178. onPay: function (event: any) {
  179. return this.throttle(
  180. async (e: any) => {
  181. const orderId = e.currentTarget.dataset.id;
  182. if (this.data.payingId) return; // 防重复
  183. this.setData({ paying: true, payingId: orderId });
  184. try {
  185. // 调用支付接口
  186. const payResult = await orderPayMethod(orderId);
  187. if (payResult && payResult.data) {
  188. const paymentParams = payResult.data;
  189. handleWeChatPayment(paymentParams, (res: any) => {
  190. // 支付成功,跳转到成功页面
  191. wx.redirectTo({
  192. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  193. });
  194. }, (error: any) => {
  195. this.setData({ paying: false, payingId: '' });
  196. if (error.errMsg === 'requestPayment:fail cancel') {
  197. wx.showToast({
  198. title: "支付已取消",
  199. icon: "none",
  200. });
  201. } else {
  202. wx.showToast({
  203. title: error.errMsg || "支付失败,请重试",
  204. icon: "none",
  205. });
  206. }
  207. });
  208. } else {
  209. wx.showToast({
  210. title: payResult.errMsg,
  211. icon: "none",
  212. });
  213. }
  214. } catch (error: any) {
  215. wx.showToast({
  216. title: error.errMsg,
  217. icon: "none",
  218. });
  219. } finally {
  220. this.setData({ paying: false, payingId: '' });
  221. }
  222. },
  223. 2000,
  224. "pay"
  225. )(event);
  226. },
  227. // 查看物流
  228. onSeeLogistics: function (e: any) {
  229. return this.throttle(
  230. (event: any) => {
  231. wx.showToast({
  232. title: "暂未开通",
  233. icon: "none",
  234. });
  235. },
  236. 2000,
  237. "logistics"
  238. )(e);
  239. },
  240. // 切换地址
  241. changeAddress: function (e: any) {
  242. return this.throttle(
  243. (event: any) => {
  244. const orderStatus = event.currentTarget.dataset.status;
  245. const id = event.currentTarget.dataset.id;
  246. // 根据订单状态判断是否可以切换地址. 待付款状态下可以切换地址
  247. if (orderStatus === "0") {
  248. wx.navigateTo({
  249. url:
  250. "/module/article/pages/manage-address/manage-address?type=orderList&orderId=" +
  251. id,
  252. });
  253. }
  254. },
  255. 2000,
  256. "changeAddress"
  257. )(e);
  258. },
  259. // 打开取消订单弹窗
  260. onCancel: function (event: any) {
  261. return this.throttle(
  262. (e: any) => {
  263. const orderId = e.currentTarget.dataset.id;
  264. // 处理订单取消逻辑
  265. this.setData({ id: orderId });
  266. this.setData({ showConfirm: true });
  267. },
  268. 2000,
  269. "cancel"
  270. )(event);
  271. },
  272. // 取消订单
  273. async cancelOrder(id: string) {
  274. this.setData({ showConfirm: true });
  275. try {
  276. await orderCancelMethod(id);
  277. /* 取消订单逻辑 */
  278. this.setData({ showConfirm: false });
  279. wx.navigateTo({
  280. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  281. });
  282. } catch (error: any) {
  283. getTickleContext.call(this).showWarnMessage(error.errMsg);
  284. }
  285. },
  286. // 确认取消订单
  287. confirmCancelDialog() {
  288. this.cancelOrder(this.data.id);
  289. },
  290. // 关闭取消订单弹窗
  291. closeDialog() {
  292. this.setData({ showConfirm: false });
  293. },
  294. // 确认收货
  295. onConfirmReceiving: function (e: any) {
  296. return this.throttle(
  297. (event: any) => {
  298. const orderId = event.currentTarget.dataset.id;
  299. wx.navigateTo({
  300. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  301. });
  302. },
  303. 2000,
  304. "confirmReceiving"
  305. )(e);
  306. },
  307. // 切换地址
  308. onChangeAddress(e: any) {
  309. const orderId = e.currentTarget.dataset.id;
  310. // 打开地址选择器,选择后更新对应订单的地址
  311. // 伪代码
  312. wx.chooseAddress({
  313. success: (res) => {
  314. // 假设你有 orders 数组
  315. const idx = this.data.orders.findIndex(
  316. (o: any) => o.orderId === orderId
  317. );
  318. if (idx !== -1) {
  319. this.setData({
  320. [`orders[${idx}].address`]: res.detailInfo,
  321. });
  322. }
  323. },
  324. });
  325. },
  326. // 订单详情
  327. onOrderDetail(e: any) {
  328. const id = e.currentTarget.dataset.id;
  329. const status = e.currentTarget.dataset.status;
  330. if (status === '0') {
  331. wx.navigateTo({
  332. url: `/module/order/pages/order-detail/order-detail?id=${id}&status=${status}`,
  333. });
  334. } else {
  335. wx.navigateTo({
  336. url: `/module/order/pages/other-detail/other-detail?id=${id}&status=${status}`,
  337. });
  338. }
  339. },
  340. //回到我的页面
  341. goMine() {
  342. wx.redirectTo({
  343. url: "/pages/mine/mine",
  344. });
  345. },
  346. // 切换服务包展开/收起
  347. toggleServicePackages(e: any) {
  348. const orderId = e.currentTarget.dataset.id;
  349. const currentExpanded = this.data.expandedItems[orderId] || false;
  350. this.setData({
  351. [`expandedItems[${orderId}]`]: !currentExpanded,
  352. });
  353. },
  354. });