api统一数据结构
This commit is contained in:
parent
d651b7250b
commit
4c45d9b92a
|
@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
use App\Http\Requests\UserRequest;
|
use App\Http\Requests\UserRequest;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
|
use App\TransFormers\UserTransformer;
|
||||||
use Illuminate\Support\Facades\Cache;
|
use Illuminate\Support\Facades\Cache;
|
||||||
|
|
||||||
class UsersController extends Controller
|
class UsersController extends Controller
|
||||||
|
@ -21,6 +22,17 @@ class UsersController extends Controller
|
||||||
// 清除验证码缓存
|
// 清除验证码缓存
|
||||||
\Cache::forget($request->verification_key);
|
\Cache::forget($request->verification_key);
|
||||||
|
|
||||||
return $this->response->created();
|
return $this->response->item($user, new UserTransformer())
|
||||||
|
->setMeta([
|
||||||
|
'access_token' => \Auth::guard('api')->fromUser($user),
|
||||||
|
'token_type' => 'Bearer',
|
||||||
|
'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
|
||||||
|
])
|
||||||
|
->setStatusCode(201);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function me()
|
||||||
|
{
|
||||||
|
return $this->response->item($this->user(), new UserTransformer());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
27
app/Transformers/UserTransformer.php
Normal file
27
app/Transformers/UserTransformer.php
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\TransFormers;
|
||||||
|
|
||||||
|
|
||||||
|
use App\Models\User;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use League\Fractal\TransformerAbstract;
|
||||||
|
|
||||||
|
class UserTransformer extends TransformerAbstract
|
||||||
|
{
|
||||||
|
public function transform(User $user)
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $user->id,
|
||||||
|
'name' => $user->name,
|
||||||
|
'email' => $user->email,
|
||||||
|
'avatar' => $user->avatar,
|
||||||
|
'introduction' => $user->introduction,
|
||||||
|
'bound_phone' => $user->phone ? true : false,
|
||||||
|
'bound_wechat' => ($user->weixin_unionid || $user->weixin_openid) ? true : false,
|
||||||
|
'last_actived_at' => $user->last_actived_at instanceof Carbon ? $user->last_actived_at->toDateTimeString() : $user->last_actived_at,
|
||||||
|
'created_at' => $user->created_at instanceof Carbon ? $user->created_at->toDateTimeString() : $user->created_at,
|
||||||
|
'updated_at' => $user->updated_at instanceof Carbon ? $user->updated_at->toDateTimeString() : $user->updated_at,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@
|
||||||
"laravel/framework": "5.5.*",
|
"laravel/framework": "5.5.*",
|
||||||
"laravel/horizon": "~1.0",
|
"laravel/horizon": "~1.0",
|
||||||
"laravel/tinker": "~1.0",
|
"laravel/tinker": "~1.0",
|
||||||
|
"liyu/dingo-serializer-switch": "^0.3.0",
|
||||||
"mews/captcha": "~2.0",
|
"mews/captcha": "~2.0",
|
||||||
"mews/purifier": "~2.0",
|
"mews/purifier": "~2.0",
|
||||||
"overtrue/easy-sms": "^1.0",
|
"overtrue/easy-sms": "^1.0",
|
||||||
|
|
45
composer.lock
generated
45
composer.lock
generated
|
@ -4,7 +4,7 @@
|
||||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
"This file is @generated automatically"
|
"This file is @generated automatically"
|
||||||
],
|
],
|
||||||
"content-hash": "e9a02a045616c7c6a7078c57f181b2f8",
|
"content-hash": "3e238e73ff229ba2ec8024b3e85640a5",
|
||||||
"packages": [
|
"packages": [
|
||||||
{
|
{
|
||||||
"name": "cakephp/chronos",
|
"name": "cakephp/chronos",
|
||||||
|
@ -2052,6 +2052,49 @@
|
||||||
],
|
],
|
||||||
"time": "2016-08-17T00:36:58+00:00"
|
"time": "2016-08-17T00:36:58+00:00"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "liyu/dingo-serializer-switch",
|
||||||
|
"version": "v0.3.0",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/liyu001989/dingo-serializer-switch.git",
|
||||||
|
"reference": "82e23a7c9f46f7ec05ae9b5999c618f5c21a3290"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/liyu001989/dingo-serializer-switch/zipball/82e23a7c9f46f7ec05ae9b5999c618f5c21a3290",
|
||||||
|
"reference": "82e23a7c9f46f7ec05ae9b5999c618f5c21a3290",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"php": ">=7.0"
|
||||||
|
},
|
||||||
|
"type": "library",
|
||||||
|
"extra": {
|
||||||
|
"laravel": {
|
||||||
|
"providers": [
|
||||||
|
"Liyu\\Dingo\\ServiceProvider"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Liyu\\Dingo\\": "src"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Yu Li",
|
||||||
|
"email": "liyu001989@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"description": "A middleware to switch dingo serializer",
|
||||||
|
"time": "2018-01-06T05:09:05+00:00"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "mews/captcha",
|
"name": "mews/captcha",
|
||||||
"version": "2.2.0",
|
"version": "2.2.0",
|
||||||
|
|
|
@ -10,9 +10,13 @@
|
||||||
| is assigned the "api" middleware group. Enjoy building your API!
|
| is assigned the "api" middleware group. Enjoy building your API!
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
$api = app('Dingo\Api\Routing\Router');
|
$api = app('Dingo\Api\Routing\Router');
|
||||||
|
|
||||||
$api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api) {
|
$api->version('v1', [
|
||||||
|
'namespace' => 'App\Http\Controllers\Api',
|
||||||
|
'middleware' => 'serializer:array',
|
||||||
|
], function ($api) {
|
||||||
$api->group([
|
$api->group([
|
||||||
'middleware' => 'api.throttle',
|
'middleware' => 'api.throttle',
|
||||||
'limit' => config('api.rate_limits.sign.limit'),
|
'limit' => config('api.rate_limits.sign.limit'),
|
||||||
|
@ -45,7 +49,12 @@ $api->version('v1', ['namespace' => 'App\Http\Controllers\Api'], function ($api)
|
||||||
->name('api.authorizations.destroy');
|
->name('api.authorizations.destroy');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// 需要 token 验证的接口
|
||||||
|
$api->group(['middleware' => 'api.auth'], function ($api) {
|
||||||
|
// 当前登录用户信息
|
||||||
|
$api->get('user', 'UsersController@me')
|
||||||
|
->name('api.user.show');
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user