diff --git a/app/admin/controller/crud/Crud.php b/app/admin/controller/crud/Crud.php index 992cee8..2b778b4 100644 --- a/app/admin/controller/crud/Crud.php +++ b/app/admin/controller/crud/Crud.php @@ -143,6 +143,10 @@ class Crud extends Backend $this->langTsData['zh-cn']['quick Search Fields'] = implode('、', $quickSearchFieldZhCnTitle); $this->controllerData['attr']['quickSearchField'] = $quickSearchField; + if (array_key_exists('admin_id', $fieldsMap)) { + $this->controllerData['attr']['autoFillAdminId'] = true; + } + $weighKey = array_search('weigh', $fieldsMap); if ($weighKey !== false) { $this->indexVueData['enableDragSort'] = true; diff --git a/app/admin/library/crud/Helper.php b/app/admin/library/crud/Helper.php index 3ee53d9..d265ecb 100644 --- a/app/admin/library/crud/Helper.php +++ b/app/admin/library/crud/Helper.php @@ -96,11 +96,12 @@ class Helper protected static array $attrType = [ 'controller' => [ - 'preExcludeFields' => 'array|string', - 'quickSearchField' => 'string|array', - 'withJoinTable' => 'array', - 'defaultSortField' => 'string|array', - 'weighField' => 'string', + 'preExcludeFields' => 'array|string', + 'quickSearchField' => 'string|array', + 'withJoinTable' => 'array', + 'defaultSortField' => 'string|array', + 'weighField' => 'string', + 'autoFillAdminId' => 'bool', ], ]; @@ -692,7 +693,14 @@ class Helper $modelMethodList = isset($modelData['relationMethodList']) ? array_merge($modelData['methods'], $modelData['relationMethodList']) : $modelData['methods']; $modelData['methods'] = $modelMethodList ? "\n" . implode("\n", $modelMethodList) : ''; $modelData['append'] = self::buildModelAppend($modelData['append'] ?? []); - $modelData['fieldType'] = self::buildModelFieldType($modelData['fieldType'] ?? []); + $fieldType = $modelData['fieldType'] ?? []; + if ($modelData['autoWriteTimestamp'] == 'true') { + $fieldType = array_merge( + ['create_time' => 'integer', 'update_time' => 'integer'], + $fieldType + ); + } + $modelData['fieldType'] = self::buildModelFieldType($fieldType); if (isset($modelData['beforeInsertMixins']['snowflake'])) { $modelData['beforeInsert'] = self::assembleStub('mixins/model/beforeInsert', [ @@ -726,6 +734,9 @@ class Helper $attrType = self::$attrType['controller'][$key] ?? ''; if (is_array($item)) { $controllerAttr .= "\n" . self::tab() . "protected $attrType \$$key = ['" . implode("', '", $item) . "'];\n"; + } elseif ($attrType === 'bool') { + $val = ($item === true || $item === 'true') ? 'true' : 'false'; + $controllerAttr .= "\n" . self::tab() . "protected $attrType \$$key = $val;\n"; } elseif ($item) { $controllerAttr .= "\n" . self::tab() . "protected $attrType \$$key = '$item';\n"; } diff --git a/app/admin/library/crud/stubs/mixins/controller/controller.stub b/app/admin/library/crud/stubs/mixins/controller/controller.stub index 942eab6..baf109d 100644 --- a/app/admin/library/crud/stubs/mixins/controller/controller.stub +++ b/app/admin/library/crud/stubs/mixins/controller/controller.stub @@ -19,6 +19,8 @@ class {%className%} extends Backend {%methods%} /** - * 若需重写查看、编辑、删除等方法,请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写 + * add、edit、del、sortable 已由父类 Backend 实现,无需重写即可直接使用 + * 若需重写,请确保调用 initializeBackend($request) 并传入 Request 参数 + * 若模型有 admin_id 字段需自动填充,可设置 protected bool $autoFillAdminId = true */ } \ No newline at end of file diff --git a/app/admin/library/traits/Backend.php b/app/admin/library/traits/Backend.php index 5f2fb28..e7e5d6c 100644 --- a/app/admin/library/traits/Backend.php +++ b/app/admin/library/traits/Backend.php @@ -97,6 +97,9 @@ trait Backend if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $data[$this->dataLimitField] = $this->auth->id; } + if ($this->autoFillAdminId && $this->dataLimitField === 'admin_id') { + $data['admin_id'] = $this->auth->id; + } $result = false; $this->model->startTrans(); diff --git a/app/common/controller/Backend.php b/app/common/controller/Backend.php index 6e7c365..855e9d0 100644 --- a/app/common/controller/Backend.php +++ b/app/common/controller/Backend.php @@ -80,6 +80,11 @@ class Backend extends Api */ protected bool $dataLimitFieldAutoFill = true; + /** + * 添加时自动填充 admin_id(当模型有 admin_id 字段但无数据权限限制时使用) + */ + protected bool $autoFillAdminId = false; + /** * 查看请求返回的主表字段 */ @@ -357,8 +362,10 @@ class Backend extends Api $order = $order ?: $this->defaultSortField; if ($order && is_string($order)) { - $order = explode(',', $order); - $order = [$order[0] => $order[1] ?? 'asc']; + $orderParts = explode(',', $order); + $orderField = trim($orderParts[0] ?? ''); + $orderDir = trim($orderParts[1] ?? 'asc'); + $order = $orderField !== '' ? [$orderField => $orderDir ?: 'asc'] : []; } if (!$this->orderGuarantee) { $this->orderGuarantee = [$pk => 'desc']; diff --git a/app/common/model/User.php b/app/common/model/User.php index 8debfbb..c6e0957 100644 --- a/app/common/model/User.php +++ b/app/common/model/User.php @@ -18,7 +18,7 @@ class User extends Model public function getAvatarAttr($value): string { - return full_url($value, false, config('buildadmin.default_avatar')); + return full_url($value ?? '', false, config('buildadmin.default_avatar')); } public function setAvatarAttr($value): string diff --git a/web/src/components/table/fieldRender/switch.vue b/web/src/components/table/fieldRender/switch.vue index 2ca05be..f6921a3 100644 --- a/web/src/components/table/fieldRender/switch.vue +++ b/web/src/components/table/fieldRender/switch.vue @@ -3,18 +3,18 @@