order-list.js 5.51 KB
import {
  buyerOrderStatusList,
  sellerOrderStatusList,
  orderStatusKey,
  ownType,
} from 'constants/order-constants';

function initialOrderState() {
  return {
    page: 1,
    pageSize: 10,
    pagetotal: 0,
    orderList: [], // 订单列表
    // scroll 组件参数,是否触发上拉事件
    pullUpLoad: true,
    isShowEmpty: false,
  };
}

const initialState = () => {
  const orderListByType = {};

  [
    [ownType.BUY, buyerOrderStatusList],
    [ownType.SELL, sellerOrderStatusList],
  ].forEach(statusInfo => {
    statusInfo[1].forEach(orderStatus => {
      const key = orderStatusKey(statusInfo[0], orderStatus.value);

      orderListByType[key] = initialOrderState();
    });
  });

  return {
    orderListByType,

    // 当前查询订单状态
    currentStatus: null,
    routeParamStatus: null,
  };
};

export default function() {
  return {
    namespaced: true,
    state: initialState,
    mutations: {
      setOrderList(state, { res, owner, status }) {
        let { page, pagetotal, data = [] } = res;
        const orderState = state.orderListByType[orderStatusKey(owner, status)];

        orderState.isShowEmpty = page === 1 && data.length === 0;
        orderState.page = ++page;
        orderState.pagetotal = pagetotal;
        orderState.orderList = orderState.orderList.concat(data);

        // 分页结束
        if (page > pagetotal) {
          orderState.pullUpLoad = false;
        }
      },
      filterOrderList(state, { orderCode, owner, status }) {
        const orderState = state.orderListByType[orderStatusKey(owner, status)];

        orderState.orderList = orderState.orderList.filter(
          order => order.orderCode !== orderCode,
        );
        orderState.isShowEmpty = orderState.orderList.length === 0;
      },
      setOrderStatus(state, currentStatus) {
        state.currentStatus = +currentStatus;
      },
      setRouteParamStatus(state, status = 1) {
        state.routeParamStatus = +status;
      },
      resetPartialData(state, { owner, status }) {
        Object.assign(state.orderListByType[orderStatusKey(owner, status)], {
          page: 1,
          orderList: [],
          pagetotal: 0,
          pullUpLoad: true,
        });
      },
      resetData(state, { owner, status } = {}) {
        const orderListState = initialOrderState();

        state.orderListByType[orderStatusKey(owner, status)] = orderListState;
      },
    },
    actions: {
      /**
       *  获取订单列表
       * @param {*}  param0 vue store context
       * @param {
       *  owner: 订单来源
       *  status: 订单状态 默认全部
       * }
       * r
       */
      async fetchOrderList({ commit, state }, { owner, status }) {
        const { page } = state.orderListByType[orderStatusKey(owner, status)];

        const res = await this.$api.get('/api/order/list', {
          tabType: owner,
          type: status,
          page,
        });

        if (res.code === 200) {
          commit('setOrderList', { res: res.data, owner, status });
        }
      },

      /**
       * 删除订单
       * @param {
       * orderCode: 订单编码
       * owner: 订单来源 buy | sell
       * } param1
       */
      async deleteOrder({ commit }, { orderCode, owner, status }) {
        // owner: ownType
        const res = await this.$api.get(`/api/${owner}/order/delete`, {
          orderCode,
        });
        const { code, data } = res;

        // data 为true时删除成功
        if (code === 200) {
          if (data) {
            commit('filterOrderList', { orderCode, owner, status });
          }
        }
        return data;
      },

      /**
       * 取消订单
       * 买家取消购买
       * 卖家取消出售
       *  @param {
       *   orderCode: 订单编码
       *   owner: 订单来源 buy | sell
       *  } param1
       */
      async cancelTrade(_, { orderCode, owner }) {
        const res = await this.$api.get(`/api/${owner}/trade/cancel`, {
          orderCode,
        });
        const isOk = res.code === 200 ? res.data : false;

        return isOk;
      },

      /**
       * 取消订单 提示信息
       * 买家取消购买
       * 卖家取消出售
       *  @param {
       *   orderCode: 订单编码
       *   owner: 订单来源 buy | sell
       *  } param1
       */
      async cancelTradeConfirmInfo(_, { orderCode, owner }) {
        const res = await this.$api.get(
          `/api/${owner}/trade/cancel/confirm/info`,
          { orderCode },
        );

        return res.code === 200 ? res.data : null;
      },

      /**
       * 确认收货
       */
      async confirmReceipt(_, { orderCode }) {
        const res = await this.$api.post('/api/buy/confirm/receipt', {
          orderCode,
        });

        return res.code === 200 ? res.data : null;
      },

      // 买家调价计算
      async computeChangePrice(_, { orderCode, price }) {
        const res = await this.$api.get(
          '/api/order/buyerask/computechangeprice',
          {
            orderCode: `${orderCode}`,
            price: +price,
          },
        );

        return res.code === 200 ? res.data : res.message || '';
      },

      // 买家调价
      async confirmChangePrice(_, { orderCode, price }) {
        const res = await this.$api.post('/api/order/buyerask/changeprice', {
          orderCode: `${orderCode}`,
          price: +price,
        });

        if (res.code === 200) {
          return { errMsg: '', isOk: true, bidData: res.data };
        } else {
          return { errMsg: res.message, isOk: false };
        }
      },
    },
  };
}