order-action.js 7.96 KB
/* eslint-disable operator-linebreak */
/* eslint-disable space-before-function-paren */
import { createNamespacedHelpers } from 'vuex';
const { mapActions, mapState } = createNamespacedHelpers('order/orderList');

import { orderActionsMap, ownType } from 'constants/order-constants';
import DialogConfirmInfo from '../../components/dialog-confirm-info';
import DialogChangeBidPrice from '../../components/dialog-change-bid-price';

export default {
  data() {
    return {
      isMixin: true,
    };
  },
  computed: {
    ...mapState(['changePrice']),
  },
  methods: {
    ...mapActions([
      'cancelTradeConfirmInfo',
      'cancelTrade',
      'deleteOrder',
      'computeChangePrice',
      'confirmChangePrice',
    ]),
    async onSellerOrderAction({ action, order }) {
      const { owner = ownType.SELL } = this.$route.params;
      const { orderCode, earnestMoney } = order;

      switch (action.name) {
        case orderActionsMap.DEL_ORDER.name: {
          this.deleteOrderConfirmDialog({ orderCode, owner });
          break;
        }
        case orderActionsMap.NOT_SOLD.name: {
          const confirmInfo = await this.cancelTradeConfirmInfo({
            orderCode,
            owner,
          });

          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) {
                  this.fetchOrderDetail(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.PAY_EARNESTMONEY.name: {
          this.$createOrderPayType({
            orderCode,
            price: earnestMoney,
            desc: '保证金',

            // 支付成功会跳route
            extra: JSON.stringify({
              back: {
                name: 'sellOrderDetail',
                params: { owner, orderCode },
              },
            }),
          }).show();
          break;
        }

        default:
          return;
      }
    },
    async onBuyerOrderAction({ action, order }) {
      const { owner = ownType.SELL } = this.$route.params;
      const {
        orderCode,
        priceInfo = {},
        bidDepositInfo = {},
        goodsInfo,
      } = order;

      switch (action.name) {
        case orderActionsMap.DEL_ORDER.name: {
          this.deleteOrderConfirmDialog({ orderCode, owner });
          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.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 = await this.confirmChangePrice({
                  price: this.changePrice,
                  orderCode,
                });

                if (isOk) {
                  this.fetchOrderDetail(this.$route.params);
                }
              },
            },
            createElement => {
              return [
                createElement(DialogChangeBidPrice, {
                  props: {
                    computePriceInfo,
                    goodsInfo,
                    orderCode,
                  },
                  slot: 'content',
                }),
              ];
            },
          ).show();
          break;
        }
        case orderActionsMap.CANCEL_ORDER.name: {
          let confirmInfo = await this.cancelTradeConfirmInfo({
            orderCode,
            owner,
          });

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

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

                if (isOk) {
                  this.fetchOrderDetail(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: {
          this.$createOrderPayType({
            orderCode,
            price: parseFloat(priceInfo.realPayPrice || ''),
            desc: '',
            extra: JSON.stringify({
              back: {
                name: 'buyOrderDetail',
                params: {
                  owner,
                  orderCode,
                },
              },
            }),
          }).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) {
                this.fetchOrderDetail(this.$route.params);
              }

              // const txt = isOk ? "收货成功" : "收货失败";
              // this.$createToast({ txt, type: "txt" }).show();
            },
          }).show();
          break;
        }
        default:
          return;
      }
    },

    // 删除订单
    deleteOrderConfirmDialog({ orderCode, owner }) {
      this.$createConfirmDialog({
        type: 'confirm',
        content: '确认删除订单?',
        confirmBtn: { style: { color: '#D0021B' } },
        onConfirm: async () => {
          const isOk = await this.deleteOrder({
            orderCode,
            owner,
          });

          if (isOk) {
            this.$router.back();
          }

          // const txt = isOk ? "删除成功" : "删除失败";
          // this.$createToast({ txt, type: "txt" }).show();
        },
      }).show();
    },
  },
};