From 5424e7d0f0af4650178b9eff3b4b13abcee7359d Mon Sep 17 00:00:00 2001 From: walkor Date: Sun, 4 Dec 2022 15:52:43 +0800 Subject: [PATCH] dict --- .../admin/app/controller/DictController.php | 159 +++++++++++ src/plugin/admin/app/model/Dict.php | 40 +++ src/plugin/admin/app/view/dict/index.html | 256 ++++++++++++++++++ src/plugin/admin/app/view/dict/insert.html | 223 +++++++++++++++ src/plugin/admin/app/view/dict/update.html | 235 ++++++++++++++++ 5 files changed, 913 insertions(+) create mode 100644 src/plugin/admin/app/controller/DictController.php create mode 100644 src/plugin/admin/app/model/Dict.php create mode 100644 src/plugin/admin/app/view/dict/index.html create mode 100644 src/plugin/admin/app/view/dict/insert.html create mode 100644 src/plugin/admin/app/view/dict/update.html diff --git a/src/plugin/admin/app/controller/DictController.php b/src/plugin/admin/app/controller/DictController.php new file mode 100644 index 0000000..60e491e --- /dev/null +++ b/src/plugin/admin/app/controller/DictController.php @@ -0,0 +1,159 @@ +method() === 'POST') { + $option_name = $this->dictNameToOptionName($request->post('name')); + if (Option::where('name', $option_name)->first()) { + return $this->json(1, '字典已经存在' . $option_name); + } + $values = (array)$request->post('value', []); + $format_values = $this->filterValue($values); + $option = new Option; + $option->name = $option_name; + $option->value = json_encode($format_values, JSON_UNESCAPED_UNICODE); + $option->save(); + return $this->json(0); + } + return view("dict/insert"); + } + + /** + * 更新 + * @param Request $request + * @return Response + * @throws BusinessException + */ + public function update(Request $request): Response + { + if ($request->method() === 'POST') { + $name = $this->dictNameToOptionName($request->post('name', '')); + $option = Option::where('name', $name)->first(); + if (!$option) { + return $this->json(1, '字典不存在'); + } + $format_values = $this->filterValue($request->post('value')); + $option->name = $this->dictNameToOptionName($request->post('name')); + $option->value = $format_values; + $option->save(); + } + return view("dict/update"); + } + + /** + * @param Request $request + * @return Response + */ + public function delete(Request $request) + { + $names = (array)$request->post('name'); + foreach ($names as $index => $name) { + $names[$index] = $this->dictNameToOptionName($name); + } + Option::whereIn('name', $names)->delete(); + return $this->json(0); + } + + /** + * 查询 + * @param Request $request + * @return Response + */ + public function select(Request $request): Response + { + $name = $request->get('name', ''); + if ($name && is_string($name)) { + $items = Option::where('name', 'like', "dict_$name%")->get()->toArray(); + } else { + $items = Option::where('name', 'like', 'dict_%')->get()->toArray(); + } + foreach ($items as &$item) { + $item['name'] = $this->optionNameTodictName($item['name']); + } + return $this->json(0, 'ok', $items); + } + + /** + * 获取 + * @param Request $request + * @param $name + * @return Response + */ + public function get(Request $request, $name): Response + { + $value = Option::where('name', $this->dictNameToOptionName($name))->value('value'); + if ($value === null) { + return $this->json(1, '字典不存在'); + } + return $this->json(1, 'ok', json_decode($value, true)); + } + + /** + * 过滤字典选项 + * @param array $values + * @return array + * @throws BusinessException + */ + protected function filterValue(array $values): array + { + $format_values = []; + foreach ($values as $item) { + if (!isset($item['value']) || !isset($item['name'])) { + throw new BusinessException('格式错误', 1); + } + $format_values[] = ['value' => $item['value'], 'name' => $item['name']]; + } + return $format_values; + } + + /** + * @param string $name + * @return string + */ + protected function dictNameToOptionName(string $name): string + { + return "dict_$name"; + } + + /** + * @param string $name + * @return string + */ + protected function optionNameTodictName(string $name): string + { + return substr($name, 5); + } + +} diff --git a/src/plugin/admin/app/model/Dict.php b/src/plugin/admin/app/model/Dict.php new file mode 100644 index 0000000..13c5e0c --- /dev/null +++ b/src/plugin/admin/app/model/Dict.php @@ -0,0 +1,40 @@ + + + + + 浏览页面 + + + + + + +
+
+
+ +
+ +
+ +
+
+ +
+ + + +
+
+ 展开 + 收起 +
+
+
+
+ + +
+
+
+
+
+ + + + + + + + + + + + + diff --git a/src/plugin/admin/app/view/dict/insert.html b/src/plugin/admin/app/view/dict/insert.html new file mode 100644 index 0000000..6f17293 --- /dev/null +++ b/src/plugin/admin/app/view/dict/insert.html @@ -0,0 +1,223 @@ + + + + + 新增字典 + + + + + + + +
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+ + +
+ + + + + + + +
+ + +
+
+ +
+
+ + +
+
+
+ + + + + + + diff --git a/src/plugin/admin/app/view/dict/update.html b/src/plugin/admin/app/view/dict/update.html new file mode 100644 index 0000000..2390781 --- /dev/null +++ b/src/plugin/admin/app/view/dict/update.html @@ -0,0 +1,235 @@ + + + + + 更新字典 + + + + + + + +
+ +
+
+ +
+
+ +
+ +
+
+
+ +
+ + +
+ + + + + + + +
+ + +
+
+ +
+
+ + +
+
+
+ + + + + + +