order-action.js 10.8 KB
/* eslint-disable new-cap */
/* eslint-disable indent */
/* eslint-disable lines-around-comment */
/* 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 refreshMixin from './refresh';

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

export default {
  mixins: [refreshMixin],
  methods: {
    ...mapActions([
      'cancelTradeConfirmInfo',
      'cancelTrade',
      'deleteOrder',
      'computeChangePrice',
      'confirmChangePrice',
      'confirmReceipt',
    ]),
    ...mapMutations(['filterOrderList', 'resetData', 'resetOrderListItem']),
    ...addressMutations(['STORE_UPDATE_ADDRESS_INFO']),

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

        // 订单类表和订单详情地址信息返回地段不一致
        addressInfo,
        userAddress,
      } = order;
      const { productId, storageId, skup, typeTag } = goodsInfo;

      if (isDetail) {
        this.setIsRefresh(true);
      }

      switch (action.name) {
        // 再次购买
        case orderActionsMap.BUY_AGAIN.name: {
          this.$router.push({
            name: 'ProductDetail',
            params: { productId },
          });
          break;
        }

        // 查看详情
        case orderActionsMap.SHOW_DETAIL.name: {
          this.setIsRefresh(false);
          const name =
            owner === ownType.SELL ? 'sellOrderDetail' : 'buyOrderDetail';

          this.$router.push({
            name,
            params: { owner, code: orderCode },
          });
          break;
        }

        // 再次出售
        case orderActionsMap.SOLD_AGAIN.name:
          this.$router.push({
            name: 'OrderSellConfirm',
            query: { productId, storageId },
          });
          break;

        // 查看物流
        case orderActionsMap.SHOW_EXPRESS.name:
          this.setIsRefresh(false);
          this.$router.push({
            name: 'orderLogisticsInfo',
            params: { owner, code: orderCode },
          });
          break;

        // 调价
        // 非入住商家
        case orderActionsMap.NOT_ENTRY_CHANGE_PRICE.name:
          this.$router.push({
            name: 'PriceChangeNoEntry',
            params: { orderCode },
          });
          break;

        // 入住商家
        case orderActionsMap.STORAGE_MANAGE.name:
          this.$router.push({
            name: 'PriceChangeEntry',
            params: { orderId: productId },
          });
          break;

        // 我要发货
        case orderActionsMap.DELIVER_GOODS.name:
          this.$router.push({
            name: 'order.deliver',
            params: { skup, code: orderCode },
          });
          break;

        // 修改地址
        case orderActionsMap.MODIFY_ADDRESS.name: {
          // 保存地址到store
          let info = JSON.stringify(addressInfo || userAddress || {});
          let updateInfo = JSON.parse(info || '{}');

          Object.assign(updateInfo, { orderCode: orderCode });
          this.STORE_UPDATE_ADDRESS_INFO(updateInfo);
          this.$router.push({
            name: 'addressModify',
            query: {
              fromPage: 'BuyerOrder',
            },
          });
          break;
        }

        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,
                    owner,
                    status,
                  });
                }
              }
              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.$route.params);
                    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';
          }
          let reportType = 'buy';

          if (typeTag) {
            reportType = typeTag === '全新瑕疵' ? 'newSecond' : 'second';
          }

          this.$createOrderPayType({
            orderCode,
            price: parseFloat(price || '').toFixed(2),
            desc: '金额',
            extra: JSON.stringify({
              forward: {
                name: pageBackName,
                params: {
                  owner,
                  code: orderCode,
                },
              },
              back: {
                name: pageBackName,
                params: {
                  owner,
                  code: orderCode,
                },
              },
              reportType,
            }),
          }).show();
          break;
        }

        case orderActionsMap.PAY_DEPOSIT.name: {
          // 是否是求购
          const isAskForBuy = status === 7;

          const routeParam = {
            name: isDetail ? 'buyOrderDetail' : 'OrderList',
            params: isDetail
              ? {
                  owner,
                  code: orderCode, // 改为新订单号
                }
              : this.$route.params,
          };

          this.$createOrderPayType({
            orderCode,
            price: parseFloat(bidDepositInfo.depositAmount).toFixed(2),
            desc: '支付定金',
            extra: JSON.stringify({
              forward: routeParam,
              back: routeParam,
              reportType: isAskForBuy ? 'qiugou_buy' : '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.$route.params);
                  this.fetchData(this.$route.params);
                }
              }
            },
          }).show();
          break;
        }

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

          let isStr = false;

          if (code !== 200) {
            isStr = true;
            this.$createToast({
              type: 'alert',
              txt: message,
              mask: true,
            }).show();

            // 有未完成调价
            if (code === 401) {
              return;
            }
          }
          let that = this;

          this.$createChangeBidPriceDialog({
            computePriceInfo: isStr ? { promotionFormulaList: [] } : data,
            goodsInfo,
            orderCode,
            onCloseAction() {},
            onConfirmAction: async price => {
              const {
                isOk,
                errMsg = '',
                bidData,
              } = await that.confirmChangePrice({
                price: price,
                orderCode,
              });
              const routeParam = {
                name: isDetail ? 'buyOrderDetail' : 'OrderList',
                params: isDetail
                  ? {
                      owner: this.$route.params.owner,
                      code: bidData.orderCode, // 改为新订单号
                    }
                  : this.$route.params,
              };

              if (isOk) {
                // 重新支付保证金
                this.$createOrderPayType({
                  orderCode: bidData.orderCode,
                  price: parseFloat(bidData.depositAmount),
                  desc: '支付定金',
                  extra: JSON.stringify({
                    forward: routeParam,
                    back: routeParam,
                    reportType: 'buy',
                  }),
                }).show();
              } else {
                if (errMsg) {
                  that
                    .$createToast({
                      type: 'alert',
                      txt: errMsg,
                    })
                    .show();
                }
              }
            },
          }).show();
          break;
        }
        default:
          return;
      }
    },
  },
};