You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.6 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. <?php
  2. namespace App\Http\Requests;
  3. class TokenRequest extends BaseRequest
  4. {
  5. //总的验证规则
  6. protected $rules = [
  7. 'id' => 'required|integer',
  8. 'token' => 'required|max:128',
  9. 'type' => 'required|integer|between:[0,6]',
  10. 'uid' => 'required|integer',
  11. 'created_at' => 'required|date_format:"Y-m-d H:i:s"',
  12. 'expired_at' => 'required|date_format:"Y-m-d H:i:s"',
  13. ];
  14. //总的自定义错误信息
  15. protected $messages = [
  16. 'id.required' => 'ID必填',
  17. 'id.integer' => 'ID必须为数字',
  18. 'token.required' => 'token必填',
  19. 'token.max' => 'token不能超过128个字符',
  20. 'type.required' => 'token类型必填',
  21. 'type.integer' => 'token类型必须是数字',
  22. 'type.between' => '0:app, 1:admin, 2:weixin, 3:wap, 4: pc',
  23. 'uid.required' => 'UID必填',
  24. 'uid.integer' => 'UID必须为数字',
  25. 'created_at.required' => '添加时间必填',
  26. 'created_at.regex' => '添加时间格式不正确,Y-m-d H:i:s',
  27. 'expired_at.required' => '过期时间必填',
  28. 'expired_at.regex' => '过期时间格式不正确,Y-m-d H:i:s',
  29. ];
  30. //场景验证规则
  31. protected $scene = [
  32. 'add' => ['token', 'type', 'uid', 'created_at', 'expired_at'],
  33. 'edit' => ['token', 'type', 'uid', 'created_at', 'expired_at'],
  34. 'del' => ['id'],
  35. ];
  36. /**
  37. * Determine if the user is authorized to make this request.
  38. *
  39. * @return bool
  40. */
  41. public function authorize()
  42. {
  43. return true; //修改为true
  44. }
  45. /**
  46. * Get the validation rules that apply to the request.
  47. *
  48. * @return array
  49. */
  50. public function rules()
  51. {
  52. return $this->rules;
  53. }
  54. /**
  55. * 获取被定义验证规则的错误消息.
  56. *
  57. * @return array
  58. */
  59. public function messages()
  60. {
  61. return $this->messages;
  62. }
  63. //获取场景验证规则
  64. public function getSceneRules($name, $fields = null)
  65. {
  66. $res = array();
  67. if(!isset($this->scene[$name]))
  68. {
  69. return false;
  70. }
  71. $scene = $this->scene[$name];
  72. if($fields != null && is_array($fields))
  73. {
  74. $scene = $fields;
  75. }
  76. foreach($scene as $k=>$v)
  77. {
  78. if(isset($this->rules[$v])){$res[$v] = $this->rules[$v];}
  79. }
  80. return $res;
  81. }
  82. //获取场景验证规则自定义错误信息
  83. public function getSceneRulesMessages()
  84. {
  85. return $this->messages;
  86. }
  87. }