init
This commit is contained in:
48
app/Console/Commands/AppVersionUpdateCommand.php
Normal file
48
app/Console/Commands/AppVersionUpdateCommand.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class AppVersionUpdateCommand extends Command
|
||||
{
|
||||
protected $signature = 'app:version {version?} {--auto}';
|
||||
|
||||
protected $description = '更新 .env 文件中的 APP_VERSION 版本号';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$envPath = base_path('.env');
|
||||
if (!file_exists($envPath)) {
|
||||
$this->error('.env 文件不存在');
|
||||
return;
|
||||
}
|
||||
$current = env('APP_VERSION');
|
||||
$version = $this->argument('version');
|
||||
if ($version) {
|
||||
if (!preg_match('/^\d+\.\d+\.\d+$/', $version)) {
|
||||
$this->error('版本号格式错误');
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
$version = preg_replace_callback('/(\d+)$/', fn ($matches) => $matches[1] + 1, $current);
|
||||
}
|
||||
|
||||
$isAuto = $this->option('auto');
|
||||
if ($isAuto) {
|
||||
file_put_contents($envPath, preg_replace('/APP_VERSION=.*/', 'APP_VERSION=' . $version, file_get_contents($envPath)));
|
||||
$this->info('更新成功 ' . $current . ' -> ' . $version);
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$confirmed = $this->confirm('当前版本号:' . $current . ' 新版号 ' . $version . ' 确认更新么', true);
|
||||
if (!$confirmed) {
|
||||
$this->line('已取消');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
file_put_contents($envPath, preg_replace('/APP_VERSION=.*/', 'APP_VERSION=' . $version, file_get_contents($envPath)));
|
||||
$this->info('更新成功 ' . $current . ' -> ' . $version);
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
}
|
||||
25
app/Foundation/PersonalAccessToken.php
Normal file
25
app/Foundation/PersonalAccessToken.php
Normal file
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Foundation;
|
||||
|
||||
use Laravel\Sanctum\PersonalAccessToken as SanctumPersonalAccessToken;
|
||||
|
||||
class PersonalAccessToken extends SanctumPersonalAccessToken
|
||||
{
|
||||
protected $table = 'app_personal_access_tokens';
|
||||
|
||||
protected $fillable = ['name', 'token', 'abilities', 'expires_at', 'meta'];
|
||||
|
||||
protected $casts = [
|
||||
'abilities' => 'json',
|
||||
'meta' => 'json',
|
||||
'last_used_at' => 'datetime',
|
||||
'expires_at' => 'datetime',
|
||||
];
|
||||
|
||||
public function addMeta($data)
|
||||
{
|
||||
$data = array_merge($this->meta, $data);
|
||||
$this->update(['meta' => $data]);
|
||||
}
|
||||
}
|
||||
5
app/Http/Controllers/Controller.php
Normal file
5
app/Http/Controllers/Controller.php
Normal file
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
abstract class Controller {}
|
||||
21
app/Http/Middleware/CreateClientTokenMiddlewire.php
Normal file
21
app/Http/Middleware/CreateClientTokenMiddlewire.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
class CreateClientTokenMiddlewire
|
||||
{
|
||||
public function handle(Request $request, \Closure $next): Response
|
||||
{
|
||||
$token = $this->createGuestToken($request);
|
||||
$request->merge(['clientToken' => $token]);
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
public function createGuestToken(Request $request)
|
||||
{
|
||||
return sha1($request->ip() . $request->header('User-Agent'));
|
||||
}
|
||||
}
|
||||
29
app/Models/Media.php
Normal file
29
app/Models/Media.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Spatie\MediaLibrary\MediaCollections\Models\Media as SpatieMedia;
|
||||
|
||||
class Media extends SpatieMedia
|
||||
{
|
||||
protected $guarded = ['id'];
|
||||
|
||||
protected $table = 'app_media';
|
||||
|
||||
public function replaceUseKey($key): self
|
||||
{
|
||||
self::delete();
|
||||
return $this->model
|
||||
->addMediaFromDisk($key, 'public')
|
||||
->usingFileName($key)
|
||||
->toMediaCollection($this->collection_name);
|
||||
}
|
||||
|
||||
protected function casts()
|
||||
{
|
||||
return [
|
||||
'created_at' => 'datetime:Y-m-d H:i:s',
|
||||
'updated_at' => 'datetime:Y-m-d H:i:s',
|
||||
];
|
||||
}
|
||||
}
|
||||
48
app/Models/User.php
Normal file
48
app/Models/User.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
// use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory;
|
||||
use Notifiable;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
];
|
||||
|
||||
/**
|
||||
* The attributes that should be hidden for serialization.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the attributes that should be cast.
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
];
|
||||
}
|
||||
}
|
||||
30
app/Providers/AppServiceProvider.php
Normal file
30
app/Providers/AppServiceProvider.php
Normal file
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Providers;
|
||||
|
||||
use App\Foundation\PersonalAccessToken;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
|
||||
class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*/
|
||||
public function register(): void {}
|
||||
|
||||
/**
|
||||
* Bootstrap any application services.
|
||||
*/
|
||||
public function boot(): void
|
||||
{
|
||||
Sanctum::usePersonalAccessTokenModel(PersonalAccessToken::class);
|
||||
|
||||
RateLimiter::for('api', function ($request) {
|
||||
$key = sha1($request->ip() . $request->header('User-Agent'));
|
||||
return Limit::perMinute(10)->by($key);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user