order-detail.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  2. import tickleBehavior, {
  3. getTickleContext,
  4. } from "../../../../core/behavior/tickle.behavior";
  5. import {
  6. getOrderDetailMethod,
  7. orderPayMethod,
  8. orderCancelMethod,
  9. orderConfirmMethod,
  10. } from "../../request";
  11. // module/article/pages/order-detail/order-detail.ts
  12. Page({
  13. behaviors: [PageContainerBehavior, tickleBehavior],
  14. onLoad(options: any) {
  15. if (options.id) {
  16. this.setData({ id: options.id });
  17. }
  18. },
  19. properties: {},
  20. data: {
  21. showDetail: false,
  22. id: "",
  23. showConfirm: false,
  24. url: "https://pic.nximg.cn/file/20190718/28170468_214109363000_2.jpg",
  25. dialogKey: "",
  26. totalPrice: 0,
  27. orderStatus: "", // 'pending' | 'received' | 'completed' | ''
  28. orderDetail: {},
  29. address: "",
  30. name: "",
  31. phone: "",
  32. // selectedAddress: null,
  33. // 节流控制
  34. isPaymentLoading: false,
  35. isConfirmLoading: false,
  36. },
  37. // 切换收货地址
  38. changeAddress(event: any) {
  39. const orderStatus = event.currentTarget.dataset.status;
  40. // 根据订单状态判断是否可以切换地址. 待支付状态下可以切换地址
  41. if (orderStatus === "0") {
  42. wx.navigateTo({
  43. url:
  44. "/module/article/pages/manage-address/manage-address?type=orderDetail&orderId=" +
  45. this.data.id,
  46. });
  47. }
  48. },
  49. // 订单支付
  50. async payment() {
  51. // 节流控制:如果正在处理中,直接返回
  52. if (this.data.isPaymentLoading) {
  53. return;
  54. }
  55. this.setData({ isPaymentLoading: true });
  56. try {
  57. const res = await orderPayMethod(this.data.id);
  58. wx.navigateTo({
  59. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  60. });
  61. } catch (error: any) {
  62. // getTickleContext.call(this).showWarnMessage(error.errMsg);
  63. wx.showToast({
  64. title: error.errMsg,
  65. icon: "none",
  66. });
  67. } finally {
  68. // 无论成功还是失败,都要重置loading状态
  69. this.setData({ isPaymentLoading: false });
  70. }
  71. },
  72. statusType(status: any) {
  73. // 规范化后端返回:将 null/undefined 转为空字符串,其它转换为字符串
  74. const code = status == null ? "" : String(status);
  75. switch (code) {
  76. case "0":
  77. this.setData({ orderStatus: "pending" });
  78. break;
  79. case "6":
  80. this.setData({ orderStatus: "received" });
  81. break;
  82. case "2":
  83. this.setData({ orderStatus: "closed" });
  84. break;
  85. case "345":
  86. this.setData({ orderStatus: "completed" });
  87. break;
  88. default:
  89. // 未知或空状态,重置为安全空串,避免默认展示为待付款
  90. this.setData({ orderStatus: "" });
  91. break;
  92. }
  93. },
  94. // 订单详情
  95. async load(id: string) {
  96. wx.showLoading({ title: "加载中" });
  97. try {
  98. const res = await getOrderDetailMethod(id);
  99. if (res && res.data) {
  100. this.setData({ orderDetail: res.data });
  101. if (
  102. !res.data.liaison ||
  103. !res.data.phone ||
  104. !res.data.provinceName ||
  105. !res.data.cityName ||
  106. !res.data.areaName ||
  107. !res.data.detailAddress
  108. ) {
  109. this.setData({
  110. showDetail: true,
  111. });
  112. } else {
  113. this.setData({
  114. showDetail: false,
  115. });
  116. this.setData({
  117. name: res.data.liaison ? `${res.data.liaison}` : "",
  118. phone: res.data.phone ? `${res.data.phone}` : "",
  119. address: `${res.data.provinceName}${
  120. res.data.cityName ? `${res.data.cityName}` : ""
  121. }${res.data.areaName ? `${res.data.areaName}` : ""}${
  122. res.data.detailAddress ? `${res.data.detailAddress}` : ""
  123. }`,
  124. });
  125. }
  126. // 0:待付款
  127. // 2 交易关闭
  128. // 6 待收货
  129. // 345 交易成功
  130. this.statusType(res.data.orderStatus);
  131. const totalPrice = res.data.items.reduce(
  132. (acc: number, item: any) => acc + item.totalPrice,
  133. 0
  134. );
  135. this.setData({ totalPrice });
  136. }
  137. } catch (error: any) {
  138. // getTickleContext.call(this).showWarnMessage(error.errMsg);
  139. wx.showToast({
  140. title: error.errMsg,
  141. icon: "none",
  142. });
  143. }
  144. wx.hideLoading();
  145. },
  146. closeDialog() {
  147. this.setData({ showConfirm: false });
  148. },
  149. async confirmDialog() {
  150. // 点击确定取消订单按钮
  151. const that = this;
  152. /* 取消订单逻辑 */
  153. wx.showModal({
  154. title: "提示",
  155. content: "确认取消该订单吗?",
  156. success: (res) => {
  157. if (res.confirm) {
  158. that.cancelOrdering();
  159. }
  160. },
  161. });
  162. },
  163. // 取消订单
  164. async cancelOrdering() {
  165. try {
  166. await orderCancelMethod(this.data.id);
  167. /* 取消订单逻辑 */
  168. this.setData({ showConfirm: false });
  169. wx.navigateTo({
  170. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  171. });
  172. } catch (error: any) {
  173. // getTickleContext.call(this).showWarnMessage(error.errMsg);
  174. wx.showToast({
  175. title: error.errMsg,
  176. icon: "none",
  177. });
  178. }
  179. },
  180. // 取消订单
  181. async cancelOrder() {
  182. // 确定弹窗出现
  183. this.setData({ showConfirm: true });
  184. },
  185. // 查看物流
  186. viewLogistics() {
  187. wx.showToast({
  188. title: "暂未开通",
  189. icon: "none",
  190. });
  191. // 物流接口现在没有 先空着
  192. // wx.navigateTo({ url: "/module/article/pages/see-logistics/see-logistics" });
  193. },
  194. // 确认收货
  195. async confirmReceipt() {
  196. // 节流控制
  197. if (this.data.isConfirmLoading) {
  198. return;
  199. }
  200. this.setData({ isConfirmLoading: true });
  201. const orderId = this.data.id;
  202. wx.navigateTo({
  203. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  204. });
  205. // 延迟重置loading状态,给页面跳转一些时间
  206. setTimeout(() => {
  207. this.setData({ isConfirmLoading: false });
  208. }, 2000);
  209. // let that = this;
  210. // wx.showModal({
  211. // title: "提示",
  212. // content: "确认收货后,订单将无法修改,请确认无误后再进行操作",
  213. // success: (res) => {
  214. // if (res.confirm) {
  215. // that.confirmReceiving();
  216. // }
  217. // },
  218. // });
  219. },
  220. async confirmReceiving() {
  221. try {
  222. await orderConfirmMethod(this.data.id);
  223. wx.navigateTo({
  224. url: "/module/article/pages/success-page/success-page?title=确认收货成功",
  225. });
  226. // 物流接口现在没有 先空着
  227. // wx.navigateTo({
  228. // url: "/module/article/pages/confirm-receiving/confirm-receiving",
  229. // });
  230. } catch (error: any) {
  231. // getTickleContext.call(this).showWarnMessage(error.errMsg);
  232. wx.showToast({
  233. title: error.errMsg,
  234. icon: "none",
  235. });
  236. }
  237. },
  238. onShow() {
  239. this.load(this.data.id);
  240. },
  241. });