创建模型验证器,验证数据正确性

This commit is contained in:
2026-04-16 14:52:13 +08:00
parent 7aaa66d2dc
commit 15fdd3ba57
22 changed files with 324 additions and 9 deletions

View File

@@ -17,4 +17,24 @@ class OperationNotice extends Model
'notice_type' => 'integer',
'status' => 'integer',
];
public function setPublishAtAttr($value)
{
if ($value === null || $value === '') {
return 0;
}
if (is_int($value)) {
return $value;
}
if (is_string($value)) {
if (ctype_digit($value)) {
return (int) $value;
}
$ts = strtotime($value);
if ($ts !== false) {
return $ts;
}
}
return 0;
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class AgentCommissionRecord extends Validate
{
protected $failException = true;
protected $rule = [
'settlement_period_id' => 'require|integer|gt:0',
'channel_id' => 'require|integer|gt:0',
'admin_id' => 'require|integer|gt:0',
'commission_rate' => 'require|float|egt:0',
'calc_base_amount' => 'require|float',
'commission_amount' => 'require|float',
'status' => 'in:0,1,2',
'remark' => 'max:255',
];
protected $scene = [
'add' => ['settlement_period_id', 'channel_id', 'admin_id', 'commission_rate', 'calc_base_amount', 'commission_amount', 'status', 'remark'],
'edit' => ['commission_rate', 'calc_base_amount', 'commission_amount', 'status', 'remark'],
];
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class AgentSettlementPeriod extends Validate
{
protected $failException = true;
protected $rule = [
'settlement_no' => 'require|max:64|unique:agent_settlement_period',
'period_start_at' => 'require|integer|egt:0',
'period_end_at' => 'require|integer|egt:0',
'status' => 'in:0,1,2',
'remark' => 'max:255',
];
protected $scene = [
'add' => ['settlement_no', 'period_start_at', 'period_end_at', 'status', 'remark'],
'edit' => ['period_start_at', 'period_end_at', 'status', 'remark'],
];
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class Channel extends Validate
{
protected $failException = true;
protected $rule = [
'code' => 'require|max:255|unique:channel',
'name' => 'require|max:255',
'agent_mode' => 'require|in:turnover,affiliate',
'status' => 'in:0,1',
'admin_id' => 'require|integer|gt:0',
'remark' => 'max:255',
];
protected $scene = [
'add' => ['code', 'name', 'agent_mode', 'status', 'admin_id', 'remark'],
'edit' => ['name', 'agent_mode', 'status', 'admin_id', 'remark'],
];
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class DepositOrder extends Validate
{
protected $failException = true;
protected $rule = [
'order_no' => 'require|max:64|unique:deposit_order',
'user_id' => 'require|integer|gt:0',
'status' => 'require|in:0,1,2,3',
];
protected $scene = [
'add' => ['order_no', 'user_id', 'status'],
'edit' => ['status'],
];
}

View File

@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class GameConfig extends Validate
{
protected $failException = true;
protected $rule = [
'config_key' => 'require|max:64|unique:game_config',
'value_type' => 'require|in:string,int,decimal,json',
'remark' => 'max:255',
];
protected $scene = [
'add' => ['config_key', 'value_type', 'remark'],
'edit' => ['value_type', 'remark'],
];
}

View File

@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class GamePeriod extends Validate
{
protected $failException = true;
protected $rule = [
'period_no' => 'require|max:64|unique:game_period',
'period_start_at' => 'integer',
'status' => 'require|in:0,1,2,3,4,5',
'draw_mode' => 'in:0,1',
'preset_number' => 'between:1,36',
'result_number' => 'between:1,36',
];
protected $scene = [
'add' => ['period_no', 'period_start_at', 'status', 'draw_mode', 'preset_number', 'result_number'],
'edit' => ['period_start_at', 'status', 'draw_mode', 'preset_number', 'result_number'],
];
}

View File

@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class OperationNotice extends Validate
{
protected $failException = true;
protected $rule = [
'title' => 'require|max:255',
'notice_type' => 'require|in:0,1',
'status' => 'require|in:0,1',
'publish_at' => 'require|checkPublishAt',
];
protected $scene = [
'add' => ['title', 'notice_type', 'status', 'publish_at'],
'edit' => ['title', 'notice_type', 'status', 'publish_at'],
];
protected function checkPublishAt($value): bool
{
if (is_int($value)) {
return $value >= 0;
}
if (is_string($value)) {
if ($value === '') {
return false;
}
if (ctype_digit($value)) {
return true;
}
return strtotime($value) !== false;
}
return false;
}
}

View File

@@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class User extends Validate
{
protected $failException = true;
protected $rule = [
'username' => 'require|max:255',
'password' => 'require|min:6|max:255',
'phone' => 'max:32',
'email' => 'email',
'channel_id' => 'require|integer|gt:0',
'status' => 'in:0,1',
];
protected $scene = [
'add' => ['username', 'password', 'phone', 'email', 'channel_id', 'status'],
'edit' => ['username', 'password', 'phone', 'email', 'channel_id', 'status'],
];
public function sceneEdit(): static
{
return $this->only(['username', 'password', 'phone', 'email', 'channel_id', 'status'])
->remove('password', 'require');
}
}

View File

@@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class UserNoticeRead extends Validate
{
protected $failException = true;
protected $rule = [
'user_id' => 'require|integer|gt:0',
'notice_id' => 'require|integer|gt:0',
'confirmed' => 'in:0,1',
'read_at' => 'integer',
];
protected $scene = [
'add' => ['user_id', 'notice_id', 'confirmed', 'read_at'],
'edit' => ['user_id', 'notice_id', 'confirmed', 'read_at'],
];
}

View File

@@ -0,0 +1,25 @@
<?php
declare(strict_types=1);
namespace app\common\validate;
use think\Validate;
class WithdrawOrder extends Validate
{
protected $failException = true;
protected $rule = [
'order_no' => 'require|max:64|unique:withdraw_order',
'user_id' => 'require|integer|gt:0',
'amount' => 'float|egt:0',
'status' => 'require|in:0,1,2,3',
'remark' => 'max:255',
];
protected $scene = [
'add' => ['order_no', 'user_id', 'amount', 'status', 'remark'],
'edit' => ['amount', 'status', 'remark'],
];
}