From ea3ca9f954822ac5afba2841a579da025f45a084 Mon Sep 17 00:00:00 2001 From: jsasg <735273025@qq.com> Date: Thu, 13 Mar 2025 11:15:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=E9=97=AE=E7=AD=94?= =?UTF-8?q?=E5=88=86=E9=A1=B5/=E8=AF=A6=E6=83=85/=E6=96=B0=E5=A2=9E/?= =?UTF-8?q?=E6=9B=B4=E6=96=B0/=E8=AE=BE=E7=BD=AE=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E5=80=BC/=E5=88=A0=E9=99=A4=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/v1/Faq.php | 154 ++++++++++++++++++++++++++ app/admin/model/v1/FaqModel.php | 48 ++++++++ app/admin/route/v1.php | 21 ++++ app/admin/validate/v1/FaqValidate.php | 57 ++++++++++ app/common/model/FaqBaseModel.php | 31 ++++++ 5 files changed, 311 insertions(+) create mode 100644 app/admin/controller/v1/Faq.php create mode 100644 app/admin/model/v1/FaqModel.php create mode 100644 app/admin/validate/v1/FaqValidate.php create mode 100644 app/common/model/FaqBaseModel.php diff --git a/app/admin/controller/v1/Faq.php b/app/admin/controller/v1/Faq.php new file mode 100644 index 00000000..741c749c --- /dev/null +++ b/app/admin/controller/v1/Faq.php @@ -0,0 +1,154 @@ +param([ + 'question', + 'created_at', + 'page/d' => 1, + 'size/d' => 10 + ]); + + $faqs = FaqModel::field([ + 'id', + 'image', + 'question', + 'recommend', + 'sort', + 'created_at' + ]) + ->withSearch(['question', 'created_at'], [ + 'question' => $param['question'] ?? null, + 'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null + ]) + ->language(request()->lang_id) + ->order(['sort' => 'asc', 'id' => 'desc']) + ->paginate([ + 'list_rows' => $param['size'], + 'page' => $param['page'], + ]); + + return success('获取成功', $faqs); + } + + // 详情 + public function read() + { + $id = request()->param('id'); + + $faq = FaqModel::withoutField([ + 'language_id', + 'created_at', + 'updated_at', + 'deleted_at', + ]) + ->bypk($id) + ->find(); + if (empty($faq)) { + return error('数据不存在'); + } + + return success('获取成功', $faq); + } + + // 新增 + public function save() + { + $post = request()->post([ + 'question', + 'image', + 'recommend', + 'sort', + 'answer' + ]); + $data = array_merge($post, ['language_id' => request()->lang_id]); + + $validate = new FaqValidate; + if (!$validate->scene('add')->check($data)) { + return error($validate->getError()); + } + + $faq = FaqModel::create($data); + if ($faq->isEmpty()) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 修改 + public function update() + { + $id = request()->param('id'); + $put = request()->put([ + 'question', + 'image', + 'recommend', + 'sort', + 'answer' + ]); + + $validate = new FaqValidate; + if (!$validate->scene('edit')->check(array_merge($put, ['id' => $id]))) { + return error($validate->getError()); + } + + $faq = FaqModel::bypk($id)->find(); + if (empty($faq)) { + return error('请确认操作对象是否存在'); + } + + if (!$faq->save($put)) { + return error('操作失败'); + } + return success('操作成功'); + } + + // 设置排序值 + public function sort() + { + $id = request()->param('id'); + $sort = request()->post('sort'); + + $faq = FaqModel::bypk($id)->find(); + if (empty($faq)) { + return error('请确认操作对象是否存在'); + } + + if ($faq->sort != $sort) { + $faq->sort = $sort; + if (!$faq->save()) { + return error('操作失败'); + } + } + return success('操作成功'); + } + + // 删除 + public function delete() + { + $id = request()->param('id'); + + $faq = FaqModel::bypk($id)->find(); + if (empty($faq)) { + return error('请确认操作对象是否存在'); + } + + if (!$faq->delete()) { + return error('操作失败'); + } + + return success('操作成功'); + } +} diff --git a/app/admin/model/v1/FaqModel.php b/app/admin/model/v1/FaqModel.php new file mode 100644 index 00000000..4f4df929 --- /dev/null +++ b/app/admin/model/v1/FaqModel.php @@ -0,0 +1,48 @@ +where('language_id', '=', $value); + } + + // 根据问题搜索 + public function searchQuestionAttr($query, $value, $data) + { + if (is_null($value)) { + return; + } + $query->where('question', 'like', "%$value%"); + } + + // 根据添加时间搜索 + public function searchCreatedAtAttr($query, $value, $data) + { + if (is_null($value)) { + return; + } + if (is_array($value)) { + if (count($value) > 1) { + $query->whereBetweenTime('created_at', $value[0], $value[1]); + } else { + $query->whereTime('created_at', '>=', $value[0]); + } + } + } +} diff --git a/app/admin/route/v1.php b/app/admin/route/v1.php index 0bb85490..2f89c0f0 100644 --- a/app/admin/route/v1.php +++ b/app/admin/route/v1.php @@ -498,6 +498,27 @@ Route::group('v1', function () { }); }); + // 问答管理 + Route::group('faq', function() { + // 问答分页 + Route::get('index', 'Faq/index'); + + // 问答详情 + Route::get('read/:id', 'Faq/read'); + + // 问答新增 + Route::post('save', 'Faq/save'); + + // 问答更新 + Route::put('update/:id', 'Faq/update'); + + // 设置问答排序值 + Route::post('sort/:id', 'Faq/sort'); + + // 问答删除 + Route::delete('delete/:id', 'Faq/delete'); + }); + // 配置项列表 Route::group('config', function() { // 配置分组 diff --git a/app/admin/validate/v1/FaqValidate.php b/app/admin/validate/v1/FaqValidate.php new file mode 100644 index 00000000..5a4ff7de --- /dev/null +++ b/app/admin/validate/v1/FaqValidate.php @@ -0,0 +1,57 @@ + ['规则1','规则2'...] + * + * @var array + */ + protected $rule = [ + 'id' => 'require|integer', + 'language_id' => 'require|integer', + 'image' => 'max:125', + 'question' => 'require|max:255', + 'recommend' => 'integer|in:0,1', + 'sort' => 'integer' + + ]; + + /** + * 定义错误信息 + * 格式:'字段名.规则名' => '错误信息' + * + * @var array + */ + protected $message = [ + 'id.require' => 'ID不能为空', + 'id.integer' => 'ID必须是整数', + 'language_id.require' => '语言ID不能为空', + 'language_id.integer' => '语言ID必须是整数', + 'language_id.mustOmit' => '语言ID不能修改', + 'image.max' => '图片不能超过125个字符', + 'question.require' => '问答标题不能为空', + 'question.max' => '问答标题不能超过255个字符', + 'recommend.integer' => '推荐值必须是整数', + 'recommend.in' => '推荐值只能是0或1', + 'sort.integer' => '排序值必须是整数' + ]; + + // 新增场景 + public function sceneAdd() + { + return $this->remove('id', 'require|integer'); + } + + // 更新场景 + public function sceneEdit() + { + return $this->remove('language_id', 'require|integer')->append('language_id', 'mustOmit'); + } +} diff --git a/app/common/model/FaqBaseModel.php b/app/common/model/FaqBaseModel.php new file mode 100644 index 00000000..fde82fb7 --- /dev/null +++ b/app/common/model/FaqBaseModel.php @@ -0,0 +1,31 @@ + 'int', + 'language_id' => 'int', + 'image' => 'string', + 'question' => 'string', + 'answer' => 'string', + 'recommend' => 'int', + 'sort' => 'int', + 'created_at' => 'datetime', + 'updated_at' => 'datetime', + 'deleted_at' => 'datetime', + ]; +}