Messages.php
11 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* 消息列表
*/
class YHMApi_App_V2_Messages extends YHMApi_App_V1_Messages {
use YHMApi_App_V2_Base;
/**
* 对话列表
*
* @param array $params
* @param string $fields
* @return array
*/
public static function talkList(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'uid 不能为空.');
}
$number = 10;
if (!empty($params['number']))
{
$number = $params['number'];
}
$mongo = Q_Db::mongo('MessageLoggers')->collection('Talk');
$list = array(
'system' => array(),
'talk' => array(),
'time' => time(),
);
####################################################
//会话消息
$query = array(
'participator' => (int) $params['uid'],
'delete_list' => array(
'$not' => array(
'$in' => array((int) $params['uid'])
)
),
'from_uid' => 0,
);
####################################################
$query['from_uid'] = array('$gt' => 0);
$talkMess = $mongo->find($query)->sort(array(
'time' => -1
))->limit($number);
foreach ($talkMess as $mess)
{
$talkArr = YHMIm_Agreement::format($mess, $params['uid'])->getMongoToTalk();
if ($params['uid'] == $talkArr['from_uid'])
{
$talkArr['unread_number'] = 0;
}
$list['talk'][] = $talkArr;
}
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '对话列表.', $list);
}
private static function readAction($readAction)
{
if ($readAction == 'before')
{
return '$lt'; //before 比什么什么大
}
return '$gt'; //after 比什么什么小
}
/**
* 消息列表
*
* @param array $params
* @param string $fields
* @return array
*/
public static function systemList(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, '用户UID不能为空.');
}
$number = empty($params['number']) ? 10 : $params['number'];
$readTime = !empty($params['read_time']) ? $params['read_time'] : 0;
$uid = $params['uid'];
$mongo = Q_Db::mongo('MessageLoggers');
$col = $mongo->collection('Message');
$list = array();
####################################################
$query = array(
'to_uid' => (int) $uid,
'delete_list' => array(
'$not' => array(
'$in' => array((int) $uid)
)
),
'from_uid' => 0,
);
$readAction = self::readAction((empty($params['read_action']) ? '' : $params['read_action']));
$query['time'] = array(
$readAction => (int) $readTime
);
$systemMess = $col->find($query)->sort(array(
'time' => -1
))->limit($number);
foreach ($systemMess as $mess)
{
$list[] = YHMIm_Agreement::format($mess, $uid)->getMongoToTalk();
}
$mongo->collection('Talk')->update(
array(
'from_uid' => 0,
'participator' => (int) $uid
), array(
'$set' => array(
'unread_number' => 0
)
)
);
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '系统消息.', $list);
}
/**
* 对话聊天
*
* @param array $params
* @param string $fields
* @return array
*/
public static function chat(array $params, $fields = '*')
{
if (empty($params['to_uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'to_uid 不能为空.');
}
if (empty($params['from_uid']) && empty($params['talk_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'from_uid or talk_id 不能为空.');
}
if (!empty($params['from_uid']) && !empty($params['talk_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'from_uid or talk_id 不能同时存在.');
}
$readTime = 0;
if (!empty($params['read_time']))
{
$readTime = $params['read_time'];
}
$mongo = Q_Db::mongo('MessageLoggers');
$list = array('talk' => array(), 'time' => time());
####################################################
if (empty($params['talk_id']))
{
$talkID = YHMIm_Utils::getTalkID(array(
(int) $params['from_uid'],
(int) $params['to_uid']
));
}
else
{
$talkID = $params['talk_id'];
}
$query = array(
'delete_list' => array(
'$not' => array(
'$in' => array((int) $params['to_uid'])
)
)
);
$query['talk_id'] = $talkID;
##########################################################################################
$mongo->collection('Message')->update(array(
'talk_id' => $talkID
), array(
'$addToSet' => array(
'seen_list' => (int) $params['to_uid']
)
));
$mongo->collection('Talk')->update(array(
'talk_id' => $talkID
), array(
'$addToSet' => array(
'seen_list' => (int) $params['to_uid']
),
'$set' => array(
'unread_number' => 0
)
));
##########################################################################################
$readAction = self::readAction((empty($params['read_action']) ? '' : $params['read_action']));
$query['time'] = array(
$readAction => (int) $readTime
);
$number = empty($params['number']) ? 10 : $params['number'];
$talkMess = $mongo->collection('Message')->find($query)->sort(array(
'time' => ($params['read_action'] == 'before' ? -1 : 1)
))->limit($number);
foreach ($talkMess as $mess)
{
if ((($mess['type']=="yhm-product")|| (($mess['type']=="yhm-order-info"))) && ($mess['from_uid'] == $mess['to_uid'])&& ($params['from_uid']!=$mess['to_uid']))
{
continue;
}
else
{
$list['talk'][] = YHMIm_Agreement::format($mess, $params['to_uid'])->getMongoToMessage();
}
}
//是否颠倒
if(!empty($params['reverse']))
{
$list['talk'] = array_reverse($list['talk']);
}
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '对话消息.', $list);
}
/**
* 删除消息
*
* @param array $params
* @param string $fields
* @return array
*/
public static function flagDel(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'uid 不能为空.');
}
if (empty($params['message_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'message_id 不能为空.');
}
try
{
Q_Db::mongo('MessageLoggers')->collection('Message')->update(array(
'_id' => new MongoId($params['message_id'])
), array(
'$addToSet' => array(
'delete_list' => (int) $params['uid']
)
));
}
catch (Exception $e)
{
return self::result(YHMConfig_Mobile::NOTICE_CODE, '删除异常.');
}
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '删除成功.');
}
/**
* 删除对话
*
* @param array $params
* @param string $fields
* @return array
*/
public static function talkFlagDel(array $params, $fields = '*')
{
if (empty($params['talk_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'talk_id 不能为空.');
}
if (empty($params['to_uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'to_uid 不能为空.');
}
$mongo = Q_Db::mongo('MessageLoggers')->collection('Talk');
$query = array(
'talk_id' => $params['talk_id']
);
$mongo->update($query, array(
'$addToSet' => array(
'delete_list' => (int) $params['to_uid']
),
'$set' => array(
'unread_number' => 0
)
));
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '删除成功.');
}
/**
* 标记消息
*
* @param array $params
* @param string $fields
* @return array
*/
public static function flagSeen(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'uid 不能为空.');
}
if (empty($params['message_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'message_id 不能为空.');
}
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '标记成功.');
}
/**
* 标记对话所有信息
* @param array $params
* @param string $fields
* @return array
*/
public static function talkFlagSeen(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'uid 不能为空.');
}
if (empty($params['talk_id']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'talk_id 不能为空.');
}
$mongo = Q_Db::mongo('MessageLoggers');
if ($mongo->isValid($params['talk_id']) == false)
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'talk_id错误.');
}
$col = $mongo->collection('Talk');
$talkID = new MongoID($params['talk_id']);
$query = array('_id' => $talkID);
$talkData = $col->findOne($query);
if (!isset($talkData['seen_list']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, '标记对话错误.');
}
$uid = (int) $params['uid'];
$col->update($query, array(
'$addToSet' => array(
'seen_list' => $uid
)
));
$mongo->collection('Message')->update(array(
'talk_id' => $talkID
), array(
'$addToSet' => array(
'seen_list' => $uid
)
));
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '标记对话成功.');
}
/**
* 未读总数
*
* @param array $params
* @param string $fields
* @return array
*/
public static function unread(array $params, $fields = '*')
{
if (empty($params['uid']))
{
return self::result(YHMConfig_Mobile::WARING_CODE, 'uid 不能为空.');
}
$col = Q_Db::mongo('MessageLoggers')->collection('Talk');
$_seenData = $col->find(array(
'participator' => (int) $params['uid'],
'unread_number' => array('$gt' => 0)
));
//系统消息未读数
$systemTalk = $col->find(array(
'from_uid' => 0,
'participator' => (int) $params['uid']
));
$systemUnread = 0;
foreach($systemTalk as $talk)
{
$systemUnread += $talk['unread_number'];
}
$seenData = array(
'unread_total_number' => 0,
'talk_unread' => array(),
'system_unread_total_number' => $systemUnread,
);
foreach ($_seenData as $mess)
{
if ($mess['from_uid'] != $params['uid'])
{
$seenData['unread_total_number'] = $seenData['unread_total_number'] + $mess['unread_number'];
$seenData['talk_unread'][$mess['from']] = $mess['unread_number'];
}
}
return self::result(YHMConfig_Mobile::SUCCESS_CODE, '未读总数.', $seenData);
}
}