From aa589ff0528ed23c0f79f8416bc966f108fe600d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A6=99=E7=A0=81=E7=94=9F=E8=8A=B1?= <18523774412@qq.com> Date: Thu, 24 Nov 2022 09:39:52 +0800 Subject: [PATCH] =?UTF-8?q?refactor(CRUD):=E5=AE=8C=E5=96=84=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E8=AE=BE=E7=BD=AE=E5=99=A8=E5=92=8C=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=99=A8=E7=9A=84=E7=B1=BB=E5=9E=8B=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/crud/Crud.php | 4 ++++ app/admin/library/crud/Helper.php | 3 ++- app/admin/library/crud/stubs/mixins/model/getters/float.stub | 5 +++++ .../library/crud/stubs/mixins/model/getters/htmlDecode.stub | 2 +- .../library/crud/stubs/mixins/model/getters/jsonDecode.stub | 2 +- .../crud/stubs/mixins/model/getters/stringToArray.stub | 2 +- .../crud/stubs/mixins/model/setters/arrayToString.stub | 2 +- app/admin/library/crud/stubs/mixins/model/setters/time.stub | 2 +- 8 files changed, 16 insertions(+), 6 deletions(-) create mode 100644 app/admin/library/crud/stubs/mixins/model/getters/float.stub diff --git a/app/admin/controller/crud/Crud.php b/app/admin/controller/crud/Crud.php index 55db1582..044c52b0 100644 --- a/app/admin/controller/crud/Crud.php +++ b/app/admin/controller/crud/Crud.php @@ -587,6 +587,10 @@ class Crud extends Backend $modelData['methods'][] = Helper::assembleStub('mixins/model/getters/htmlDecode', [ 'field' => $fieldName ]); + } elseif ($field['originalDesignType'] == 'float') { + $modelData['methods'][] = Helper::assembleStub('mixins/model/getters/float', [ + 'field' => $fieldName + ]); } } diff --git a/app/admin/library/crud/Helper.php b/app/admin/library/crud/Helper.php index 338fc9af..27bd1c33 100644 --- a/app/admin/library/crud/Helper.php +++ b/app/admin/library/crud/Helper.php @@ -553,7 +553,8 @@ class Helper */ public static function analyseField(&$field) { - $field['type'] = self::analyseFieldType($field); + $field['type'] = self::analyseFieldType($field); + $field['originalDesignType'] = $field['designType']; // 表单项类型转换对照表 $designTypeComparison = [ diff --git a/app/admin/library/crud/stubs/mixins/model/getters/float.stub b/app/admin/library/crud/stubs/mixins/model/getters/float.stub new file mode 100644 index 00000000..1ac2d68a --- /dev/null +++ b/app/admin/library/crud/stubs/mixins/model/getters/float.stub @@ -0,0 +1,5 @@ + + public function get{%field%}Attr($value): float + { + return (float)$value; + } \ No newline at end of file diff --git a/app/admin/library/crud/stubs/mixins/model/getters/htmlDecode.stub b/app/admin/library/crud/stubs/mixins/model/getters/htmlDecode.stub index 121eb832..47ea9469 100644 --- a/app/admin/library/crud/stubs/mixins/model/getters/htmlDecode.stub +++ b/app/admin/library/crud/stubs/mixins/model/getters/htmlDecode.stub @@ -1,5 +1,5 @@ - public function get{%field%}Attr($value) + public function get{%field%}Attr($value): string { return !$value ? '' : htmlspecialchars_decode($value); } \ No newline at end of file diff --git a/app/admin/library/crud/stubs/mixins/model/getters/jsonDecode.stub b/app/admin/library/crud/stubs/mixins/model/getters/jsonDecode.stub index 94ddca39..6ae96a71 100644 --- a/app/admin/library/crud/stubs/mixins/model/getters/jsonDecode.stub +++ b/app/admin/library/crud/stubs/mixins/model/getters/jsonDecode.stub @@ -1,5 +1,5 @@ - public function get{%field%}Attr($value) + public function get{%field%}Attr($value): array { return !$value ? [] : json_decode($value, true); } \ No newline at end of file diff --git a/app/admin/library/crud/stubs/mixins/model/getters/stringToArray.stub b/app/admin/library/crud/stubs/mixins/model/getters/stringToArray.stub index 75e3d8a0..67937091 100644 --- a/app/admin/library/crud/stubs/mixins/model/getters/stringToArray.stub +++ b/app/admin/library/crud/stubs/mixins/model/getters/stringToArray.stub @@ -1,5 +1,5 @@ - public function get{%field%}Attr($value) + public function get{%field%}Attr($value): array { if (!$value) return []; if (!is_array($value)) { diff --git a/app/admin/library/crud/stubs/mixins/model/setters/arrayToString.stub b/app/admin/library/crud/stubs/mixins/model/setters/arrayToString.stub index ff15cce0..a54fc9d6 100644 --- a/app/admin/library/crud/stubs/mixins/model/setters/arrayToString.stub +++ b/app/admin/library/crud/stubs/mixins/model/setters/arrayToString.stub @@ -1,5 +1,5 @@ - public function set{%field%}Attr($value) + public function set{%field%}Attr($value): string { return $value ? (is_array($value) ? implode(',', $value) : $value) : ''; } \ No newline at end of file diff --git a/app/admin/library/crud/stubs/mixins/model/setters/time.stub b/app/admin/library/crud/stubs/mixins/model/setters/time.stub index f7812029..27a749f3 100644 --- a/app/admin/library/crud/stubs/mixins/model/setters/time.stub +++ b/app/admin/library/crud/stubs/mixins/model/setters/time.stub @@ -1,5 +1,5 @@ - public function set{%field%}Attr($value) + public function set{%field%}Attr($value): string { return $value ? date('H:i:s', strtotime($value)) : ''; } \ No newline at end of file