fix : update cors and admin user cache

This commit is contained in:
2026-02-24 18:02:18 +08:00
parent 03b57ad514
commit 09d396c77c
16 changed files with 278 additions and 22 deletions

View File

@@ -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
```

View File

@@ -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,
];
/**

View File

@@ -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
{
/**

View File

@@ -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);
}
}

View File

@@ -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;

View File

@@ -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
) {}
/**

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Exception\Handler;
use App\Constants\ResultCode;
use App\Exception\ErrException;
use Hyperf\HttpMessage\Exception\HttpException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;
use Throwable;
class MainHttpExceptionHandler extends BaseErrExceptionHandler
{
/**
* @param Throwable $throwable
* @param ResponseInterface $response
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(Throwable $throwable, ResponseInterface $response): ResponseInterface
{
if ($throwable instanceof HttpException) {
$msg = match ($throwable->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;
}
}

View File

@@ -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;
}
/**

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace App\Middleware;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Hyperf\Context\Context;
class CorsMiddleware implements MiddlewareInterface
{
public function __construct(protected ContainerInterface $container)
{
}
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = Context::get(ResponseInterface::class);
$response = $response->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);
}
}

View File

@@ -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();
}
}

View File

@@ -89,6 +89,8 @@ class UserService extends BaseAdminService
if (!$res) throw new ErrException('修改失败');
$this->updateAdminUserInfo($this->adminId);
return $this->adminReturn->success();
}

View File

@@ -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",

69
composer.lock generated
View File

@@ -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",

View File

@@ -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,
],
],
],

View File

@@ -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,
],
];

46
restart_prod.sh Normal file
View File

@@ -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[*]}"' '