app = $app; $this->request = $this->app->request; // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { $this->domain = $this->request->domain() . "/"; } // 验证数据 protected function validate(array $data, $validate, array $message = [], bool $batch = false) { if (is_array($validate)) { $v = new Validate(); $v->rule($validate); } else { if (strpos($validate, '.')) { // 支持场景 [$validate, $scene] = explode('.', $validate); } $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate); $v = new $class(); if (!empty($scene)) { $v->scene($scene); } } $v->message($message); // 是否批量验证 if ($batch || $this->batchValidate) { $v->batch(true); } return $v->failException(true)->check($data); } // 操作成功 protected function success($msg = '', string $url = null, $data = '') { $result = [ 'code' => 1, 'msg' => $msg, 'data' => $data ]; $response = json($result); throw new HttpResponseException($response); } // 操作错误 protected function error($msg = '', $data = '', $code = 0) { $result = [ 'code' => $code, 'msg' => $msg, 'data' => $data, ]; $response = json($result); throw new HttpResponseException($response); } // 添加前缀 protected function addPrefix($value, $domain = "") { if (empty($domain)) { $domain = $this->domain; } if (empty($value)) { return ""; } if (preg_match('/(http:\/\/)|(https:\/\/)/i', $value)) { //判断是否存在 return $value; } else { if (str_starts_with($value, "/")) { $value = substr($value, 1, strlen($value)); } $value = $domain . $value; return $value; } } }