1. 维护代码健壮

2. 更新项目结构文档
This commit is contained in:
技术老胡
2026-04-27 16:20:41 +08:00
parent 9a16a88640
commit 0e5de50337
198 changed files with 21038 additions and 702 deletions

View File

@@ -0,0 +1,103 @@
<?php
namespace app\http\mer\controller\file;
use app\common\base\BaseController;
use app\exception\ValidationException;
use app\http\admin\validation\FileRecordValidator;
use app\service\file\FileRecordService;
use support\Request;
use support\Response;
use Webman\Http\UploadFile;
/**
* 商户端文件控制器。
*
* 供插件配置动态表单中的上传字段使用。
*
* @property FileRecordService $fileRecordService 文件记录服务
*/
class FileRecordController extends BaseController
{
/**
* 构造方法。
*
* @param FileRecordService $fileRecordService 文件记录服务
* @return void
*/
public function __construct(
protected FileRecordService $fileRecordService
) {
}
/**
* 上传文件记录。
*
* @param Request $request 请求对象
* @return Response 响应对象
* @throws ValidationException
*/
public function upload(Request $request): Response
{
$data = $this->validated(array_merge($this->payload($request), ['scene' => $request->input('scene')]), FileRecordValidator::class, 'store');
$uploadedFile = $request->file('file');
if ($uploadedFile === null) {
throw new ValidationException('请先选择上传文件');
}
$createdBy = $this->currentMerchantId($request);
$createdByName = $this->currentMerchantNo($request);
if (is_array($uploadedFile)) {
$items = [];
foreach ($uploadedFile as $file) {
if ($file instanceof UploadFile) {
$items[] = $this->fileRecordService->upload($file, $data, $createdBy, $createdByName);
}
}
if ($items === []) {
throw new ValidationException('上传文件无效');
}
return $this->success([
'list' => $items,
'total' => count($items),
]);
}
if (!$uploadedFile instanceof UploadFile) {
throw new ValidationException('上传文件无效');
}
return $this->success($this->fileRecordService->upload($uploadedFile, $data, $createdBy, $createdByName));
}
/**
* 获取文件预览响应。
*
* @param Request $request 请求对象
* @param string $id 文件记录ID
* @return Response 响应对象
*/
public function preview(Request $request, string $id): Response
{
$data = $this->validated(['id' => (int) $id], FileRecordValidator::class, 'preview');
return $this->fileRecordService->previewResponse((int) $data['id']);
}
/**
* 获取文件下载响应。
*
* @param Request $request 请求对象
* @param string $id 文件记录ID
* @return Response 响应对象
*/
public function download(Request $request, string $id): Response
{
$data = $this->validated(['id' => (int) $id], FileRecordValidator::class, 'download');
return $this->fileRecordService->downloadResponse((int) $data['id']);
}
}

View File

@@ -100,6 +100,138 @@ class MerchantPortalController extends BaseController
return $this->success($this->merchantPortalService->myChannels($payload, $merchantId, $page, $pageSize));
}
public function channelCreateMeta(Request $request): Response
{
return $this->success($this->merchantPortalService->channelCreateMeta());
}
public function createChannel(Request $request): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated($this->payload($request), MerchantPortalValidator::class, 'channelStore');
return $this->success($this->merchantPortalService->createChannel($merchantId, $data));
}
public function updateChannel(Request $request, string $id): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated(
array_merge($this->payload($request), ['id' => (int) $id]),
MerchantPortalValidator::class,
'channelUpdate'
);
$channel = $this->merchantPortalService->updateChannel($merchantId, (int) $data['id'], $data);
if (!$channel) {
return $this->fail('通道不存在', 404);
}
return $this->success($channel);
}
public function deleteChannel(Request $request, string $id): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated(['id' => (int) $id], MerchantPortalValidator::class, 'channelDestroy');
if (!$this->merchantPortalService->deleteChannel($merchantId, (int) $data['id'])) {
return $this->fail('通道不存在', 404);
}
return $this->success(true);
}
public function pluginConfigs(Request $request): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$payload = $this->validated($this->payload($request), MerchantPortalValidator::class, 'pluginConfigIndex');
$page = max(1, (int) ($payload['page'] ?? 1));
$pageSize = max(1, (int) ($payload['page_size'] ?? 10));
return $this->success($this->merchantPortalService->pluginConfigs($payload, $merchantId, $page, $pageSize));
}
public function createPluginConfig(Request $request): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated($this->payload($request), MerchantPortalValidator::class, 'pluginConfigStore');
return $this->success($this->merchantPortalService->createPluginConfig($merchantId, $data));
}
public function updatePluginConfig(Request $request, string $id): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated(
array_merge($this->payload($request), ['id' => (int) $id]),
MerchantPortalValidator::class,
'pluginConfigUpdate'
);
$config = $this->merchantPortalService->updatePluginConfig($merchantId, (int) $data['id'], $data);
if (!$config) {
return $this->fail('插件配置不存在', 404);
}
return $this->success($config);
}
public function deletePluginConfig(Request $request, string $id): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
$data = $this->validated(['id' => (int) $id], MerchantPortalValidator::class, 'pluginConfigDestroy');
if (!$this->merchantPortalService->deletePluginConfig($merchantId, (int) $data['id'])) {
return $this->fail('插件配置不存在', 404);
}
return $this->success(true);
}
public function pluginConfigOptions(Request $request): Response
{
$merchantId = $this->currentMerchantId($request);
if ($merchantId <= 0) {
return $this->fail('未获取到当前商户信息', 401);
}
return $this->success([
'configs' => $this->merchantPortalService->pluginConfigOptions($merchantId, (string) $request->get('plugin_code', '')),
]);
}
public function pluginSchema(Request $request, string $code): Response
{
return $this->success($this->merchantPortalService->pluginSchema($code));
}
/**
* 获取路由解析结果。
*
@@ -113,7 +245,12 @@ class MerchantPortalController extends BaseController
return $this->fail('未获取到当前商户信息', 401);
}
$payload = $this->validated($this->payload($request), MerchantPortalValidator::class, 'routePreview');
$rawPayload = $this->payload($request);
if (empty($rawPayload['pay_type_id']) || empty($rawPayload['pay_amount'])) {
return $this->success($this->merchantPortalService->routePreview($merchantId, 0, 0));
}
$payload = $this->validated($rawPayload, MerchantPortalValidator::class, 'routePreview');
$payTypeId = (int) ($payload['pay_type_id'] ?? 0);
$payAmount = (int) ($payload['pay_amount'] ?? 0);
$statDate = trim((string) ($payload['stat_date'] ?? ''));
@@ -150,7 +287,9 @@ class MerchantPortalController extends BaseController
return $this->fail('未获取到当前商户信息', 401);
}
return $this->success($this->merchantPortalService->issueCredential($merchantId));
$data = $this->validated($this->payload($request), MerchantPortalValidator::class, 'issueCredential');
return $this->success($this->merchantPortalService->issueCredential($merchantId, $data));
}
/**

View File

@@ -2,6 +2,7 @@
namespace app\http\mer\middleware;
use app\exception\UnauthorizedException;
use app\service\merchant\auth\MerchantAuthService;
use support\Context;
use Webman\Http\Request;
@@ -34,6 +35,7 @@ class MerchantAuthMiddleware implements MiddlewareInterface
* @param Request $request 请求对象
* @param callable $handler handler
* @return Response 响应对象
* @throws UnauthorizedException
*/
public function process(Request $request, callable $handler): Response
{
@@ -42,11 +44,7 @@ class MerchantAuthMiddleware implements MiddlewareInterface
if ($token === '') {
if ((int) env('AUTH_MIDDLEWARE_STRICT', 1) === 1) {
return json([
'code' => 401,
'msg' => 'merchant unauthorized',
'data' => null,
]);
throw new UnauthorizedException('商户未授权');
}
} else {
$result = $this->merchantAuthService->authenticateToken(
@@ -55,11 +53,7 @@ class MerchantAuthMiddleware implements MiddlewareInterface
$request->header('user-agent', '')
);
if (!$result) {
return json([
'code' => 401,
'msg' => 'merchant unauthorized',
'data' => null,
]);
throw new UnauthorizedException('商户未授权');
}
Context::set('auth.merchant_id', (int) $result['merchant']->id);

View File

@@ -29,6 +29,26 @@ class MerchantPortalValidator extends Validator
'pay_type_id' => 'required|integer|min:1',
'pay_amount' => 'required|integer|min:1',
'stat_date' => 'sometimes|date',
'id' => 'sometimes|integer|min:1',
'keyword' => 'sometimes|string|max:128',
'plugin_code' => 'sometimes|string|alpha_dash|min:2|max:32',
'config' => 'nullable|array',
'settlement_cycle_type' => 'sometimes|integer|in:0,1,2,3,4',
'settlement_cutoff_time' => 'nullable|date_format:H:i:s',
'name' => 'sometimes|string|min:2|max:128',
'api_config_id' => 'sometimes|integer|min:1',
'daily_limit_amount' => 'nullable|integer|min:0',
'daily_limit_count' => 'nullable|integer|min:0',
'min_amount' => 'nullable|integer|min:0',
'max_amount' => 'nullable|integer|min:0',
'remark' => 'nullable|string|max:500',
'status' => 'sometimes|integer|in:0,1',
'rotate_v1' => 'sometimes|integer|in:0,1',
'rotate_v2' => 'sometimes|integer|in:0,1',
'sign_type' => 'sometimes|integer|in:0,1',
'sort_no' => 'nullable|integer|min:0',
'page' => 'sometimes|integer|min:1',
'page_size' => 'sometimes|integer|min:1|max:100',
];
/**
@@ -51,6 +71,26 @@ class MerchantPortalValidator extends Validator
'pay_type_id' => '支付方式',
'pay_amount' => '支付金额',
'stat_date' => '统计日期',
'id' => '记录ID',
'keyword' => '关键字',
'plugin_code' => '支付插件',
'config' => '插件配置',
'settlement_cycle_type' => '结算周期',
'settlement_cutoff_time' => '结算截止时间',
'name' => '通道名称',
'api_config_id' => '插件配置',
'daily_limit_amount' => '单日限额',
'daily_limit_count' => '单日限笔',
'min_amount' => '单笔最小金额',
'max_amount' => '单笔最大金额',
'remark' => '备注',
'status' => '状态',
'rotate_v1' => 'V1 凭证',
'rotate_v2' => 'V2 凭证',
'sign_type' => '签名类型',
'sort_no' => '排序',
'page' => '页码',
'page_size' => '每页条数',
];
/**
@@ -71,7 +111,47 @@ class MerchantPortalValidator extends Validator
],
'passwordUpdate' => ['current_password', 'password', 'password_confirm'],
'routePreview' => ['pay_type_id', 'pay_amount', 'stat_date'],
'pluginConfigIndex' => ['keyword', 'plugin_code', 'page', 'page_size'],
'pluginConfigStore' => ['plugin_code', 'config', 'settlement_cycle_type', 'settlement_cutoff_time', 'remark'],
'pluginConfigUpdate' => ['id', 'plugin_code', 'config', 'settlement_cycle_type', 'settlement_cutoff_time', 'remark'],
'pluginConfigDestroy' => ['id'],
'channelStore' => ['name', 'pay_type_id', 'plugin_code', 'api_config_id', 'daily_limit_amount', 'daily_limit_count', 'min_amount', 'max_amount', 'remark', 'status', 'sort_no'],
'channelUpdate' => ['id', 'name', 'pay_type_id', 'plugin_code', 'api_config_id', 'daily_limit_amount', 'daily_limit_count', 'min_amount', 'max_amount', 'remark', 'status', 'sort_no'],
'channelDestroy' => ['id'],
'issueCredential' => ['rotate_v1', 'rotate_v2', 'sign_type', 'status'],
];
public function rules(): array
{
$rules = parent::rules();
return match ($this->scene()) {
'pluginConfigStore' => array_merge($rules, [
'plugin_code' => 'required|string|alpha_dash|min:2|max:32',
]),
'pluginConfigUpdate' => array_merge($rules, [
'id' => 'required|integer|min:1',
'plugin_code' => 'required|string|alpha_dash|min:2|max:32',
]),
'pluginConfigDestroy', 'channelDestroy' => array_merge($rules, [
'id' => 'required|integer|min:1',
]),
'channelStore' => array_merge($rules, [
'name' => 'required|string|min:2|max:128',
'pay_type_id' => 'required|integer|min:1',
'plugin_code' => 'required|string|alpha_dash|min:2|max:32',
'api_config_id' => 'required|integer|min:1',
'status' => 'required|integer|in:0,1',
]),
'channelUpdate' => array_merge($rules, [
'id' => 'required|integer|min:1',
'name' => 'required|string|min:2|max:128',
'pay_type_id' => 'required|integer|min:1',
'plugin_code' => 'required|string|alpha_dash|min:2|max:32',
'api_config_id' => 'required|integer|min:1',
'status' => 'required|integer|in:0,1',
]),
default => $rules,
};
}
}