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 userInfo() { if (!$this->session->has(self::BULL_SOCIAL_SESSION_KEY)) { return false; } $socialCookie = $this->session->get(self::BULL_SOCIAL_SESSION_KEY); $socialOAuth = json_decode($socialCookie, true); $request = new \Buzz\Message\Request(); $request->setHost("https://openapi.baidu.com"); $request->setResource("/social/api/2.0/user/info?access_token=" . $socialOAuth['access_token']); $response = new \Buzz\Message\Response(); $client = $this->getCurlClient(); $client->send($request, $response); if (!$response->isOk()) { return false; } $socialUser = json_decode($response->getContent(), true); if (count($socialUser) < 3) { return false; } $socialUserModel = SocialUserModel::findFirst('social_uid=' . intval($socialUser['social_uid'])); $time = date('Y-m-d H:i:s'); if (empty($socialUserModel)) { $socialUserModel = new SocialUserModel(); $socialUserModel->assign($socialUser); if ($socialUserModel->save() == false) { // foreach ($socialUserModel->getMessages() as $message) { // echo $message. "<br />"; // } return false; } } if ($socialUserModel->user_id > 0) { $this->session->set('identity', $socialUserModel->user_id); return true; } try { $this->db->begin(); $userModel = new UserModel(); $userModel->username = '******' . \BullSoft\Utility::generateRandomString(8); $userModel->nickname = $socialUser['username']; $userModel->password = \BullSoft\Utility::generateRandomString(); $userModel->photo = $socialUser['tinyurl']; $userModel->email = \BullSoft\Utility::generateRandomString(32) . "@"; $userModel->level = 1; $userModel->is_active = 'N'; $userModel->active_code = \BullSoft\Utility::generateRandomString(32); $userModel->addtime = $time; $userModel->modtime = $time; if ($userModel->save() == false) { /* foreach ($userModel->getMessages() as $message) { */ /* echo $message. "<br />"; */ /* } */ $this->db->rollback("不能保存用户!"); } $socialUserModel->user_id = $userModel->id; if ($socialUserModel->save() == false) { /* foreach ($socialUserModel->getMessages() as $message) { */ /* echo $message. "<br />"; */ /* } */ $this->db->rollback("不能保存用户!"); } $this->session->set('identity', $userModel->id); $this->db->commit(); } catch (\Exception $e) { $this->db->rollback(); } 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; }