order-action.js 7.53 KB
/* eslint-disable operator-linebreak */
/* eslint-disable space-before-function-paren */
import { orderActionsMap, ownType } from 'constants/order-constants';
import { createNamespacedHelpers } from 'vuex';
import DialogConfirmInfo from '../components/dialog-confirm-info';
import DialogChangeBidPrice from '../components/dialog-change-bid-price';

const { mapActions, mapMutations } = createNamespacedHelpers('order/orderList');

export default {
  data() {
    return {
      // 求购调价
      changePrice: 0,
    };
  },
  methods: {
    ...mapActions([
      'cancelTradeConfirmInfo',
      'cancelTrade',
      'deleteOrder',
      'computeChangePrice',
      'confirmChangePrice',
    ]),
    ...mapMutations(['filterOrderList', 'resetData']),

    async onAction({ action, order, isDetail = false } = {}) {
      const { owner = ownType.SELL } = this.$route.params;
      const {
        orderCode,
        realPrice = '',
        bidDepositInfo = {},
        goodsInfo = {},
        priceInfo = {},
      } = order;

      switch (action.name) {
        case orderActionsMap.DEL_ORDER.name:
          this.$createConfirmDialog({
            content: '确认删除订单?',
            confirmBtn: { style: { color: '#D0021B' } },
            cancelBtn: { active: true },
            onConfirm: async () => {
              const isOk = await this.deleteOrder({
                orderCode,
                owner,
              });

              if (isOk) {
                if (isDetail) {
                  this.$router.back();
                } else {
                  this.filterOrderList(orderCode);
                }
              }
              const txt = isOk ? '删除成功' : '删除失败';

              this.$createToast({ txt, type: 'txt' }).show();
            },
          }).show();
          break;

        case orderActionsMap.CANCEL_ORDER.name: {
          let confirmInfo = await this.cancelTradeConfirmInfo({
            orderCode,
            owner,
          });

          if (Array.isArray(confirmInfo)) {
            confirmInfo = { confirmDesc: '确定取消求购' };
          }

          const confirmBtnText = confirmInfo.needPenalty
            ? '赔付并取消订单'
            : '取消订单';

          this.$createConfirmDialog(
            {
              confirmBtn: { text: confirmBtnText, style: { color: '#D0021B' } },
              cancelBtn: { text: '保留订单', active: true },
              onConfirm: async () => {
                const isOk = await this.cancelTrade({
                  orderCode,
                  owner,
                });

                if (isOk) {
                  if (isDetail) {
                    this.fetchOrderDetail(this.$route.params);
                  } else {
                    this.resetData();
                    this.fetchData(this.$route.params);
                  }
                }
                const txt = isOk ? '取消成功' : '取消失败';

                this.$createToast({ txt, type: 'txt' }).show();
              },
            },
            createElement => {
              return [
                createElement(DialogConfirmInfo, {
                  props: { info: confirmInfo },
                  slot: 'content',
                }),
              ];
            },
          ).show();
          break;
        }

        case orderActionsMap.NOW_BUY.name: {
          let price = realPrice,
            pageBackName = 'OrderList';

          if (isDetail) {
            price = priceInfo.realPayPrice;
            pageBackName = 'buyOrderDetail';
          }
          this.$createOrderPayType({
            orderCode,
            price: parseFloat(price || ''),
            desc: '',
            extra: JSON.stringify({
              back: {
                name: pageBackName,
                params: {
                  owner,
                  orderCode,
                },
              },
            }),
          }).show();
          break;
        }

        case orderActionsMap.PAY_DEPOSIT.name: {
          this.$createOrderPayType({
            orderCode,
            price: parseFloat(bidDepositInfo.depositAmount),
            desc: '支付定金',
            extra: JSON.stringify({
              forward: {
                name: 'OrderList',
                params: {
                  owner: 'buy',
                },
              },
            }),
          }).show();
          break;
        }

        case orderActionsMap.CONFIRM_DELIVERY.name: {
          this.$createConfirmDialog({
            content: '确认收货?',
            confirmBtn: { style: { color: '#D0021B' } },
            cancelBtn: { active: true },
            onConfirm: async () => {
              const isOk = await this.confirmReceipt({
                orderCode,
                owner,
              });

              if (isOk) {
                if (isDetail) {
                  this.fetchOrderDetail(this.$route.params);
                } else {
                  this.resetData();
                  this.fetchData(this.$route.params);
                }
              }
            },
          }).show();
          break;
        }

        case orderActionsMap.CHANGE_BID_PRICE.name: {
          const { goodPrice } = goodsInfo;
          const computePriceInfo = await this.computeChangePrice({
            orderCode,
            price: goodPrice,
          });

          if (typeof computePriceInfo === 'string') {
            this.$createToast({
              type: 'alert',
              txt: computePriceInfo,
              mask: true,
            }).show();
            return;
          }

          this.$createDialog(
            {
              type: 'prompt',
              confirmBtn: { text: '调整求购价' },
              cancelBtn: { active: true },
              onConfirm: async () => {
                if (!this.changePrice) {
                  return;
                }
                const { isOk, errMsg = '', bidData} = await this.confirmChangePrice({
                  price: this.changePrice,
                  orderCode,
                });

                if (isOk) {
                  // 重新支付保证金
                  this.$createOrderPayType({
                    orderCode: bidData.orderCode,
                    price: parseFloat(bidData.depositAmount),
                    desc: '支付定金',
                    extra: JSON.stringify({
                      forward: {
                        name: isDetail ? 'buyOrderDetail' : 'OrderList',
                        params: this.$route.params,
                      },
                    }),
                  }).show();

                  // if (isDetail) {
                  //   this.fetchOrderDetail(this.$route.params);
                  // } else {
                  //   this.resetData();
                  //   this.fetchData(this.$route.params);
                  // }
                } else {
                  if (errMsg) {
                    this.$createToast({
                      type: 'alert',
                      txt: errMsg,
                    });
                  }
                }
              },
            },
            createElement => {
              return [
                createElement(DialogChangeBidPrice, {
                  props: {
                    computePriceInfo,
                    goodsInfo,
                    orderCode,
                    onChangePrice: v => (this.changePrice = v),
                  },
                  slot: 'content',
                }),
              ];
            },
          ).show();
          break;
        }
        default:
          return;
      }
    },
  },
};