order-detail.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. import { getFullImageUrl } from "../../../../utils/util";
  12. // module/article/pages/order-detail/order-detail.ts
  13. Page({
  14. behaviors: [PageContainerBehavior, tickleBehavior],
  15. onLoad(options: any) {
  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. res.data.photo = getFullImageUrl(res.data.photo);
  97. this.setData({ orderDetail: res.data });
  98. if (
  99. !res.data.liaison ||
  100. !res.data.phone ||
  101. !res.data.provinceName ||
  102. !res.data.cityName ||
  103. !res.data.areaName ||
  104. !res.data.detailAddress
  105. ) {
  106. this.setData({
  107. showDetail: true,
  108. });
  109. } else {
  110. this.setData({
  111. showDetail: false,
  112. });
  113. this.setData({
  114. name: res.data.liaison ? `${res.data.liaison}` : "",
  115. phone: res.data.phone ? `${res.data.phone}` : "",
  116. address: `${res.data.provinceName}${
  117. res.data.cityName ? `${res.data.cityName}` : ""
  118. }${res.data.areaName ? `${res.data.areaName}` : ""}${
  119. res.data.detailAddress ? `${res.data.detailAddress}` : ""
  120. }`,
  121. });
  122. }
  123. // 0:待付款
  124. // 2 交易关闭
  125. // 6 待收货
  126. // 345 交易成功
  127. this.statusType(res.data.orderStatus);
  128. const totalPrice = res.data.items.reduce(
  129. (acc: number, item: any) => acc + item.totalPrice,
  130. 0
  131. );
  132. this.setData({ totalPrice });
  133. }
  134. } catch (error: any) {
  135. wx.showToast({
  136. title: error.errMsg,
  137. icon: "none",
  138. });
  139. }
  140. wx.hideLoading();
  141. },
  142. closeDialog() {
  143. this.setData({ showConfirm: false });
  144. },
  145. async confirmDialog() {
  146. const that = this;
  147. wx.showModal({
  148. title: "提示",
  149. content: "确认取消该订单吗?",
  150. success: (res) => {
  151. if (res.confirm) {
  152. that.cancelOrdering();
  153. }
  154. },
  155. });
  156. },
  157. // 取消订单
  158. async cancelOrdering() {
  159. try {
  160. await orderCancelMethod(this.data.id);
  161. this.setData({ showConfirm: false });
  162. wx.navigateTo({
  163. url: "/module/article/pages/success-page/success-page?title=订单取消成功",
  164. });
  165. } catch (error: any) {
  166. wx.showToast({
  167. title: error.errMsg,
  168. icon: "none",
  169. });
  170. }
  171. },
  172. // 取消订单
  173. async cancelOrder() {
  174. this.setData({ showConfirm: true });
  175. },
  176. // 查看物流
  177. viewLogistics() {
  178. wx.showToast({
  179. title: "暂未开通",
  180. icon: "none",
  181. });
  182. // 物流接口现在没有 先空着
  183. // wx.navigateTo({ url: "/module/article/pages/see-logistics/see-logistics" });
  184. },
  185. // 确认收货
  186. async confirmReceipt() {
  187. if (this.data.isConfirmLoading) {
  188. return;
  189. }
  190. this.setData({ isConfirmLoading: true });
  191. const orderId = this.data.id;
  192. wx.navigateTo({
  193. url: `/module/article/pages/confirm-receiving/confirm-receiving?orderId=${orderId}`,
  194. });
  195. // 延迟重置loading状态,给页面跳转一些时间
  196. setTimeout(() => {
  197. this.setData({ isConfirmLoading: false });
  198. }, 2000);
  199. // let that = this;
  200. // wx.showModal({
  201. // title: "提示",
  202. // content: "确认收货后,订单将无法修改,请确认无误后再进行操作",
  203. // success: (res) => {
  204. // if (res.confirm) {
  205. // that.confirmReceiving();
  206. // }
  207. // },
  208. // });
  209. },
  210. async confirmReceiving() {
  211. try {
  212. await orderConfirmMethod(this.data.id);
  213. wx.navigateTo({
  214. url: "/module/article/pages/success-page/success-page?title=确认收货成功",
  215. });
  216. // 物流接口现在没有 先空着
  217. // wx.navigateTo({
  218. // url: "/module/article/pages/confirm-receiving/confirm-receiving",
  219. // });
  220. } catch (error: any) {
  221. wx.showToast({
  222. title: error.errMsg,
  223. icon: "none",
  224. });
  225. }
  226. },
  227. onShow() {
  228. if(this.data.id){
  229. this.load(this.data.id);
  230. }
  231. },
  232. });