app = $app; $this->request = $this->app->request; // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { } /** * 验证数据 * @access protected * @param array $data 数据 * @param string|array $validate 验证器名或者验证规则数组 * @param array $message 提示信息 * @param bool $batch 是否批量验证 * @return array|string|true * @throws ValidateException */ 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 : 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); } /** * 操作成功跳转的快捷方法 * @access protected * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @param array $header 发送的Header信息 * @return void */ protected function success($msg = '', string $url = NULL, $data = '', int $wait = 3, array $header = []) { if (is_null($url) && isset($_SERVER["HTTP_REFERER"])) { $url = $_SERVER["HTTP_REFERER"]; } elseif ($url) { $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)app()->route->buildUrl($url); } $result = [ 'code' => 1, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait, 'message' => $msg, ]; if (!$result['url']) { $result['url'] = $_SERVER["HTTP_REFERER"]; } $type = $this->getResponseType(); // 把跳转模板的渲染下沉,这样在 response_send 行为里通过getData()获得的数据是一致性的格式 if ('html' == strtolower($type)) { $type = 'view'; $response = Response::create(app()->config->get('jump.dispatch_success_tmpl'), $type)->assign($result)->header($header); } else { $response = Response::create($result, $type)->header($header); } throw new HttpResponseException($response); } /** * 操作错误跳转的快捷方法 * @access protected * @param mixed $msg 提示信息 * @param string $url 跳转的URL地址 * @param mixed $data 返回的数据 * @param integer $wait 跳转等待时间 * @param array $header 发送的Header信息 * @return void */ protected function error($msg = '', string $url = NULL, $data = '', int $wait = 3, array $header = []) { if (is_null($url)) { $url = request::isAjax() ? '' : 'javascript:history.back(-1);'; } elseif ($url) { $url = (strpos($url, '://') || 0 === strpos($url, '/')) ? $url : (string)app()->route->buildUrl($url); } $result = [ 'code' => 0, 'msg' => $msg, 'data' => $data, 'url' => $url, 'wait' => $wait, 'error' => $msg, ]; if (!$result['url']) { $result['url'] = $_SERVER["HTTP_REFERER"]; } $type = $this->getResponseType(); // pr($result); // exit; if ('html' == strtolower($type)) { $type = 'view'; $response = Response::create(app()->config->get('jump.dispatch_error_tmpl'), $type)->assign($result)->header($header); } else { $response = Response::create($result, $type)->header($header); } throw new HttpResponseException($response); } /** * 返回封装后的API数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param integer $code 返回的code * @param mixed $msg 提示信息 * @param string $type 返回数据格式 * @param array $header 发送的Header信息 * @return void */ protected function result($data, $code = 0, $msg = '', $type = 'json', array $header = []) { $result = [ 'code' => $code, 'msg' => $msg, 'time' => time(), 'data' => $data, ]; $type = $type ?: $this->getResponseType(); $response = Response::create($result, $type)->header($header); throw new HttpResponseException($response); } /** * Ajax方式返回数据到客户端 * @access protected * @param mixed $data 要返回的数据 * @param String $type AJAX返回数据格式 * @return void */ protected function ajaxReturn($data, $type = '') { if (empty($type)) $type = Config::get('app.DEFAULT_AJAX_RETURN'); switch (strtoupper($type)) { case 'JSON' : // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); exit(json_encode($data)); case 'XML' : // 返回xml格式数据 header('Content-Type:text/xml; charset=utf-8'); exit(xml_encode($data)); case 'JSONP': // 返回JSON数据格式到客户端 包含状态信息 header('Content-Type:application/json; charset=utf-8'); $handler = isset($_GET[C('VAR_JSONP_HANDLER')]) ? $_GET[C('VAR_JSONP_HANDLER')] : C('DEFAULT_JSONP_HANDLER'); exit($handler . '(' . json_encode($data) . ');'); case 'EVAL' : // 返回可执行的js脚本 header('Content-Type:text/html; charset=utf-8'); exit($data); } } /** * URL重定向 * @access protected * @param string $url 跳转的URL表达式 * @param integer $code http code * @param array $with 隐式传参 * @return void */ protected function redirect($url, $code = 302, $with = []) { $response = Response::create($url, 'redirect'); $response->code($code)->with($with); throw new HttpResponseException($response); } /** * 获取当前的response 输出类型 * @access protected * @return string */ protected function getResponseType() { return request::isJson() || request::isAjax() ? 'json' : 'html'; } // 成功响应 protected function successResponse($message) { if(is_array($message)){ return array_merge(['code' => 200], $message); } else{ return ['code' => 200, 'message' => $message]; } } // 错误响应 protected function errorResponse($message) { if(is_array($message)){ return array_merge(['code' => 400], $message); } else{ return ['code' => 400, 'message' => $message]; } } // 正确响应 protected function successResponseHtml($title, $message, $url) { View::assign('title', $title); View::assign('content', $message); View::assign('url', $url); View::assign('color', 'success'); View::assign('buttonType', 'primary'); return View::fetch('common/success'); } // 错误响应 protected function errorResponseHtml($title, $message, $url) { View::assign('title', $title); View::assign('content', $message); View::assign('url', $url); View::assign('color', 'error'); View::assign('buttonType', 'danger'); return View::fetch('common/success'); } }