order-detail.ts 6.2 KB

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