order-confirm.js 8.23 KB
import { get, find, filter } from 'lodash';

export const Types = {
  FETCH_ORDER_ADDRESS: 'FETCH_ORDER_ADDRESS',
  FETCH_ORDER_USER_STATUS: 'FETCH_ORDER_USER_STATUS',
  FETCH_ORDER_FEE: 'FETCH_ORDER_FEE',
  CHANGE_PRICE: 'CHANGE_PRICE',
  CHANGE_AGREE: 'CHANGE_AGREE',
  FETCH_ORDER_BUY_ORDER: 'FETCH_ODER_BUY_ORDER',
  FETCH_ORDER_COUPON_LIST: 'FETCH_ORDER_COUPON_LIST',
  CHANGE_SELECT_COUPON_LIST: 'CHANGE_SELECT_COUPON_LIST',
  CHANGE_SELECT_PROMOTION: 'CHANGE_SELECT_PROMOTION',
  COMPUTE_ORDER: 'COMPUTE_ORDER',
  UPDATE_ORDER: 'UPDATE_ORDER',
  CHANGE_BUY_AGREE: 'CHANGE_BUY_AGREE',
  CHANGE_SELL_NUM: 'CHANGE_SELL_NUM',
  CLEAR_SELL_STATUS: 'CLEAR_SELL_STATUS',
  CLEAR_BUY_STATUS: 'CLEAR_BUY_STATUS'
};

export const UserType = {
  sell: 'sell',
  buy: 'buy'
};

const COUPON_TYPE = {
  HUO_DONG: 100
};

export default function() {
  return {
    namespaced: true,
    state: {
      buyAddress: null,
      address: null,
      fee: {
        income: '',
        bankTransferFee: '',
        platformFee: {
          amount: '',
          appraiseFee: '',
          goodsPaymentRatePercent: '',
          packageFee: '',
          payChannelPercentage: '',
          serviceFee: ''
        }
      },
      alipayStatus: false,
      userStatus: false,

      price: '',
      agree: false,
      num: 1,

      buyAgree: false,

      orderDetail: {},
      selectedCouponList: [],
      selectedPromotion: null,
      couponList: []
    },
    mutations: {
      [Types.FETCH_ORDER_ADDRESS](state, data) {
        state.address = data;
      },
      [Types.FETCH_ORDER_FEE](state, data) {
        state.fee = data;
      },
      [Types.CHANGE_PRICE](state, data) {
        state.price = data;
      },
      [Types.CHANGE_AGREE](state, data) {
        state.agree = data;
      },
      [Types.CHANGE_BUY_AGREE](state, data) {
        state.buyAgree = data;
      },
      [Types.FETCH_ORDER_BUY_ORDER](state, data) {
        state.orderDetail = data;
      },
      [Types.CLEAR_SELL_STATUS](state) {
        state.agree = false;
        state.price = '';
        state.num = 1;
        state.address = null;
        state.fee = {
          income: '',
          bankTransferFee: '',
          platformFee: {
            amount: '',
            appraiseFee: '',
            goodsPaymentRatePercent: '',
            packageFee: '',
            payChannelPercentage: '',
            serviceFee: ''
          }
        };
      },
      [Types.CHANGE_SELL_NUM](state, data) {
        state.num = data;
      },
      [Types.CLEAR_BUY_STATUS](state) {
        state.buyAgree = false;
        state.orderDetail = {};
        state.selectedCouponList = [];
        state.selectedPromotion = null;
        state.couponList = [];
      },
      [Types.UPDATE_ORDER](state, { amount, couponInfo, couponList, promotionFormulaList, promotionList, promotionTips }) {
        state.orderDetail.amount = amount;
        state.orderDetail.promotionFormulaList = promotionFormulaList;

        state.orderDetail.recommendedCouponInfo = couponInfo;
        state.orderDetail.couponList = couponList;

        state.orderDetail.promotionList = promotionList;
        state.orderDetail.promotionTips = promotionTips;
      },
      [Types.CHANGE_SELECT_COUPON_LIST](state, { couponCode, couponType }) {
        const item = find(get(state.orderDetail, 'couponList', []), { coupon_code: couponCode });

        if (!item) {
          return;
        }

        if (item.selected === 'Y') {
          item.selected = 'N';
          state.orderDetail.recommendedCouponInfo.coupon_code =
            filter(get(state.orderDetail, 'couponList', []), { selected: 'Y' }).map(i => i.coupon_code).join(',');
        } else {
          filter(get(state.orderDetail, 'couponList', []), { coupon_type: couponType }).forEach(i => {
            i.selected = 'N';
          });

          item.selected = 'Y';

          state.orderDetail.recommendedCouponInfo.coupon_code =
            filter(get(state.orderDetail, 'couponList', []), { selected: 'Y' }).map(i => i.coupon_code).join(',');

          // 清除促消活动
          if (item.coupon_type === COUPON_TYPE.HUO_DONG) {
            get(state.orderDetail, 'promotionList', []).forEach(i => {
              i.selected = 'N';
            });

            state.orderDetail.promotionTips.promotionIds = '';
          }
        }
      },
      [Types.CHANGE_SELECT_PROMOTION](state, { promotionId }) {
        const item = find(get(state.orderDetail, 'promotionList', []), { promotionId });

        if (!item) {
          return;
        }

        if (item.selected === 'Y') {
          get(state.orderDetail, 'promotionList', []).forEach(i => {
            i.selected = 'N';
          });
          state.orderDetail.promotionTips.promotionIds = '';
        } else {
          get(state.orderDetail, 'promotionList', []).forEach(i => {
            i.selected = 'N';
          });
          item.selected = 'Y';
          state.orderDetail.promotionTips.promotionIds = promotionId;

          // 清除活动优惠券
          get(state.orderDetail, 'couponList', []).filter(i => i.coupon_type === COUPON_TYPE.HUO_DONG).forEach(i => {
            i.selected = 'N';
          });

          state.orderDetail.recommendedCouponInfo.coupon_code =
            filter(get(state.orderDetail, 'couponList', []), { selected: 'Y' }).map(i => i.coupon_code).join(',');
        }

      }
    },
    actions: {
      async fetchOrderAddress({ commit, state }, payload) {
        const orderCount = await this.$api.get('/api/order/confirm/count', payload);

        if (state.address?.address_id) {
          return;
        }

        if (get(orderCount, 'data.cnt', 0)) {
          const addressInfo = await this.$api.get('/api/order/confirm/address');

          const address = find(get(addressInfo, 'data', []), { is_default: 'Y' });

          if (address) {
            commit(Types.FETCH_ORDER_ADDRESS, address);
          }
        }
      },

      async fetchOrderPrice({ commit }, payload) {
        const order = await this.$api.post('/api/order/confirm/compute', payload);

        if (order.code !== 200) {
          return {
            code: order.code,
            error: order.message
          };
        }

        commit(Types.FETCH_ORDER_FEE, order.data);
        return order.data;
      },

      async fetchUserStatus() {
        return this.$api.get('/api/order/user/status');
      },

      async submitOrder({ commit }, payload) {
        return this.$api.post('/api/order/presubmit', payload);
      },

      async fetchPayList({ commit }, payload) {
        return this.$api.post('/api/order/paytype', payload);
      },

      async fetchPayment({ commit }, { skup }) {
        const orderInfo = await this.$api.post('/api/order/confirm/buypayment', { skup, api_version: 1 });

        if (orderInfo.code !== 200) {
          return orderInfo;
        }

        commit(Types.FETCH_ORDER_BUY_ORDER, orderInfo.data);

        return orderInfo;
      },

      async computeOrder({ commit }, { skup, couponCode, addressId, promotionId }) {
        const orderInfo = await this.$api.post('/api/order/confirm/buycompute', {
          skup,
          addressId,
          coupon_code: couponCode,
          promotionId,
          depositRequirement: 'N'
        });

        if (orderInfo.code !== 200) {
          return orderInfo;
        }

        commit(Types.UPDATE_ORDER, orderInfo.data);

        return orderInfo;
      },

      async buyPayAction(ctx, { skup, couponCode, addressId, promotionId }) {
        const order = await this.$api.post('/api/order/submit', {
          skup,
          addressId,
          coupon_code: couponCode,
          promotionId,
          depositRequirement: 'N'
        });

        return order;
      },

      async payAction(ctx, { orderCode }) {
        const payResult = await this.$api.post('/api/order/pay', {
          orderCode,
          payment: 9
        });

        if (payResult.code !== 200) {
          return Promise.reject(payResult);
        }

        return payResult;
      },

      async fetchOrderGoods(ctx, { orderCode }) {
        return this.$api.get('/api/order/goods', {
          orderCode
        });
      },

      async fetchOrderStatus(ctx, { orderCode }) {
        return this.$api.get('/api/order/status', {
          orderCode
        });
      }
    },
    getters: {},
  };
}