From c2c096ae7bf862046715609127bc175b4d06ce9d Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Fri, 1 Dec 2023 14:36:19 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20app/Admin/Services/BaseSer?= =?UTF-8?q?vice.php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Admin/Services/BaseService.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/app/Admin/Services/BaseService.php b/app/Admin/Services/BaseService.php index bac6f16..56ff18f 100644 --- a/app/Admin/Services/BaseService.php +++ b/app/Admin/Services/BaseService.php @@ -19,7 +19,7 @@ class BaseService extends AdminService public function getTree() { - $list = $this->query()->orderByDesc('sort')->get()->toArray(); + $list = $this->query()->get()->toArray(); return array2tree($list); } @@ -79,8 +79,8 @@ class BaseService extends AdminService public function update($primaryKey, $data): bool { - $data = $this->resloveData($data); $model = $this->query()->whereKey($primaryKey)->firstOrFail(); + $data = $this->resloveData($data, $model); $validate = $this->validate($data, $model); if ($validate !== true) { $this->setError($validate); @@ -103,30 +103,44 @@ class BaseService extends AdminService /** * 处理表单数据 - * + * * @param array $data + * @param Model $model 空 : 添加, 非空: 修改 * @return array */ - public function resloveData($data) + public function resloveData($data, $model = null) { return $data; } /** * 表单验证 - * + * * @param array $data * @param Model $model 空: 添加, 非空: 修改 * @return mixed true: 验证通过, string: 错误提示 */ public function validate($data, $model = null) { + // $createRules = [ + // 'key' => ['required', Rule::unique('keywords', 'key')], + // 'name' => ['required'], + // ]; + // $updateRules = [ + // 'key' => [Rule::unique('keywords', 'key')->ignore($model->id)] + // ]; + // $validator = Validator::make($data, $model ? $updateRules : $createRules, [ + // 'key.unique' => ':input 已经存在' + // ]); + // if ($validator->fails()) { + // return $validator->errors()->first(); + // } return true; } /** * 删除的前置方法 - * + * * @param array $ids 主键id * @return mixed true: 继续后续操作, string: 中断操作, 返回错误提示 */