12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- <?php
- namespace app\api\validate;
- use think\Validate;
- /**
- * 身体档案验证器
- */
- class BodyProfile extends Validate
- {
- // 验证规则
- protected $rule = [
- 'profile_name' => 'require|length:1,100',
- 'gender' => 'require|in:1,2',
- 'age' => 'require|integer|between:1,120',
- 'height' => 'require|float|between:50,250',
- 'weight' => 'require|float|between:10,300',
- 'is_own' => 'in:0,1',
- 'relation' => 'length:0,50',
- 'profile_photo' => 'url',
- 'body_photos' => 'array|checkBodyPhotos',
- ];
- // 验证消息
- protected $message = [
- 'profile_name.require' => '档案名称不能为空',
- 'profile_name.length' => '档案名称长度不能超过100个字符',
- 'gender.require' => '请选择性别',
- 'gender.in' => '性别参数错误',
- 'age.require' => '年龄不能为空',
- 'age.integer' => '年龄必须是整数',
- 'age.between' => '年龄必须在1-120岁之间',
- 'height.require' => '身高不能为空',
- 'height.float' => '身高必须是数字',
- 'height.between' => '身高必须在50-250厘米之间',
- 'weight.require' => '体重不能为空',
- 'weight.float' => '体重必须是数字',
- 'weight.between' => '体重必须在10-300公斤之间',
- 'is_own.in' => '档案类型参数错误',
- 'relation.length' => '关系描述不能超过50个字符',
- 'profile_photo.url' => '档案照片必须是有效的URL',
- 'body_photos.array' => '身体照片必须是数组格式',
- 'body_photos.checkBodyPhotos' => '身体照片格式错误,必须包含front、side、back三个角度的照片',
- ];
- // 验证场景
- protected $scene = [
- 'create' => ['profile_name', 'gender', 'age', 'height', 'weight', 'is_own', 'relation', 'profile_photo', 'body_photos'],
- 'update' => ['profile_name', 'age', 'height', 'weight', 'relation', 'profile_photo', 'body_photos'],
- ];
- /**
- * 验证身体照片格式
- * @param mixed $value
- * @param string $rule
- * @param array $data
- * @return bool
- */
- protected function checkBodyPhotos($value, $rule, $data)
- {
- if (!is_array($value)) {
- return false;
- }
- // 必须包含的照片角度
- $requiredAngles = ['front', 'side', 'back'];
-
- foreach ($requiredAngles as $angle) {
- if (!isset($value[$angle]) || empty($value[$angle])) {
- return false;
- }
-
- // 验证URL格式
- if (!filter_var($value[$angle], FILTER_VALIDATE_URL)) {
- return false;
- }
- }
- return true;
- }
- }
|