删除一些无用if判断

This commit is contained in:
guaosi 2019-03-23 09:39:05 +08:00
parent c56b37667f
commit c9ce40d34d
5 changed files with 10 additions and 22 deletions

View File

@ -6,6 +6,7 @@ use Exception;
use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\AuthenticationException;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\QueryException;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException; use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException; use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
@ -56,6 +57,7 @@ class ExceptionReport
TokenInvalidException::class=>['token不正确',400], TokenInvalidException::class=>['token不正确',400],
NotFoundHttpException::class=>['没有找到该页面',404], NotFoundHttpException::class=>['没有找到该页面',404],
MethodNotAllowedHttpException::class=>['访问方式不正确',405], MethodNotAllowedHttpException::class=>['访问方式不正确',405],
QueryException::class=>['参数错误',401],
]; ];
public function register($className,callable $callback){ public function register($className,callable $callback){

View File

@ -37,13 +37,9 @@ class AdminController extends Controller
//用户注册 //用户注册
public function store(UserRequest $request) public function store(UserRequest $request)
{ {
$admin = Admin::create($request->all()); Admin::create($request->all());
if ($admin) {
return $this->setStatusCode(201)->success('用户注册成功'); return $this->setStatusCode(201)->success('用户注册成功');
} }
return $this->failed('用户注册失败');
}
//用户登录 //用户登录
public function login(Request $request) public function login(Request $request)

View File

@ -5,6 +5,7 @@ namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\UserRequest; use App\Http\Requests\Api\UserRequest;
use App\Http\Resources\Api\UserResource; use App\Http\Resources\Api\UserResource;
use App\Jobs\Api\SaveLastTokenJob; use App\Jobs\Api\SaveLastTokenJob;
use App\Models\Book;
use App\Models\User; use App\Models\User;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
@ -30,13 +31,9 @@ class UserController extends Controller
} }
//用户注册 //用户注册
public function store(UserRequest $request){ public function store(UserRequest $request){
$user = User::create($request->all()); User::create($request->all());
if($user){
return $this->setStatusCode(201)->success('用户注册成功'); return $this->setStatusCode(201)->success('用户注册成功');
} }
return $this->failed('用户注册失败');
}
//用户登录 //用户登录
public function login(Request $request){ public function login(Request $request){
$token=Auth::claims(['guard'=>'api'])->attempt(['name'=>$request->name,'password'=>$request->password]); $token=Auth::claims(['guard'=>'api'])->attempt(['name'=>$request->name,'password'=>$request->password]);
@ -61,13 +58,4 @@ class UserController extends Controller
Auth::logout(); Auth::logout();
return $this->success('退出成功...'); return $this->success('退出成功...');
} }
public function test(Request $request){
$token = Auth::guard('api')->getToken();
$token = Auth::guard('admin')->setToken($token)->refresh();
dd(Auth::guard('admin')->setToken($token)->check());
return $token;
$token = Auth::guard('api')->getToken();
Auth::guard('api')->setToken($token)->invalidate();
}
} }

View File

@ -51,4 +51,7 @@ class User extends Authenticatable implements JWTSubject
{ {
$this->attributes['password'] = bcrypt($value); $this->attributes['password'] = bcrypt($value);
} }
public function books(){
return $this->hasMany(Book::class,'user_id','id')->limit(2);
}
} }

View File

@ -14,7 +14,6 @@ use Illuminate\Http\Request;
*/ */
Route::namespace('Api')->prefix('v1')->middleware('cors')->group(function () { Route::namespace('Api')->prefix('v1')->middleware('cors')->group(function () {
Route::get('/test', 'UserController@test')->name('users.test');
Route::middleware('api.guard')->group(function () { Route::middleware('api.guard')->group(function () {
//用户注册 //用户注册
Route::post('/users', 'UserController@store')->name('users.store'); Route::post('/users', 'UserController@store')->name('users.store');