From 4c847f26682af21c09a60622346832031559ff89 Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sat, 16 Jun 2018 09:15:01 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=EF=BC=8C=E6=B4=BB=E8=B7=83?= =?UTF-8?q?=E7=94=A8=E6=88=B7=EF=BC=8C=E6=9C=AC=E5=9C=B0=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Api/AuthorizationsController.php | 2 +- app/Http/Controllers/Api/Controller.php | 7 ++++++ app/Http/Controllers/Api/LinksController.php | 15 +++++++++++ app/Http/Controllers/Api/UsersController.php | 6 +++++ app/Http/Kernel.php | 3 +++ app/Http/Middleware/ChangeLocale.php | 25 +++++++++++++++++++ app/Http/Requests/Api/TopicRequest.php | 2 +- app/Transformers/LinkTransformer.php | 25 +++++++++++++++++++ routes/api.php | 8 +++++- 9 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 app/Http/Controllers/Api/LinksController.php create mode 100644 app/Http/Middleware/ChangeLocale.php create mode 100644 app/Transformers/LinkTransformer.php diff --git a/app/Http/Controllers/Api/AuthorizationsController.php b/app/Http/Controllers/Api/AuthorizationsController.php index 537b356..16e8603 100644 --- a/app/Http/Controllers/Api/AuthorizationsController.php +++ b/app/Http/Controllers/Api/AuthorizationsController.php @@ -66,7 +66,7 @@ class AuthorizationsController extends Controller filter_var($username, FILTER_VALIDATE_EMAIL) ? $credentials['email'] = $username : $credentials['phone'] = $username; $credentials['password'] = $request->get('password'); if (!$token = \Auth::guard('api')->attempt($credentials)) { - return $this->response->errorUnauthorized('用户名或密码错误'); + return $this->response->errorUnauthorized(trans('auth.failed')); } return $this->respondWithToken($token)->setStatusCode(201); } diff --git a/app/Http/Controllers/Api/Controller.php b/app/Http/Controllers/Api/Controller.php index 46c3d6b..10e9540 100644 --- a/app/Http/Controllers/Api/Controller.php +++ b/app/Http/Controllers/Api/Controller.php @@ -4,8 +4,15 @@ namespace App\Http\Controllers\Api; use App\Http\Controllers\Controller as BaseController; use Dingo\Api\Routing\Helpers; +use Symfony\Component\HttpKernel\Exception\HttpException; class Controller extends BaseController { use Helpers; + + + public function errorResponse($statusCode, $message = '', $code = 0) + { + throw new HttpException($statusCode, $message, null, [], $code); + } } diff --git a/app/Http/Controllers/Api/LinksController.php b/app/Http/Controllers/Api/LinksController.php new file mode 100644 index 0000000..7cd9c83 --- /dev/null +++ b/app/Http/Controllers/Api/LinksController.php @@ -0,0 +1,15 @@ +getAllCached(); + return $this->response->collection($links, new LinkTransformer()); + } +} diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 17afac7..ff1388c 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -44,6 +44,12 @@ class UsersController extends Controller return $this->response->item($user, new UserTransformer()); } + public function activedIndex(User $user) + { + $activedUsers = $user->getActiveUsers(); + return $this->response->collection($activedUsers, new UserTransformer()); + } + public function me() { return $this->response->item($this->user(), new UserTransformer()); diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 6d4e6ad..2dc8e17 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -2,6 +2,7 @@ namespace App\Http; +use App\Http\Middleware\ChangeLocale; use App\Http\Middleware\RecordLastActivedTime; use Illuminate\Foundation\Http\Kernel as HttpKernel; @@ -79,5 +80,7 @@ class Kernel extends HttpKernel 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, // 访问节流,类似于 『1 分钟只能请求 10 次』的需求,一般在 API 中使用 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, + //语言设置 + 'change-locale' => ChangeLocale::class, ]; } diff --git a/app/Http/Middleware/ChangeLocale.php b/app/Http/Middleware/ChangeLocale.php new file mode 100644 index 0000000..b741212 --- /dev/null +++ b/app/Http/Middleware/ChangeLocale.php @@ -0,0 +1,25 @@ +header('accept-language'); + if ($language) { + preg_match('/^([A-Za-z-]+),?/', $language, $matches); + \App::setLocale($matches ? $matches[1] : 'zh-CN'); + } + return $next($request); + } +} diff --git a/app/Http/Requests/Api/TopicRequest.php b/app/Http/Requests/Api/TopicRequest.php index e01b45d..8bdf469 100644 --- a/app/Http/Requests/Api/TopicRequest.php +++ b/app/Http/Requests/Api/TopicRequest.php @@ -16,7 +16,7 @@ class TopicRequest extends FormRequest public function rules() { switch ($this->method()) { - case 'POSt': + case 'POST': return [ 'title' => 'required|string', 'body' => 'required|string', diff --git a/app/Transformers/LinkTransformer.php b/app/Transformers/LinkTransformer.php new file mode 100644 index 0000000..181b1db --- /dev/null +++ b/app/Transformers/LinkTransformer.php @@ -0,0 +1,25 @@ + $link->id, + 'title' => $link->title, + 'link' => $link->link + ]; + } +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 53ceb6d..5439151 100644 --- a/routes/api.php +++ b/routes/api.php @@ -15,7 +15,7 @@ $api = app('Dingo\Api\Routing\Router'); $api->version('v1', [ 'namespace' => 'App\Http\Controllers\Api', - 'middleware' => ['serializer:array', 'bindings'],//注意里的bindings中间件,使用这个才能使用隐式路由绑定 + 'middleware' => ['serializer:array', 'bindings', 'change-locale'],//注意里的bindings中间件,使用这个才能使用隐式路由绑定 ], function ($api) { $api->group([ 'middleware' => 'api.throttle', @@ -57,6 +57,12 @@ $api->version('v1', [ $api->get('users/{user}/topics', 'TopicsController@userIndex')->name('api.users.topics.index'); //话题详情 $api->get('topics/{topic}', 'TopicsController@show')->name('api.topics.show'); + // 资源推荐 + $api->get('links', 'LinksController@index') + ->name('api.links.index'); + // 活跃用户 + $api->get('actived/users', 'UsersController@activedIndex') + ->name('api.actived.users.index'); }); // 需要 token 验证的接口