// +---------------------------------------------------------------------- namespace app\outapi\controller; use crmeb\basic\BaseController; use think\facade\Validate; /** * 基类 所有控制器继承的类 * Class AuthController * @package app\controller\out * @method success($msg = 'ok', array $data = []) * @method fail($msg = 'error', array $data = []) */ class AuthController extends BaseController { /** * 当前对外接口ID * @var */ protected $outId; /** * 当前对外接口信息 * @var */ protected $outInfo; /** * 当前对外接口权限 * @var array */ protected $auth = []; /** * 初始化 */ protected function initialize() { $this->outId = $this->request->outId(); $this->outInfo = $this->request->outInfo(); $this->auth = $this->outInfo['rule'] ?? []; } /** * 验证数据 * @param array $data * @param $validate * @param null $message * @param bool $batch * @return bool */ final protected function validate(array $data, $validate, $message = null, bool $batch = false) { if (is_array($validate)) { $v = new Validate(); $v->rule($validate); } else { if (strpos($validate, '.')) { // 支持场景 list($validate, $scene) = explode('.', $validate); } $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate); $v = new $class(); if (!empty($scene)) { $v->scene($scene); } if (is_string($message) && empty($scene)) { $v->scene($message); } } if (is_array($message)) $v->message($message); // 是否批量验证 if ($batch) { $v->batch(true); } return $v->failException(true)->check($data); } }