예제 #1
0
 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;
 }