feat: 产品相关接口
This commit is contained in:
115
app/admin/validate/v1/ProductValidate.php
Normal file
115
app/admin/validate/v1/ProductValidate.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class ProductValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'category_id' => 'integer',
|
||||
'spu' => 'require|max:255',
|
||||
'name' => 'require|max:125',
|
||||
'short_name' => 'max:64',
|
||||
'cover_image' => 'max:255',
|
||||
'desc' => 'max:255',
|
||||
'video_img' => 'max:255',
|
||||
'video_url' => 'max:255',
|
||||
'is_sale' => 'in:0,1',
|
||||
'is_new' => 'in:0,1',
|
||||
'is_hot' => 'in:0,1',
|
||||
'sort' => 'integer',
|
||||
'detail' => 'max:65535',
|
||||
'status' => 'in:-1,1',
|
||||
'seo_title' => 'max:255',
|
||||
'seo_keywords' => 'max:255',
|
||||
'seo_desc' => 'max:255',
|
||||
'created_at' => 'checkFormatDatetimeRange'
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'category_id.integer' => '分类ID必须为整数',
|
||||
'spu.require' => 'spu不能为空',
|
||||
'spu.max' => 'spu不能超过255个字符',
|
||||
'name.require' => '名称不能为空',
|
||||
'name.max' => '名称不能超过125个字符',
|
||||
'short_name.max' => '短标题不能超过64个字符',
|
||||
'cover_image.max' => '封面图不能超过255个字符',
|
||||
'desc.max' => '描述不能超过255个字符',
|
||||
'video_img.max' => '视频封面图不能超过255个字符',
|
||||
'video_url.max' => '视频地址不能超过255个字符',
|
||||
'is_sale.in' => '上架状态值错误',
|
||||
'is_new.in' => '新品状态值错误',
|
||||
'is_hot.in' => '热门状态值错误',
|
||||
'sort.integer' => '排序值类型错误',
|
||||
'detail.max' => '详情不能超过65535个字符',
|
||||
'status.in' => '状态值错误',
|
||||
'seo_title.max' => 'seo标题不能超过255个字符',
|
||||
'seo_keywords.max' => 'seo关键字不能超过255个字符',
|
||||
'seo_desc.max' => 'seo描述不能超过255个字符',
|
||||
'created_at.checkFormatDatetimeRange' => '添加时间格式错误'
|
||||
];
|
||||
|
||||
/**
|
||||
* 分页列表验证场景
|
||||
*/
|
||||
public function sceneIndex()
|
||||
{
|
||||
return $this->only(['created_id', 'is_sale', 'created_at']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据更新验证场景
|
||||
*/
|
||||
public function sceneUpdate()
|
||||
{
|
||||
return $this->only([
|
||||
'category_id',
|
||||
'spu',
|
||||
'name',
|
||||
'short_name',
|
||||
'cover_image',
|
||||
'desc',
|
||||
'video_img',
|
||||
'video_url',
|
||||
'is_sale',
|
||||
'is_new',
|
||||
'is_hot',
|
||||
'sort',
|
||||
'detail',
|
||||
'status',
|
||||
'seo_title',
|
||||
'seo_keywords',
|
||||
'seo_desc',
|
||||
]);
|
||||
}
|
||||
|
||||
protected function checkFormatDatetimeRange($value, $rule, $data)
|
||||
{
|
||||
$val = explode(',', $value);
|
||||
if (empty($val)) return '时间错误';
|
||||
$v1 = strtotime($val[0]);
|
||||
if (!$v1) return '时间错误';
|
||||
if (count($val) == 2) {
|
||||
$v2 = strtotime($val[1]);
|
||||
if (!$v2) {
|
||||
return '时间错误';
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user