/** * 检查消息关系是否存在 * @param Array $messageId * @return Int */ public function has($messageId) { $pdo = PdoFactory::instance(self::DB_MASTER); $sql = 'SELECT 1 FROM ' . $this->table . ' WHERE messageId=:messageId'; $stmt = $pdo->prepare($sql); $stmt->execute(array(':messageId' => $messageId)); return $stmt->columnCount(); }
/** * 取得消息ID * @param Array $data */ public static function getId() { $pdo = PdoFactory::instance(self::DB_MASTER); $sql = 'INSERT INTO ' . self::TABLE_NAME . ' VALUES()'; $stmt = $pdo->prepare($sql); $stmt->execute(); return $pdo->lastInsertId(); }
/** * 查询单条消息 * @param Bigint $messageId * @param Array $fields */ public function get($messageId, $fields = array()) { $pdo = PdoFactory::instance(self::DB_MASTER); $fields = empty($fields) ? $this->fields : $fields; $sql = 'SELECT ' . implode(',', $fields) . ' FROM ' . $this->table . ' WHERE messageId=:messageId'; $stmt = $pdo->prepare($sql); $stmt->execute(array(':messageId' => $messageId)); return $stmt->fetch(PDO::FETCH_ASSOC); }
public function dataAction() { $message = $this->getRequest()->getParams(); $pdo = PdoFactory::instance('messageMaster'); $senderRelationDao = new Dao\RelationModel($pdo, conf\Table::relation($message['sender'])); $receiverRelationDao = new Dao\RelationModel($pdo, conf\Table::relation($message['receiver'])); $messageDao = new Dao\MessageModel($pdo, conf\Table::message($message['messageId'])); $respones = array(); $respones['sender'] = $senderRelationDao->has($message['messageId']); $respones['receiver'] = $receiverRelationDao->has($message['messageId']); $respones['message'] = $messageDao->has($message['messageId']); $this->getResponse()->setBody(json_encode($respones)); }
/** * 存储一条消息 * @param Array $message */ public function save($data) { $message = $this->filter($data); if (!$this->check($message)) { $this->setErrCode(Error::PARAMS_ERROR); return false; } $messageId = Dao\MessageIdModel::getId(); $message['messageId'] = $messageId; $message['sendTime'] = date('Y-m-d H:i:s'); $userModel = new UserModel($this->userId); $message['isRead'] = 0; if ($userModel->getTalkId($message['sender']) == $message['receiver']) { $message['isRead'] = 1; } $messageDao = new Dao\MessageModel(Table::message($messageId)); $relationSenderDao = new Dao\RelationModel(Table::relation($message['sender'])); $relationReceiverDao = new Dao\RelationModel(Table::relation($message['sender'])); $relationData = array('sender' => $message['sender'], 'receiver' => $message['receiver'], 'senderNickname' => $message['senderNickname'], 'receiverNickname' => $message['receiverNickname'], 'messageId' => $messageId, 'isRead' => $message['isRead']); $pdo = PdoFactory::instance(Dao\MessageModel::DB_MASTER); $pdo->beginTransaction(); try { if (!$messageDao->save($message)) { throw new \Exception('save message fail!'); } if (!$relationSenderDao->has($messageId) && $relationSenderDao->save($relationData)) { throw new \Exception('save sender relation fail!'); } if (!$relationReceiverDao->has($messageId) && $relationReceiverDao->save($relationData)) { throw new \Exception('save receiver relation fail!'); } $pdo->commit(); } catch (\Exception $e) { $pdo->rollBack(); $this->setErrCode(Error::SAVE_MESSAGE_ERROR); Log::fatal(self::LOG_DIR, $e->getMessage()); return false; } $messageContentModel = new MessageContentModel(); //处理消息关系 $message['summary'] = $messageContentModel->getMessageSummary($message['content']); $contactModel = new ContactModel($this->userId); $contactModel->addContact($message); //追加未读消息 if ($message['isRead'] == 0) { $userModel->appendUnreadMessage($message['sender'], $messageId); } return true; }
/** * 创建 message 表 */ private static function createMessage() { $pdo = PdoFactory::instance('messageMaster'); $sql = "CREATE TABLE `message_[index]` (\n\t\t\t\t `messageId` bigint(19) unsigned NOT NULL DEFAULT '0',\n\t\t\t\t `catId` smallint(5) unsigned NOT NULL DEFAULT '0',\n\t\t\t\t `sender` int(11) NOT NULL,\n\t\t\t\t `senderNickname` varchar(60) NOT NULL,\n\t\t\t\t `receiver` int(11) NOT NULL,\n\t\t\t\t `receiverNickname` varchar(60) NOT NULL,\n\t\t\t\t `content` text NOT NULL,\n\t\t\t\t `isRead` tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\t `isReplyed` tinyint(1) unsigned DEFAULT NULL,\n\t\t\t\t `senderSaved` tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\t `receiverSaved` tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\t `senderDeleted` tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\t `receiverDeleted` tinyint(1) NOT NULL DEFAULT '0',\n\t\t\t\t `sendTime` datetime NOT NULL,\n\t\t\t\t `senderSaveTime` datetime DEFAULT NULL,\n\t\t\t\t `receiverSaveTime` datetime DEFAULT NULL,\n\t\t\t\t `senderDeleteTime` datetime DEFAULT NULL,\n\t\t\t\t `receiverDeleteTime` datetime DEFAULT NULL,\n\t\t\t\t PRIMARY KEY (`messageId`)\n\t\t\t\t) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='会员消息表'"; try { for ($i = 0; $i < conf\Table::MESSAGE_MOD; $i++) { $createSql = str_replace('[index]', $i, $sql); $stmt = $pdo->prepare($createSql); $stmt->execute(); } } catch (\Exception $e) { echo $e->getMessage() . PHP_EOL; return false; } echo 'create Message table ok!' . PHP_EOL; return true; }
/** * 取得消息数据 * @param $messageId */ private function getMessage($messageId) { $message = $this->cacheModel->getCacheMessage($messageId); $messageDao = null; if (empty($message) || !is_array($message)) { $pdo = PdoFactory::instance('messageSlave'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $messageDao->get($messageId); $this->cacheModel->cacheMessage($message); } //设置消息其他信息 $message['senderPhoto'] = $this->userModel->getPhoto($message['sender']); $message['receiverPhoto'] = $this->userModel->getPhoto($message['receiver']); //消息富文本 $message['richContent'] = $this->messageContentModel->getRichContent($message['content']); return $message; }
/** * 设置消息已读 * @param $messageId */ public function setIsRead($messageId) { $pdo = PdoFactory::instance('messageMaster'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $this->cacheModel->getCacheMessage($messageId); if (empty($message) || !is_array($message)) { $pdo = PdoFactory::instance('messageSlave'); $messageDao = new Dao\MessageModel($pdo, Table::message($messageId)); $message = $messageDao->get($messageId); } if (empty($message) || !is_array($message)) { return false; } $messageDao->update($messageId, array('isRead' => 1)); $message['isRead'] = 1; $this->cacheModel->cacheMessage($message); $this->userModel->removeUnreadMessage($this->userId, $messageId); return true; }