[游戏管理]游戏对局
This commit is contained in:
112
app/admin/controller/game/Period.php
Normal file
112
app/admin/controller/game/Period.php
Normal file
@@ -0,0 +1,112 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\game;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\service\GamePeriodService;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 游戏对局(期号)
|
||||
*/
|
||||
class Period extends Backend
|
||||
{
|
||||
protected ?object $model = null;
|
||||
|
||||
protected string|array $preExcludeFields = ['id', 'create_time', 'update_time'];
|
||||
|
||||
protected string|array $quickSearchField = ['id', 'period_no'];
|
||||
|
||||
protected string|array $defaultSortField = ['id' => 'desc'];
|
||||
|
||||
protected string|array $orderGuarantee = ['id' => 'desc'];
|
||||
|
||||
protected bool $modelValidate = false;
|
||||
|
||||
protected function initController(WebmanRequest $request): ?Response
|
||||
{
|
||||
$this->model = new \app\common\model\GamePeriod();
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取 / 保存:自动创建下一期、手动创建下一期 开关(存 game_config)
|
||||
*/
|
||||
public function periodSettings(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$method = $request->method();
|
||||
if ($method === 'GET') {
|
||||
return $this->success('', GamePeriodService::getPeriodSettings());
|
||||
}
|
||||
if ($method === 'POST') {
|
||||
$data = $request->post();
|
||||
if (!is_array($data)) {
|
||||
return $this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
try {
|
||||
GamePeriodService::savePeriodSettings($data);
|
||||
} catch (Throwable $e) {
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
return $this->success(__('Saved successfully'));
|
||||
}
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动创建下一期(受开关与「存在进行中期号」约束)
|
||||
*/
|
||||
public function createNextManual(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$result = GamePeriodService::createNextPeriodForManual();
|
||||
if ($result['ok']) {
|
||||
return $this->success($result['msg'], ['period_no' => $result['period_no'] ?? '']);
|
||||
}
|
||||
return $this->error($result['msg']);
|
||||
}
|
||||
|
||||
protected function _add(): Response
|
||||
{
|
||||
if ($this->request && $this->request->method() === 'POST') {
|
||||
$data = $this->request->post();
|
||||
if (!is_array($data)) {
|
||||
return $this->error(__('Parameter %s can not be empty', ['']));
|
||||
}
|
||||
$data = $this->applyInputFilter($data);
|
||||
$data = $this->excludeFields($data);
|
||||
if (!isset($data['period_start_at']) || $data['period_start_at'] === '' || $data['period_start_at'] === null) {
|
||||
$data['period_start_at'] = time();
|
||||
}
|
||||
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
|
||||
$data[$this->dataLimitField] = $this->auth->id;
|
||||
}
|
||||
$result = false;
|
||||
$this->model->startTrans();
|
||||
try {
|
||||
$result = $this->model->save($data);
|
||||
$this->model->commit();
|
||||
} catch (Throwable $e) {
|
||||
$this->model->rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
if ($result !== false) {
|
||||
return $this->success(__('Added successfully'));
|
||||
}
|
||||
return $this->error(__('No rows were added'));
|
||||
}
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
}
|
||||
43
app/common/model/GamePeriod.php
Normal file
43
app/common/model/GamePeriod.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace app\common\model;
|
||||
|
||||
use support\think\Model;
|
||||
|
||||
class GamePeriod extends Model
|
||||
{
|
||||
protected $name = 'game_period';
|
||||
|
||||
protected $autoWriteTimestamp = true;
|
||||
|
||||
protected $type = [
|
||||
'create_time' => 'integer',
|
||||
'update_time' => 'integer',
|
||||
'period_start_at' => 'integer',
|
||||
'status' => 'integer',
|
||||
'draw_mode' => 'integer',
|
||||
'preset_number' => 'integer',
|
||||
'result_number' => 'integer',
|
||||
];
|
||||
|
||||
/**
|
||||
* 后台表单 datetime 可能提交字符串,入库前转为 Unix 时间戳
|
||||
* ThinkPHP 修改器签名为 (mixed $value, array $data)
|
||||
*/
|
||||
public function setPeriodStartAtAttr($value, $data = [])
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return 0;
|
||||
}
|
||||
if (is_int($value)) {
|
||||
return $value;
|
||||
}
|
||||
if (is_string($value)) {
|
||||
$t = strtotime($value);
|
||||
if ($t !== false) {
|
||||
return $t;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user