actions.js 937 Bytes
import * as Types from './types';

export default {
  fetchProductFavStatus({state, commit}, {productId}) {
    if (!state.favBatchs.length) {
      commit(Types.FETCH_PRODUCT_FAV_REQUEST, {productId, promise: () => {
        return new Promise((resolve, reject) => {
          setTimeout(async() => {
            let fetchIds;
    
            if (state.favBatchs.some(id => id === productId)) {
              fetchIds = state.favBatchs.map(id => id);
            } else {
              fetchIds = [productId];
            }
            const result = await this.$api.get('/api/favorite/batchCheckIsFavorite', {
              favIds: fetchIds,
            });

            if (result.code === 200) {
              resolve(result.data);
            } else {
              resolve([]);
            }
          }, 50);
        });
      }});
    } else {
      commit(Types.FETCH_PRODUCT_FAV_REQUEST, {productId});
    }
    

    
  }
};