-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathannouncement.js
165 lines (156 loc) · 3.37 KB
/
announcement.js
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
function get(socket,data,fn){
console.log("announcement/get");
console.log(data)
var result={
code:0,
time:0,
data:[]
};
var returnFn=function(){
if(socket){
socket.emit("announcement_get",result);
}
else if(fn){
var returnString = JSON.stringify(result);
fn(returnString);
}
}
//returnFn();
//return;
data_mg.updateTime.find({"parentKey":"announcement"},function(err,doc){
if(err){
result.code=0;
returnFn()
}else{
if(doc&&doc.length&&doc[0].childKey>data.data){
result.time=doc[0].childKey;
data_mg.announcement.find({},function(err,docA){
if(err){
result.code=0
}else{
result.code=1;
result.data=docA;
}
returnFn();
})
}else{
result.code=2;
returnFn();
}
}
})
};
function add(socket,data,fn){
console.log("announcement/add");
//data.data={
//"id":uuid(),/*id*/
//"title":"eee",/*标题*/
//"message":"rewr",/*内容*/
//"start":0,/*生效时间*/
//"end":0/*结束时间*/
//}
if(typeof(data.data)=="string"){
data.data=JSON.parse(data.data)
}
var result={code:0};
var returnFn=function(){
if(socket){
socket.emit("announcement_add",result);
}
else if(fn){
var returnString = JSON.stringify(result);
fn(returnString);
}
}
var newAnnouncement=new data_mg.announcement(data.data);
console.log("添加公告")
newAnnouncement.save(function(err){
if(err){
console.log(err)
result.code=0;
returnFn()
}else{
console.log("更新时间")
data_mg.updateTime.update({"parentKey":"announcement"},{$set:{"childKey":new Date().getTime()}},{},function(errA){
if(errA){
console.log(errA)
result.code=0
}else{
result.code=1
}
returnFn();
})
}
})
};
function edit(socket,data,fn){
console.log("announcement/edit");
if(typeof(data.data)=="string"){
data.data=JSON.parse(data.data)
}
console.log(data.data)
var result={code:0};
var returnFn=function(){
if(socket){
socket.emit("announcement_edit",result);
}
else if(fn){
var returnString = JSON.stringify(result);
fn(returnString);
}
}
console.log("更新公告")
data_mg.announcement.update({"id":data.data.id},{$set:data.data},{},function(err){
if(err){
console.log(err)
result.code=0;
returnFn();
}else{
console.log("更新时间")
data_mg.updateTime.update({"parentKey":"announcement"},{$set:{"childKey":new Date().getTime()}},{},function(errA){
if(errA){
console.log(errA)
result.code=0
}else{
result.code=1
}
returnFn();
})
}
})
};
function remove(socket,data,fn){
console.log("announcement/remove");
//data.data="ehdjk"/*id*/
console.log(data.data);
var result={code:0};
var returnFn=function(){
if(socket){
socket.emit("announcement_remove",result);
}
else if(fn){
var returnString = JSON.stringify(result);
fn(returnString);
}
}
console.log("删除公告")
data_mg.announcement.remove({"id":data.data},function(err){
if(err){console.log(err)
result.code=0;
returnFn();
}else{console.log("更新时间")
data_mg.updateTime.update({"parentKey":"announcement"},{$set:{"childKey":new Date().getTime()}},function(errA){
if(errA){console.log(errA)
result.code=0
}else{
result.code=1
}
returnFn();
})
}
})
};
exports.get=get;
exports.add=add;
exports.edit=edit;
exports.remove=remove;