<?php namespace BossHttpControllers; use IlluminateFoundationBusDispatchesJobs; use IlluminateRoutingController as BaseController; use IlluminateFoundationValidationValidatesRequests; use IlluminateFoundationAuthAccessAuthorizesRequests; use BossHttpResponse; use Validator; use Auth; use BossModelsMenu; use BossCommonUtil; use BossModelsCode; use WebLog; class Controller extends BaseController { protected $validate_errors; protected $detail; protected $menu; protected $isPage; //page or ajax protected $callmethod; //post,get protected $pageSize = Appenv::PER_PAGE; protected static $rules = [ "default" => [] ]; protected static $rulesMessages = [ "default" => [ 'required' => '部分参数缺失 [:attribute]', 'string' => '格式错误,期望为字符串 [:attribute]', 'integer' => '格式错误,期望为整数 [:attribute]', 'between' => '范围错误 [:attribute],要求参数在:min - :max', 'in' => '限定范围错误 [:attribute],要求参数在 :values 内', 'array' => '格式错误[:attribute] 期望为数组', 'min' => '数量不正确', 'max' => '选中数过多,最多允许:max个 [:attribute]', 'unique' => '数据重复 [:attribute]', 'exists' => '记录不存在 [:attribute]', //定义[code:xxx] 会返回$code码 'boss_afterinput' => '结束时间不应小于开始时间 [:attribute]' ] ]; protected static $rulesCodes = [ "default" => [ "Exists" => Code::ERR_DATA_NOTFIND //这里需要用大写,如果有下划线也去除boss_books => BossBooks ] ]; public function init($request = null) { $this->isPage = (strpos($request->getPathInfo(), "/page/" ) === 0)?true:false; $this->callmethod = $request->getMethod(); $this->controller = $request->controller; $this->action = $request->action; $input = [ "uri" => $request->getRequestUri(), "input" => $request->input(), "ip" => $request->getClientIp() ]; WebLog::info($input); //处理query case $query = $request->query(); if(!empty($query)) { foreach($query as $k => $v) { if(strtolower($k) != $k) { $request->query->add([strtolower($k) => $v]); } } } $pageSize = intval($request->query("pagesize")); if($pageSize > 0 && $pageSize <= Appenv::MAX_PER_PAGE) { $this->pageSize = $pageSize; } WebLog::debug("current pagesize ".$this->pageSize); if(defined("BOSS_DEBUG")) { WebLog::info("boss debug enable"); DB::enableQueryLog(); } return $this; } /** * 返回用户对象 * @param string [id,account] * @return object */ protected function getUserInfo($key = null) { $user = Auth::user(); if ($key) { return $user->$key; } return $user; } /** * 返回用户权限对应的菜单 */ protected function getMenu() { if (!$this->menu) { $user = $this->getUserInfo(); if($user) { $obj = new Menu($user->ops_ids_1); $this->menu = $obj->getList(); } } return $this->menu; } /* * 验证规则 * @param $scence 场景 */ protected function rules($scene = 'default') { $rules = Util::array_merge_recursive_distinct(self::$rules,static::$rules); $rule = $rules['default']; if (isset($rules[$scene])) { $rule = array_merge($rule, $rules[$scene]); } return $rule; } /** * 验证规则messages,错误描述,最终保存于detail */ protected function rulesMessages($scene = 'default') { $rulesMessages = Util::array_merge_recursive_distinct(self::$rulesMessages,static::$rulesMessages); $rulesMessage = $rulesMessages['default']; if (isset($rulesMessages[$scene])) { $rulesMessage = array_merge($rulesMessage, $rulesMessages[$scene]); } return $rulesMessage; } /** * 验证码 */ protected function rulesCodes($scene = 'default') { $rulesCodes = Util::array_merge_recursive_distinct(self::$rulesCodes,static::$rulesCodes); $rulesCode = $rulesCodes['default']; if (isset($rulesCodes[$scene])) { $rulesCodes = array_merge($rulesMessage, $rulesCodes[$scene]); } return $rulesCode; } protected function setDetail($value = null) { $this->detail = $value; } /** * 输出 */ protected function response($data = null){ $res = new Response(); if(!empty($this->detail)) { $res->setDetail($this->detail); } if($this->isPage && $this->callmethod == "GET") { $res->setMenu($this->getMenu()); $userinfo = $this->getUserInfo(); if($userinfo) { $info = [ "uid" => $userinfo->id, "nickname" => $userinfo->nickname, "account" => $userinfo->account ]; $res->setInfo($info); } } if($this->controller == "auth") { $res->showStatus(); } return $res->send($data); } /** * 设置page对象 */ protected function setPageInfo($data) { $pageInfo = [ "totalCount" => isset($data['total']) ? $data['total'] : 0, "pageIndex" => isset($data['current_page']) ? $data['current_page'] : 0, "pageNum" => isset($data['per_page']) ?$data['per_page'] : 0 , "pageMax" => isset($data['last_page']) ? $data['last_page'] : 0 ]; return $pageInfo; } /* * 调用validate */ protected function checkValidate($data, $scence = 'default', &$errors = "", &$code = Code::ERR_PARAMETER) { $errors = ""; $code = Code::ERR_PARAMETER; $validator = Validator::make($data, $this->rules($scence), $this->rulesMessages($scence)); if($validator->fails()) { $rulesCode = $this->rulesCodes($scence); $failed = $validator->failed(); /** * array:1 [ * "id" => array:1 [ * "Exists" => array:4 [ * 0 => "cms_free_topic" * 1 => "id" * 2 => "is_del" * 3 => "0" * ] * ] * ] */ $findRule = 0; foreach($failed as $v) { foreach($v as $rule => $vv) { if(isset($rulesCode[$rule])) { $findRule = 1; $code = $rulesCode[$rule]; break; } } if($findRule) { break; } } Weblog::debug($failed); $errors = $validator->errors()->all(); $this->validate_errors = $errors; $this->setDetail($errors); return false; } return true; } /** * */ protected function formatList($data) { $data = [ "pageInfo" => $this->setPageInfo($data), "listInfo" => isset($data['data']) ? $data['data'] : array() ]; return $data; } protected function getPageInfo($list){ $list = $list->toArray(); return $this->setPageInfo($list); } }