feat : config

This commit is contained in:
2024-10-30 17:47:37 +08:00
parent d572ca9539
commit 39b5df9f0a
5 changed files with 185 additions and 3 deletions

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Service\Admin\System\ConfigService;
class ConfigController extends AbstractController
{
public function getConfigModule()
{
return (new ConfigService)->getConfigModule();
}
public function getConfigForm()
{
return (new ConfigService)->getConfigForm();
}
public function updateConfig()
{
return (new ConfigService)->handle();
}
}

View File

@@ -6,13 +6,12 @@ namespace App\Controller\Admin;
use App\Middleware\Admin\JwtAuthMiddleware;
use App\Request\Admin\EmployeeRequest;
use App\Service\Admin\EmployeeService;
use App\Service\Admin\User\EmployeeService;
use Exception;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Validation\Annotation\Scene;
use Symfony\Component\HttpFoundation\JsonResponse;
#[Controller(prefix: "admin/employee")]
#[Middlewares([

73
app/Model/Config.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace App\Model;
use Hyperf\DbConnection\Model\Model;
/**
* @property int $id
* @property string $name
* @property int $pid
* @property string $key
* @property string $value
* @property string $desc
* @property int $input_type
* @property int $sort
* @property string $param
* @property string $create_time
* @property string $update_time
*/
class Config extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'config';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'pid' => 'integer', 'status' => 'integer', 'input_type' => 'integer', 'sort' => 'integer'];
const CREATED_AT = 'create_time';
const UPDATED_AT = 'update_time';
/**
* 获取
* @return array
*/
public function getTopConfigModule(): array
{
$res = $this->where('pid', 0)->get(['id', 'name']);
if (empty($res)) return [];
return $res->toArray();
}
/**
* @param int $pid
* @return array
*/
public function getConfigByPid(int $pid): array
{
$res = $this
->where('pid', $pid)
->orderBy('sort','desc')
->get(['id', 'pid', 'name', 'value','param','desc','input_type','sort','key']);
if (empty($res)) return [];
return $res->toArray();
}
}

View File

@@ -0,0 +1,83 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Admin\System;
use App\Exception\AdminException;
use App\Model\Config;
use App\Service\Admin\BaseService;
use Hyperf\Di\Annotation\Inject;
class ConfigService extends BaseService
{
/**
* 注入设置模型
* @var Config
*/
#[Inject]
protected Config $configModel;
/**
* 修改配置
* @return array
*/
public function handle(): array
{
$editKey = $this->request->input('key');
$editValue = $this->request->input('value');
switch ($editKey)
{
case 'test':
default:
break;
}
$res = (new Config)->where('key', $editKey)->update(['value' => $editValue]);
if (!$res) throw new AdminException('配置更新失败');
return $this->return->success();
}
/**
* 获取配置模块
* @return array
*/
public function getConfigModule(): array
{
$res = $this->configModel->getTopConfigModule();
return $this->return->success('success', ['list' => $res]);
}
/**
* 获取数据
* @return array
*/
public function getConfigForm(): array
{
$list = $this->configModel->getConfigByPid($this->request->input('pid'));
foreach ($list as &$item) {
switch ($item['type']) {
case 2:
$data = explode('|', $item['value']);
$item['min'] = (int)$data[0];
$item['max'] = (int)$data[1];
break;
default:
break;
}
}
return $this->return->success('success', ['list' => $list]);
}
}

View File

@@ -8,13 +8,14 @@
declare(strict_types=1);
namespace App\Service\Admin;
namespace App\Service\Admin\User;
use App\Constants\Admin\UserCode;
use App\Exception\AdminException;
use App\Extend\StringUtil;
use App\Lib\Crypto\CryptoFactory;
use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use Exception;
use Hyperf\Di\Annotation\Inject;
use function Hyperf\Config\config;