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
import { fetchAreaList, fetchAccessAreaList } from '@/api/base/index';
import typeMapping from './typeMapping';
const GET_AREA_LIST = 'GET_AREA_LIST';
const FETCH_STATE = 'FETCH_STATE';
const FETCH_END = 'FETCH_END';
const state = () => {
let areaState = {};
typeMapping.map(type => {
areaState[`list${type.value}`] = [];
areaState[`fetching${type.value}`] = false;
});
return areaState;
};
const initGetters = () => {
let getters = {};
typeMapping.map(type => {
getters[`areaList${type.value}`] = state => state[`list${type.value}`];
});
return getters;
};
const getters = initGetters();
const initAction = () => {
let actions = {};
typeMapping.map(type => {
actions[`fetchAreaList${type.value}`] = ({ state, commit }) => {
if (state[`fetching${type.value}`]) return;
commit(FETCH_STATE, type.value);
let fetchHandle = null;
if (type.value == 0) {
fetchHandle = fetchAreaList;
} else {
fetchHandle = fetchAccessAreaList;
}
return fetchHandle().then(res => {
const list = res.data;
commit(GET_AREA_LIST, { type: type.value, list });
commit(FETCH_END, type.value);
});
};
});
return actions;
};
const actions = initAction();
const mutations = {
[GET_AREA_LIST](state, { type, list }) {
state[`list${type}`] = list;
},
[FETCH_STATE](state, type) {
state[`fetching${type}`] = true;
},
[FETCH_END](state, type) {
state[`fetching${type}`] = false;
},
};
export default {
namespaced: true,
state,
getters,
actions,
mutations,
};