index.js 6.06 KB
import {
    FETCH_HOME_REQUEST,
    FETCH_HOME_FAILURE,
    FETCH_HOME_SUCCESS,
    REFRESH_HOME_RESET,
    FETCH_CHANNEL_REQUEST,
    FETCH_CHANNEL_SUCCESS,
    FETCH_CHANNEL_FAILURE,
    REFRESH_CHANNEL_RESET,
    FETCH_ABOUT_REQUEST,
    FETCH_ABOUT_SUCCESS,
    FETCH_ABOUT_FAILURE
} from './types';
import {
    HOME_CONTENT_CODE,
    CHANNEL_MAN_CONTENT_CODE,
    CHANNEL_WOMEN_CONTENT_CODE,
    SLIDER_CONTENT_CODE,
    ABOUT_CONTENT_CODE
} from '../content-code';
import _ from 'lodash/core';

export function createChannel() {
    return {
        state: {
            home: {},
            slider: [],
            about: {},
            gender: 'men',
            men: {
                sort: {},
                floors: [],
                isFetching: false
            },
            women: {
                sort: {},
                floors: [],
                isFetching: false
            },
            fethingHome: false,
            fetchingAbout: false,
        },
        mutations: {
            [FETCH_HOME_REQUEST](state) {
                state.fethingHome = true;
            },
            [FETCH_HOME_FAILURE](state) {
                state.fethingHome = false;
            },
            [FETCH_HOME_SUCCESS](state, {result, slider}) {
                state.fethingHome = false;
                state.home = result.data;
                state.slider = slider.data[0].data;
            },
            [REFRESH_HOME_RESET](state) {
                state.home = [];
                state.slider = [];
            },
            [FETCH_ABOUT_REQUEST](state) {
                state.fetchingAbout = true;
            },
            [FETCH_ABOUT_FAILURE](state) {
                state.fetchingAbout = false;
            },
            [FETCH_ABOUT_SUCCESS](state, {data}) {
                state.fetchingAbout = false;
                state.about = data || [];
            },
            [FETCH_CHANNEL_REQUEST](state, gender) {
                state.gender = gender;
                state[gender].isFetching = true;
            },
            [FETCH_CHANNEL_FAILURE](state) {
                state[state.gender].isFetching = false;
            },
            [REFRESH_CHANNEL_RESET](state) {
                state[state.gender].floors = [];
                state[state.gender].sort = [];
            },
            [FETCH_CHANNEL_SUCCESS](state, {data, sort, cache}) {
                if (!cache) {
                    const keyCvt = {
                        men: 'MEN男士',
                        women: 'WOMEN女士',
                    };

                    _.each(data, item => {
                        if (item.template_name === 'blkCategory') {
                            item.data = {
                                gender: state.gender,
                                sort: sort[keyCvt[state.gender]]
                            };
                        }
                    });
                    state[state.gender].floors = data;
                    state[state.gender].sort = sort[keyCvt[state.gender]];
                }

                state[state.gender].isFetching = false;
            },
        },
        actions: {
            async [FETCH_HOME_REQUEST]({commit, state}, refresh) {
                if (!_.isEmpty(state.home) && !refresh) {
                    return Promise.resolve(state.home);
                }

                commit(FETCH_HOME_REQUEST);

                if (refresh) {
                    commit(REFRESH_HOME_RESET);
                }
                try {
                    const getContent = this.$api.get('/api/resource/get', {
                        home_invalid_cache: Date.now(),
                        content_code: HOME_CONTENT_CODE
                    });
                    const getSlider = this.$api.get('/api/resource/get', {
                        home_invalid_cache: Date.now(),
                        content_code: SLIDER_CONTENT_CODE
                    });

                    let [result, slider] = await Promise.all([getContent, getSlider]);

                    commit(FETCH_HOME_SUCCESS, {result, slider});
                    return result;
                } catch (e) {
                    commit(FETCH_HOME_FAILURE);
                }
            },
            async [FETCH_CHANNEL_REQUEST]({commit, state}, {gender, refresh}) {
                gender = gender || state.gender;
                commit(FETCH_CHANNEL_REQUEST, gender);
                if (!_.isEmpty(state[gender].floors) && !refresh) {
                    commit(FETCH_CHANNEL_SUCCESS, {cache: true});
                    return Promise.resolve();
                }

                if (refresh) {
                    commit(REFRESH_CHANNEL_RESET);
                }
                try {
                    let getSort = this.$api.get('/sort/list', {
                        home_invalid_cache: Date.now(),
                        channel: gender
                    });

                    let getResource = this.$api.get('/api/resource/get', {
                        home_invalid_cache: Date.now(),
                        content_code: gender === 'men' ? CHANNEL_MAN_CONTENT_CODE : CHANNEL_WOMEN_CONTENT_CODE
                    });

                    let [sort, resource] = await Promise.all([getSort, getResource]);

                    commit(FETCH_CHANNEL_SUCCESS, {data: resource.data, sort: sort.data});
                    return {sort, resource};
                } catch (e) {
                    commit(FETCH_CHANNEL_FAILURE);
                }
            },
            async [FETCH_ABOUT_REQUEST]({commit, state}) {
                if (!_.isEmpty(state.about)) {
                    return Promise.resolve(state.about);
                }
                commit(FETCH_ABOUT_REQUEST);

                try {
                    const result = await this.$api.get('/api/resource/get', {
                        content_code: ABOUT_CONTENT_CODE
                    });

                    commit(FETCH_ABOUT_SUCCESS, {data: result.data});
                    return result;
                } catch (e) {
                    commit(FETCH_ABOUT_FAILURE);
                }
            }
        }
    };
}