buyer-ask-order.js 9.22 KB
/**
 * buyer-ask-order.js
 *@author dennis
 *@createtime 9/25/19
 *@description 买家求购状态管理
 */

import {Types} from './types';
import {
  get
} from 'lodash';

const {
  BUYER_ORDERCOUNT_REQUEST,
  BUYER_ORDERCOUNT_SUCCESS,
  BUYER_ORDERCOUNT_FAILURE,
  BUYER_ADDRESS_REQUEST,
  BUYER_ADDRESS_SUCCESS,
  BUYER_ADDRESS_FAILURE,
  BUYER_ASK_CONFIG_SUCCESS,
  BUYER_ASK_CONFIG_FAILURE,
  BUYER_ASK_COMPUTE_REQUEST,
  BUYER_ASK_COMPUTE_SUCCESS,
  BUYER_ASK_COMPUTE_FAILURE,
  BUYER_ASK_PREPUBLISH_SUCCESS,
  BUYER_ASK_PREPUBLISH_FAILURE,
  BUYER_ASK_PREPUBLISH_REQUEST,
  BUYER_ASK_PUBLISH_REQUEST,
  BUYER_ASK_PUBLISH_SUCCESSS,
  BUYER_ASK_PUBLISH_FAILURE,
  BUYER_ASK_SET_SHOWTOAST,
  BUYER_ASK_SET_CHOOSEDAY,
  BUYER_ASK_SET_SHOWDIALOG,
  BUYER_ASK_SET_STORAGEID,
  BUYER_ASK_SET_PRODUCTINFO,
} = Types;

const TIP = '请求失败';

const DEFAULT_COMPUTE_INFO = {
  depositAmount: '0',
  promotionFormulaList: [
    {
      promotion: '商品金额',
      promotionAmount: '¥-'
    },
    {
      promotion: '运费',
      promotionAmount: '¥-'
    },
    {
      promotion: '实付金额',
      promotionAmount: '¥-'
    }
  ]
};

const DEFUALT_CONFIG_TIP = {
  timeLimit: {
    defaultItem: {
      desc: '7天',
      id: 3
    },
    items: [
      {
        desc: '1天',
        id: 1
      },
      {
        desc: '3天',
        id: 2
      },
      {
        desc: '7天',
        id: 3
      },
      {
        desc: '15天',
        id: 4
      },
      {
        desc: '30天',
        id: 5
      }
    ]
  },
  tips: '求购须支付定金。卖家接单后,你需要在24小时内支付商品款。卖家将在你付款后36小时内发货。'
};

const TEST_PRODUCT_INFO = {
  least_price: 1119.00,
  max_price: 20000.00,
  max_sort_id: 10,
  min_price: 0.01,
  offer_price: 200.00,
  product_code: 'OMRG001S181851110112',
  product_id: 10000068,
  product_name: 'Timberland 红色女靴',
  sale_time: '2016.01.01',
  image: 'http://img11.static.yhbimg.com/goodsimg/2018/10/18/17/0124eed582cab20be7047c796c314cbc76.jpg?imageMogr2/thumbnail/{width}x{height}/background/d2hpdGU=/position/center/quality/80'
};

export default function() {

  return {
    namespaced: true,
    state: {
      notFirstOrder: false,
      storageId: '10000130',
      originProductData: TEST_PRODUCT_INFO,

      addressInfo: null,

      configTip: DEFUALT_CONFIG_TIP,

      iscomputefetch: false,
      isprepublish: false,
      ispublish: false,
      computeInfo: DEFAULT_COMPUTE_INFO,
      preTip: null,
      publishresult: {},
      toasMessage: '',
      isShowToast: false,
      chooseDay: '7天',
      isShowDialog: false,
    },
    getters: {
      dayOptions(state) {
        let orginallist = get(state, ['configTip', 'timeLimit', 'items'], []);

        return orginallist.map(item => {
          return get(item, ['desc'], '');
        });

      },

      chooseDayId(state) {
        let orginallist = get(state, ['configTip', 'timeLimit', 'items'], []);

        let dayid = 3;

        orginallist.forEach(item => {
          if (item.desc === state.chooseDay) {
            dayid = item.id;
          }
        });

        return dayid;

      },
    },

    mutations: {

      [BUYER_ASK_SET_PRODUCTINFO](state, payload) {
        state.originProductData = payload
      },

      [BUYER_ASK_SET_STORAGEID](state, payload) {
        state.storageId = payload
      },

      [BUYER_ASK_SET_SHOWTOAST](state, payload) {
        state.isShowToast = payload ? true : false;
      },

      [BUYER_ASK_SET_SHOWDIALOG](state, payload) {
        state.isShowDialog = payload ? true : false;
      },

      [BUYER_ASK_SET_CHOOSEDAY](state, payload) {
        state.chooseDay = payload;
      },

      [BUYER_ORDERCOUNT_REQUEST](state) {

      },

      [BUYER_ORDERCOUNT_SUCCESS](state, data) {
        state.notFirstOrder = get(data, ['cnt'], 0) > 0;
      },

      [BUYER_ORDERCOUNT_FAILURE](state, message) {

      },

      [BUYER_ADDRESS_REQUEST]() {

      },

      [BUYER_ADDRESS_SUCCESS](state, payload) {
        let list = payload || [];
        let defaultAddress;

        list.forEach(item => {

          if (item.is_default === 'Y') {
            defaultAddress = item;
          }
        });

        state.addressInfo = defaultAddress;

      },

      [BUYER_ADDRESS_FAILURE]() {

      },

      [BUYER_ASK_CONFIG_SUCCESS](state, payload) {

        state.configTip = payload;
      },

      [BUYER_ASK_CONFIG_FAILURE](state, payload) {

      },

      [BUYER_ASK_COMPUTE_REQUEST](state) {
        state.iscomputefetch = true;
      },

      [BUYER_ASK_COMPUTE_SUCCESS](state, payload) {
        state.iscomputefetch = false;
        state.computeInfo = payload;
      },

      [BUYER_ASK_COMPUTE_FAILURE](state, payload) {
        state.iscomputefetch = false;
        state.computeInfo = DEFAULT_COMPUTE_INFO;
        state.toasMessage = payload;
        state.isShowToast = true;
      },

      [BUYER_ASK_PREPUBLISH_REQUEST](state) {
        state.isprepublish = true;
      },

      [BUYER_ASK_PREPUBLISH_SUCCESS](state, payload) {
        state.isprepublish = false;
        if (payload) {
          state.preTip = payload;
        } else {
          state.preTip = null;
        }
        state.isShowDialog = true;

      },

      [BUYER_ASK_PREPUBLISH_FAILURE](state, payload) {
        state.isprepublish = false;
        state.preTip = null;
        state.toasMessage = payload;
        state.isShowToast = true;
      },

      [BUYER_ASK_PUBLISH_REQUEST](state) {
        state.ispublish = true;
      },

      [BUYER_ASK_PUBLISH_SUCCESSS](state, payload) {
        state.ispublish = false;
        state.publishresult = payload;

      },

      [BUYER_ASK_PUBLISH_FAILURE](state, payload) {
        state.ispublish = false;
        state.publishresult = {};
        state.toasMessage = payload;
        state.isShowToast = true;
      }

    },

    actions: {

      fetchBuyerOrderCount({commit, dispatch}, {tabType = 'buy', uid} = {}) {
        commit(BUYER_ORDERCOUNT_REQUEST);
        this.$api.get('/api/order/ordercount', {
          tabType,
          uid
        }).then(result => {

          if (result.code === 200) {
            commit(BUYER_ORDERCOUNT_SUCCESS, result.data);
            if (get(result, ['data', 'cnt'], 0) > 0) {
              dispatch('fetchAddress', {});
            } else {
              console.log('first order');
            }
          } else {
            commit(BUYER_ORDERCOUNT_FAILURE, result.message);
          }

        }, error => {
          commit(BUYER_ORDERCOUNT_FAILURE, '');
          console.log(error);
        });

      },

      fetchAddress({commit}, {uid} = {}) {
        commit(BUYER_ADDRESS_REQUEST);
        this.$api.get('/api/address/gethidden', {
          uid
        }).then(result => {
          if (result.code === 200) {
            commit(BUYER_ADDRESS_SUCCESS, result.data);
          } else {
            commit(BUYER_ADDRESS_FAILURE, result.message);
          }
        }, error => {
          commit(BUYER_ADDRESS_FAILURE, '');
          console.log(error);
        });
      },

      fetchConfig({commit}, {} = {}) {
        this.$api.get('/api/order/buyaskconfig', {

        }).then(result => {
          if (result.code === 200) {
            commit(BUYER_ASK_CONFIG_SUCCESS, result.data);
          } else {
            console.log(result);
          }
        }, error => {
          console.log(error);
        });
      },

      buyerCompute({commit}, {price = 0, storage_id = 0, uid} = {}) {
        commit(BUYER_ASK_COMPUTE_REQUEST);
        this.$api.get('/api/order/buyeraskcompute', {
          uid,
          price,
          storage_id,
        }).then(result => {

          if (result.code === 200) {

            commit(BUYER_ASK_COMPUTE_SUCCESS, get(result, ['data'], {}));
          } else {
            commit(BUYER_ASK_COMPUTE_FAILURE, get(result, ['message'], TIP));
          }

        }, error => {

          console.log(error);

          commit(BUYER_ASK_COMPUTE_FAILURE, TIP);

        });
      },

      buyerPrePublish({commit, dispatch}, {price = 0, storage_id = 0, uid, address_id = ''} = {}) {
        commit(BUYER_ASK_PREPUBLISH_REQUEST);
        this.$api.get('/api/order/buyeraskprepublish', {
          price,
          storage_id,
          uid,
          address_id,
        }).then(result => {
          if (result.code === 200) {
            commit(BUYER_ASK_PREPUBLISH_SUCCESS, get(result, ['data', 'dialog'], null));
          } else {
            commit(BUYER_ASK_PREPUBLISH_FAILURE, result.message);
          }
        }, error => {
          console.log(error);
          commit(BUYER_ASK_PREPUBLISH_FAILURE, TIP);
        });
      },

     async buyerPublish({commit, dispatch}, {price = 0, storage_id = 0, uid, address_id = '', time_limit_id = ''} = {}) {
        commit(BUYER_ASK_PUBLISH_REQUEST);
       return this.$api.get('/api/order/buyeraskpublish', {
          price,
          storage_id,
          uid,
          address_id,
          time_limit_id
        }).then(result => {
          if (result.code === 200) {
            commit(BUYER_ASK_PUBLISH_SUCCESSS, result.data);
          } else {
            commit(BUYER_ASK_PUBLISH_FAILURE, result.message);
          }
        }, error => {
          console.log(error);
          commit(BUYER_ASK_PUBLISH_FAILURE, TIP);
        });
      }
    }
  };

}