Files
webman-buildadmin/app/admin/controller/game/RewardConfig.php

165 lines
5.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\admin\controller\game;
use Throwable;
use app\common\controller\Backend;
use app\common\library\GameRewardConfigTemplate;
use app\common\model\GameRewardConfig;
use app\common\validate\GameRewardConfig as GameRewardConfigValidate;
use support\think\Db;
use support\Response;
use Webman\Http\Request as WebmanRequest;
/**
* 游戏奖励配置(渠道表单页,路由 /admin/game/rewardConfig
* 约定game_channel_id = 0 为超管维护的「全渠道默认模板」,新建渠道时优先从此行复制到新渠道
*/
class RewardConfig extends Backend
{
/** 默认模板渠道主键(非真实渠道,仅存库一行) */
private const DEFAULT_TEMPLATE_CHANNEL_ID = 0;
public function index(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
[$channelId, $err] = $this->resolveTargetChannelId($request, false);
if ($err !== null) {
return $err;
}
$row = GameRewardConfig::where('game_channel_id', $channelId)->find();
if ($row) {
return $this->success('', [
'row' => $row->toArray(),
]);
}
$defaults = GameRewardConfigTemplate::getDefaultJsonColumns();
return $this->success('', [
'row' => [
'id' => null,
'game_channel_id' => $channelId,
'tier_reward_form' => $defaults['tier_reward_form'],
'bigwin_form' => $defaults['bigwin_form'],
],
]);
}
public function save(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$data = $request->post();
if (!$data) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
[$channelId, $err] = $this->resolveTargetChannelId($request, true);
if ($err !== null) {
return $err;
}
if (!$this->channelExists($channelId)) {
return $this->error(__('Record not found'));
}
$tier = $data['tier_reward_form'] ?? null;
$big = $data['bigwin_form'] ?? null;
if (!is_string($tier) || !is_string($big)) {
return $this->error(__('Parameter error'));
}
try {
$validate = new GameRewardConfigValidate();
$validate->scene('channel_form')->check([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
$existing = GameRewardConfig::where('game_channel_id', $channelId)->find();
try {
if ($existing) {
$existing->save([
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
} else {
$m = new GameRewardConfig();
$m->save([
'game_channel_id' => $channelId,
'tier_reward_form' => $tier,
'bigwin_form' => $big,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
return $this->success(__('Update successful'));
}
/**
* @return array{0: int, 1: Response|null}
*/
private function resolveTargetChannelId(WebmanRequest $request, bool $isPost): array
{
if ($this->auth->isSuperAdmin()) {
$raw = $isPost ? ($request->post('game_channel_id') ?? $request->get('game_channel_id')) : $request->get('game_channel_id');
if ($raw === null || $raw === '') {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, null];
}
if (!is_numeric($raw)) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Parameter error'))];
}
$cid = intval(strval($raw));
if ($cid < 0) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Parameter error'))];
}
if ($cid === self::DEFAULT_TEMPLATE_CHANNEL_ID) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, null];
}
return [$cid, null];
}
$ids = Db::name('game_channel')->where('admin_id', $this->auth->id)->order('id', 'asc')->column('id');
if ($ids === []) {
return [self::DEFAULT_TEMPLATE_CHANNEL_ID, $this->error(__('Record not found'))];
}
return [intval(strval($ids[0])), null];
}
private function channelExists(int $channelId): bool
{
if ($channelId === self::DEFAULT_TEMPLATE_CHANNEL_ID) {
return $this->auth->isSuperAdmin();
}
if ($this->auth->isSuperAdmin()) {
return Db::name('game_channel')->where('id', $channelId)->count() > 0;
}
return Db::name('game_channel')->where('id', $channelId)->where('admin_id', $this->auth->id)->count() > 0;
}
}