BaseValidate.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\common\validate;
  20. use app\common\service\JsonService;
  21. use think\Validate;
  22. class BaseValidate extends Validate
  23. {
  24. public string $method = 'GET';
  25. /**
  26. * @notes 设置请求方式
  27. * @author 令狐冲
  28. * @date 2021/7/13 00:07
  29. */
  30. public function post()
  31. {
  32. if (!$this->request->isPost()) {
  33. JsonService::throw('请求方式错误,请使用post请求方式');
  34. }
  35. $this->method = 'POST';
  36. return $this;
  37. }
  38. /**
  39. * @notes 设置请求方式
  40. * @author 令狐冲
  41. * @date 2021/7/13 00:07
  42. */
  43. public function get()
  44. {
  45. if (!$this->request->isGet()) {
  46. JsonService::throw('请求方式错误,请使用get请求方式');
  47. }
  48. return $this;
  49. }
  50. /**
  51. * @notes 切面验证接收到的参数
  52. * @param null $scene 场景验证
  53. * @param array $validate_data 验证参数,可追加和覆盖掉接收的参数
  54. * @return array
  55. * @author 令狐冲
  56. * @date 2021/6/29 21:05
  57. */
  58. public function goCheck($scene = null, $validate_data = [])
  59. {
  60. //接收参数
  61. if ($this->method == 'GET') {
  62. $params = request()->get();
  63. } else {
  64. $params = request()->post();
  65. }
  66. //合并验证参数
  67. $params = array_merge($params, $validate_data);
  68. //场景
  69. if ($scene) {
  70. $result = $this->scene($scene)->check($params);
  71. } else {
  72. $result = $this->check($params);
  73. }
  74. if (!$result) {
  75. $exception = is_array($this->error) ? implode(';', $this->error) : $this->error;
  76. JsonService::throw($exception);
  77. }
  78. // 3.成功返回数据
  79. return $params;
  80. }
  81. }