diff --git a/README.md b/README.md index eebb39d..66ecc40 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ ## 仓库 -- [Hyperf_Micro_Svc](https://gitee.com/ctexthuang/hyperf-micro-svc) - git远程仓库地址 +- [Geeco_server](https://gitee.com/ctexthuang/hyperf-micro-svc) - git远程仓库地址 ## 特性 -- **最新技术栈**:使用 PHP8.3/hyperf3.1/swoole5.1.4/phpredis 6.0.2 等后端前沿技术开发 +- **最新技术栈**:使用 PHP8.3/hyperf3.1/swoole5.1.8/phpredis 6.0.2/PostgreSQL18 等后端前沿技术开发 ## 文档 @@ -16,6 +16,7 @@ - [swoole](https://www.swoole.com/) - 熟悉 swoole 特性 - [php8.3](https://www.php.net/) - 熟悉 php 基础语法 - [hyperf](https://hyperf.wiki/3.1/) - 熟悉 `hyperf` 基本语法 +- [postgreSql18](https://www.postgresql.org/docs/18/index.html) - 熟悉 PostgreSQL18 基本语法 ## 安装和使用 - 安装 swoole 和 phpredis 扩展 @@ -34,15 +35,30 @@ git clone https://gitee.com/ctexthuang/hyperf-micro-svc ```bash composer install + +# 开发环境: +composer install -vvv +# 生产环境安装: +composer install --no-dev --optimize-autoloader ``` -- 运行 +- 数据同步 ```bash cp .env.example .env vim .env +mysql -u root -p -e "CREATE DATABASE xxx CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;" +php bin/hyperf.php migrate +php bin/hyperf.php db:seed + +# 仅某个文件 seeder +php bin/hyperf.php db:seed --path=seeders/create_time_zone_line.php + +- 运行 + +``` php bin/hyperf.php start ``` diff --git a/app/Aspect/AdminReturnLogAspect.php b/app/Aspect/AdminReturnLogAspect.php index 0230a3b..221fc3d 100644 --- a/app/Aspect/AdminReturnLogAspect.php +++ b/app/Aspect/AdminReturnLogAspect.php @@ -4,10 +4,10 @@ declare(strict_types=1); namespace App\Aspect; +use App\Annotation\ResponseFormat; use App\Common\Trait\AdminUserTrait; use App\Common\Trait\ClientIpTrait; use App\Lib\Log\Logger; -use App\Lib\Return\AdminReturn; use App\Model\AdminUser; use App\Model\AdminUserOperationLog; use Hyperf\Context\Context; @@ -18,7 +18,7 @@ use Hyperf\Di\Aop\ProceedingJoinPoint; use Hyperf\Di\Exception\Exception; use Hyperf\HttpServer\Contract\RequestInterface; -#[Aspect] +#[Aspect(priority: 1)] class AdminReturnLogAspect extends AbstractAspect { use ClientIpTrait; @@ -29,7 +29,7 @@ class AdminReturnLogAspect extends AbstractAspect * @var array|\class-string[] */ public array $classes = [ - AdminReturn::class, + ResponseFormat::class, ]; /** diff --git a/app/Aspect/ResponseFormatAspect.php b/app/Aspect/ResponseFormatAspect.php index 9aa42d7..4622dab 100644 --- a/app/Aspect/ResponseFormatAspect.php +++ b/app/Aspect/ResponseFormatAspect.php @@ -12,7 +12,7 @@ use Hyperf\HttpServer\Request; use Psr\Container\ContainerInterface; use Hyperf\Di\Aop\ProceedingJoinPoint; -#[Aspect] +#[Aspect(priority: 0)] class ResponseFormatAspect extends AbstractAspect { /** diff --git a/app/Common/Trait/AdminUserTrait.php b/app/Common/Trait/AdminUserTrait.php index 8d58ecd..63ea4a7 100644 --- a/app/Common/Trait/AdminUserTrait.php +++ b/app/Common/Trait/AdminUserTrait.php @@ -51,4 +51,16 @@ trait AdminUserTrait return $userInfo; } + + /** + * @param int $adminId + * @return void + */ + public function updateAdminUserInfo(int $adminId): void + { + $key = RedisKey::getAdminUserInfoKey($adminId); + if ($this->redis->with()->exists($key)) $this->redis->with()->del($key); + if (Context::has($key)) Context::destroy($key); + $this->getAdminUserInfo($adminId); + } } \ No newline at end of file diff --git a/app/Exception/Handler/AppExceptionHandler.php b/app/Exception/Handler/AppExceptionHandler.php index 17b5fd6..477f328 100644 --- a/app/Exception/Handler/AppExceptionHandler.php +++ b/app/Exception/Handler/AppExceptionHandler.php @@ -12,25 +12,50 @@ declare(strict_types=1); namespace App\Exception\Handler; +use App\Constants\ResultCode; +use App\Exception\ErrException; use Hyperf\Contract\StdoutLoggerInterface; use Hyperf\ExceptionHandler\ExceptionHandler; use Hyperf\HttpMessage\Stream\SwooleStream; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; +use Psr\Http\Message\MessageInterface; use Psr\Http\Message\ResponseInterface; use Throwable; -class AppExceptionHandler extends ExceptionHandler +class AppExceptionHandler extends BaseErrExceptionHandler { - public function __construct(protected StdoutLoggerInterface $logger) + /** + * @param Throwable $throwable + * @param ResponseInterface $response + * @return MessageInterface|ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function handle(Throwable $throwable, ResponseInterface $response): MessageInterface|ResponseInterface { + $this->logger->error()->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); + $this->logger->error()->error($throwable->getTraceAsString()); + $throwable = $this->modifyException($throwable); + return $this->handlerResponse($throwable,$response); } - public function handle(Throwable $throwable, ResponseInterface $response) + /** + * @param Throwable $throwable + * @return ErrException + */ + protected function modifyException(Throwable $throwable): ErrException { $this->logger->error(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile())); $this->logger->error($throwable->getTraceAsString()); - return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); +// return $response->withHeader('Server', 'Hyperf')->withStatus(500)->withBody(new SwooleStream('Internal Server Error.')); + return New ErrException(sprintf('%s[%s] in %s', $throwable->getMessage(), $throwable->getLine(), $throwable->getFile()),ResultCode::ERROR); } + /** + * @param Throwable $throwable + * @return bool + */ public function isValid(Throwable $throwable): bool { return true; diff --git a/app/Exception/Handler/BaseErrExceptionHandler.php b/app/Exception/Handler/BaseErrExceptionHandler.php index 62cbf1b..d7fe4cf 100644 --- a/app/Exception/Handler/BaseErrExceptionHandler.php +++ b/app/Exception/Handler/BaseErrExceptionHandler.php @@ -3,6 +3,7 @@ namespace App\Exception\Handler; +use App\Lib\Log\Logger; use App\Lib\Return\AdminReturn; use App\Lib\Return\ApiReturn; use Hyperf\ExceptionHandler\ExceptionHandler; @@ -20,6 +21,7 @@ abstract class BaseErrExceptionHandler extends ExceptionHandler public function __construct( private readonly Request $request, private readonly ContainerInterface $container, + protected readonly Logger $logger ) {} /** diff --git a/app/Exception/Handler/MainHttpExceptionHandler.php b/app/Exception/Handler/MainHttpExceptionHandler.php new file mode 100644 index 0000000..6464c45 --- /dev/null +++ b/app/Exception/Handler/MainHttpExceptionHandler.php @@ -0,0 +1,44 @@ +getStatusCode()) { + 404 => 'The route does not exist.', + 405 => 'The HTTP method not allowed.', + default => $throwable->getMessage(), + }; + $throwable = new ErrException($msg,ResultCode::ERROR); + return $this->handlerResponse($throwable,$response); + } + return $response; + } + + /** + * @param Throwable $throwable + * @return bool + */ + public function isValid(Throwable $throwable): bool + { + return $throwable instanceof HttpException; + } +} \ No newline at end of file diff --git a/app/Exception/Handler/ValidationExceptionHandler.php b/app/Exception/Handler/ValidationExceptionHandler.php index 29ad235..6f83a74 100644 --- a/app/Exception/Handler/ValidationExceptionHandler.php +++ b/app/Exception/Handler/ValidationExceptionHandler.php @@ -2,6 +2,8 @@ namespace App\Exception\Handler; +use App\Constants\ResultCode; +use App\Exception\ErrException; use Hyperf\Validation\ValidationException; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; @@ -19,9 +21,11 @@ class ValidationExceptionHandler extends BaseErrExceptionHandler */ public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface { - return $throwable instanceof ValidationException - ? $this->handlerResponse($throwable, $response) - : $response; + if ($throwable instanceof ValidationException) { + $throwable = new ErrException($throwable->validator->errors()->first(),ResultCode::ERROR); + return $this->handlerResponse($throwable,$response); + } + return $response; } /** diff --git a/app/Middleware/CorsMiddleware.php b/app/Middleware/CorsMiddleware.php new file mode 100644 index 0000000..dcf8142 --- /dev/null +++ b/app/Middleware/CorsMiddleware.php @@ -0,0 +1,35 @@ +withHeader('Access-Control-Allow-Origin', '*') + ->withHeader('Access-Control-Allow-Credentials', 'true') + ->withHeader('Access-Control-Allow-Headers', '*') // Headers 可以根据实际情况进行改写。 + ->withHeader('Access-Control-Request-Headers', '*') + ->withHeader('Access-Control-Allow-Methods', '*') + ->withHeader('Access-Control-Max-Age', 1800); + Context::set(ResponseInterface::class, $response); + if ($request->getMethod() == 'OPTIONS') { + return $response; + } + return $handler->handle($request); + } +} diff --git a/app/Service/Admin/AdminUser/PermissionService.php b/app/Service/Admin/AdminUser/PermissionService.php index fc3166f..28d4942 100644 --- a/app/Service/Admin/AdminUser/PermissionService.php +++ b/app/Service/Admin/AdminUser/PermissionService.php @@ -130,6 +130,8 @@ class PermissionService extends BaseAdminService if (!$this->adminUserRepository->updateById($userInfo->id, $data)) throw new ErrException('更新失败'); + $this->updateAdminUserInfo($this->adminId); + return $this->adminReturn->success(); } } \ No newline at end of file diff --git a/app/Service/Admin/AdminUser/UserService.php b/app/Service/Admin/AdminUser/UserService.php index d420e39..104bb2d 100644 --- a/app/Service/Admin/AdminUser/UserService.php +++ b/app/Service/Admin/AdminUser/UserService.php @@ -89,6 +89,8 @@ class UserService extends BaseAdminService if (!$res) throw new ErrException('修改失败'); + $this->updateAdminUserInfo($this->adminId); + return $this->adminReturn->success(); } diff --git a/composer.json b/composer.json index 594a154..cfd55db 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "hyperf/config": "~3.1.0", "hyperf/constants": "~3.1.0", "hyperf/database": "~3.1.0", + "hyperf/database-pgsql": "^3.1", "hyperf/db-connection": "~3.1.0", "hyperf/engine": "^2.10", "hyperf/framework": "~3.1.0", diff --git a/composer.lock b/composer.lock index a21bb43..0388422 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": "ab3bf1b529644b23090f06e38ff6037a", + "content-hash": "d2802d82e4914e3e7c928d7a3aa7923d", "packages": [ { "name": "carbonphp/carbon-doctrine-types", @@ -1892,6 +1892,73 @@ ], "time": "2025-09-04T02:14:51+00:00" }, + { + "name": "hyperf/database-pgsql", + "version": "v3.1.66", + "source": { + "type": "git", + "url": "https://github.com/hyperf/database-pgsql.git", + "reference": "f6cc86dea8910e555f17f37629cc27dd07ccb532" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/hyperf/database-pgsql/zipball/f6cc86dea8910e555f17f37629cc27dd07ccb532", + "reference": "f6cc86dea8910e555f17f37629cc27dd07ccb532", + "shasum": "" + }, + "require": { + "hyperf/collection": "~3.1.0", + "hyperf/database": "~3.1.0", + "hyperf/stringable": "~3.1.0", + "hyperf/support": "~3.1.0", + "hyperf/tappable": "~3.1.0", + "php": ">=8.1" + }, + "type": "library", + "extra": { + "hyperf": { + "config": "Hyperf\\Database\\PgSQL\\ConfigProvider" + }, + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "psr-4": { + "Hyperf\\Database\\PgSQL\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A pgsql handler for hyperf/database.", + "homepage": "https://hyperf.io", + "keywords": [ + "database", + "hyperf", + "pgsql", + "php", + "swoole" + ], + "support": { + "docs": "https://hyperf.wiki", + "issues": "https://github.com/hyperf/hyperf/issues", + "pull-request": "https://github.com/hyperf/hyperf/pulls", + "source": "https://github.com/hyperf/hyperf" + }, + "funding": [ + { + "url": "https://hyperf.wiki/#/zh-cn/donate", + "type": "custom" + }, + { + "url": "https://opencollective.com/hyperf", + "type": "open_collective" + } + ], + "time": "2026-01-25T04:50:06+00:00" + }, { "name": "hyperf/db-connection", "version": "v3.1.57", diff --git a/config/autoload/logger.php b/config/autoload/logger.php index 48e43b1..b1a9dab 100644 --- a/config/autoload/logger.php +++ b/config/autoload/logger.php @@ -81,13 +81,11 @@ return [ ], 'cron' => [ 'handler' => [ - [ - 'class' => RotatingFileHandler::class, - 'constructor' => [ - 'filename' => BASE_PATH . '/runtime/logs/crontab/cron.log', - 'level' => Monolog\Logger::INFO, - 'maxFiles' => 30, - ], + 'class' => RotatingFileHandler::class, + 'constructor' => [ + 'filename' => BASE_PATH . '/runtime/logs/crontab/cron.log', + 'level' => Monolog\Logger::INFO, + 'maxFiles' => 30, ], ], ], diff --git a/config/autoload/middlewares.php b/config/autoload/middlewares.php index 7ce4f87..a542fad 100644 --- a/config/autoload/middlewares.php +++ b/config/autoload/middlewares.php @@ -10,10 +10,12 @@ declare(strict_types=1); * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ +use App\Middleware\CorsMiddleware; use Hyperf\Validation\Middleware\ValidationMiddleware; return [ 'http' => [ + CorsMiddleware::class, ValidationMiddleware::class, ], ]; diff --git a/restart_prod.sh b/restart_prod.sh new file mode 100644 index 0000000..4958f6c --- /dev/null +++ b/restart_prod.sh @@ -0,0 +1,46 @@ +#!/bin/bash +HomeDir=/home/production/cdiscount/server + +# 替 .env +rm -rf "$HomeDir/.env" +cp "$HomeDir/.env.prod" "$HomeDir/.env" +env_file="$HomeDir/.env" + +# 设置 CRONTAB_ENABLE +if [ -e "$HomeDir/../cron_true.txt" ]; then + sed -i 's/^CRONTAB_ENABLE=.*/CRONTAB_ENABLE=true/' "$env_file" +else + sed -i 's/^CRONTAB_ENABLE=.*/CRONTAB_ENABLE=false/' "$env_file" +fi + +# 从 server.php 中提取所有 'port' => 数字 的值(使用 grep + awk) +# 匹配类似:'port' => 9501, +ports=$(grep -o "'port'[[:space:]]*=>[[:space:]]*[0-9]\+" "$HomeDir/config/autoload/server.php" | awk -F '=>' '{print $2}' | tr -d ' ,') +# 转为数组 +read -a port_array <<< "$ports" +if [ ${#port_array[@]} -eq 0 ]; then + echo "Error: No valid ports found in server.php. Exiting." + exit 1 +fi +echo "Detected ports: ${port_array[*]}" + +# 只 kill 正在被监听的端口 +for port in "${port_array[@]}"; do + if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then + echo "Warning: Skipping invalid port '$port'" + continue + fi + if ss -tuln | grep -q ":$port\b"; then + echo "Port $port is in use. Killing related processes..." + sudo lsof -i:"$port" -t | sudo xargs -r kill -9 + else + echo "Port $port is not in use. Skip killing." + fi +done +echo 'Cleanup done.' + +# 启动服务 +source /etc/profile +mkdir -p /home/log +nohup php "$HomeDir/bin/hyperf.php" start > /home/log/hyperf_run.log 2>&1 & +echo '!!!Service restarted successfully on ports: '"${port_array[*]}"' !!!' \ No newline at end of file