完成模板

This commit is contained in:
Your Name 2022-09-25 21:39:39 +08:00
parent 184b44b1ae
commit 61b4aa1e57
11 changed files with 144 additions and 36 deletions

View File

@ -0,0 +1,25 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\UserRequest;
use App\Http\Resources\Api\UserResource;
use App\Jobs\Api\SaveLastTokenJob;
use App\Models\Book;
use App\Models\Url;
use App\Models\Log;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Exceptions\TokenExpiredException;
class HomeController extends Controller
{
public function index()
{
$urls = Url::all();
$log = Log::all();
return $this->success(["urls"=>count($urls),"log"=>count($log)]);
}
}

View File

@ -1,4 +1,5 @@
<?php <?php
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\UserRequest; use App\Http\Requests\Api\UserRequest;
@ -6,22 +7,48 @@ use App\Http\Resources\Api\UserResource;
use App\Jobs\Api\SaveLastTokenJob; use App\Jobs\Api\SaveLastTokenJob;
use App\Models\Book; use App\Models\Book;
use App\Models\Url; use App\Models\Url;
use App\Models\Log;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Exceptions\TokenExpiredException; use Tymon\JWTAuth\Exceptions\TokenExpiredException;
use Illuminate\Support\Facades\DB;
class UrlController extends Controller class UrlController extends Controller
{ {
public function index(){ public function index()
{
$urls = Url::all(); $urls = Url::all();
return $this->success($urls); return $this->success($urls);
} }
public function add(Request $req){ public function add(Request $req)
$urls = new Url; {
//dd($req["name"]); $hosts = $req->hosts;
$urls->host="www"; foreach ($hosts as $v) {
$urls->url="www.baidu.com"; $urls = new Url;
$urls->save(); $urls->host = $v;
return $this->success($urls); $urls->save();
}
return $this->success("");
}
public function del(Request $req)
{
$id = $req->id;
Url::where('id', $id)->delete();
return $this->success("");
}
public function set(Request $req)
{
$host = $req->host;
$url = $req->url;
$log = new Log;
$log->host = $host;
$log->url = $url;
$log->save();
Url::where('host', $host)->update(['url' => $url]);
return $this->success("");
} public function text()
{
//$users = DB::select('select * from `users` where `name` = giaogiao limit 1');
return $this->success("");
} }
} }

View File

@ -36,6 +36,7 @@ class UserController extends Controller
} }
//用户登录 //用户登录
public function login(Request $request){ public function login(Request $request){
//dd($request->name);
$token=Auth::claims(['guard'=>'api'])->attempt(['name'=>$request->name,'password'=>$request->password]); $token=Auth::claims(['guard'=>'api'])->attempt(['name'=>$request->name,'password'=>$request->password]);
if($token) { if($token) {
//如果登陆先检查原先是否有存token有的话先失效然后再存入最新的token //如果登陆先检查原先是否有存token有的话先失效然后再存入最新的token
@ -47,15 +48,23 @@ class UserController extends Controller
//因为让一个过期的token再失效会抛出异常所以我们捕捉异常不需要做任何处理 //因为让一个过期的token再失效会抛出异常所以我们捕捉异常不需要做任何处理
} }
} }
SaveLastTokenJob::dispatch($user,$token); //SaveLastTokenJob::dispatch($user,$token);
return $this->setStatusCode(201)->success(['token' => 'bearer ' . $token]); return $this->setStatusCode(201)->success(['token' => 'bearer ' . $token]);
} }
return $this->failed('账号或密码错误',400); //return $this->failed('账号或密码错误',400);
return $this->setStatusCode(200)->success(["code"=>"400"]);
} }
//用户退出 //用户退出
public function logout(){ public function logout(){
Auth::logout(); Auth::logout();
return $this->success('退出成功...'); return $this->success('退出成功...');
} }
public function set(Request $req){
$password = $req->password;
$user = Auth::user();
$user->password=$password;
$user->save();
return $this->success("成功");
}
} }

View File

@ -63,7 +63,7 @@ class RefreshTokenMiddleware extends BaseMiddleware
//刷新了token将token存入数据库 //刷新了token将token存入数据库
$user = Auth::user(); $user = Auth::user();
SaveLastTokenJob::dispatch($user,$token); //SaveLastTokenJob::dispatch($user,$token);
} catch (JWTException $exception) { } catch (JWTException $exception) {
// 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。 // 如果捕获到此异常,即代表 refresh 也过期了,用户无法刷新令牌,需要重新登录。

14
app/Models/Log.php Normal file
View File

@ -0,0 +1,14 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Log extends Model
{
//
protected $fillable = [
'host', 'url'
];
//protected $datas = ['deleted_at'];
}

View File

@ -13,6 +13,7 @@
"laravel/framework": "5.7.*", "laravel/framework": "5.7.*",
"laravel/horizon": "^3.0", "laravel/horizon": "^3.0",
"laravel/tinker": "^1.0", "laravel/tinker": "^1.0",
"lcobucci/jwt": "3.3.3",
"medz/cors": "^1.4", "medz/cors": "^1.4",
"tymon/jwt-auth": "1.0.0-rc.3" "tymon/jwt-auth": "1.0.0-rc.3"
}, },

24
composer.lock generated
View File

@ -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": "6b09d5d362af7b644cb7384daedb42c8", "content-hash": "c8eb7a51f392be8df739c6dba97059c1",
"packages": [ "packages": [
{ {
"name": "cakephp/chronos", "name": "cakephp/chronos",
@ -1401,16 +1401,16 @@
}, },
{ {
"name": "lcobucci/jwt", "name": "lcobucci/jwt",
"version": "3.4.6", "version": "3.3.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/lcobucci/jwt.git", "url": "https://github.com/lcobucci/jwt.git",
"reference": "3ef8657a78278dfeae7707d51747251db4176240" "reference": "c1123697f6a2ec29162b82f170dd4a491f524773"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/lcobucci/jwt/zipball/3ef8657a78278dfeae7707d51747251db4176240", "url": "https://api.github.com/repos/lcobucci/jwt/zipball/c1123697f6a2ec29162b82f170dd4a491f524773",
"reference": "3ef8657a78278dfeae7707d51747251db4176240", "reference": "c1123697f6a2ec29162b82f170dd4a491f524773",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -1425,9 +1425,6 @@
"phpunit/phpunit": "^5.7 || ^7.3", "phpunit/phpunit": "^5.7 || ^7.3",
"squizlabs/php_codesniffer": "~2.3" "squizlabs/php_codesniffer": "~2.3"
}, },
"suggest": {
"lcobucci/clock": "*"
},
"type": "library", "type": "library",
"extra": { "extra": {
"branch-alias": { "branch-alias": {
@ -1435,11 +1432,6 @@
} }
}, },
"autoload": { "autoload": {
"files": [
"compat/class-aliases.php",
"compat/json-exception-polyfill.php",
"compat/lcobucci-clock-polyfill.php"
],
"psr-4": { "psr-4": {
"Lcobucci\\JWT\\": "src" "Lcobucci\\JWT\\": "src"
} }
@ -1462,7 +1454,7 @@
], ],
"support": { "support": {
"issues": "https://github.com/lcobucci/jwt/issues", "issues": "https://github.com/lcobucci/jwt/issues",
"source": "https://github.com/lcobucci/jwt/tree/3.4.6" "source": "https://github.com/lcobucci/jwt/tree/3.3.3"
}, },
"funding": [ "funding": [
{ {
@ -1474,7 +1466,7 @@
"type": "patreon" "type": "patreon"
} }
], ],
"time": "2021-09-28T19:18:28+00:00" "time": "2020-08-20T13:22:28+00:00"
}, },
{ {
"name": "league/flysystem", "name": "league/flysystem",
@ -7431,7 +7423,7 @@
"platform-dev": [], "platform-dev": [],
"platform-overrides": { "platform-overrides": {
"ext-pcntl": "7.3", "ext-pcntl": "7.3",
"ext-posix": "7.2" "ext-posix": "7.3"
}, },
"plugin-api-version": "2.2.0" "plugin-api-version": "2.2.0"
} }

View File

@ -1,14 +1,14 @@
<?php <?php
return [ return [
'allow-credentials' => env('CORS_ALLOW_CREDENTIAILS', true), // set "Access-Control-Allow-Credentials" 👉 string "false" or "true". 'allow-credentials' => env('CORS_ALLOW_CREDENTIAILS', false), // set "Access-Control-Allow-Credentials" 👉 string "false" or "true".
'allow-headers' => ['*'], // ex: Content-Type, Accept, X-Requested-With 'allow-headers' => ['*'], // ex: Content-Type, Accept, X-Requested-With
'expose-headers' => ['Authorization'], 'expose-headers' => [],
'origins' => ['*'], // ex: http://localhost 'origins' => ['*'], // ex: http://localhost
'methods' => ['*'], // ex: GET, POST, PUT, PATCH, DELETE 'methods' => ['*'], // ex: GET, POST, PUT, PATCH, DELETE
'max-age' => env('CORS_ACCESS_CONTROL_MAX_AGE', 0), 'max-age' => env('CORS_ACCESS_CONTROL_MAX_AGE', 0),
'laravel' => [ 'laravel' => [
'allow-route-perfix' => env('CORS_LARAVEL_ALLOW_ROUTE_PERFIX', '*'), // The perfix is using \Illumante\Http\Request::is method. 👉 'allow-route-prefix' => env('CORS_LARAVEL_ALLOW_ROUTE_PREFIX', '*'), // The prefix is using \Illumante\Http\Request::is method. 👉
'route-group-mode' => env('CORS_LARAVEL_ROUTE_GROUP_MODE', false), 'route-group-mode' => env('CORS_LARAVEL_ROUTE_GROUP_MODE', false),
], ],
]; ];

View File

@ -101,7 +101,7 @@ return [
| |
*/ */
'ttl' => env('JWT_TTL', 600000), 'ttl' => env('JWT_TTL', 6000000),
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------

View File

@ -0,0 +1,36 @@
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateJobsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('jobs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('queue')->index();
$table->longText('payload');
$table->unsignedTinyInteger('attempts');
$table->unsignedInteger('reserved_at')->nullable();
$table->unsignedInteger('available_at');
$table->unsignedInteger('created_at');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('jobs');
}
}

View File

@ -14,10 +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::post('/url/add', 'UrlController@add');
Route::post('/url/all', 'UrlController@index');
Route::post('/url/set', 'UrlController@set');
Route::post('/url/del', 'UrlController@del');
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');
@ -32,6 +28,14 @@ Route::namespace('Api')->prefix('v1')->middleware('cors')->group(function () {
Route::get('/users/{user}', 'UserController@show')->name('users.show'); Route::get('/users/{user}', 'UserController@show')->name('users.show');
//用户退出 //用户退出
Route::get('/logout', 'UserController@logout')->name('users.logout'); Route::get('/logout', 'UserController@logout')->name('users.logout');
Route::post('/url/add', 'UrlController@add');
Route::post('/url/text', 'UrlController@text');
Route::post('/url/all', 'UrlController@index');
Route::post('/url/set', 'UrlController@set');
Route::post('/url/del', 'UrlController@del');
Route::post('/home/all', 'HomeController@index');
Route::post('/user/set', 'UserController@set');
}); });
}); });
Route::middleware('admin.guard')->group(function () { Route::middleware('admin.guard')->group(function () {