BaseValidate.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. declare(strict_types=1);
  15. namespace app\common\validate;
  16. use app\common\service\JsonService;
  17. use think\Validate;
  18. class BaseValidate extends Validate
  19. {
  20. public string $method = 'GET';
  21. /**
  22. * @notes 设置请求方式
  23. * @author 令狐冲
  24. * @date 2021/12/27 14:13
  25. */
  26. public function post()
  27. {
  28. if (!$this->request->isPost()) {
  29. JsonService::throw('请求方式错误,请使用post请求方式');
  30. }
  31. $this->method = 'POST';
  32. return $this;
  33. }
  34. /**
  35. * @notes 设置请求方式
  36. * @author 令狐冲
  37. * @date 2021/12/27 14:13
  38. */
  39. public function get()
  40. {
  41. if (!$this->request->isGet()) {
  42. JsonService::throw('请求方式错误,请使用get请求方式');
  43. }
  44. return $this;
  45. }
  46. /**
  47. * @notes 切面验证接收到的参数
  48. * @param null $scene 场景验证
  49. * @param array $validateData 验证参数,可追加和覆盖掉接收的参数
  50. * @return array
  51. * @author 令狐冲
  52. * @date 2021/12/27 14:13
  53. */
  54. public function goCheck($scene = null, array $validateData = []): array
  55. {
  56. //接收参数
  57. if ($this->method == 'GET') {
  58. $params = request()->get();
  59. } else {
  60. $params = request()->post();
  61. }
  62. //合并验证参数
  63. $params = array_merge($params, $validateData);
  64. //场景
  65. if ($scene) {
  66. $result = $this->scene($scene)->check($params);
  67. } else {
  68. $result = $this->check($params);
  69. }
  70. if (!$result) {
  71. $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
  72. JsonService::throw($exception);
  73. }
  74. // 3.成功返回数据
  75. return $params;
  76. }
  77. }