public function beforeDispatch(\Phalcon\Events\Event $event, \Phalcon\Mvc\Dispatcher $dispatcher) { $controller = $dispatcher->getControllerName(); $action = $dispatcher->getActionName(); $dbUser = null; $userId = $this->session->get('identity'); if (!$userId) { } else { $dbUser = \BullSoft\Sample\Models\User::findFirst(intval($userId)); $this->di->set('user', $dbUser); } return true; }
public function createAction() { if (!$this->user) { $this->flashJson(403); return; } $comment = array(); $comment['product_id'] = intval($this->request->getPost('product_id', 'int')); if ($comment['product_id'] < 1) { $this->flashJson(500, array(), "非法请求"); return; } $productModel = ProductModel::findFirst($comment['product_id']); if (empty($productModel)) { $this->flashJson(500, array(), "商品不存在"); return; } $content = trim($this->request->getPost('comment')); if (mb_strlen($content, "UTF-8") < 4) { $this->flashJson(500, array('comment'), "内容长度至少4个字"); return; } $comment['content'] = $content; $comment['reply_to_comment_id'] = intval($this->request->getPost('comment_id', 'int')); if ($comment['reply_to_comment_id'] < 0) { $this->flashJson(500, array(), "非法请求"); return; } if ($comment['reply_to_comment_id'] > 0) { $commentModel = CommentModel::findFirst($comment['reply_to_comment_id']); if (empty($commentModel)) { $this->flashJson(500, array(), "你所评论的主题不存在"); return; } } $comment['reply_to_user_id'] = intval($this->request->getPost('user_id', 'int')); if ($comment['reply_to_user_id'] < 0) { $this->flashJson(500, array(), "非法请求"); return; } if ($comment['reply_to_user_id'] > 0) { $userModel = UserModel::findFirst($comment['reply_to_user_id']); } else { if ($comment['reply_to_comment_id'] > 0) { $comment['reply_to_user_id'] = $commentModel->user_id; $userModel = $commentModel->user; } else { if ($comment['reply_to_comment_id'] == 0) { $comment['reply_to_user_id'] = $productModel->user_id; $userModel = $productModel->user; } } } if (empty($userModel)) { $this->flashJson(500, array(), "你所评论的用户不存在"); return; } $comment['user_id'] = $this->user->id; $time = date('Y-m-d H:i:s'); $comment['addtime'] = $time; $comment['modtime'] = $time; $model = new CommentModel(); $model->assign($comment); if ($model->save() == false) { $this->flashJson(500, array(), '评论插入失败'); } else { if (isset($userModel)) { $comment['reply_to']["user_id"] = $userModel->id; $comment['reply_to']["nickname"] = $userModel->nickname; $comment['reply_to']["image_url"] = $userModel->photo; } $comment['id'] = $model->id; $comment['user']["user_id"] = $this->user->id; $comment['user']["nickname"] = $this->user->nickname; $comment['user']["image_url"] = $this->user->photo; $this->flashJson(200, $comment); } return; }