coupon.js
2.24 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
export default function() {
return {
namespaced: true,
state: {
unused: {
type: 'unused',
isFetching: false,
reachedEnd: false,
isEmpty: false,
list: [],
filter: 0,
page: 0,
limit: 10,
total: 0
},
used: {
type: 'used',
isFetching: false,
reachedEnd: false,
isEmpty: false,
list: [],
filter: 0,
page: 0,
limit: 10,
total: 0
},
overtime: {
type: 'overtime',
isFetching: false,
reachedEnd: false,
isEmpty: false,
list: [],
filter: 0,
page: 0,
limit: 10,
total: 0
},
},
mutations: {
addList(state, { data }) {
state[data.type] = data;
}
},
actions: {
async fetchCouponList({ commit, state }, param) {
if (!param.type) {
return;
}
let couponData = {...state[param.type]};
if (!param.isReset && couponData.reachedEnd) {
return;
}
let params = {
page: couponData.page,
limit: couponData.limit,
type: couponData.type,
};
if (param.isReset) {
params.page = 1;
} else {
params.page += 1;
}
let result = await this.$api.get('/api/ufo/coupon/list', {...params});
if (result.code === 200) {
let data = result.data;
if (typeof data === 'object' && Object.keys(data).length) {
for (let key in data) {
if (key === 'coupons') {
couponData.list = data.page > 1 ? couponData.list.concat(data.coupons) : data.coupons;
} else {
couponData[key] = data[key];
}
}
if (couponData.list.length === couponData.total) {
couponData.reachedEnd = true;
}
couponData.list.length ? couponData.isEmpty = false : couponData.isEmpty = true;
commit('addList', { data: couponData });
}
} else {
couponData.isEmpty = true;
commit('addList', { data: couponData });
}
return couponData.list || [];
},
},
};
}