order-list.ts 10 KB

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