BodyProfile.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. /**
  5. * 身体档案验证器
  6. */
  7. class BodyProfile extends Validate
  8. {
  9. // 验证规则
  10. protected $rule = [
  11. 'profile_name' => 'require|length:1,100',
  12. 'gender' => 'require|in:1,2',
  13. 'age' => 'require|integer|between:1,120',
  14. 'height' => 'require|float|between:50,250',
  15. 'weight' => 'require|float|between:10,300',
  16. 'is_own' => 'in:0,1',
  17. 'relation' => 'length:0,50',
  18. 'profile_photo' => 'url',
  19. 'body_photos' => 'array|checkBodyPhotos',
  20. ];
  21. // 验证消息
  22. protected $message = [
  23. 'profile_name.require' => '档案名称不能为空',
  24. 'profile_name.length' => '档案名称长度不能超过100个字符',
  25. 'gender.require' => '请选择性别',
  26. 'gender.in' => '性别参数错误',
  27. 'age.require' => '年龄不能为空',
  28. 'age.integer' => '年龄必须是整数',
  29. 'age.between' => '年龄必须在1-120岁之间',
  30. 'height.require' => '身高不能为空',
  31. 'height.float' => '身高必须是数字',
  32. 'height.between' => '身高必须在50-250厘米之间',
  33. 'weight.require' => '体重不能为空',
  34. 'weight.float' => '体重必须是数字',
  35. 'weight.between' => '体重必须在10-300公斤之间',
  36. 'is_own.in' => '档案类型参数错误',
  37. 'relation.length' => '关系描述不能超过50个字符',
  38. 'profile_photo.url' => '档案照片必须是有效的URL',
  39. 'body_photos.array' => '身体照片必须是数组格式',
  40. 'body_photos.checkBodyPhotos' => '身体照片格式错误,必须包含front、side、back三个角度的照片',
  41. ];
  42. // 验证场景
  43. protected $scene = [
  44. 'create' => ['profile_name', 'gender', 'age', 'height', 'weight', 'is_own', 'relation', 'profile_photo', 'body_photos'],
  45. 'update' => ['profile_name', 'age', 'height', 'weight', 'relation', 'profile_photo', 'body_photos'],
  46. ];
  47. /**
  48. * 验证身体照片格式
  49. * @param mixed $value
  50. * @param string $rule
  51. * @param array $data
  52. * @return bool
  53. */
  54. protected function checkBodyPhotos($value, $rule, $data)
  55. {
  56. if (!is_array($value)) {
  57. return false;
  58. }
  59. // 必须包含的照片角度
  60. $requiredAngles = ['front', 'side', 'back'];
  61. foreach ($requiredAngles as $angle) {
  62. if (!isset($value[$angle]) || empty($value[$angle])) {
  63. return false;
  64. }
  65. // 验证URL格式
  66. if (!filter_var($value[$angle], FILTER_VALIDATE_URL)) {
  67. return false;
  68. }
  69. }
  70. return true;
  71. }
  72. }