From 861aa9519d51844e0a33275fc4d0e814b8809878 Mon Sep 17 00:00:00 2001 From: wanghousheng Date: Sun, 21 Jan 2024 23:21:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/common/model/Goods.php | 15 ++++-- app/common/model/ServerCategory.php | 58 ++++++++++++++++++++ app/store/controller/Goods.php | 13 ++--- app/store/controller/Server.php | 83 +++++++++++++++++++++++++++++ app/store/model/ServerCategory.php | 47 ++++++++++++++++ 5 files changed, 205 insertions(+), 11 deletions(-) create mode 100644 app/common/model/ServerCategory.php create mode 100644 app/store/controller/Server.php create mode 100644 app/store/model/ServerCategory.php diff --git a/app/common/model/Goods.php b/app/common/model/Goods.php index 0b0962b3..9da09235 100644 --- a/app/common/model/Goods.php +++ b/app/common/model/Goods.php @@ -1,4 +1,5 @@ setQueryDefaultValue($param, [ @@ -246,7 +249,7 @@ class Goods extends BaseModel // 商品分类 if ($params['categoryId'] > 0) { // 关联商品与分类关系记录表 - $GoodsCategoryRelName = (new GoodsCategoryRelModel)->getName(); + $GoodsCategoryRelName = (new GoodsCategoryRelModel())->getName(); $query->join($GoodsCategoryRelName, "{$GoodsCategoryRelName}.goods_id = {$this->name}.goods_id"); // 设置分类ID条件 $query->where('goods_category_rel.category_id', '=', (int)$params['categoryId']); @@ -267,7 +270,9 @@ class Goods extends BaseModel */ protected function setGoodsListData($list, callable $callback = null) { - if ($list->isEmpty()) return $list; + if ($list->isEmpty()) { + return $list; + } // 遍历商品列表整理数据 foreach ($list as &$goods) { $goods = $this->setGoodsData($goods, $callback); diff --git a/app/common/model/ServerCategory.php b/app/common/model/ServerCategory.php new file mode 100644 index 00000000..23426472 --- /dev/null +++ b/app/common/model/ServerCategory.php @@ -0,0 +1,58 @@ +hasOne('UploadFile', 'file_id', 'image_id'); + } + + /** + * @notes:分类详情 + * @param $where + * @param array $with + * @return ServerCategory|array|null + * @author: wanghousheng + */ + public static function detail($where, array $with = []) + { + return static::get($where, $with); + } + + /** + * @notes:获取全部记录 + * @param array $where + * @return array + * @throws DataNotFoundException + * @throws DbException + * @throws ModelNotFoundException + * @author: wanghousheng + */ + public function getList(array $where = []): array + { + $where = $this->setQueryDefaultValue($where); + return $this->with(['image']) + ->where($where) + ->order(['sort', 'create_time']) + ->select() + ->toArray(); + } +} \ No newline at end of file diff --git a/app/store/controller/Goods.php b/app/store/controller/Goods.php index 5674f476..5fd05c21 100644 --- a/app/store/controller/Goods.php +++ b/app/store/controller/Goods.php @@ -12,6 +12,7 @@ declare (strict_types=1); namespace app\store\controller; +use think\db\exception\DbException; use think\response\Json; use cores\exception\BaseException; use app\store\model\Goods as GoodsModel; @@ -26,13 +27,13 @@ class Goods extends Controller /** * 商品列表 * @return Json - * @throws \think\db\exception\DbException + * @throws DbException */ public function list(): Json { // 获取列表记录 $model = new GoodsModel; - $list = $model->getList($this->request->param()); + $list= $model->getList($this->request->param()); return $this->renderSuccess(compact('list')); } @@ -55,7 +56,7 @@ class Goods extends Controller * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException + * @throws DbException * @throws \think\db\exception\ModelNotFoundException */ public function detail(int $goodsId): Json @@ -72,7 +73,7 @@ class Goods extends Controller * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException + * @throws DbException * @throws \think\db\exception\ModelNotFoundException */ public function basic(int $goodsId): Json @@ -88,7 +89,7 @@ class Goods extends Controller * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException + * @throws DbException * @throws \think\db\exception\ModelNotFoundException */ public function add(): Json @@ -106,7 +107,7 @@ class Goods extends Controller * @return Json * @throws BaseException * @throws \think\db\exception\DataNotFoundException - * @throws \think\db\exception\DbException + * @throws DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit(int $goodsId): Json diff --git a/app/store/controller/Server.php b/app/store/controller/Server.php new file mode 100644 index 00000000..e7e4481e --- /dev/null +++ b/app/store/controller/Server.php @@ -0,0 +1,83 @@ +request->post('name'); + $where = []; + if (!empty($name)) { + $where[] = ['name', 'like', `%$name%`]; + } + $model = new ServerCategory(); + try { + $list = $model->getList($where); + } catch (DataNotFoundException|ModelNotFoundException|DbException $e) { + return $this->renderError($e->getMessage() ?: '接口异常'); + } + return $this->renderSuccess(compact('list')); + } + + /** + * @notes:添加分类 + * @return Json + * @author: wanghousheng + */ + public function addCategory(): Json + { + $name = $this->request->post('name'); + if (!$name) { + return $this->renderError('名称不能为空'); + } + $image_id = intval($this->request->post('image_id')); + if (!$image_id) { + return $this->renderError('图片不能为空'); + } + $status = intval($this->request->post('status')); + $sort = intval($this->request->post('sort')); + $model = new ServerCategory(); + if ($model->add(compact('image_id', 'name', 'status', 'sort'))) { + return $this->renderSuccess('添加成功'); + } + return $this->renderError($model->getError() ?: '添加失败'); + } + + /** + * @notes:编辑分类 + * @param int $categoryId + * @return Json + * @author: wanghousheng + */ + public function editCategory(int $categoryId) + { + $name = $this->request->post('name'); + if (!$name) { + return $this->renderError('名称不能为空'); + } + $image_id = intval($this->request->post('image_id')); + if (!$image_id) { + return $this->renderError('图片不能为空'); + } + $status = intval($this->request->post('status')); + $sort = intval($this->request->post('sort')); + $category_id = $categoryId; + $model = new ServerCategory(); + if ($model->edit(compact('image_id', 'name', 'status', 'sort', 'category_id'))) { + return $this->renderSuccess('编辑成功'); + } + return $this->renderError($model->getError() ?: '编辑失败'); + } +} diff --git a/app/store/model/ServerCategory.php b/app/store/model/ServerCategory.php new file mode 100644 index 00000000..0257a24a --- /dev/null +++ b/app/store/model/ServerCategory.php @@ -0,0 +1,47 @@ +save($data); + } + + /** + * @notes:编辑 + * @param $data + * @return bool + * @author: wanghousheng + */ + public function edit($data): bool + { + // 是否删除图片 + !isset($data['image_id']) && $data['image_id'] = 0; + return $this->save($data) !== false; + } + + /** + * @notes:删除 + * @return bool + * @author: wanghousheng + */ + public function remove(): bool + { + if (!static::detail(['category_id' => $this['category_id']])) { + $this->error = '记录不存在'; + return false; + } + return $this->delete(); + } +} \ No newline at end of file