store.js 4.9 KB
import {
    FETCH_API_FAILURE,
    FETCH_API_REQUEST,
    FETCH_API_SUCCESS,
    FETCH_GROUP_FAILURE,
    FETCH_GROUP_REQUEST,
    FETCH_GROUP_SUCCESS
} from './types';
import api from 'common/api';

export default {
    state: {
        cacheApis: {},
        groups: [],
        fetchGroupsing: false,
        apis: [],
        featchApising: false,
        groupName: '',
        apiPage: 1,
        apiRows: 10,
        apiCount: 0,
        apiKeyword: '',
        apiSearchType: ''
    },
    mutations: {
        [FETCH_GROUP_REQUEST](state) {
            state.fetchGroupsing = true;
        },
        [FETCH_GROUP_FAILURE](state) {
            state.fetchGroupsing = false;
        },
        [FETCH_GROUP_SUCCESS](state, params) {
            state.fetchGroupsing = false;
            state.groups = params.list;

            params.list.forEach(group => {
                state.cacheApis[group.name] = {
                    rows: [],
                    total: 0
                };
            });
        },
        [FETCH_API_REQUEST](state, params) {
            if (params.groupName === '') {
                state.groupName = '';
            } else if (params.groupName) {
                state.groupName = params.groupName;
            }
            if (params.keyword === '') {
                state.apiKeyword = '';
            } else if (params.keyword) {
                state.apiKeyword = params.keyword;
            }
            if (params.searchType === '') {
                state.apiSearchType = '';
            } else if (params.searchType) {
                state.apiSearchType = params.searchType;
            }
            state.apiPage = params.page || 1;
            state.apiRows = params.rows || state.apiRows;
            state.featchApising = true;
        },
        [FETCH_API_FAILURE](state) {
            state.featchApising = false;
        },
        [FETCH_API_SUCCESS](state, params) {
            state.featchApising = false;
            state.apiCount = params.data.total;
            state.apis = params.data.rows;

            if (!state.apiSearchType && state.groupName && !params.cache) {
                const groupCache = state.cacheApis[state.groupName];
                const start = (state.apiPage - 1) * state.apiRows;

                if (groupCache.rows.length >= start) {
                    groupCache.rows.splice(start, state.apiRows, ...params.data.rows);
                    groupCache.total = params.data.total;
                }
                groupCache.__cached = true;
            }
        }
    },
    actions: {
        [FETCH_GROUP_REQUEST]({commit}) {
            commit(FETCH_GROUP_REQUEST);
            return api.get('/api/findGroup').then(res => {
                commit(FETCH_GROUP_SUCCESS, {list: res});
            }, () => {
                commit(FETCH_GROUP_FAILURE);
            });
        },
        [FETCH_API_REQUEST]({commit, state}, params) {
            let apiPromise;

            commit(FETCH_API_REQUEST, params);
            if (state.apiSearchType === 'controller') {
                apiPromise = api.get('/api/findByName', {
                    groupName: state.groupName || void 0,
                    key: state.apiKeyword,
                    page: state.apiPage,
                    rows: state.apiRows
                });
            } else if (state.apiSearchType === 'keyword') {
                apiPromise = api.get('/api/find', {
                    groupName: state.groupName || void 0,
                    key: state.apiKeyword,
                    page: state.apiPage,
                    rows: state.apiRows
                });
            } else {
                const cacheGroup = state.cacheApis[state.groupName];

                if (cacheGroup && cacheGroup.__cached) {
                    const start = (state.apiPage - 1) * state.apiRows;
                    const end = state.apiPage * state.apiRows > cacheGroup.total ? cacheGroup.total : state.apiPage * state.apiRows;

                    if (cacheGroup.rows.length >= end) {
                        commit(FETCH_API_SUCCESS, {
                            data: {
                                total: cacheGroup.total,
                                rows: cacheGroup.rows.slice(start, end)
                            },
                            cache: true
                        });
                        return Promise.resolve();
                    }
                }
                apiPromise = api.get('/api/findByGroup', {
                    key: state.groupName,
                    page: state.apiPage,
                    rows: state.apiRows
                });
            }
            return apiPromise.then(res => {
                if (res.code === 200) {
                    commit(FETCH_API_SUCCESS, {data: res.data});
                } else {
                    commit(FETCH_API_FAILURE);
                }
            }, () => {
                commit(FETCH_API_FAILURE);
            });
        }
    }
};