From f5d5929028bd64f804e82a2ee11868229583f27c Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Sat, 13 Aug 2022 17:47:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=85=85=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Admin/User/DropController.php | 2 +- .../Controllers/Remote/TaskController.php | 122 +++++ app/Models/User/Drop.php | 35 +- app/Notifications/CommonNotification.php | 49 ++ composer.json | 1 + composer.lock | 422 +++++++++++++++++- .../2014_10_12_000000_create_users_table.php | 2 +- .../2022_08_12_081236_create_drops_table.php | 4 +- ...08_12_082631_create_transcations_table.php | 2 +- .../2022_08_13_051148_create_hosts_table.php | 2 +- .../2022_08_13_083649_create_tasks_table.php | 6 +- 11 files changed, 636 insertions(+), 11 deletions(-) create mode 100644 app/Http/Controllers/Remote/TaskController.php create mode 100644 app/Notifications/CommonNotification.php diff --git a/app/Http/Controllers/Admin/User/DropController.php b/app/Http/Controllers/Admin/User/DropController.php index 9c4d435..74fd42f 100644 --- a/app/Http/Controllers/Admin/User/DropController.php +++ b/app/Http/Controllers/Admin/User/DropController.php @@ -34,7 +34,7 @@ public function store(Request $request) $data = [ 'payment' => 'admin', - 'amount' => $request->amount, + 'amount' => intval($request->amount), 'status' => 1, 'user_id' => $request->route('user'), 'type' => 'add', diff --git a/app/Http/Controllers/Remote/TaskController.php b/app/Http/Controllers/Remote/TaskController.php new file mode 100644 index 0000000..acaa8dc --- /dev/null +++ b/app/Http/Controllers/Remote/TaskController.php @@ -0,0 +1,122 @@ +validate([ + 'host_id' => 'sometimes|integer|exists:hosts,id', + 'user_id' => 'integer|exists:users,id', + ]); + + $this->user_id = $request->user_id; + $this->host_id = $request->host_id; + } + + /** + * Display a listing of the resource. + * + * @return \Illuminate\Http\Response + */ + public function index() + { + // + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(Request $request) + { + // + $request->validate([ + 'title' => 'required|max:255', + 'progress' => 'sometimes|integer|max:100', + 'status' => 'required|in:pending,processing,need_operation,done,success,failed,error,canceled', + ]); + + return $this->created($this->pushTask([ + 'title' => $request->title, + 'progress' => $request->progress, + 'status' => $request->status, + ])); + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + $request->validate([ + 'id' => 'max:40|required', + ]); + + $tasks = $this->getTasks(); + + // check if task exists + if (!isset($tasks[$id])) { + // create task + $task = [ + 'title' => $request->title, + 'progress' => 0, + 'status' => $request->status, + ]; + $this->pushTask($task, $request->id); + } else { + $task = $tasks[$id]; + } + + // patch task + $task = array_merge($task, $request->only(['title', 'progress', 'status'])); + + // update task + $this->pushTask($task, $request->id); + } + + public function getTasks() + { + $cache_key = 'user_tasks_' . $this->user_id; + return Cache::get($cache_key, []); + } + + public function pushTask($task, $id = null) + { + $cache_key = 'user_tasks_' . $this->user_id; + $data = [ + 'user_id' => $this->user_id, + 'done_at' => null, + 'host_id' => $this->host_id + ]; + + if ($id === null) { + $data['id'] = Uuid::uuid6()->toString(); + } else { + $data['id'] = $id; + } + + $task = array_merge($task, $data); + + $tasks = $this->getTasks(); + $tasks[] = $task; + + Cache::put($cache_key, $tasks, 600); + return $task; + } +} diff --git a/app/Models/User/Drop.php b/app/Models/User/Drop.php index 5ca2040..94909b1 100644 --- a/app/Models/User/Drop.php +++ b/app/Models/User/Drop.php @@ -13,10 +13,20 @@ class Drop extends Model { use HasFactory, Lock; + protected $cache_key, $cache; + protected $fillable = [ 'payment', 'amount', 'user_id', 'type' ]; + // casts + protected $casts = [ + 'amount' => 'double', + 'total' => 'double', + 'rate' => 'integer', + 'status' => 'boolean', + ]; + // user public function user() { @@ -41,9 +51,28 @@ public static function boot() // created self::created(function ($drops) { $drop = new self(); - $drop->await('user_drops_' . $drops->user_id, function () use ($drops) { - $drops->user->drops += $drops->total; - $drops->user->save(); + $drop->await('user_' . $drops->user_id, function () use ($drops) { + $cache = Cache::tags(['users']); + $drops->load('user'); + $cache_key = 'user_' . $drops->user_id; + + // if cache has user + if ($cache->has($cache_key)) { + $user = $cache->get($cache_key); + if (!($user instanceof User)) { + $user = $drops->user; + } + + $user->drops += $drops->total; + + + $cache->put($cache_key, $user, 600); + + $user->save(); + } else { + $drops->user->drops += $drops->total; + $drops->user->save(); + } }); }); } diff --git a/app/Notifications/CommonNotification.php b/app/Notifications/CommonNotification.php new file mode 100644 index 0000000..ebc109d --- /dev/null +++ b/app/Notifications/CommonNotification.php @@ -0,0 +1,49 @@ + $this->, + ]; + } +} diff --git a/composer.json b/composer.json index 9117d1b..31d8ced 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "license": "MIT", "require": { "php": "^8.0.2", + "doctrine/dbal": "^3.3", "guzzlehttp/guzzle": "^7.2", "laravel/framework": "^9.19", "laravel/horizon": "^5.9", diff --git a/composer.lock b/composer.lock index 244647f..1551963 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4cf71a6a9ffdf51850bec92dd6da12ee", + "content-hash": "f0cfd82920c282a29cd54ef1abb2d4ea", "packages": [ { "name": "brick/math", @@ -149,6 +149,371 @@ }, "time": "2021-08-13T13:06:58+00:00" }, + { + "name": "doctrine/cache", + "version": "2.2.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/cache.git", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/cache/zipball/1ca8f21980e770095a31456042471a57bc4c68fb", + "reference": "1ca8f21980e770095a31456042471a57bc4c68fb", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": "~7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": ">2.2,<2.4" + }, + "require-dev": { + "cache/integration-tests": "dev-master", + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5 || ^8.5 || ^9.5", + "psr/cache": "^1.0 || ^2.0 || ^3.0", + "symfony/cache": "^4.4 || ^5.4 || ^6", + "symfony/var-exporter": "^4.4 || ^5.4 || ^6" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Common\\Cache\\": "lib/Doctrine/Common/Cache" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + } + ], + "description": "PHP Doctrine Cache library is a popular cache implementation that supports many different drivers such as redis, memcache, apc, mongodb and others.", + "homepage": "https://www.doctrine-project.org/projects/cache.html", + "keywords": [ + "abstraction", + "apcu", + "cache", + "caching", + "couchdb", + "memcached", + "php", + "redis", + "xcache" + ], + "support": { + "issues": "https://github.com/doctrine/cache/issues", + "source": "https://github.com/doctrine/cache/tree/2.2.0" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fcache", + "type": "tidelift" + } + ], + "time": "2022-05-20T20:07:39+00:00" + }, + { + "name": "doctrine/dbal", + "version": "3.3.8", + "source": { + "type": "git", + "url": "https://github.com/doctrine/dbal.git", + "reference": "f873a820227bc352d023791775a01f078a30dfe1" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/dbal/zipball/f873a820227bc352d023791775a01f078a30dfe1", + "reference": "f873a820227bc352d023791775a01f078a30dfe1", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "composer-runtime-api": "^2", + "doctrine/cache": "^1.11|^2.0", + "doctrine/deprecations": "^0.5.3|^1", + "doctrine/event-manager": "^1.0", + "php": "^7.3 || ^8.0", + "psr/cache": "^1|^2|^3", + "psr/log": "^1|^2|^3" + }, + "require-dev": { + "doctrine/coding-standard": "9.0.0", + "jetbrains/phpstorm-stubs": "2022.1", + "phpstan/phpstan": "1.8.2", + "phpstan/phpstan-strict-rules": "^1.3", + "phpunit/phpunit": "9.5.21", + "psalm/plugin-phpunit": "0.17.0", + "squizlabs/php_codesniffer": "3.7.1", + "symfony/cache": "^5.2|^6.0", + "symfony/console": "^2.7|^3.0|^4.0|^5.0|^6.0", + "vimeo/psalm": "4.24.0" + }, + "suggest": { + "symfony/console": "For helpful console commands such as SQL execution and import of files." + }, + "bin": [ + "bin/doctrine-dbal" + ], + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\DBAL\\": "src" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + } + ], + "description": "Powerful PHP database abstraction layer (DBAL) with many features for database schema introspection and management.", + "homepage": "https://www.doctrine-project.org/projects/dbal.html", + "keywords": [ + "abstraction", + "database", + "db2", + "dbal", + "mariadb", + "mssql", + "mysql", + "oci8", + "oracle", + "pdo", + "pgsql", + "postgresql", + "queryobject", + "sasql", + "sql", + "sqlite", + "sqlserver", + "sqlsrv" + ], + "support": { + "issues": "https://github.com/doctrine/dbal/issues", + "source": "https://github.com/doctrine/dbal/tree/3.3.8" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fdbal", + "type": "tidelift" + } + ], + "time": "2022-08-05T15:35:35+00:00" + }, + { + "name": "doctrine/deprecations", + "version": "v1.0.0", + "source": { + "type": "git", + "url": "https://github.com/doctrine/deprecations.git", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/deprecations/zipball/0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "reference": "0e2a4f1f8cdfc7a92ec3b01c9334898c806b30de", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": "^7.1|^8.0" + }, + "require-dev": { + "doctrine/coding-standard": "^9", + "phpunit/phpunit": "^7.5|^8.5|^9.5", + "psr/log": "^1|^2|^3" + }, + "suggest": { + "psr/log": "Allows logging deprecations via PSR-3 logger implementation" + }, + "type": "library", + "autoload": { + "psr-4": { + "Doctrine\\Deprecations\\": "lib/Doctrine/Deprecations" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A small layer on top of trigger_error(E_USER_DEPRECATED) or PSR-3 logging with options to disable all deprecations or selectively for packages.", + "homepage": "https://www.doctrine-project.org/", + "support": { + "issues": "https://github.com/doctrine/deprecations/issues", + "source": "https://github.com/doctrine/deprecations/tree/v1.0.0" + }, + "time": "2022-05-02T15:47:09+00:00" + }, + { + "name": "doctrine/event-manager", + "version": "1.1.1", + "source": { + "type": "git", + "url": "https://github.com/doctrine/event-manager.git", + "reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/doctrine/event-manager/zipball/41370af6a30faa9dc0368c4a6814d596e81aba7f", + "reference": "41370af6a30faa9dc0368c4a6814d596e81aba7f", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "conflict": { + "doctrine/common": "<2.9@dev" + }, + "require-dev": { + "doctrine/coding-standard": "^6.0", + "phpunit/phpunit": "^7.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Doctrine\\Common\\": "lib/Doctrine/Common" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Guilherme Blanco", + "email": "guilhermeblanco@gmail.com" + }, + { + "name": "Roman Borschel", + "email": "roman@code-factory.org" + }, + { + "name": "Benjamin Eberlei", + "email": "kontakt@beberlei.de" + }, + { + "name": "Jonathan Wage", + "email": "jonwage@gmail.com" + }, + { + "name": "Johannes Schmitt", + "email": "schmittjoh@gmail.com" + }, + { + "name": "Marco Pivetta", + "email": "ocramius@gmail.com" + } + ], + "description": "The Doctrine Event Manager is a simple PHP event system that was built to be used with the various Doctrine projects.", + "homepage": "https://www.doctrine-project.org/projects/event-manager.html", + "keywords": [ + "event", + "event dispatcher", + "event manager", + "event system", + "events" + ], + "support": { + "issues": "https://github.com/doctrine/event-manager/issues", + "source": "https://github.com/doctrine/event-manager/tree/1.1.x" + }, + "funding": [ + { + "url": "https://www.doctrine-project.org/sponsorship.html", + "type": "custom" + }, + { + "url": "https://www.patreon.com/phpdoctrine", + "type": "patreon" + }, + { + "url": "https://tidelift.com/funding/github/packagist/doctrine%2Fevent-manager", + "type": "tidelift" + } + ], + "time": "2020-05-29T18:28:51+00:00" + }, { "name": "doctrine/inflector", "version": "2.0.4", @@ -2472,6 +2837,61 @@ ], "time": "2021-12-04T23:24:31+00:00" }, + { + "name": "psr/cache", + "version": "3.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/cache.git", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/cache/zipball/aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "reference": "aa5030cfa5405eccfdcb1083ce040c2cb8d253bf", + "shasum": "", + "mirrors": [ + { + "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%", + "preferred": true + } + ] + }, + "require": { + "php": ">=8.0.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Cache\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "https://www.php-fig.org/" + } + ], + "description": "Common interface for caching libraries", + "keywords": [ + "cache", + "psr", + "psr-6" + ], + "support": { + "source": "https://github.com/php-fig/cache/tree/3.0.0" + }, + "time": "2021-02-03T23:26:27+00:00" + }, { "name": "psr/container", "version": "2.0.2", diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 703902f..b8ab975 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -21,7 +21,7 @@ public function up() $table->string('password')->nullable(); // 积分(8位小数点) - $table->double('drops', 8, 6)->default(0); + $table->double('drops', 60, 8)->default(0); $table->rememberToken(); $table->timestamps(); diff --git a/database/migrations/2022_08_12_081236_create_drops_table.php b/database/migrations/2022_08_12_081236_create_drops_table.php index 0b12a71..3e06978 100644 --- a/database/migrations/2022_08_12_081236_create_drops_table.php +++ b/database/migrations/2022_08_12_081236_create_drops_table.php @@ -21,13 +21,13 @@ public function up() $table->string('payment')->index(); // amount - $table->decimal('amount', 8, 2)->default(0); + $table->double('amount', 60, 8)->default(0); // 汇率 $table->integer('rate')->default(1); // 实际收入 - $table->double('total', 8, 6)->default(0); + $table->double('total', 60, 8)->default(0); $table->boolean('status')->default(0)->index(); diff --git a/database/migrations/2022_08_12_082631_create_transcations_table.php b/database/migrations/2022_08_12_082631_create_transcations_table.php index 910f636..1c3f339 100644 --- a/database/migrations/2022_08_12_082631_create_transcations_table.php +++ b/database/migrations/2022_08_12_082631_create_transcations_table.php @@ -27,7 +27,7 @@ public function up() $table->string('payment')->index(); // amount - $table->double('amount', 8, 6)->default(0); + $table->double('amount', 60, 8)->default(0); $table->timestamps(); diff --git a/database/migrations/2022_08_13_051148_create_hosts_table.php b/database/migrations/2022_08_13_051148_create_hosts_table.php index 9962f28..d852a28 100644 --- a/database/migrations/2022_08_13_051148_create_hosts_table.php +++ b/database/migrations/2022_08_13_051148_create_hosts_table.php @@ -28,7 +28,7 @@ public function up() $table->foreignIdFor(User::class)->index(); // price - $table->double('price', 8, 6)->index(); + $table->double('price', 60, 8)->index(); // config $table->json('configuration')->nullable(); diff --git a/database/migrations/2022_08_13_083649_create_tasks_table.php b/database/migrations/2022_08_13_083649_create_tasks_table.php index fc00cc1..e169d26 100644 --- a/database/migrations/2022_08_13_083649_create_tasks_table.php +++ b/database/migrations/2022_08_13_083649_create_tasks_table.php @@ -1,6 +1,7 @@ integer('progress')->default(0); // status - $table->enum('status', ['pending', 'done', 'success', 'failed', 'error', 'cancelled', 'processing'])->index(); + $table->enum('status', ['pending', 'done', 'success', 'failed', 'error', 'cancelled', 'processing', 'need_operation'])->index(); // user id $table->foreignIdFor(User::class)->index(); @@ -32,6 +33,9 @@ public function up() // provider module id $table->foreignIdFor(ProviderModule::class)->index(); + // host id + $table->foreignIdFor(Host::class)->index(); + $table->timestamps(); }); }