资源,活跃用户,本地化
This commit is contained in:
parent
cf998a4ecc
commit
4c847f2668
|
@ -66,7 +66,7 @@ class AuthorizationsController extends Controller
|
||||||
filter_var($username, FILTER_VALIDATE_EMAIL) ? $credentials['email'] = $username : $credentials['phone'] = $username;
|
filter_var($username, FILTER_VALIDATE_EMAIL) ? $credentials['email'] = $username : $credentials['phone'] = $username;
|
||||||
$credentials['password'] = $request->get('password');
|
$credentials['password'] = $request->get('password');
|
||||||
if (!$token = \Auth::guard('api')->attempt($credentials)) {
|
if (!$token = \Auth::guard('api')->attempt($credentials)) {
|
||||||
return $this->response->errorUnauthorized('用户名或密码错误');
|
return $this->response->errorUnauthorized(trans('auth.failed'));
|
||||||
}
|
}
|
||||||
return $this->respondWithToken($token)->setStatusCode(201);
|
return $this->respondWithToken($token)->setStatusCode(201);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,15 @@ namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Http\Controllers\Controller as BaseController;
|
use App\Http\Controllers\Controller as BaseController;
|
||||||
use Dingo\Api\Routing\Helpers;
|
use Dingo\Api\Routing\Helpers;
|
||||||
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
|
||||||
class Controller extends BaseController
|
class Controller extends BaseController
|
||||||
{
|
{
|
||||||
use Helpers;
|
use Helpers;
|
||||||
|
|
||||||
|
|
||||||
|
public function errorResponse($statusCode, $message = '', $code = 0)
|
||||||
|
{
|
||||||
|
throw new HttpException($statusCode, $message, null, [], $code);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
15
app/Http/Controllers/Api/LinksController.php
Normal file
15
app/Http/Controllers/Api/LinksController.php
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Models\Link;
|
||||||
|
use App\Transformers\LinkTransformer;
|
||||||
|
|
||||||
|
class LinksController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Link $link)
|
||||||
|
{
|
||||||
|
$links = $link->getAllCached();
|
||||||
|
return $this->response->collection($links, new LinkTransformer());
|
||||||
|
}
|
||||||
|
}
|
|
@ -44,6 +44,12 @@ class UsersController extends Controller
|
||||||
return $this->response->item($user, new UserTransformer());
|
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()
|
public function me()
|
||||||
{
|
{
|
||||||
return $this->response->item($this->user(), new UserTransformer());
|
return $this->response->item($this->user(), new UserTransformer());
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
namespace App\Http;
|
namespace App\Http;
|
||||||
|
|
||||||
|
use App\Http\Middleware\ChangeLocale;
|
||||||
use App\Http\Middleware\RecordLastActivedTime;
|
use App\Http\Middleware\RecordLastActivedTime;
|
||||||
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
use Illuminate\Foundation\Http\Kernel as HttpKernel;
|
||||||
|
|
||||||
|
@ -79,5 +80,7 @@ class Kernel extends HttpKernel
|
||||||
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
|
||||||
// 访问节流,类似于 『1 分钟只能请求 10 次』的需求,一般在 API 中使用
|
// 访问节流,类似于 『1 分钟只能请求 10 次』的需求,一般在 API 中使用
|
||||||
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
|
||||||
|
//语言设置
|
||||||
|
'change-locale' => ChangeLocale::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
25
app/Http/Middleware/ChangeLocale.php
Normal file
25
app/Http/Middleware/ChangeLocale.php
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use Closure;
|
||||||
|
|
||||||
|
class ChangeLocale
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Handle an incoming request.
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Http\Request $request
|
||||||
|
* @param \Closure $next
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle($request, Closure $next)
|
||||||
|
{
|
||||||
|
$language = $request->header('accept-language');
|
||||||
|
if ($language) {
|
||||||
|
preg_match('/^([A-Za-z-]+),?/', $language, $matches);
|
||||||
|
\App::setLocale($matches ? $matches[1] : 'zh-CN');
|
||||||
|
}
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,7 +16,7 @@ class TopicRequest extends FormRequest
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
switch ($this->method()) {
|
switch ($this->method()) {
|
||||||
case 'POSt':
|
case 'POST':
|
||||||
return [
|
return [
|
||||||
'title' => 'required|string',
|
'title' => 'required|string',
|
||||||
'body' => 'required|string',
|
'body' => 'required|string',
|
||||||
|
|
25
app/Transformers/LinkTransformer.php
Normal file
25
app/Transformers/LinkTransformer.php
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: xing
|
||||||
|
* Date: 2018/6/16
|
||||||
|
* Time: 8:25
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Transformers;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Models\Link;
|
||||||
|
use League\Fractal\TransformerAbstract;
|
||||||
|
|
||||||
|
class LinkTransformer extends TransformerAbstract
|
||||||
|
{
|
||||||
|
public function transform(Link $link)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $link->id,
|
||||||
|
'title' => $link->title,
|
||||||
|
'link' => $link->link
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,7 +15,7 @@ $api = app('Dingo\Api\Routing\Router');
|
||||||
|
|
||||||
$api->version('v1', [
|
$api->version('v1', [
|
||||||
'namespace' => 'App\Http\Controllers\Api',
|
'namespace' => 'App\Http\Controllers\Api',
|
||||||
'middleware' => ['serializer:array', 'bindings'],//注意里的bindings中间件,使用这个才能使用隐式路由绑定
|
'middleware' => ['serializer:array', 'bindings', 'change-locale'],//注意里的bindings中间件,使用这个才能使用隐式路由绑定
|
||||||
], function ($api) {
|
], function ($api) {
|
||||||
$api->group([
|
$api->group([
|
||||||
'middleware' => 'api.throttle',
|
'middleware' => 'api.throttle',
|
||||||
|
@ -57,6 +57,12 @@ $api->version('v1', [
|
||||||
$api->get('users/{user}/topics', 'TopicsController@userIndex')->name('api.users.topics.index');
|
$api->get('users/{user}/topics', 'TopicsController@userIndex')->name('api.users.topics.index');
|
||||||
//话题详情
|
//话题详情
|
||||||
$api->get('topics/{topic}', 'TopicsController@show')->name('api.topics.show');
|
$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 验证的接口
|
// 需要 token 验证的接口
|
||||||
|
|
Loading…
Reference in New Issue
Block a user