/** * 发送消息 * @param array $message * @param bool $is_notify * @return array|int */ public static function sendMessage(array $message, $is_notify = false) { $db = self::db(); // 取得当前用户UID,如果没有则返回错误代码0 $from_uid = intval(self::getLoggedUserInfo('uid')); if ($from_uid < 1) { return 0; } if ($is_notify) { $type = 'notify'; $room_id = $message['room_id']; } else { // 检查消息类型 $type = isset($message['message_type']) ? $message['message_type'] : false; $types = array('text', 'voice', 'image', 'position', 'card'); if (!$type || !in_array($type, $types)) { return 1; } // 检查房间是否存在 $room_id = isset($message['room_id']) ? intval($message['room_id']) : 0; if ($room_id <= 0 || !self::hasRoom($room_id)) { return 2; } } // 取得房间成员 $table = self::table('message_member'); $where = 'list_id=' . $room_id; $to_uid_list = $db->select('member_uid')->from($table)->where($where)->column(); if (!$is_notify) { if (!$to_uid_list || !in_array($from_uid, $to_uid_list)) { return 3; } } // 4 私信隐私检查 // TODO // 准备消息数据 $data['from_uid'] = $from_uid; $data['type'] = $type; $data['list_id'] = $room_id; $data['mtime'] = time(); $return = $data; // 开始具体检查内容 if ($type == 'notify') { // 通知动态信息,仅内部发送 $data['content'] = @trim($message['content']) ?: '[动态]'; $return['content'] = $data['content']; $data['content'] = Util::htmlEncode($data['content']); if (@is_array($message['attach'])) { $return = array_merge($message['attach'], $return); $data['attach_ids'] = @serialize($message['attach']); } else { $data['attach_ids'] = ''; } } elseif ($type == 'text') { // 普通消息 $content = @trim($message['content']) ?: null; if (empty($content)) { return 5; } $return['content'] = $content; $data['content'] = Util::htmlEncode($content); $data['attach_ids'] = ''; } elseif ($type == 'card') { // 发名片消息 if (!isset($message['uid']) || !self::hasUser($message['uid'])) { return 6; } $data['content'] = $return['content'] = '[名片]'; $return['uid'] = $message['uid']; $data['attach_ids'] = serialize(array('uid' => $message['uid'])); } else { // 发送 带附件的消息 // 取得附件ID $attach_id = @intval(Util::desDecrypt($message['attach_id'])); if ($attach_id <= 0) { return 7; } $return['attach_id'] = $attach_id; if ($type == 'voice') { //语音消息 if (!@is_numeric($message['length'])) { return 8; } $data['attach_ids'] = serialize(array('attach_id' => $attach_id, 'length' => $message['length'])); $return['length'] = $message['length']; $data['content'] = $return['content'] = '[语音]'; } elseif ($type == 'position') { // 位置消息 $latitude = @trim($message['latitude']) ?: null; $longitude = @trim($message['longitude']) ?: null; $location = @trim($message['location']) ?: null; if (!is_numeric($latitude) || !is_numeric($longitude) || !$location) { return 9; } $data['attach_ids'] = serialize(array('attach_id' => $attach_id, 'latitude' => $latitude, 'longitude' => $longitude, 'location' => Util::htmlEncode($location))); $return['latitude'] = $latitude; $return['longitude'] = $longitude; $return['location'] = $location; $data['content'] = $return['content'] = '[位置]'; } else { $data['attach_ids'] = serialize(array('attach_id' => $attach_id)); $data['content'] = $return['content'] = '[图片]'; } } // 添加消息内容 $db = self::db(); $table = self::table('message_content'); $message_id = $db->insert($table)->cols($data)->query(); if ($message_id) { $return['message_id'] = $message_id; try { $time = $data['mtime']; $table = self::table('message_member'); // 更新其他成员新消息数量 $db->query("UPDATE `{$table}` SET `new`=`new`+1,`message_num`=`message_num`+1 " . "WHERE `list_id`={$room_id} AND `member_uid`<>{$from_uid}"); // 更新自己消息总数和最后发布时间 $db->query("UPDATE `{$table}` SET `message_num`=`message_num`+1,`list_ctime`={$time} " . "WHERE `list_id`={$room_id} AND `member_uid`={$from_uid}"); // 更新最后一条消息 $table = self::table('message_list'); $lastMessage = array('mtime' => $time, 'last_message' => serialize($return)); $db->update($table)->cols($lastMessage)->where("`list_id`={$room_id}")->query(); // 加入推送暂存表 $table = self::table('message_push'); $insert_sql = "INSERT INTO `{$table}` (`message_id`,`list_id`,`uid`,`ctime`) VALUES "; $insert_val = ''; foreach ($to_uid_list as $to_uid) { if ($is_notify || $to_uid != $from_uid) { $insert_val .= "('{$message_id}',{$room_id},{$to_uid},{$time}),"; } } if ($insert_val) { $db->query($insert_sql . rtrim($insert_val, ',')); } // 返回数据 $return['room_id'] = $return['list_id']; if (!empty($attach_id)) { $return['attach_id'] = $message['attach_id']; } unset($return['list_id']); $return['from_uname'] = self::getLoggedUserInfo('uname'); return array('return' => $return, 'to_user_list' => $to_uid_list); } catch (\Exception $e) { // none } } return 10; }
/** * 将消息列表整理为标准的返回格式 * @param array $list 需要整理的消息列表 * @return array 返回整理好的消息列表 */ public static function parseMessage($list) { if (!$list) { return array(); } $array = array(); $users = self::getUserInfo(Util::arrayColumn($list, 'from_uid')); foreach ($list as $key => $rs) { $array[$key]['message_id'] = (int) $rs['message_id']; $array[$key]['from_uid'] = (int) $rs['from_uid']; $array[$key]['from_uname'] = (string) $users[$rs['from_uid']]['uname']; $array[$key]['type'] = $rs['type']; $array[$key]['content'] = Util::htmlDecode($rs['content']); $array[$key]['room_id'] = (int) $rs['list_id']; $array[$key]['mtime'] = (int) $rs['mtime']; if (empty($rs['attach_ids'])) { $attach = array(); } else { $attach = @unserialize($rs['attach_ids']); } if ($rs['type'] == 'notify') { if ($attach) { $array[$key] = array_merge($attach, $array[$key]); } } elseif ($rs['type'] == 'voice') { $array[$key]['length'] = @$attach['length']; $array[$key]['attach_id'] = @Util::desEncrypt($attach['attach_id']); } elseif ($rs['type'] == 'image') { $array[$key]['attach_id'] = @Util::desEncrypt($attach['attach_id']); } elseif ($rs['type'] == 'position') { $array[$key]['latitude'] = @$attach['latitude']; $array[$key]['longitude'] = @$attach['longitude']; $array[$key]['location'] = @Util::htmlEncode($attach['location']); $array[$key]['attach_id'] = @Util::desEncrypt($attach['attach_id']); } elseif ($rs['type'] == 'card') { $array[$key]['uid'] = @(int) $attach['uid']; } else { $array[$key]['type'] = 'text'; } } return $array; }