coupon-service.js
4.17 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
const _ = require('lodash');
const CouponApi = require('./coupon-api');
const helpers = global.yoho.helpers;
const setPager = require(`${global.utils}/pager`).setPager;
const handleFilterUrl = require(`${global.utils}/parameter`).fullParamToMinPath;
const COUPON_BASE_URI = '/home/coupons';
const COUPON_TYPE = {
UNUSED: 'notuse',
USED: 'use',
INVALID: 'overtime'
};
module.exports = class extends global.yoho.BaseModel {
constructor(ctx) {
super(ctx);
}
_setShowNum(num) {
num = num > 0 ? num : 0;
num = num > 99 ? '99+' : num;
return num ? `(${num})` : '';
}
_setCouponTabs(nums, type) {
nums = nums || {};
return [
{
active: type === COUPON_TYPE.UNUSED,
url: helpers.urlFormat(COUPON_BASE_URI),
name: `可用${this._setShowNum(nums[COUPON_TYPE.UNUSED])}`
},
{
active: type === COUPON_TYPE.USED,
url: helpers.urlFormat(COUPON_BASE_URI, {type: COUPON_TYPE.USED}),
name: `已使用${this._setShowNum(nums[COUPON_TYPE.USED])}`
},
{
active: type === COUPON_TYPE.INVALID,
url: helpers.urlFormat(COUPON_BASE_URI, {type: COUPON_TYPE.INVALID}),
name: `已失效${this._setShowNum(nums[COUPON_TYPE.INVALID])}`
}
];
}
getCouponData(uid, params) {
params = params || {};
const page = parseInt(`0${params.page}`, 10) || 1;
const type = params.type || COUPON_TYPE.UNUSED;
const couponApiCase = new CouponApi(this.ctx);
let resData = {};
return Promise.all([
couponApiCase.getCouponList(uid, params.type, page, params),
couponApiCase.getCouponNum(uid)
]).then(result => {
Object.assign(resData, _.get(result, '[0].data', {}));
console.log('resData:', resData);
if (resData.couponList && resData.couponList.length) {
_.forEach(resData.couponList, value => {
if (type === COUPON_TYPE.USED) {
value.isUsed = true;
} else if (type === COUPON_TYPE.INVALID) {
if (value.is_overtime === 'Y') {
value.isOvertime = true;
} else {
value.isUseless = true;
}
} else {
value.toUse = helpers.urlFormat(handleFilterUrl('/list', {
cpc_id: value.coupon_id,
coupon_code: value.coupon_code,
phrase: `以下商品可使用【${value.coupon_name}】优惠券`,
status: 1
}));
}
});
} else {
resData.couponList = false;
}
resData.tabs = this._setCouponTabs(_.get(result, '[1].data', {}), type);
if (_.get(resData, 'tabs[0].active') && resData.filters) {
const filterUrl = helpers.urlFormat(COUPON_BASE_URI,
Object.assign({}, params, {filter: '__filter__', page: 1}));
let filter = params.filter || 0;
_.forEach(resData.filters, value => {
if ((+filter === +value.filter_id)) {
value.active = true;
value.url = 'javascript:;'; // eslint-disable-line
} else {
value.url = filterUrl.replace('__filter__', value.filter_id);
}
});
} else {
resData.filters = false;
}
let total = _.get(resData, 'total', 0);
let pageTotal = _.get(resData, 'pageNum') || 1;
if (resData.couponList && resData.couponList.length) {
resData.pager = Object.assign({
count: total,
curPage: page,
totalPages: pageTotal
}, setPager(pageTotal, params));
}
return resData;
});
}
};