store.js
4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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);
});
}
}
};