validator; } /** 分页列表 */ public function serviceList(Request $request) { if (method_exists($this, 'complexQuery')) { $result = $this->complexQuery($request->get()); } else { $config = $this->buildQueryConfig($request, 'index'); $limit = (int)$request->get('limit', 15); $page = (int)$request->get('page', 1); $result = $this->logicList($config, $limit, $page); } return $this->postProcess('List', $result); } /** 全量列表 */ public function serviceListAll(Request $request) { if (method_exists($this, 'complexQuery')) { $result = $this->complexQuery($request->get()); } else { $config = $this->buildQueryConfig($request, 'default'); $result = $this->logicListAll($config); } return $this->postProcess('List', $result); } /** 查看单条 */ public function serviceShow(Request $request) { $config = $this->buildQueryConfig($request, 'show'); $item = $this->logicShow($config); return $this->postProcess('Show', $item); } /** 创建 */ public function serviceStore(Request $request, array $data = []) { // $data = $request->post(); $dl = $this->getDataLimitFromRequest($request); $created = $this->logicCreate($data, $dl); return $this->postProcess('Store', $created); } /** 更新 */ public function serviceUpdate(Request $request, array $data = []) { // $data= $request->post(); $config = $this->buildQueryConfig($request, 'update'); $updated = $this->logicUpdateBy($config, $data); return $this->postProcess('Update', $updated); } /** 删除 */ public function serviceDestroy(Request $request) { $config = $this->buildQueryConfig($request, 'destroy'); $deleted = $this->logicDeleteBy($config); return $this->postProcess('Destroy', $deleted); } /** ----------------- 公共辅助方法 ----------------- */ /** * 构造 CrudUtil 的查询配置1 * * 前端可以通过以下参数控制: * - filter_= → where * - sort_by=&order= * - fields=id,name,email → select * - with=roles,posts → 关联 * - withFields=roles:id,name;... → 关联字段 * - dataLimitType=personal|auth * - dataLimitField=admin_id */ protected function buildQueryConfig(Request $request, string $scene = 'default'): array { $p = $request->all(); $config = []; $config['model'] = $this->modelClass; switch ($scene) { case 'default': $config['where']['status'] = 1; $config['order']['sort'] = 'desc'; $config['order']['id'] = 'desc'; break; case 'index': $config['where']['status'] = 1; $config['order']['sort'] = 'desc'; $config['order']['id'] = 'desc'; break; case 'show': $config['where']['id'] = $p['id']; $config['where']['status'] = 1; break; case 'store': break; case 'update': $config['where']['id'] = $p['id']; break; case 'destroy': $config['where']['id'] = $p['id']; break; } return $config; // // 1. where 从 filter_ 前缀自动映射 // $where = []; // foreach ($p as $key => $val) { // if (strpos($key, 'filter_') === 0 && $val !== '') { // $col = substr($key, 7); // // 支持 %like% 查询 // $where[$col] = strpos($val, '%') !== false // ? ['like', $val] // : $val; // } // } // // 2. 其余配置 // return [ // 'model' => $this->modelClass, // 'where' => $where, // 'order' => [ // $p['sort_by'] ?? 'id' => $p['order'] ?? 'desc' // ], // 'fields' => !empty($p['fields']) // ? explode(',', $p['fields']) // : [], // 'with' => !empty($p['with']) // ? explode(',', $p['with']) // : [], // 'withFields' => $this->parseWithFields( // $p['withFields'] ?? '' // ), // 'dataLimit' => $this->getDataLimitFromRequest($request), // ]; } /** 解析 withFields 字符串 "rel:field,field;..." */ protected function parseWithFields(string $str): array { if (!$str) { return []; } $out = []; foreach (explode(';', $str) as $pair) { [$rel, $flds] = explode(':', $pair); $out[$rel] = explode(',', $flds); } return $out; } /** 从请求读取 dataLimitType, dataLimitField */ protected function getDataLimitFromRequest(Request $req): array { $type = $req->get('dataLimitType'); if (!$type) { return []; } return [ 'type' => $type, 'field' => $req->get('dataLimitField', 'admin_id'), ]; } /** * 处理复杂查询 * 覆盖此方法,实现复杂查询逻辑 */ // protected function complexQuery(array $params): array // { // throw new \LogicException('请在子类实现 complexQuery'); // } /** * 根据操作类型,调用对应的后置钩子 * * @param string $op 操作:List, Show, Store, Update, Destroy * @param mixed $data 原始返回值 * @return mixed 处理后的返回值 */ protected function postProcess(string $op, mixed $data): mixed { $hook = "afterService{$op}"; return method_exists($this, $hook) ? $this->{$hook}($data) : $data; } }