BaseOpenValidate.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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\openapi\validate;
  20. use app\common\validate\BaseValidate;
  21. use app\common\service\JsonService;
  22. /**
  23. * 对外接口验证基类
  24. * Class BaseOpenValidate
  25. * @package app\openapi\validate
  26. */
  27. class BaseOpenValidate extends BaseValidate
  28. {
  29. /**
  30. * 严格参数验证 - 不允许多传参数
  31. * @param null $scene
  32. * @param array $validate_data
  33. * @return array
  34. */
  35. public function goCheckStrict($scene = null, $validate_data = [])
  36. {
  37. $method = $this->request->method();
  38. // 接收参数
  39. if ($method == 'GET') {
  40. $params = request()->get();
  41. } else {
  42. $params = request()->post();
  43. }
  44. // 合并验证参数
  45. $params = array_merge($params, $validate_data);
  46. // 获取允许的参数列表
  47. $allowedParams = $this->getAllowedParams($scene);
  48. // 检查多余参数
  49. if (!empty($allowedParams)) {
  50. $extraParams = array_diff(array_keys($params), $allowedParams);
  51. if (!empty($extraParams)) {
  52. JsonService::throw('不允许的参数: ' . implode(', ', $extraParams));
  53. }
  54. }
  55. // 执行常规验证
  56. return $this->goCheck($scene, $params);
  57. }
  58. /**
  59. * 获取允许的参数列表
  60. * @param string|null $scene
  61. * @return array
  62. */
  63. protected function getAllowedParams($scene = null): array
  64. {
  65. $allowedParams = [];
  66. // 获取验证规则中的参数
  67. if (property_exists($this, 'rule')) {
  68. $allowedParams = array_keys($this->rule);
  69. }
  70. // 如果有场景验证,获取场景对应的参数
  71. if (!empty($scene) && property_exists($this, 'scene')) {
  72. $scenes = $this->scene ?? [];
  73. if (isset($scenes[$scene])) {
  74. $allowedParams = $scenes[$scene];
  75. }
  76. }
  77. return $allowedParams;
  78. }
  79. }