store.js 4.46 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;
            }
            state.apiSearchType = params.searchType;
            state.apiKeyword = params.keyword;
            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;
                }
            }
        }
    },
    actions: {
        [FETCH_GROUP_REQUEST]({commit}) {
            commit(FETCH_GROUP_REQUEST);
            return api.get('/gateway/api/findGroup').then(res => {
                commit(FETCH_GROUP_SUCCESS, {list: res});
            }, () => {
                commit(FETCH_GROUP_FAILURE);
            });
        },
        [FETCH_API_REQUEST]({commit, state}, params) {
            let apiPromise;

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

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