diff --git a/app/admin/controller/Files.php b/app/admin/controller/Files.php new file mode 100644 index 00000000..03653749 --- /dev/null +++ b/app/admin/controller/Files.php @@ -0,0 +1,149 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\admin\controller; + +use think\response\Json; +use cores\library\Version; +use app\store\model\UploadFile as UploadFileModel; + +/** + * 文件库管理 + * Class Files + * @package app\store\controller + */ +class Files extends Controller +{ + /** + * 文件列表 + * @return Json + * @throws \cores\exception\BaseException + * @throws \think\db\exception\DbException + */ + public function list(): Json + { + $this->env(); + $model = new UploadFileModel; + $list = $model->getList($this->request->param()); + return $this->renderSuccess(compact('list')); + } + + /** + * 编辑文件 + * @param int $fileId + * @return Json + */ + public function edit(int $fileId): Json + { + // 文件详情 + $model = UploadFileModel::detail($fileId); + // 更新记录 + if ($model->edit($this->postForm())) { + return $this->renderSuccess('更新成功'); + } + return $this->renderError($model->getError() ?: '更新失败'); + } + + /** + * 删除文件(批量) + * @param array $fileIds 文件id集 + * @return Json + * @throws \think\Exception + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function delete(array $fileIds): Json + { + $model = new UploadFileModel; + if (!$model->setDelete($fileIds)) { + return $this->renderError($model->getError() ?: '操作失败'); + } + return $this->renderSuccess('操作成功'); + } + + /** + * 移动文件到指定分组(批量) + * @param int $groupId + * @param array $fileIds + * @return Json + */ + public function moveGroup(int $groupId, array $fileIds): Json + { + $model = new UploadFileModel; + if (!$model->moveGroup($groupId, $fileIds)) { + return $this->renderError($model->getError() ?: '操作失败'); + } + return $this->renderSuccess('操作成功'); + } + + /** + * 临时方法:环境检测并删除废弃的库文件 + * 文件:vendor/topthink/framework/src/think/Filesystem.php + * 文件:vendor/topthink/framework/src/think/facade/Filesystem.php + * 文件:vendor/topthink/framework/tests/FilesystemTest.php + * 目录:vendor/topthink/framework/src/think/filesystem + * @return void + * @throws \cores\exception\BaseException + */ + private function env() + { + // 判断当前版本小于2.2.7则不执行 + if (Version::compare(Version::getVersion(), '2.2.7') === -1) { + return; + } + // 要删除的文件列表 + $files = [ + 'vendor/topthink/framework/src/think/Filesystem.php', + 'vendor/topthink/framework/src/think/facade/Filesystem.php', + 'vendor/topthink/framework/tests/FilesystemTest.php' + ]; + foreach ($files as $file) { + $filePath = root_path() . $file; + file_exists($filePath) && unlink($filePath); + } + // 要删除的目录列表 + $folders = ['vendor/topthink/framework/src/think/filesystem/']; + foreach ($folders as $folder) { + $folderPath = root_path() . $folder; + is_dir($folderPath) && $this->deleteFolder($folderPath); + } + } + + /** + * 临时方法:递归删除指定目录下所有文件 + * @param $path + * @return void + */ + private function deleteFolder($path): void + { + if (!is_dir($path)) { + return; + } + // 扫描一个文件夹内的所有文件夹和文件 + foreach (scandir($path) as $val) { + // 排除目录中的.和.. + if (!in_array($val, ['.', '..', '.gitignore'])) { + // 如果是目录则递归子目录,继续操作 + if (is_dir($path . $val)) { + // 子目录中操作删除文件夹和文件 + $this->deleteFolder($path . $val . '/'); + // 目录清空后删除空文件夹 + rmdir($path . $val . '/'); + } else { + // 如果是文件直接删除 + unlink($path . $val); + } + } + } + } +} diff --git a/app/admin/controller/Goods.php b/app/admin/controller/Goods.php index 47bd25e3..2b05a80c 100644 --- a/app/admin/controller/Goods.php +++ b/app/admin/controller/Goods.php @@ -12,69 +12,141 @@ declare (strict_types=1); namespace app\admin\controller; -use think\facade\Db; +use think\db\exception\DbException; use think\response\Json; +use cores\exception\BaseException; +use app\store\model\Goods as GoodsModel; /** - * 商城管理 - * Class Store - * @package app\admin\controller + * 商品管理控制器 + * Class Goods + * @package app\store\controller */ class Goods extends Controller { /** - * 强制验证当前访问的控制器方法method - * @var array + * 商品列表 + * @return Json + * @throws DbException */ - protected array $methodRules = [ - 'index' => 'GET', - 'recycle' => 'GET', - 'add' => 'POST', - 'move' => 'POST', - 'delete' => 'POST', - ]; + public function list(): Json + { + // 获取列表记录 + $model = new GoodsModel; + $list= $model->getList($this->request->param()); + return $this->renderSuccess(compact('list')); + } /** - * 商城列表 + * 根据商品ID集获取列表记录 + * @param array $goodsIds * @return Json - * @throws \think\db\exception\DbException */ - public function index(): Json + public function listByIds(array $goodsIds): Json { + // 获取列表记录 + $model = new GoodsModel; + $list = $model->getListByIds($goodsIds); + return $this->renderSuccess(compact('list')); + } - $params = $this->request->param(); - $sort = $this->request->param('sort', 'id'); - $order = $this->request->param('order', 'desc'); - $where = []; - if (isset($params['channel']) && $params['channel']) { - $where[] = ['channel', '=', $params['channel']]; - } - if (isset($params['min_profit_rate']) && $params['min_profit_rate']) { - $where[] = ['profit_rate', '>=', $params['min_profit_rate']]; - } - if (isset($params['max_profit_rate']) && $params['max_profit_rate']) { - $where[] = ['profit_rate', '<=', $params['max_profit_rate']]; - } - if (isset($params['min_price']) && $params['min_price']) { - $where[] = ['net_price', '>=', $params['min_price']]; - } - if (isset($params['max_price']) && $params['max_price']) { - $where[] = ['net_price', '<=', $params['max_price']]; + /** + * 商品详情(详细信息) + * @param int $goodsId + * @return Json + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function detail(int $goodsId): Json + { + // 获取商品详情 + $model = new GoodsModel; + $goodsInfo = $model->getDetail($goodsId); + return $this->renderSuccess(compact('goodsInfo')); + } + + /** + * 商品详情(基础信息) + * @param int $goodsId + * @return Json + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function basic(int $goodsId): Json + { + // 获取商品详情 + $model = new GoodsModel; + $detail = $model->getBasic($goodsId); + return $this->renderSuccess(compact('detail')); + } + + /** + * 添加商品 + * @return Json + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function add(): Json + { + $model = new GoodsModel; + if ($model->add($this->postForm())) { + return $this->renderSuccess('添加成功'); } - if (!empty($params['catalog_name'])) { - $where[] = ['catalog_name', 'like', "%{$params["catalog_name"]}%"]; + return $this->renderError($model->getError() ?: '添加失败'); + } + + /** + * 商品编辑 + * @param int $goodsId + * @return Json + * @throws BaseException + * @throws \think\db\exception\DataNotFoundException + * @throws DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit(int $goodsId): Json + { + // 商品详情 + $model = GoodsModel::detail($goodsId); + // 更新记录 + if ($model->edit($this->postForm())) { + return $this->renderSuccess('更新成功'); } - $list = Db::connect("dataCenterMysql")->table('goods_sku') - ->where($where) - ->order($sort, $order) - ->paginate($this->request->param('per_page', 15))->each(function ($item, $key) { - $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']); - $item['update_time'] = date("Y-m-d H:i:s", $item['update_time']); - $item['channel'] = config('app.platformList')[$item['channel']] ?? ""; - return $item; - }); - return $this->renderSuccess(compact('list')); + return $this->renderError($model->getError() ?: '更新失败'); } + /** + * 修改商品状态(上下架) + * @param array $goodsIds 商品id集 + * @param bool $state 为true表示上架 + * @return Json + */ + public function state(array $goodsIds, bool $state): Json + { + $model = new GoodsModel; + if (!$model->setStatus($goodsIds, $state)) { + return $this->renderError($model->getError() ?: '操作失败'); + } + return $this->renderSuccess('操作成功'); + } + /** + * 删除商品 + * @param array $goodsIds + * @return Json + */ + public function delete(array $goodsIds): Json + { + $model = new GoodsModel; + if (!$model->setDelete($goodsIds)) { + return $this->renderError($model->getError() ?: '删除失败'); + } + return $this->renderSuccess('删除成功'); + } } diff --git a/app/admin/controller/Goods1.php b/app/admin/controller/Goods1.php new file mode 100644 index 00000000..47bd25e3 --- /dev/null +++ b/app/admin/controller/Goods1.php @@ -0,0 +1,80 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\admin\controller; + +use think\facade\Db; +use think\response\Json; + +/** + * 商城管理 + * Class Store + * @package app\admin\controller + */ +class Goods extends Controller +{ + /** + * 强制验证当前访问的控制器方法method + * @var array + */ + protected array $methodRules = [ + 'index' => 'GET', + 'recycle' => 'GET', + 'add' => 'POST', + 'move' => 'POST', + 'delete' => 'POST', + ]; + + /** + * 商城列表 + * @return Json + * @throws \think\db\exception\DbException + */ + public function index(): Json + { + + $params = $this->request->param(); + $sort = $this->request->param('sort', 'id'); + $order = $this->request->param('order', 'desc'); + $where = []; + if (isset($params['channel']) && $params['channel']) { + $where[] = ['channel', '=', $params['channel']]; + } + if (isset($params['min_profit_rate']) && $params['min_profit_rate']) { + $where[] = ['profit_rate', '>=', $params['min_profit_rate']]; + } + if (isset($params['max_profit_rate']) && $params['max_profit_rate']) { + $where[] = ['profit_rate', '<=', $params['max_profit_rate']]; + } + if (isset($params['min_price']) && $params['min_price']) { + $where[] = ['net_price', '>=', $params['min_price']]; + } + if (isset($params['max_price']) && $params['max_price']) { + $where[] = ['net_price', '<=', $params['max_price']]; + } + if (!empty($params['catalog_name'])) { + $where[] = ['catalog_name', 'like', "%{$params["catalog_name"]}%"]; + } + $list = Db::connect("dataCenterMysql")->table('goods_sku') + ->where($where) + ->order($sort, $order) + ->paginate($this->request->param('per_page', 15))->each(function ($item, $key) { + $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']); + $item['update_time'] = date("Y-m-d H:i:s", $item['update_time']); + $item['channel'] = config('app.platformList')[$item['channel']] ?? ""; + return $item; + }); + return $this->renderSuccess(compact('list')); + } + + +} diff --git a/app/admin/controller/files/Group.php b/app/admin/controller/files/Group.php new file mode 100644 index 00000000..5384529e --- /dev/null +++ b/app/admin/controller/files/Group.php @@ -0,0 +1,86 @@ + +// +---------------------------------------------------------------------- +declare (strict_types=1); + +namespace app\admin\controller\files; + +use think\response\Json; +use app\admin\controller\Controller; +use app\store\model\UploadGroup as GroupModel; + +/** + * 文件分组 + * Class Group + * @package app\store\controller\content + */ +class Group extends Controller +{ + /** + * 文件分组列表 + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function list(): Json + { + $model = new GroupModel; + $list = $model->getList(); + return $this->renderSuccess(compact('list')); + } + + /** + * 添加文件分组 + * @return Json + */ + public function add(): Json + { + // 新增记录 + $model = new GroupModel; + if ($model->add($this->postForm())) { + return $this->renderSuccess('添加成功'); + } + return $this->renderError($model->getError() ?: '添加失败'); + } + + /** + * 编辑文件分组 + * @param int $groupId + * @return Json + * @throws \think\db\exception\DataNotFoundException + * @throws \think\db\exception\DbException + * @throws \think\db\exception\ModelNotFoundException + */ + public function edit(int $groupId): Json + { + // 分组详情 + $model = GroupModel::detail($groupId); + // 更新记录 + if ($model->edit($this->postForm())) { + return $this->renderSuccess('更新成功'); + } + return $this->renderError($model->getError() ?: '更新失败'); + } + + /** + * 删除文件分组 + * @param int $groupId + * @return Json + */ + public function delete(int $groupId): Json + { + $model = GroupModel::detail($groupId); + if (!$model->remove()) { + return $this->renderError($model->getError() ?: '删除失败'); + } + return $this->renderSuccess('删除成功'); + } +} diff --git a/app/api/service/Goods.php b/app/api/service/Goods.php index 8746e856..8b5958cb 100644 --- a/app/api/service/Goods.php +++ b/app/api/service/Goods.php @@ -179,7 +179,7 @@ class Goods extends GoodsService } $list = $goodsList->toArray(); - $list['data'] = $this->formatGoodsList($goodsList); + $list['data'] = $this->newFormatGoodsList($list['data']); return $list; } @@ -205,7 +205,7 @@ class Goods extends GoodsService ], 3); $list2 = $goodsList->toArray()['data']; - $v['goods_list'] = $this->formatGoodsList($list2); + $v['goods_list'] = $this->newFormatGoodsList($list2); } return $list;