coupon.js
4.29 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* 领券频道
* @author: 赵彪<bill.zhao@yoho.cn>
* @date: 2016/04/14
*/
var $ = require('yoho.jquery'),
lazyLoad = require('yoho.lazyload'),
Dialog = require('../common/dialog').Dialog;
var alertConfig,
makeAlert;
// j面跳转对象
var redirect = {
// 去逛逛跳转链接
gunangSrc: null,
// 查看优惠券跳转链接
checkCouponSrc: null,
goToGuang: function() {
window.location.href = this.gunangSrc;
},
goToCheck: function() {
window.location.href = this.checkCouponSrc;
},
// 处理网络返回
403: function(url) {
window.location.href = url;
},
200: function(url) {
this.checkCouponSrc = url;
}
};
//加载底部图片
lazyLoad($('img.lazy'));
require('../common/slider');
$('.slide-container').slider();
// 根据配置执行展示弹窗
function couponAlert(opt) {
var newAlert = new Dialog(opt);
newAlert.show();
}
// 配置弹窗
alertConfig = {
success: {
content: '恭喜您,成功领取优惠券',
subContents: ['特殊情况下到账有延时', '请耐心等待'],
className: 'subcontent-dialog',
refreshOnClose: true,
btns: [
{
id: 1,
name: '去购物啦',
btnClass: ['black', 'btn-close'],
cb: function() {
redirect.goToGuang();
}
},
{
id: 2,
name: '查看优惠券',
btnClass: ['btn-close'],
cb: function() {
redirect.goToCheck();
}
}
]
},
alreadyGot: {
content: '您已领取过优惠券',
subContent: '快去选购心仪的潮品吧',
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '去使用',
btnClass: ['btn-close'],
cb: function() {
redirect.goToGuang();
}
}
]
},
expired: {
content: '优惠券已过期',
subContent: '去领最新的优惠券吧',
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '关闭',
btnClass: ['btn-close']
}
]
},
failed: {
content: '领取失败',
subContents: ['请刷新重试,', '多次无效请联系客服'],
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '刷新',
btnClass: ['btn-close'],
cb: function() {
window.location.reload();
}
}
]
}
};
// 对应不同的网络返回码展示不同的弹窗
makeAlert = {
200: function() {
couponAlert(alertConfig.success);
},
401: function() {
couponAlert(alertConfig.alreadyGot);
},
315: function() {
couponAlert(alertConfig.expired);
},
400: function() {
couponAlert(alertConfig.failed);
},
403: function() {
// do not make alert when user are not login in
}
};
function requestCoupon(id) {
$.ajax({
type: 'GET',
url: '/coupon/sendcoupon',
data: {
id: id
},
success: function(res) {
var code = res.code;
// 如果返回的数据里有url,则执行redirect里的方法
res.url && res.url.length > 0 && redirect[code] && redirect[code](res.url + '?'+ location.href.split('?')[1]);
// 如果能找到对应code则执行相应方法,如果没有的对应code则执行error提示
makeAlert[code] ? makeAlert[code]() : makeAlert['400']();
},
error: function(err) {
var Alert = require('../common/dialog').Alert;
new Alert('网络异常').show();
}
});
}
$('.enable .info').on('click', function(e) {
e.preventDefault();
requestCoupon($(this).closest('a').data('id'));
redirect.gunangSrc = $(this).closest('a').get(0).href;
});