order-detail.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if (this.data.isPaymentLoading) {
  52. return;
  53. }
  54. this.setData({ isPaymentLoading: true });
  55. try {
  56. await orderPayMethod(this.data.id);
  57. wx.redirectTo({
  58. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  59. });
  60. } catch (error: any) {
  61. wx.showToast({
  62. title: error.errMsg,
  63. icon: "none",
  64. });
  65. } finally {
  66. this.setData({ isPaymentLoading: false });
  67. }
  68. },
  69. statusType(status: any) {
  70. const code = status == null ? "" : String(status);
  71. switch (code) {
  72. case "0":
  73. this.setData({ orderStatus: "pending" });
  74. break;
  75. case "6":
  76. this.setData({ orderStatus: "received" });
  77. break;
  78. case "2":
  79. this.setData({ orderStatus: "closed" });
  80. break;
  81. case "345":
  82. this.setData({ orderStatus: "completed" });
  83. break;
  84. default:
  85. this.setData({ orderStatus: "" });
  86. break;
  87. }
  88. },
  89. // 订单详情
  90. async load(id: string) {
  91. wx.showLoading({ title: "加载中" });
  92. try {
  93. const res = await getOrderDetailMethod(id);
  94. if (res && res.data) {
  95. this.setData({ orderDetail: res.data });
  96. if (
  97. !res.data.liaison ||
  98. !res.data.phone ||
  99. !res.data.provinceName ||
  100. !res.data.cityName ||
  101. !res.data.areaName ||
  102. !res.data.detailAddress
  103. ) {
  104. this.setData({
  105. showDetail: true,
  106. });
  107. } else {
  108. this.setData({
  109. showDetail: false,
  110. });
  111. this.setData({
  112. name: res.data.liaison ? `${res.data.liaison}` : "",
  113. phone: res.data.phone ? `${res.data.phone}` : "",
  114. address: `${res.data.provinceName}${
  115. res.data.cityName ? `${res.data.cityName}` : ""
  116. }${res.data.areaName ? `${res.data.areaName}` : ""}${
  117. res.data.detailAddress ? `${res.data.detailAddress}` : ""
  118. }`,
  119. });
  120. }
  121. // 0:待付款
  122. // 2 交易关闭
  123. // 6 待收货
  124. // 345 交易成功
  125. this.statusType(res.data.orderStatus);
  126. const totalPrice = res.data.items.reduce(
  127. (acc: number, item: any) => acc + item.totalPrice,
  128. 0
  129. );
  130. this.setData({ totalPrice });
  131. }
  132. } catch (error: any) {
  133. wx.showToast({
  134. title: error.errMsg,
  135. icon: "none",
  136. });
  137. }
  138. wx.hideLoading();
  139. },
  140. closeDialog() {
  141. this.setData({ showConfirm: false });
  142. },
  143. async confirmDialog() {
  144. const that = this;
  145. wx.showModal({
  146. title: "提示",
  147. content: "确认取消该订单吗?",
  148. success: (res) => {
  149. if (res.confirm) {
  150. that.cancelOrdering();
  151. }
  152. },
  153. });
  154. },
  155. // 取消订单
  156. async cancelOrdering() {
  157. try {
  158. await orderCancelMethod(this.data.id);
  159. this.setData({ showConfirm: false });
  160. wx.navigateTo({
  161. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  162. });
  163. } catch (error: any) {
  164. wx.showToast({
  165. title: error.errMsg,
  166. icon: "none",
  167. });
  168. }
  169. },
  170. // 取消订单
  171. async cancelOrder() {
  172. this.setData({ showConfirm: true });
  173. },
  174. // 查看物流
  175. viewLogistics() {
  176. wx.showToast({
  177. title: "暂未开通",
  178. icon: "none",
  179. });
  180. // 物流接口现在没有 先空着
  181. // wx.navigateTo({ url: "/module/article/pages/see-logistics/see-logistics" });
  182. },
  183. // 确认收货
  184. async confirmReceipt() {
  185. if (this.data.isConfirmLoading) {
  186. return;
  187. }
  188. this.setData({ isConfirmLoading: true });
  189. const orderId = this.data.id;
  190. wx.navigateTo({
  191. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  192. });
  193. // 延迟重置loading状态,给页面跳转一些时间
  194. setTimeout(() => {
  195. this.setData({ isConfirmLoading: false });
  196. }, 2000);
  197. // let that = this;
  198. // wx.showModal({
  199. // title: "提示",
  200. // content: "确认收货后,订单将无法修改,请确认无误后再进行操作",
  201. // success: (res) => {
  202. // if (res.confirm) {
  203. // that.confirmReceiving();
  204. // }
  205. // },
  206. // });
  207. },
  208. async confirmReceiving() {
  209. try {
  210. await orderConfirmMethod(this.data.id);
  211. wx.navigateTo({
  212. url: "/module/article/pages/success-page/success-page?title=确认收货成功",
  213. });
  214. // 物流接口现在没有 先空着
  215. // wx.navigateTo({
  216. // url: "/module/article/pages/confirm-receiving/confirm-receiving",
  217. // });
  218. } catch (error: any) {
  219. wx.showToast({
  220. title: error.errMsg,
  221. icon: "none",
  222. });
  223. }
  224. },
  225. onShow() {
  226. if(this.data.id){
  227. this.load(this.data.id);
  228. }
  229. },
  230. });