Messages.php 11 KB
<?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);
    }
}