ConfigController.php
3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace App\Http\Container\AdminSection\Controllers\System;
use App\Helpers\OperationLog;
use App\Http\Container\AdminSection\Requests\System\ConfigCreateRequest;
use App\Http\Container\AdminSection\Requests\System\ConfigUpdateRequest;
use App\Http\Request\BooleanStatusRequest;
use App\Models\SystemConfig;
use App\Models\User;
use App\Support\Controller;
use Hikoon\LaravelApi\Facades\Response;
use Hikoon\LaravelApi\Support\ApiCode;
use Illuminate\Http\Request;
class ConfigController extends Controller
{
/**
* @param \Illuminate\Http\Request $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function index(Request $request): Response
{
$pageSize = $request->get('pageSize', 20);
$filter = $request->mergeIfMissing(['parent_id' => 0])->except('page', 'pageSize', 'fetchType');
$fetchType = $request->get('fetchType', 'page');
$build = SystemConfig::filter($filter);
return match ($fetchType) {
'all' => $this->successWithData($build->get()),
default => $this->successWithData($build->paginate($pageSize))
};
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\ConfigCreateRequest $request
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function store(ConfigCreateRequest $request): Response
{
$config = SystemConfig::query()->create($request->validated());
OperationLog::admin()->createAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));
return $this->successWithData($config, ApiCode::CREATE_SUCCESS);
}
/**
* @param \App\Models\SystemConfig $config
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function show(SystemConfig $config): Response
{
$config->load('children');
return $this->successWithData($config);
}
/**
* @param \App\Http\Container\AdminSection\Requests\System\ConfigUpdateRequest $request
* @param \App\Models\SystemConfig $config
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function update(ConfigUpdateRequest $request, SystemConfig $config): Response
{
//如果是修改Demo标签
if ($config->getAttribute('identifier') === 'CtGly-jsLjAu5yJvLrN7L') {
$currentIds = array_filter(explode(',', $config->getAttribute('content')));
$nextIds = array_filter(explode(',', $request->get('content')));
$diffIds = array_filter($currentIds, static fn($value) => !in_array($value, $nextIds, false));
if (count($diffIds) > 0) {
User::query()->where('demo_type', 0)
->whereHas('authTags', fn($query) => $query->whereIn('tag_id', $diffIds))
->whereDoesntHave('authTags', fn($query) => $query->whereIn('tag_id', $nextIds))
->update(['demo_type' => 1]);
}
}
$config->update($request->validated());
OperationLog::admin()->updateAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));
return $this->successWithData($config, ApiCode::UPDATE_SUCCESS);
}
/**
* @param \App\Http\Request\BooleanStatusRequest $request
* @param \App\Models\SystemConfig $config
* @return \Hikoon\LaravelApi\Facades\Response
*/
public function changeStatus(BooleanStatusRequest $request, SystemConfig $config): Response
{
$config->update($request->validated());
OperationLog::admin()->statusAction()->subject($config)->content('配置字典《%s》', $config->getAttribute('name'));
return $this->success(ApiCode::UPDATE_SUCCESS);
}
}