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
import { fetchCollectList, addCollect, delCollect } from '@/api/menu/menu';
const GET_COLLECT_LIST = 'GET_COLLECT_LIST';
const ADD_COLLECT_LIST = 'ADD_COLLECT_LIST';
const DEL_COLLECT_LIST = 'DEL_COLLECT_LIST';
let fetch = 0;
const state = () => ({
collectList: [],
});
const getters = {
collectList: state => state.collectList,
};
const actions = {
fetchCollectList({ commit }) {
if (fetch) return;
fetch++;
return fetchCollectList()
.then(res => {
fetch--;
const data = res.data;
commit(GET_COLLECT_LIST, data);
return res;
})
.catch(err => {
console.error(err);
fetch--;
});
},
addCollect({ commit, getters }, entity) {
return addCollect({
data: { ...entity, sortId: getters.collectList.length + 1 },
}).then(res => {
commit(ADD_COLLECT_LIST, entity.menuId);
});
},
delCollect({ commit }, entity) {
return delCollect({
params: entity,
}).then(res => {
commit(DEL_COLLECT_LIST, entity.id);
});
},
};
const mutations = {
[GET_COLLECT_LIST](state, list) {
state.collectList = list;
},
[ADD_COLLECT_LIST](state, id) {
state.collectList.push({
menuId: id,
});
},
[DEL_COLLECT_LIST](state, id) {
state.collectList = state.collectList.filter(
item => item.menuId.toString() !== id
);
},
};
export default {
state,
getters,
actions,
mutations,
};