message.page.js
3.25 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
/**
* 我的信息
* @author: yyqing<yanqing.yang@yoho.cn>
* @date: 2016/3/21
*/
var $ = require('yoho-jquery');
var Dialog = require('../common/dialog');
var $container = $('#message-main-container'),
$msgCheck = $container.find('.msg-check'),
$readBtn = $container.find('.read-choose-btn'),
$checked;
var $pickBtn = $('.pick-coupon-btn');
var operationId = [];
var pickBusy = false;
var Alert = Dialog.Alert,
Confirm = Dialog.confirm;
function hasNew() {
var newMsg = false;
$checked = $container.find('.msg-check:checked');
operationId = [];
$checked.each(function() {
var $par = $(this).parent().parent();
operationId.push($(this).val());
if ($par.hasClass('new-msg')) {
newMsg = true;
}
});
if (newMsg) {
$readBtn.addClass('has-new');
} else {
$readBtn.removeClass('has-new');
}
}
function msgOperation(type, data) {
var url;
switch (type) {
case 'del':
url = '/home/message/del';
break;
case 'read':
url = '/home/message/read';
break;
default:
break;
}
if (!url) {
return;
}
$.ajax({
type: 'GET',
url: url,
data: data
}).then(function(jsonData) {
if (jsonData.code === 200) {
if (type === 'read') {
$checked.removeAttr('checked');
$checked.parent().parent().removeClass('new-msg');
hasNew();
} else {
window.location.reload();
}
} else {
new Alert(jsonData.message);
}
});
}
$container.on('change', 'input[type="checkbox"]', function() {
var checked = $(this).attr('checked');
if ($(this).hasClass('choose-all')) {
if (checked === 'checked') {
$msgCheck.attr('checked', true);
} else {
$msgCheck.removeAttr('checked');
}
}
hasNew();
});
$container.on('click', '.del-btn', function() {
var data = $(this).data();
new Confirm({
content: '您确定要删除这条短消息?',
cb: function() {
msgOperation('del', data);
}
}).show();
});
$container.on('click', '.del-choose-btn', function() {
if (!operationId.length) {
new Alert('请选中您要删除的消息');
return;
}
new Confirm({
content: '确定要删除您选中的消息?',
cb: function() {
msgOperation('del', {
id: operationId.join(',')
});
}
}).show();
});
$container.on('click', '.read-choose-btn', function() {
if ($(this).hasClass('has-new')) {
msgOperation('read', {
id: operationId.join(',')
});
}
});
$pickBtn.click(function() {
var data = $(this).data();
if (pickBusy) {
return;
}
pickBusy = true;
$.ajax({
type: 'GET',
url: '/home/message/pickCoupon',
data: data
}).then(function(jsonData) {
var msg = jsonData.message;
if (jsonData.code === 200) {
msg = '领取成功';
$(this).removeClass('pick-coupon-btn');
}
pickBusy = false;
new Alert(msg);
});
});