130 lines
4.6 KiB
PHP
130 lines
4.6 KiB
PHP
<?php
|
||
|
||
namespace app\admin\controller\game;
|
||
|
||
use app\common\controller\Backend;
|
||
use app\common\library\game\ZiHuaDictionary as ZiHuaDictionaryLib;
|
||
use app\common\service\GameHotDataCoordinator;
|
||
use app\common\service\GameHotDataLock;
|
||
use InvalidArgumentException;
|
||
use support\think\Db;
|
||
use support\Response;
|
||
use Throwable;
|
||
use Webman\Http\Request as WebmanRequest;
|
||
|
||
/**
|
||
* 36 字花字典独立编辑(仅 game_config.zi_hua_36_dictionary)
|
||
*/
|
||
class ZiHuaDictionary extends Backend
|
||
{
|
||
protected bool $modelValidate = false;
|
||
protected array $noNeedPermission = ['index', 'save'];
|
||
|
||
private function hasNodePermission(WebmanRequest $request, string $action): bool
|
||
{
|
||
if (!$this->auth) {
|
||
return false;
|
||
}
|
||
$controllerPath = get_controller_path($request);
|
||
if (!$controllerPath) {
|
||
return false;
|
||
}
|
||
return $this->auth->check($controllerPath . '/' . $action);
|
||
}
|
||
|
||
protected function initController(WebmanRequest $request): ?Response
|
||
{
|
||
return null;
|
||
}
|
||
|
||
/**
|
||
* GET:读取 36 条;POST 不支持
|
||
*/
|
||
public function index(WebmanRequest $request): Response
|
||
{
|
||
$response = $this->initializeBackend($request);
|
||
if ($response !== null) {
|
||
return $response;
|
||
}
|
||
if (!$this->hasNodePermission($request, 'index')) {
|
||
return $this->error(__('You have no permission'), [], 401);
|
||
}
|
||
if ($request->method() !== 'GET') {
|
||
return $this->error(__('Parameter error'));
|
||
}
|
||
$row = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
|
||
$items = ZiHuaDictionaryLib::parseFromConfigValue($row['config_value'] ?? null);
|
||
return $this->success('', [
|
||
'items' => $items,
|
||
'categories' => ZiHuaDictionaryLib::CATEGORIES,
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 保存 JSON 数组(仅 value_type=json)
|
||
*/
|
||
public function save(WebmanRequest $request): Response
|
||
{
|
||
$response = $this->initializeBackend($request);
|
||
if ($response !== null) {
|
||
return $response;
|
||
}
|
||
if (!$this->hasNodePermission($request, 'save')) {
|
||
return $this->error(__('You have no permission'), [], 401);
|
||
}
|
||
if ($request->method() !== 'POST') {
|
||
return $this->error(__('Parameter error'));
|
||
}
|
||
$payload = $request->post();
|
||
if (!is_array($payload)) {
|
||
return $this->error(__('Parameter %s can not be empty', ['']));
|
||
}
|
||
$items = $payload['items'] ?? null;
|
||
if (!is_array($items)) {
|
||
return $this->error('items 必须为数组');
|
||
}
|
||
try {
|
||
$clean = ZiHuaDictionaryLib::prepareItemsForSave($items);
|
||
$json = ZiHuaDictionaryLib::encodeForDb($clean);
|
||
} catch (InvalidArgumentException $e) {
|
||
return $this->error($e->getMessage());
|
||
}
|
||
|
||
$now = time();
|
||
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(ZiHuaDictionaryLib::CONFIG_KEY);
|
||
$lock = GameHotDataLock::tryAcquire(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey);
|
||
if (!$lock['acquired']) {
|
||
return $this->error('该配置正在被其他操作占用,请稍后再试');
|
||
}
|
||
try {
|
||
try {
|
||
$exists = Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->find();
|
||
if ($exists) {
|
||
Db::name('game_config')->where('config_key', ZiHuaDictionaryLib::CONFIG_KEY)->update([
|
||
'config_value' => $json,
|
||
'value_type' => 'json',
|
||
'update_time' => $now,
|
||
]);
|
||
} else {
|
||
Db::name('game_config')->insert([
|
||
'config_key' => ZiHuaDictionaryLib::CONFIG_KEY,
|
||
'config_value' => $json,
|
||
'value_type' => 'json',
|
||
'remark' => '36字花字典 JSON 数组(独立表单维护)',
|
||
'create_time' => $now,
|
||
'update_time' => $now,
|
||
]);
|
||
}
|
||
} catch (Throwable $e) {
|
||
return $this->error($e->getMessage());
|
||
}
|
||
|
||
GameHotDataCoordinator::afterGameConfigKeyCommitted(ZiHuaDictionaryLib::CONFIG_KEY);
|
||
|
||
return $this->success(__('Saved successfully'));
|
||
} finally {
|
||
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
|
||
}
|
||
}
|
||
}
|