Files
wh-api/app/Service/Trait/ColumnConfigTrait.php
ykxiao 0b2299c427
Some checks failed
Build Docker / build (push) Has been cancelled
协程版仓库后端项目
2025-07-08 14:59:47 +08:00

148 lines
3.9 KiB
PHP
Executable File

<?php
/**
* Author: ykxiao
* Date: 2024/12/24
* Time: 下午3:05
* Description:
*
* (c) ykxiao <yk_9001@hotmail.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Service\Trait;
use App\Context\UserContext;
use App\Model\ColumnConfig;
use App\Model\TableConfig;
use App\Service\QueueService;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Router\Dispatched;
use Hyperf\Stringable\Str;
use Psr\Http\Message\ServerRequestInterface;
use function Hyperf\Collection\collect;
use function Hyperf\Config\config;
trait ColumnConfigTrait
{
#[Inject]
protected QueueService $queueService;
#[Inject]
protected ServerRequestInterface $clientRequest;
/**
* 获取列配置.
* @throws Exception
*/
public function getColumnConfig(): array
{
$user = UserContext::getCurrentUser();
if (! $user) {
return [];
}
$method = Str::snake($this->getCurrentAction());
$this->saveColumnConfig($user, $method, $this->clientRequest->getParsedBody());
$this->saveTableConfig($user);
// 从数据库中查询对应的列配置详情
$fields = ColumnConfig::query()
->where(['creator_id' => $user['id'], 'method' => $method])
->select(['prop', 'label', 'sortable', 'sort', 'width', 'hide', 'fix', 'filter', 'is_search', 'search_type', 'condition'])
->orderBy('sort')
->get();
return $fields->isNotEmpty() ? $fields->toArray() : $this->getDefaultConfig($method);
}
/**
* 获取表格配置.
* @throws Exception
*/
public function getTableConfig(): array
{
$user = UserContext::getCurrentUser();
if (! $user) {
return [];
}
$config = TableConfig::query()
->where(['creator_id' => $user['id'], 'method' => Str::snake($this->getCurrentAction())])
->select(['size', 'config'])
->first();
return $config ? $config->toArray() : [];
}
/**
* 获取默认配置.
*/
private function getDefaultConfig(string $method): array
{
$configList = config('column_config.' . $method);
if (empty($configList)) {
return [];
}
return collect($configList)->map(function ($item, $key) {
$item['sort'] = $key + 1;
return $item;
})->toArray();
}
/**
* 获取当前请求所匹配的方法.
* @throws Exception
*/
private function getCurrentAction(): string
{
$dispatched = $this->clientRequest->getAttribute(Dispatched::class);
return $dispatched->handler->callback[1] ?? '';
}
/**
* 保存列配置.
* @throws Exception
*/
private function saveColumnConfig(array $user, string $method, array $params): void
{
$this->queueService->make(compact('user', 'method', 'params'))->saveColumnConfig();
}
/**
* @throws Exception
*/
private function saveTableConfig(array $user): void
{
$body = $this->clientRequest->getParsedBody();
if (empty($body['table_config'])) {
return;
}
$query = TableConfig::query();
$params = $body['table_config'];
$method = Str::snake($this->getCurrentAction());
$query->updateOrCreate(
['creator_id' => $user['id'], 'method' => $method],
[
'method' => $method,
'size' => $params['size'] ?? 'medium',
'config' => $params['config'] ?? [],
],
);
}
}