| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- <?php
- // +----------------------------------------------------------------------
- // | likeshop100%开源免费商用商城系统
- // +----------------------------------------------------------------------
- // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
- // | 开源版本可自由商用,可去除界面版权logo
- // | 商业版本务必购买商业授权,以免引起法律纠纷
- // | 禁止对系统程序代码以任何目的,任何形式的再发布
- // | gitee下载:https://gitee.com/likeshop_gitee
- // | github下载:https://github.com/likeshop-github
- // | 访问官网:https://www.likeshop.cn
- // | 访问社区:https://home.likeshop.cn
- // | 访问手册:http://doc.likeshop.cn
- // | 微信公众号:likeshop技术社区
- // | likeshop团队 版权所有 拥有最终解释权
- // +----------------------------------------------------------------------
- // | author: likeshopTeam
- // +----------------------------------------------------------------------
- namespace app\openapi\validate;
- use app\common\validate\BaseValidate;
- use app\common\service\JsonService;
- /**
- * 对外接口验证基类
- * Class BaseOpenValidate
- * @package app\openapi\validate
- */
- class BaseOpenValidate extends BaseValidate
- {
- /**
- * 严格参数验证 - 不允许多传参数
- * @param null $scene
- * @param array $validate_data
- * @return array
- */
- public function goCheckStrict($scene = null, $validate_data = [])
- {
- $method = $this->request->method();
- // 接收参数
- if ($method == 'GET') {
- $params = request()->get();
- } else {
- $params = request()->post();
- }
- // 合并验证参数
- $params = array_merge($params, $validate_data);
-
- // 获取允许的参数列表
- $allowedParams = $this->getAllowedParams($scene);
- // 检查多余参数
- if (!empty($allowedParams)) {
- $extraParams = array_diff(array_keys($params), $allowedParams);
- if (!empty($extraParams)) {
- JsonService::throw('不允许的参数: ' . implode(', ', $extraParams));
- }
- }
- // 执行常规验证
- return $this->goCheck($scene, $params);
- }
-
- /**
- * 获取允许的参数列表
- * @param string|null $scene
- * @return array
- */
- protected function getAllowedParams($scene = null): array
- {
- $allowedParams = [];
-
- // 获取验证规则中的参数
- if (property_exists($this, 'rule')) {
- $allowedParams = array_keys($this->rule);
- }
-
- // 如果有场景验证,获取场景对应的参数
- if (!empty($scene) && property_exists($this, 'scene')) {
- $scenes = $this->scene ?? [];
- if (isset($scenes[$scene])) {
- $allowedParams = $scenes[$scene];
- }
- }
-
- return $allowedParams;
- }
- }
|