Inspection.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace app\api\validate;
  3. use think\Validate;
  4. class Inspection extends Validate
  5. {
  6. /**
  7. * 验证规则
  8. */
  9. protected $rule = [
  10. // 基础信息
  11. 'id' => 'integer|gt:0',
  12. 'name' => 'require|length:1,50|chsAlpha',
  13. 'phone' => 'require|mobile|checkPhoneUnique',
  14. 'province_adcode' => 'require|max:50',
  15. 'city_adcode' => 'require|max:50',
  16. 'district_adcode' => 'require|max:50',
  17. 'address' => 'require|max:255',
  18. 'id_card_front' => 'require|max:255',
  19. 'id_card_back' => 'require|max:255',
  20. ];
  21. /**
  22. * 提示消息
  23. */
  24. protected $message = [
  25. // 基础信息
  26. // 'id.require' => '申请ID不能为空',
  27. 'id.integer' => '申请ID必须是整数',
  28. 'id.gt' => '申请ID必须大于0',
  29. 'name.require' => '姓名不能为空',
  30. 'name.length' => '姓名长度必须在1-50个字符之间',
  31. 'name.chsAlpha' => '姓名只能包含中文、字母',
  32. 'phone.require' => '联系电话不能为空',
  33. 'phone.mobile' => '联系电话格式不正确',
  34. 'phone.checkPhoneUnique' => '该手机号已被申请',
  35. 'province_adcode.require' => '省级编码不能为空',
  36. 'province_adcode.max' => '省级编码长度不能超过50个字符',
  37. 'city_adcode.require' => '市级编码不能为空',
  38. 'city_adcode.max' => '市级编码长度不能超过50个字符',
  39. 'district_adcode.require' => '区县编码不能为空',
  40. 'district_adcode.max' => '区县编码长度不能超过50个字符',
  41. 'address.require' => '详细地址不能为空',
  42. 'address.max' => '详细地址长度不能超过255个字符',
  43. 'id_card_front.require' => '身份证正面照片不能为空',
  44. 'id_card_front.max' => '身份证正面照片路径长度不能超过255个字符',
  45. 'id_card_back.require' => '身份证反面照片不能为空',
  46. 'id_card_back.max' => '身份证反面照片路径长度不能超过255个字符',
  47. ];
  48. /**
  49. * 验证场景
  50. */
  51. protected $scene = [
  52. // 创建申请
  53. 'create' => ['name', 'phone', 'province_adcode', 'city_adcode', 'district_adcode','address', 'id_card_front', 'id_card_back'],
  54. // 查看详情
  55. 'detail' => ['id'],
  56. // 修改申请
  57. 'update' => ['id', 'name', 'phone', 'province_adcode', 'city_adcode', 'district_adcode','address', 'id_card_front', 'id_card_back'],
  58. ];
  59. /**
  60. * 自定义验证方法 - 验证手机号是否已申请
  61. */
  62. protected function checkPhoneUnique($value, $rule, $data = [])
  63. {
  64. $model = new \app\common\model\inspection\InspectionApplication();
  65. $where = ['phone' => $value];
  66. // 如果是修改,排除当前记录
  67. if (isset($data['id']) && $data['id'] > 0) {
  68. $where['id'] = ['neq', $data['id']];
  69. }
  70. $exists = $model->where($where)->find();
  71. if ($exists) {
  72. return '该手机号已提交过申请';
  73. }
  74. return true;
  75. }
  76. }