order-detail.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import DictionariesBehavior from "../../../../core/behavior/dictionaries.behavior";
  2. import PageContainerBehavior from "../../../../core/behavior/page-container.behavior";
  3. import { handleWeChatPayment } from "../../../../utils/util";
  4. import tickleBehavior, {
  5. getTickleContext,
  6. } from "../../../../core/behavior/tickle.behavior";
  7. import { getOrderDetailMethod, orderPayMethod } from "../../request";
  8. // module/order/pages/select-goods/select-goods.ts
  9. Page({
  10. behaviors: [PageContainerBehavior, DictionariesBehavior, tickleBehavior],
  11. properties: {
  12. schemeId: { type: String, value: "" },
  13. },
  14. data: {
  15. title: "",
  16. id: "",
  17. totalGoodsCount: 0,
  18. goodsList: [] as Array<{
  19. category: string;
  20. goods: Array<{
  21. id: string;
  22. name: string;
  23. description?: string;
  24. image: string;
  25. price: number;
  26. }>;
  27. }>,
  28. selectAll: true,
  29. selectedCount: 0,
  30. totalPrice: 0,
  31. orderDetail: {},
  32. remarkLength: 0,
  33. showDetail: false,
  34. isPaymentLoading: false, // 支付加载状态
  35. orderStatus: "", // 订单状态:pending, paid, closed, completed
  36. },
  37. onLoad(options: any) {
  38. const remark = (this.data.orderDetail as any)?.remark || '';
  39. this.setData({
  40. remarkLength: remark.length
  41. });
  42. if (options.id) {
  43. this.setData({ id: options.id });
  44. }
  45. if (options.status) {
  46. this.statusType(options.status);
  47. }
  48. },
  49. onShow() {
  50. if (this.data.id) {
  51. this.load(this.data.id);
  52. }
  53. },
  54. observers: {},
  55. // 切换收货地址
  56. changeAddress(_event: any) {
  57. // 只有待付款状态才能切换地址
  58. if (this.data.orderStatus !== "pending") {
  59. return;
  60. }
  61. wx.navigateTo({
  62. url:
  63. "/module/article/pages/manage-address/manage-address?type=orderDetail&orderId=" +
  64. this.data.id,
  65. });
  66. },
  67. statusType(status: any) {
  68. const code = status == null ? "" : String(status);
  69. switch (code) {
  70. case "0":
  71. this.setData({ orderStatus: "pending", title: "待付款" });
  72. break;
  73. case "6":
  74. this.setData({ orderStatus: "received", title: "已付款" });
  75. break;
  76. case "2":
  77. this.setData({ orderStatus: "closed", title: "交易关闭" });
  78. break;
  79. case "345":
  80. this.setData({ orderStatus: "completed", title: "交易成功" });
  81. break;
  82. default:
  83. this.setData({ orderStatus: "", title: "" });
  84. break;
  85. }
  86. },
  87. // 订单详情
  88. async load(id: string) {
  89. wx.showLoading({ title: "加载中" });
  90. try {
  91. const res = await getOrderDetailMethod(id);
  92. if (res && res.data) {
  93. this.setData({ orderDetail: res.data });
  94. if (
  95. !res.data.liaison ||
  96. !res.data.phone ||
  97. !res.data.provinceName ||
  98. !res.data.cityName ||
  99. !res.data.areaName ||
  100. !res.data.detailAddress
  101. ) {
  102. this.setData({
  103. showDetail: true,
  104. });
  105. } else {
  106. this.setData({
  107. showDetail: false,
  108. });
  109. this.setData({
  110. name: res.data.liaison ? `${res.data.liaison}` : "",
  111. phone: res.data.phone ? `${res.data.phone}` : "",
  112. address: `${res.data.provinceName}${res.data.cityName ? `${res.data.cityName}` : ""
  113. }${res.data.areaName ? `${res.data.areaName}` : ""}${res.data.detailAddress ? `${res.data.detailAddress}` : ""
  114. }`,
  115. });
  116. }
  117. // 0:待付款
  118. // 2 交易关闭
  119. // 6 已付款
  120. // 345 交易成功
  121. this.statusType(res.data.orderStatus);
  122. const groupForPendingPay = res.data?.groupForPendingPay || {};
  123. const goodsList: any[] = [];
  124. let totalGoodsCount = 0;
  125. // 遍历 groupForPendingPay
  126. Object.keys(groupForPendingPay).forEach((categoryName: string) => {
  127. const categoryItems = groupForPendingPay[categoryName] || [];
  128. if (categoryItems.length > 0) {
  129. const goods = categoryItems.map((item: any) => {
  130. const pricingType = item?.conditioningProgramDetail?.pricingType; //0是一口价 1按穴位/经络次数计费
  131. // 合计的商品件数
  132. totalGoodsCount += 1;
  133. return {
  134. id: item.id || '',
  135. name: item.conditioningProgramName || '',
  136. description: (() => {
  137. const dose = item?.convertDose ?? '';
  138. const unit = item?.convertUnit ?? '';
  139. return pricingType === '0' ? `${dose} ${unit}` : `1次`;
  140. })(),
  141. image: item.conditioningProgramPhoto || '',
  142. // price: item.totalPrice || 0,
  143. price: item?.unitPrice || 0,
  144. quantity: item?.totalMeasure || 0,
  145. };
  146. });
  147. goodsList.push({
  148. category: categoryName,
  149. goods: goods
  150. });
  151. }
  152. });
  153. this.setData({ goodsList, totalGoodsCount });
  154. }
  155. } catch (error: any) {
  156. wx.showToast({
  157. title: error.errMsg,
  158. icon: "none",
  159. });
  160. }
  161. wx.hideLoading();
  162. },
  163. // 立即支付
  164. async onPayment() {
  165. if (this.data.isPaymentLoading) {
  166. return;
  167. }
  168. this.setData({ isPaymentLoading: true });
  169. try {
  170. // 调用支付接口
  171. const payResult = await orderPayMethod(this.data.id, (this.data.orderDetail as any)?.remark || '');
  172. if (payResult) {
  173. const paymentParams = payResult;
  174. handleWeChatPayment(paymentParams, (res: any) => {
  175. // 支付成功,跳转到成功页面
  176. wx.redirectTo({
  177. url: "/module/article/pages/success-page/success-page?title=订单支付成功",
  178. });
  179. }, (error: any) => {
  180. this.setData({ isPaymentLoading: false });
  181. if (error?.errMsg === 'requestPayment:fail cancel') {
  182. // 支付取消跳到支付订单页面
  183. wx.navigateBack({ delta: 1 });
  184. }
  185. console.log(error, '支付失败');
  186. // wx.showToast({
  187. // title: error?.errMsg || error?.message || "支付失败,请重试",
  188. // icon: "none",
  189. // });
  190. });
  191. } else {
  192. wx.showToast({
  193. title: payResult.errMsg,
  194. icon: "none",
  195. });
  196. }
  197. } catch (error: any) {
  198. wx.showToast({
  199. title: error.errMsg,
  200. icon: "none",
  201. });
  202. } finally {
  203. this.setData({ isPaymentLoading: false });
  204. }
  205. },
  206. // 备注输入处理
  207. onRemarkInput(e: any) {
  208. const value = e.detail.value;
  209. const length = value.length;
  210. // 限制最大长度为200
  211. if (length > 200) {
  212. return;
  213. }
  214. this.setData({
  215. 'orderDetail.remark': value,
  216. remarkLength: length
  217. });
  218. },
  219. // 复制订单号
  220. copyOrderNo(e: any) {
  221. const orderNo = e.currentTarget.dataset.orderno;
  222. if (!orderNo) {
  223. getTickleContext.call(this).showWarnMessage("订单号为空");
  224. return;
  225. }
  226. wx.setClipboardData({
  227. data: orderNo,
  228. success: () => {
  229. wx.showToast({
  230. title: '已复制',
  231. icon: 'none'
  232. });
  233. }
  234. });
  235. },
  236. // 返回上一页
  237. onGoBack() {
  238. wx.navigateBack({
  239. delta: 1
  240. });
  241. },
  242. });