seller-ask-order.js 4.62 KB
/**
 * seller-ask-order.js
 *@author dennis
 *@createtime 9/29/19
 *@description 卖家求购变现状态管理
 */

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

const TIP = '请求失败';

const {
  SELLER_ORDERCOUNT_REQUEST,
  SELLER_ORDERCOUNT_SUCCESS,
  SELLER_ORDERCOUNT_FAILURE,
  SELLER_ADDRESS_REQUEST,
  SELLER_ADDRESS_SUCCESS,
  SELLER_ADDRESS_FAILURE,
  SELLER_ASK_COMPUTE_REQUEST,
  SELLER_ASK_COMPUTE_SUCCESS,
  SELLER_ASK_COMPUTE_FAILURE,
  SELLER_ASK_PUBLISH_REQUEST,
  SELLER_ASK_PUBLISH_SUCCESS,
  SELLER_ASK_PUBLISH_FAILURE,
  SELLER_ASK_SET_PRICE,
  SELLER_ASK_SET_SKUP,
  SELLER_ASK_SET_PRODUCTINFO,
} = Types;

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',
  goodImg: 'http://img11.static.yhbimg.com/goodsimg/2018/10/18/17/0124eed582cab20be7047c796c314cbc76.jpg?imageMogr2/thumbnail/{width}x{height}/background/d2hpdGU=/position/center/quality/80',
  colorName: '黑色',
  sizeName: '42码',
  goodPrice: 1119.00,
};

export default function() {

  return {
    namespaced: true,

    state: {
      originProductData: TEST_PRODUCT_INFO,
      price: 1200,
      computeTip: null,
      addressInfo: null,
      publishinfo: null,
      toastMessage: '',

    },
    getter: {

    },

    mutations: {

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

      [SELLER_ORDERCOUNT_SUCCESS](state, payload) {

      },

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

        list.forEach(item => {

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

        state.addressInfo = defaultAddress;

      },

      [SELLER_ASK_COMPUTE_REQUEST](state) {

      },

      [SELLER_ASK_COMPUTE_SUCCESS](state, payload) {
        state.computeTip = payload
      },

      [SELLER_ASK_COMPUTE_FAILURE](state, payload) {
        state.computeTip = null
      },

      [SELLER_ASK_PUBLISH_REQUEST](state) {

      },

      [SELLER_ASK_PUBLISH_SUCCESS](state, payload) {
        state.publishinfo = payload
      },

      [SELLER_ASK_PUBLISH_FAILURE](state, payload) {
        state.publishinfo = null
      }
    },

    actions: {

      fetchSellerOrderCount({commit, dispatch}, {tabType = 'sell', uid} = {}) {
        this.$api.get('/api/order/ordercount', {
          tabType,
          uid
        }).then(result => {

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

            // commit(BUYER_ORDERCOUNT_FAILURE, result.message);
          }
        }, error => {

          console.log(error);
        });
      },

      fetchSellerAddress({commit}, {uid} = {}) {

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

            // commit(BUYER_ADDRESS_FAILURE, result.message);
          }
        }, error => {
          console.log(error);
        });
      },

      sellerCompute({commit}, {price, skup, uid} = {}) {
        commit(SELLER_ASK_COMPUTE_REQUEST);
        this.$api.get('/api/order/sellask/compute', {
          price,
          skup,
          uid,
        }).then(result => {
          if (result.code === 200) {
            commit(SELLER_ASK_COMPUTE_SUCCESS, result.data);
          } else {
            commit(SELLER_ASK_COMPUTE_FAILURE, result.message);
          }
        }, error => {
          console.log(error);
          commit(SELLER_ASK_COMPUTE_FAILURE, TIP);
        });
      },

      sellerPublish({commit}, {price, skup, address_id, uid} = {}) {
        commit(SELLER_ASK_PUBLISH_REQUEST);
       return this.$api.get('/api/order/sellask/publish', {
          price,
          skup,
          address_id,
          uid,
        }).then(result => {
          if (result.code === 200) {
            commit(SELLER_ASK_PUBLISH_SUCCESS, result.data);
          } else {
            commit(SELLER_ASK_PUBLISH_FAILURE, result.message);
          }
        }, error => {
          console.log(error);
          commit(SELLER_ASK_PUBLISH_FAILURE, TIP);
        });
      }
    },
  };
}