From bf5db8ca8931d4029a8186772a207b94d6eae9c9 Mon Sep 17 00:00:00 2001 From: ztt <835303992@qq.com> Date: Wed, 28 Feb 2024 16:33:33 +0800 Subject: [PATCH] =?UTF-8?q?=E9=97=A8=E5=BA=97=E8=AF=84=E8=AE=BA=E5=A2=9E?= =?UTF-8?q?=E5=88=A0=E6=94=B9=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/service/Goods.php | 1 + app/common/model/store/shop/Comment.php | 10 ++ app/store/controller/shop/Comment.php | 98 +++++++++++++ app/store/model/store/shop/Comment.php | 178 ++++++++++++++++++++++++ 4 files changed, 287 insertions(+) create mode 100644 app/store/controller/shop/Comment.php create mode 100644 app/store/model/store/shop/Comment.php diff --git a/app/api/service/Goods.php b/app/api/service/Goods.php index 8af54b0a..56eb1a29 100644 --- a/app/api/service/Goods.php +++ b/app/api/service/Goods.php @@ -235,6 +235,7 @@ class Goods extends GoodsService $info['evaluate'] = Comment::with('file') ->field(['nickname,content,image_id']) ->hidden(['file']) + ->where('is_delete', '=', 0) ->where('shop_id','=', $info['shop_id']) ->select(); // $info['imageList'] = [ diff --git a/app/common/model/store/shop/Comment.php b/app/common/model/store/shop/Comment.php index 1974c862..fb4455df 100644 --- a/app/common/model/store/shop/Comment.php +++ b/app/common/model/store/shop/Comment.php @@ -47,4 +47,14 @@ class Comment extends BaseModel return $this->hasOne(UploadFile::class,'file_id', 'image_id')->bind(['image' => 'preview_url']); } + /** + * 评论详情 + * @param $where + * @return static|array|null + */ + public static function detail($where) + { + return static::get($where); + } + } diff --git a/app/store/controller/shop/Comment.php b/app/store/controller/shop/Comment.php new file mode 100644 index 00000000..4fd23694 --- /dev/null +++ b/app/store/controller/shop/Comment.php @@ -0,0 +1,98 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\store\controller\shop; + +use think\response\Json; +use app\store\controller\Controller; +use app\store\model\store\shop\Comment as CommentModel; + +/** + * 门店评论控制器 + * Class Clerk + * @package app\store\controller\shop + */ +class Comment extends Controller +{ + /** + * 列表记录 + * @return Json + * @throws \think\db\exception\DbException + */ + public function list(): Json + { + // 店员列表 + $model = new CommentModel; + $list = $model->getList($this->request->param()); + return $this->renderSuccess(compact('list')); + } + + /** + * 全部记录 + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function all(): Json + { + // 店员列表 + $model = new CommentModel; + $list = $model->getAll($this->request->param()); + return $this->renderSuccess(compact('list')); + } + + /** + * 添加评论 + * @return Json + */ + public function add(): Json + { + // 新增记录 + $model = new CommentModel; + if ($model->add($this->postForm())) { + return $this->renderSuccess('添加成功'); + } + return $this->renderError($model->getError() ?: '添加失败'); + } + + /** + * 编辑店员 + * @param int $commentId + * @return Json + */ + public function edit(int $commentId): Json + { + // 评论详情 + $model = CommentModel::detail($commentId); + // 更新记录 + if ($model->edit($this->postForm())) { + return $this->renderSuccess('更新成功'); + } + return $this->renderError($model->getError() ?: '更新失败'); + } + + /** + * 删除店员 + * @param int $commentId + * @return Json + */ + public function delete(int $commentId): Json + { + // 店员详情 + $model = CommentModel::detail($commentId); + if (!$model->setDelete()) { + return $this->renderError($model->getError() ?: '删除失败'); + } + return $this->renderSuccess('删除成功'); + } +} diff --git a/app/store/model/store/shop/Comment.php b/app/store/model/store/shop/Comment.php new file mode 100644 index 00000000..a076b40e --- /dev/null +++ b/app/store/model/store/shop/Comment.php @@ -0,0 +1,178 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\store\model\store\shop; + +use app\common\model\store\shop\Comment as CommenModel; + +/** + * 商家门店店员模型 + * Class Clerk + * @package app\store\model\store\shop + */ +class Comment extends CommenModel +{ + // 表单验证场景: 新增 + const FORM_SCENE_ADD = 'add'; + + // 表单验证场景: 编辑 + const FORM_SCENE_EDIT = 'edit'; + + /** + * 获取列表数据 + * @param array $param + * @return \think\Collection + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function getAll(array $param = []): \think\Collection + { + // 检索查询条件 + $filter = $this->getFilter($param); + // 查询列表数据 + return $this->with(['image']) + ->where($filter) + ->where('is_delete', '=', 0) + ->order(['create_time' => 'desc', $this->getPk()]) + ->select(); + } + + /** + * 获取列表数据 + * @param array $param + * @return \think\Paginator + * @throws \think\db\exception\DbException + */ + public function getList(array $param = []): \think\Paginator + { + // 检索查询条件 + $filter = $this->getFilter($param); + // 查询列表数据 + return $this->with(['image']) + ->where($filter) + ->where('is_delete', '=', 0) + ->order(['create_time' => 'desc', $this->getPk()]) + ->paginate(15); + } + + /** + * 检索查询条件 + * @param array $param + * @return array + */ + private function getFilter(array $param = []): array + { + // 默认查询参数 + $params = $this->setQueryDefaultValue($param, [ + 'shop_id' => 0, // 门店id + 'search' => '', // 搜索关键词: 姓名/手机号 + 'status' => '', // 门店状态(0禁用 1启用) + ]); + // 检索查询条件 + $filter = []; + // 门店id + $params['shop_id'] > 0 && $filter[] = ['shop_id', '=', (int)$params['shop_id']]; + // 门店状态 + is_numeric($params['status']) && $filter[] = ['status', '=', (int)$params['status']]; + // 搜索关键词 + !empty($params['search']) && $filter[] = ['nickname|content', 'like', "%{$params['search']}%"]; + return $filter; + } + + /** + * 新增记录 + * @param array $data + * @return bool + */ + public function add(array $data): bool + { + // 表单验证 + if (!$this->validateForm($data, self::FORM_SCENE_ADD)) { + return false; + } + $data['store_id'] = self::$storeId; + return $this->save($data); + } + + /** + * 编辑记录 + * @param array $data + * @return bool|false + */ + public function edit(array $data): bool + { + // 表单验证 + if (!$this->validateForm($data, self::FORM_SCENE_EDIT, $this)) { + return false; + } + return $this->save($data) !== false; + } + + /** + * 软删除 + * @return bool|false + */ + public function setDelete(): bool + { + return $this->save(['is_delete' => 1]); + } + + /** + * 表单验证 + * @param $data + * @param string $scene + * @param mixed $detail + * @return bool + */ + private function validateForm($data, string $scene, $detail = null): bool + { + if ($scene === self::FORM_SCENE_ADD) { + if (empty($data['nickname'])) { + $this->error = '请选择指定的会员'; + return false; + } + + if (empty($data['content'])) { + $this->error = '请输入评论内容'; + return false; + } + + if (empty($data['image_id'])) { + $this->error = '请输入图片ID'; + return false; + } + } + if ($scene === self::FORM_SCENE_EDIT) { + if (empty($data['image_id'])) { + $this->error = '请输入图片ID'; + return false; + } + } + return true; + } + + /** + * 验证门店是否已存在该店员 + * @param int $userId + * @param int $shopId + * @return bool + */ + private function isExistForShop(int $userId, int $shopId): bool + { + $model = new static; + return (bool)$model->where('user_id', '=', $userId) + ->where('shop_id', '=', $shopId) + ->where('is_delete', '=', 0) + ->value($model->getPk()); + } +}