public function uploadFile()
 {
     $request = Request::capture();
     $file = $request->file('file');
     $fileName = $request->input('filename');
     $width = $request->input('width');
     $height = $request->input('height');
     if ($file->isValid()) {
         $tmpName = $file->getFilename();
         $realPath = $file->getRealPath();
         $extension = $file->getClientOriginalExtension();
         $fileType = $file->getMimeType();
     }
     $picURL = 'urla-------';
     /*
     $client = S3Client::factory(array(
         'region'      => 'us-west-2',
         'version'     => 'latest',
         'credentials' => [
             'key'    => 'AKIAICY5UKOXG57U6HGQ',
             'secret' => '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',
         ],
     ));
     
     $s3key = 'tempKey';
     $result = $client->putObject(array(
         'ACL'        => 'public-read',
         'Bucket'     => 'questionbucket',
         'Key'        => $s3key,
         'SourceFile' => $realPath
     ));
     
     $picURL = $result['ObjectURL'];
     */
     try {
         $picture = Picture::create(['original_pic' => $picURL, 'bmiddle_pic' => $picURL, 'thumbnail_pic' => $picURL, 'pictureName' => $fileName, 'width' => $width, 'height' => $height]);
         return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, $picture->getAttributes(), '上传成功');
     } catch (Exception $e) {
         return Utility::response_format(Utility::RESPONSE_CODE_DB_ERROR, '', $e->getMessage());
     }
 }
 public function getHotAnswer()
 {
     $request = Request::capture();
     $token = $request->input('token');
     $questionId = $request->input('questionId');
     $answersQuery = Answer::select('id', 'answer_brief', 'answer_time', 'question_id', 'user_id', 'is_resolved')->where('question_id', $questionId);
     $answerIds = $answersQuery->lists('id')->toArray();
     $upAnswers = AnswerUp::select('answer_id', DB::raw('count(*) as up_number'))->whereIn('answer_id', $answerIds)->groupBy('answer_id')->orderBy('up_number', 'desc')->take(2);
     //        print_r($upAnswers->lists('answer_id')->toArray());
     $answersQuery = Answer::select('id', 'answer_brief', 'answer_time', 'question_id', 'user_id', 'is_resolved')->where('question_id', $questionId)->whereIn('id', $upAnswers->lists('answer_id')->toArray());
     $answers = $answersQuery->get()->toArray();
     $upNumber = $upAnswers->get()->toArray();
     $result = array();
     foreach ($answers as $an) {
         foreach ($upNumber as $up) {
             if ($up['answer_id'] == $an['id']) {
                 $an = array_merge($an, ['upNumber' => $up['up_number']]);
                 array_push($result, $an);
             }
         }
     }
     return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, $result, '请求成功');
 }
 public function addUserTags()
 {
     $request = Request::capture();
     $token = $request->input('token');
     $tags = $request->input('tags');
     if ($tags == null) {
         return Utility::response_format(Utility::RESPONSE_CODE_Error, '', '标签不能为空');
     }
     $userId = AuthController::getUserIdByToken($token);
     if ($userId == null) {
         return Utility::response_format(Utility::RESPONSE_CODE_AUTH_ERROR, '', '认证失败');
     }
     DB::beginTransaction();
     try {
         $array = explode(',', $tags);
         $result = UserTags::where('user_id', $userId)->delete();
         foreach ($array as $tag) {
             $result = UserTags::create(['user_id' => $userId, 'tag_id' => $tag]);
         }
         DB::commit();
         return Utility::response_format(Utility::RESPONSE_CODE_SUCCESS, '', '添加成功');
     } catch (Exception $e) {
         DB::rollBack();
         return Utility::response_format(Utility::RESPONSE_CODE_DB_ERROR, '', $e->getMessage());
     }
 }
 public function register()
 {
     $request = Request::capture();
     $username = $request->input('username');
     $password = $request->input('password');
     $nickname = $request->input('nickname');
     $age = $request->input('age');
     $information = $request->input('information');
     $location = $request->input('location');
     $headPic = $request->input('headPicture');
     $homePic = $request->input('homePicture');
     if ($username == null || $password == null) {
         // 账号 密码 不能为空
         return Utility::response_format(Utility::RESPONSE_CODE_Error, '', '账号密码不能为空');
     } else {
         if (UserAccount::where('username', $username)->first() != null) {
             return Utility::response_format(Utility::RESPONSE_CODE_Error, '', '账号已存在');
         } else {
             if (Utility::isValidateUsername($username) == false) {
                 // 用户名无效
             } else {
                 if (Utility::isValidatePassword($password) == false) {
                     // 密码无效
                 }
             }
         }
     }
     $token = Utility::genToken();
     DB::beginTransaction();
     try {
         $user = UserAccount::create(['username' => $username, 'password' => $password, 'token' => $token]);
         $userInfo = UserInfo::create(['user_name' => $nickname, 'user_age' => $age, 'user_information' => $information, 'user_location' => $location, 'user_id' => $user->id, 'head_pic' => $headPic, 'home_pic' => $homePic]);
         DB::commit();
         $userInfo['token'] = $user->token;
         $userInfo['tags'] = $user->userTags;
         return $userInfo;
     } catch (Exception $e) {
         DB::rollBack();
         return Utility::response_format(Utility::RESPONSE_CODE_DB_ERROR, '', $e->getMessage());
     }
 }