app = $app; $this->request = $this->app->request; // 参数校验 $this->valid_params(); // 控制器初始化 $this->initialize(); } // 初始化 protected function initialize() { $action_name = $this->request->action(); $this->log_notice(['请求参数', $this->request->param(false)], '记录动作', 'action-log:' . $action_name); } /** * 验证数据 * @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 : $this->app->parseClass('validate', $validate); $v = new $class(); if (!empty($scene)) { $v->scene($scene); } } $v->message($message); // 是否批量验证 if ($batch || $this->batchValidate) { $v->batch(true); } $result = $v->failException(false)->check($data); if (!$result) { throw new ParamsValidateException($v->getError()); } return true; } protected function valid_params() { $is_logined = false; $cookie_user_id = cookie('act_admin_id'); if (!empty($cookie_user_id)) { if ($cookie_user_id) { $cookie_user_id = \a_decode($cookie_user_id); if ($cookie_user_id) { $cookie_user_id_sp = explode('|', $cookie_user_id); //查看是否有这个用户 $admin = D('act1_user')->where(array('id' => $cookie_user_id_sp[0], 'deleted' => 0, 'role'=> 'admin'))->find(); if (!empty($admin)) { $is_logined = true; $this->user_id = $cookie_user_id_sp[0]; $this->user_info = $admin; } } } } else { $this->user_id = null; } $request = $this->app->request; $action_name = $request->action(); if(isset($this->not_check_action_login) && (in_array($action_name, $this->not_check_action_login) || in_array('*', $this->not_check_action_login))) { // if($action_name == 'login' || $action_name == 'login_id' || $action_name == 'get_code') { // }else { if ($is_logined) { //logined } else { //not login throw new NotLoginException(); } } // 判断是否需要校验接口权限 if (array_key_exists($action_name, $this->role_actions ?? [])) { //判断是否登录了 if ($is_logined && $this->user_info) { //logined $roles = explode(',', $this->user_info['role'] ?? ''); $is_allow = false; foreach ($roles as $role) { foreach ($this->role_actions[$action_name] as $one) { if ($one == $role) { $is_allow = true; } } } if (!$is_allow) { throw new NoAccessException(); } } else { //no access throw new NoAccessException(); } } //没有列举出来的action也不校验登录 //带#的action_name是不校验是否登录,但是要校验参数 if (array_key_exists($action_name, $this->check_actions)) { //判断是否登录了 if ($is_logined) { //logined } else { //not login throw new NotLoginException(); } } //校验参数 $checks = array(); if (array_key_exists($action_name, $this->check_actions)) { $checks = $this->check_actions[$action_name]; } if (array_key_exists('#' . $action_name, $this->check_actions)) { $checks = $this->check_actions['#' . $action_name]; } if (!empty($checks)) { $valid_rule = []; $valid_msg = []; $valid_data = []; foreach ($checks as $k => $v) { if (\substr($k, 0, 1) === '#') { $k = \substr($k, 1); if (!isset($v[0])) { $v[] = ''; } } else { if (isset($v[0])) { if (strpos($v[0], 'require') === false) { $v[0] = 'require|' . $v[0]; } } else { $v[] = 'require'; } } $valid_data[$k] = input($k . ''); $valid_rule[$k] = $v[0]; if (count($v) > 1) { $msg = $v[1]; foreach ($msg as $k1 => $v1) { $valid_msg[$k . '.' . $k1] = $k . $v1; } } } $this->validate($valid_data, $valid_rule, $valid_msg); } } protected function log_notice($any, $msg = '', $tag = 'log_notice') { if ($any instanceof ErrorException || $any instanceof Exception) { $str = c_formate_exception($any); } else { $str = json_encode($any, JSON_UNESCAPED_UNICODE); } Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . $str . '][WHO:' . $this->user_id . ']', 'NOTICE'); } protected function log_warn($any, $msg = '', $tag = 'log_warn') { if ($any instanceof ErrorException || $any instanceof Exception) { $str = c_formate_exception($any); } else { $str = json_encode($any, JSON_UNESCAPED_UNICODE); } Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . json_encode($any, JSON_UNESCAPED_UNICODE) . '][WHO:' . $this->user_id . ']', 'WARN'); } protected function log_error($any, $msg = '', $tag = 'log_error') { if ($any instanceof ErrorException || $any instanceof Exception) { $str = c_formate_exception($any); } else { $str = json_encode($any, JSON_UNESCAPED_UNICODE); } Log::write('[' . $this->className . ']' . '[' . $tag . ']' . '[' . $msg . ']' . '[' . json_encode($any, JSON_UNESCAPED_UNICODE) . '][WHO:' . $this->user_id . ']', 'ERROR'); } }