317 lines
12 KiB
PHP
317 lines
12 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
namespace app\api\controller\v1;
|
||
|
||
use app\api\model\GoodsCategory as GoodsCategoryModel;
|
||
use app\api\validate\GoodsCategory as GoodsCategoryValidate;
|
||
|
||
/**
|
||
* 商品分类
|
||
* Class GoodsCategory
|
||
* @package app\api\controller
|
||
*/
|
||
class GoodsCategory extends Base
|
||
{
|
||
public function initialize()
|
||
{
|
||
parent::initialize();
|
||
$this->model = new GoodsCategoryModel();
|
||
$this->validate = new GoodsCategoryValidate();
|
||
}
|
||
|
||
/**
|
||
* 列表树
|
||
*/
|
||
public function tree():\think\Response\Json
|
||
{
|
||
$inputData['type'] = 0;
|
||
$inputData = $this->request->param();
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
$this->order['sort'] = 'desc';
|
||
//默认接口列表为全部输出
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
}
|
||
if (isset($inputData['language_id'])) {
|
||
unset($inputData['language_id']);
|
||
}
|
||
//前台勾选显示正常启用列表,用status字段来进行筛选
|
||
if (isset($inputData['status'])) {
|
||
switch ($inputData['status']) {
|
||
case 0:
|
||
unset($inputData['status']);
|
||
break;
|
||
default:
|
||
$this->focus['status'] = 0;
|
||
unset($inputData['status']);
|
||
break;
|
||
}
|
||
}
|
||
|
||
$list = $this->model->getIndexList($this->page,$this->size,$this->field,$this->simple,$this->focus,$this->order);
|
||
//策划规定,尤达品牌只做2级分类,不允许添加3级或者4级
|
||
// if ($inputData['type'] == 1) {
|
||
$indexTree = list_to_tree($list);
|
||
$addEditList = list_to_tree($list,3);//表示遇到第三级就跳出
|
||
$tree = ['add'=>$addEditList,'index'=>$indexTree];
|
||
// } else {
|
||
// $tree = list_to_tree($list);
|
||
// $tree = ['add'=>$tree,'index'=>$tree];
|
||
// }
|
||
return $this->msg(200, '获取列表成功', $tree);
|
||
}
|
||
|
||
/**
|
||
* 新增
|
||
* @return \think\response\Json
|
||
*/
|
||
public function create():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
$inputData['platform_id'] = $this->platformId;
|
||
// //策划规定,尤达品牌只做2级分类,不允许添加3级或者4级
|
||
// if ($inputData['platform_id'] == 1) {
|
||
// $inputData['pid'] = [$inputData['pid']];
|
||
// }
|
||
//语言包
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
}
|
||
if (isset($inputData['language_id'])) {
|
||
unset($inputData['language_id']);
|
||
}
|
||
//处理PID结构
|
||
// if (count($inputData['pid']) >= 1) {
|
||
// $inputData['pid'] = array_pop($inputData['pid']);
|
||
// } else {
|
||
// return $this->msg(-1,'pid必填');
|
||
// }
|
||
if (isset($inputData['pid']) && $inputData['pid'] == '') {
|
||
$inputData['pid'] = 0;
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
|
||
$result = $this->model->addData($inputData);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'新增成功');
|
||
} else {
|
||
return $this->msg(504,'新增失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 详情
|
||
* @return \think\response\Json
|
||
*/
|
||
public function read():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
}
|
||
if (isset($inputData['language_id'])) {
|
||
unset($inputData['language_id']);
|
||
}
|
||
$result = $this->model->getInfo($inputData);
|
||
//获取当前分类的所有子类
|
||
$sonCategory = $this->model->getChildCategory($inputData['id']);
|
||
//获取所有分类数据
|
||
$cates = $this->model->where(['status'=>0,'site_id'=>$result['site_id'],'platform_id'=>$this->platformId])->select()->toArray();
|
||
//取出编辑时上级分类列表。
|
||
$list = [];
|
||
foreach ($cates as $cate) {
|
||
if (!in_array($cate['id'],$sonCategory)) {
|
||
$list[] = $cate;
|
||
}
|
||
}
|
||
$addEditList = list_to_tree($list,3);//表示遇到第三级就跳出
|
||
$result['list'] = $addEditList;
|
||
//获取当前分类下是否有子分类。
|
||
$hasChild = $this->model->where('pid',$inputData['id'])->find();
|
||
$result['has_child'] = (bool) $hasChild;
|
||
if ($result) {
|
||
return $this->msg(200,'获取列表成功',$result);
|
||
} else {
|
||
return $this->msg(504,'获取列表失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改
|
||
* @return \think\response\Json
|
||
*/
|
||
public function update():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
}
|
||
if (isset($inputData['language_id'])) {
|
||
unset($inputData['language_id']);
|
||
}
|
||
// //策划规定,尤达品牌只做2级分类,不允许添加3级或者4级
|
||
// if ($inputData['platform_id'] == 1) {
|
||
// $inputData['pid'] = [$inputData['pid']];
|
||
// }
|
||
// dd($inputData);
|
||
//处理PID结构
|
||
// $inputData['pid'] = $inputData['pid'][0];
|
||
if (isset($inputData['pid']) && $inputData['pid'] == '') {
|
||
$inputData['pid'] = 0;
|
||
}
|
||
|
||
if ($inputData['pid'] == $inputData['id']) {
|
||
return $this->msg(-1,'不能选择当前分类为自己的父分类');
|
||
}
|
||
//状态修改判断
|
||
if (isset($inputData['status']) && $inputData['status'] != '' && $inputData['status'] == 1) {
|
||
//判断当前修改的分类是否有子分类。如果有,将所有子分类全部禁用
|
||
$sonCategory = $this->model->getChildCategory($inputData['id']);
|
||
$newData = [];
|
||
foreach ($sonCategory as $item) {
|
||
$newData[] = [
|
||
'id' => $item,
|
||
'status'=> 1
|
||
];
|
||
}
|
||
$this->model->changeStatus($newData,true);
|
||
}
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
$result = $this->model->editData($inputData);
|
||
if ($result) {
|
||
return $this->msg(200,'更改成功');
|
||
} else {
|
||
return $this->msg(504,'更改失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分类商品
|
||
* @return \think\response\Json
|
||
*/
|
||
public function categoryGoodsList():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
unset($inputData['language_id']);
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
|
||
if ($inputData['goods_category_id'] == '') {
|
||
return $this->msg(200, '获取列表成功', []);
|
||
}
|
||
|
||
$list = $this->model->getGategoryListData($inputData);
|
||
//商品编辑的时候。去除当前分类当前商品ID
|
||
if (isset($inputData['goods_id']) && $inputData['goods_id'] > 0) {
|
||
foreach ($list as $key => $item) {
|
||
if ($item['id'] == $inputData['goods_id']) {
|
||
unset($list[$key]);
|
||
}
|
||
}
|
||
}
|
||
if (!empty($list)) {
|
||
$newList = [];
|
||
$newList['page'] = array_merge($list,[]);
|
||
$newList['type'] = $inputData['type']??0;
|
||
return $this->msg(200, '获取列表成功', $newList);
|
||
} else {
|
||
return $this->msg(504, '此分类下没有商品,请重新选择');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分类商品
|
||
* @return \think\response\Json
|
||
*/
|
||
public function categoryGoodsInfo():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id','type']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['language_id'];
|
||
$inputData['site_id'] = $inputData['language_id'];
|
||
}
|
||
if (isset($inputData['language_id'])) {
|
||
unset($inputData['language_id']);
|
||
}
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
// dd($inputData);
|
||
//
|
||
if (isset($inputData['id'])) {
|
||
$list = $this->model->getGategoryListDataInfo($inputData);
|
||
dd($list);
|
||
// if ($list) {
|
||
$list['type'] = $inputData['type']??0;
|
||
return $this->msg(200, '获取列表成功', $list);
|
||
// } else {
|
||
// return $this->msg(504, '获取列表失败');
|
||
// }
|
||
} else {
|
||
// $list['type'] = $inputData['type'];
|
||
$list = [
|
||
['connect_good_id'=>'','goods_category_id'=>''],
|
||
['connect_good_id'=>'','goods_category_id'=>''],
|
||
['connect_good_id'=>'','goods_category_id'=>''],
|
||
['connect_good_id'=>'','goods_category_id'=>''],
|
||
];
|
||
return $this->msg(200, '获取列表成功', $list);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 删除
|
||
* @return \think\response\Json
|
||
*/
|
||
public function del():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
//查找是否有子类。
|
||
$hasChild = $this->model->where('pid',$inputData['id'])->find();
|
||
//查找是否有商品。
|
||
$hasGoods = $this->model->hasGoodsOnCategory($inputData);
|
||
if (empty($hasChild) && empty($hasGoods)) {
|
||
$result = $this->model->delData($inputData);
|
||
if ($result) {
|
||
return $this->msg(200,"删除成功");
|
||
} else {
|
||
return $this->msg(504,"删除失败");
|
||
}
|
||
} else {
|
||
return $this->msg(504,"有子分类或者分类下有商品,不能删除");
|
||
}
|
||
|
||
}
|
||
|
||
}
|