message.js
2.56 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
/**
* 我的信息
* @author: yyqing<yanqing.yang@yoho.cn>
* @date: 2016/3/21
*/
var $ = require('yoho.jquery');
var $container = $('#message-main-container'),
$msgCheck = $container.find('.msg-check'),
$readBtn = $container.find('.read-choose-btn'),
$checked;
var operationId = [];
require('./message-content');
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/delMessage';
break;
case 'read':
url = '/home/message/readMessage';
break;
}
$.ajax({
type: 'GET',
url: url,
data: data
}).then(function(jsonData) {
if (!jsonData.code) {
if (type === 'del') {
$container.html(jsonData);
$msgCheck = $container.find('.msg-check');
$readBtn = $container.find('.read-choose-btn');
}
} else {
if (jsonData.code === 200) {
if (type === 'read') {
$checked.removeClass('new-msg');
}
} else {
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() {
if (confirm('您确定要删除这条短消息?')) {
msgOperation('del', $(this).data());
}
});
$container.on('click', '.del-choose-btn', function() {
if (confirm('确定要删除您选中的消息')) {
msgOperation('del', {
id: operationId.join(',')
});
}
});
$container.on('click', '.read-choose-btn', function() {
if ($(this).hasClass('has-new')) {
msgOperation('read', {
id: operationId.join(',')
});
}
});