488 lines
18 KiB
PHP
488 lines
18 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
namespace app\api\controller\v1;
|
||
|
||
use app\api\model\Goods as GoodsModel;
|
||
use app\api\validate\Goods as GoodsValidate;
|
||
|
||
/**
|
||
* 商品类
|
||
* Class Goods
|
||
* @package app\api\controller
|
||
*/
|
||
class Goods extends Base
|
||
{
|
||
public function initialize()
|
||
{
|
||
parent::initialize();
|
||
$this->model = new GoodsModel();
|
||
$this->validate = new GoodsValidate();
|
||
}
|
||
|
||
/**
|
||
* 列表
|
||
*/
|
||
public function index():\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['site_id'] = $inputData['language_id'];
|
||
unset($inputData['language_id']);
|
||
}
|
||
|
||
if (isset($inputData['goods_category_id']) && $inputData['goods_category_id'] != '' && $inputData['goods_category_id'] != 0 ) {
|
||
//批量获取当前类与子类ID
|
||
$inputData['goods_category_id'] = $this->model->getAllSonCategory(['id'=>$inputData['goods_category_id']]);
|
||
}
|
||
|
||
if (isset($inputData['status']) && $inputData['status'] != '' ) {
|
||
$this->focus['status'] = $inputData['status'];
|
||
}
|
||
|
||
//组建查询条件
|
||
if (isset($inputData['keyword']) && $inputData['keyword'] != '') {
|
||
$this->simple['model_number'] = $inputData['keyword'];
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
|
||
if (!empty($this->focus)) {
|
||
if (!$this->commonValidate(__FUNCTION__,$this->focus)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
}
|
||
|
||
//强制商品列表按添加时间倒序
|
||
$this->order['create_time'] = 'desc';
|
||
|
||
$result = $this->model->getAllList($inputData,$this->simple,$this->focus,$this->order);
|
||
if (!empty($result['page'])) {
|
||
foreach ($result['page'] as &$value) {
|
||
$value['language_id'] = $value['site_id'];
|
||
$value['language_name'] = $this->model->getLanguageName($value['site_id']);
|
||
$value['platform_name'] = $this->model->getPlatformName($value['platform_id']);
|
||
}
|
||
return $this->msg(200,'获取列表成功',$result);
|
||
} else {
|
||
if (isset($inputData['keyword']) && $inputData['keyword'] != '') {
|
||
if (isset($this->simple['model_number'])) {
|
||
unset($this->simple['model_number']);
|
||
}
|
||
$this->simple['name'] = $inputData['keyword'];
|
||
}
|
||
$result = $this->model->getAllList($inputData,$this->simple,$this->focus,$this->order);
|
||
if (!empty($result['page'])) {
|
||
foreach ($result['page'] as &$value) {
|
||
$value['language_name'] = $this->model->getLanguageName($value['site_id']);
|
||
$value['platform_name'] = $this->model->getPlatformName($value['platform_id']);
|
||
}
|
||
return $this->msg(200,'获取列表成功',$result);
|
||
}
|
||
// if (isset($result['count']) && $result['count'] == 0) {
|
||
return $this->msg(200,'获取列表成功',$result);
|
||
// } else {
|
||
// return $this->msg(504,'获取列表失败');
|
||
// }
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 新增
|
||
*/
|
||
public function create():\think\Response\Json
|
||
{
|
||
$details = $this->request->param('details','','htmlspecialchars');
|
||
$inputData = $this->request->param();
|
||
$inputData['details'] = $details;
|
||
$inputData['platform_id'] = $this->platformId;
|
||
// 过滤ID
|
||
if (isset($inputData['id']) && $inputData['id'] == '') {
|
||
unset($inputData['id']);
|
||
}
|
||
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['site_id'] = $inputData['language_id'];
|
||
unset($inputData['language_id']);
|
||
}
|
||
//主要参数不能为空
|
||
if ((isset($inputData['attr'][0]['attribute_id']) && $inputData['attr'][0]['attribute_id'] == '') || (isset($inputData['attr'][0]['value']) && $inputData['attr'][0]['value'] == "")) {
|
||
return $this->msg(504,'主要参数必填,请检查!');
|
||
}
|
||
|
||
//主要参数不能为空
|
||
if (isset($inputData['goods_images']) && empty($inputData['goods_images'])) {
|
||
return $this->msg(504,'必须上传商品主图,请检查!');
|
||
}
|
||
|
||
//主要参数不能为空
|
||
if (!isset($inputData['details']) || empty($inputData['details'])) {
|
||
return $this->msg(504,'图文介绍必填,请检查!');
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
|
||
//动态检测数据是否完整
|
||
// $isExitsLang = $this->model->dynamicCheck(\app\api\model\Site::class,['status'=>0,'id'=>$inputData['site_id']]);
|
||
// if (!$isExitsLang) {
|
||
// return $this->msg(504,'语言数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsGoodsCategory = $this->model->dynamicCheck(\app\api\model\GoodsCategory::class,['status'=>0,'id'=>$inputData['goods_category_id']]);
|
||
// if (!$isExitsGoodsCategory) {
|
||
// return $this->msg(504,'商品分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $flag = true;
|
||
// foreach ($inputData['attr'] as $datum) {
|
||
// $isExitsAttr = $this->model->dynamicCheck(\app\api\model\Attribute::class,['id'=>$datum['attribute_id']]);
|
||
// $flag = $isExitsAttr && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'商品参数有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
//
|
||
// foreach ($inputData['connect_goods'] as $item) {
|
||
// $isExitsGoodsCategory = $this->model->dynamicCheck(\app\api\model\GoodsCategory::class,['status'=>0,'id'=>$item['goods_category_id']]);
|
||
// $flag = $isExitsGoodsCategory && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'商品分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsGoods = $this->model->dynamicCheck(\app\api\model\Goods::class,['status'=>0,'id'=>$item['connect_good_id']]);
|
||
// $flag = $isExitsGoods && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'关联商品数据有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
//
|
||
// foreach ($inputData['soft_manual'] as $item) {
|
||
// $isExitsSoft = $this->model->dynamicCheck(\app\api\model\Download::class,['status'=>0,'id'=>$item['soft_manual_id']]);
|
||
// $flag = $isExitsSoft && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'下载文件数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsFileType = $this->model->dynamicCheck(\app\api\model\FileType::class,['status'=>0,'id'=>$item['file_type_id']]);
|
||
// $flag = $isExitsFileType && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'文件分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
|
||
$result = $this->model->addData($inputData);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'新增成功');
|
||
} else {
|
||
return $this->msg(504,'新增失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 修改
|
||
*/
|
||
public function update():\think\Response\Json
|
||
{
|
||
$details = $this->request->param('details','','htmlspecialchars');
|
||
$inputData = $this->request->param();
|
||
$inputData['details'] = $details;
|
||
$inputData['platform_id'] = $this->platformId;
|
||
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['site_id'] = $inputData['language_id'];
|
||
unset($inputData['language_id']);
|
||
}
|
||
//主要参数不能为空
|
||
if ((isset($inputData['attr'][0]['attribute_id']) && $inputData['attr'][0]['attribute_id'] == '') || (isset($inputData['attr'][0]['value']) && $inputData['attr'][0]['value'] == "")) {
|
||
return $this->msg(504,'主要参数必填,请检查!');
|
||
}
|
||
|
||
//主要参数不能为空
|
||
if (isset($inputData['goods_images']) && empty($inputData['goods_images'])) {
|
||
return $this->msg(504,'必须上传商品主图,请检查!');
|
||
}
|
||
|
||
//主要参数不能为空
|
||
if (!isset($inputData['details']) || empty($inputData['details'])) {
|
||
return $this->msg(504,'图文介绍必填,请检查!');
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
|
||
// //动态检测数据是否完整
|
||
// $isExitsLang = $this->model->dynamicCheck(\app\api\model\Site::class,['status'=>0,'id'=>$inputData['site_id']]);
|
||
// if (!$isExitsLang) {
|
||
// return $this->msg(504,'语言数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsGoodsCategory = $this->model->dynamicCheck(\app\api\model\GoodsCategory::class,['status'=>0,'id'=>$inputData['goods_category_id']]);
|
||
// if (!$isExitsGoodsCategory) {
|
||
// return $this->msg(504,'商品分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $flag = true;
|
||
// foreach ($inputData['attr'] as $datum) {
|
||
// $isExitsAttr = $this->model->dynamicCheck(\app\api\model\Attribute::class,['id'=>$datum['attribute_id']]);
|
||
// $flag = $isExitsAttr && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'商品参数有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
//
|
||
// foreach ($inputData['connect_goods'] as $item) {
|
||
// $isExitsGoodsCategory = $this->model->dynamicCheck(\app\api\model\GoodsCategory::class,['status'=>0,'id'=>$item['goods_category_id']]);
|
||
// $flag = $isExitsGoodsCategory && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'关联商品,商品分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsGoods = $this->model->dynamicCheck(\app\api\model\Goods::class,['status'=>0,'id'=>$item['connect_good_id']]);
|
||
// $flag = $isExitsGoods && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'关联商品数据有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
//
|
||
// foreach ($inputData['soft_manual'] as $item) {
|
||
// $isExitsSoft = $this->model->dynamicCheck(\app\api\model\Download::class,['status'=>0,'id'=>$item['soft_manual_id']]);
|
||
// $flag = $isExitsSoft && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'下载文件数据有变动,请重新填写再提交!');
|
||
// }
|
||
// $isExitsFileType = $this->model->dynamicCheck(\app\api\model\FileType::class,['status'=>0,'id'=>$item['file_type_id']]);
|
||
// $flag = $isExitsFileType && $flag;
|
||
// if (!$flag) {
|
||
// return $this->msg(504,'文件分类数据有变动,请重新填写再提交!');
|
||
// }
|
||
// }
|
||
$result = $this->model->editData($inputData);
|
||
if ($result) {
|
||
return $this->msg(200,'更改成功');
|
||
} else {
|
||
return $this->msg(504,'更改失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 详情
|
||
*/
|
||
public function read():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
|
||
if (isset($inputData['language_id']) && $inputData['language_id'] != 0 && $inputData['language_id'] != '') {
|
||
$this->focus['site_id'] = $inputData['site_id'] = $inputData['language_id'];;
|
||
unset($inputData['language_id']);
|
||
}
|
||
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
$result = $this->model->getInfo($inputData);
|
||
$result['details'] = htmlspecialchars_decode($result['details']);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'获取列表成功',$result);
|
||
} else {
|
||
return $this->msg(504,'获取列表失败');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取当前品牌语言列表
|
||
* @return mixed
|
||
*/
|
||
public function languageList():\think\Response\Json
|
||
{
|
||
$result = $this->model->getPlatformLanguage($this->platformId);
|
||
return $this->msg(200,'获取成功',$result);
|
||
}
|
||
|
||
/**
|
||
* 批量修改分类
|
||
*/
|
||
public function batchCategoryModify():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
|
||
if (count($inputData['id']) > 0) {
|
||
//构建分类数据
|
||
$newCateData = [];
|
||
foreach ($inputData['id'] as $item) {
|
||
//分类不能超过3个,
|
||
$newCateData[] = [
|
||
'id' => $item,
|
||
'goods_category_id' => $inputData['goods_category_id']
|
||
];
|
||
}
|
||
|
||
$result = $this->model->goodsBatchEditCategory($newCateData);
|
||
if ($result) {
|
||
return $this->msg(200,'更改成功');
|
||
} else {
|
||
return $this->msg(504,'更改失败');
|
||
}
|
||
} else {
|
||
return $this->msg(504,'提交的数据有问题,请检查后再提交');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量删除
|
||
*/
|
||
public function batchGoodsDel():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
if (count($inputData['id']) > 0) {
|
||
$newData = [];
|
||
//构建数据
|
||
foreach ($inputData['id'] as $item) {
|
||
$newData[] = [
|
||
'id' => $item,
|
||
];
|
||
}
|
||
|
||
$result = $this->model->batchDelData($newData);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'删除成功');
|
||
} else {
|
||
return $this->msg(504,'删除失败');
|
||
}
|
||
} else {
|
||
return $this->msg(504,'提交的数据有问题,请检查后再提交');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量启用
|
||
*/
|
||
public function batchGoodsEnable():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
if (count($inputData['id']) > 0) {
|
||
//验证分类ID
|
||
$newData = [];
|
||
if (count($inputData['id']) > 0) {
|
||
foreach ($inputData['id'] as $item) {
|
||
$newData[] = [
|
||
'id' => $item,
|
||
'status' => 0
|
||
];
|
||
}
|
||
}
|
||
|
||
$result = $this->model->batchEditData($newData);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'启用成功');
|
||
} else {
|
||
return $this->msg(504,'启用失败');
|
||
}
|
||
|
||
} else {
|
||
return $this->msg(504,'提交的数据有问题,请检查后再提交');
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 排序
|
||
*/
|
||
public function changeSort():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id','sort']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
$result = $this->model->changeSort($inputData);
|
||
if ($result) {
|
||
return $this->msg(200,"操作成功");
|
||
} else {
|
||
return $this->msg(504,"操作失败");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 启用禁用
|
||
*/
|
||
public function changeStatus():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['id','status']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
$result = $this->model->changeStatus($inputData);
|
||
if ($result) {
|
||
return $this->msg(200,"操作成功");
|
||
} else {
|
||
return $this->msg(504,"操作失败");
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 分类商品
|
||
* @return \think\response\Json
|
||
*/
|
||
public function categoryGoodsInfo():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param(['goods_category_id','type']);
|
||
$inputData['platform_id'] = $this->platformId;
|
||
if (!$this->commonValidate(__FUNCTION__,$inputData)) {
|
||
return $this->msg(-1,$this->validate->getError());
|
||
}
|
||
//
|
||
if (isset($inputData['goods_category_id'])) {
|
||
$list = $this->model->getGoodsInfoByGoodsCategoryId($inputData);
|
||
if ($list) {
|
||
$list['type'] = $inputData['type'];
|
||
return $this->msg(200, '获取列表成功', $list);
|
||
} else {
|
||
return $this->msg(504, '获取列表失败');
|
||
}
|
||
} else {
|
||
// $list['type'] = $inputData['type'];
|
||
$list = [
|
||
['connect_good_id'=>'','category'=>''],
|
||
['connect_good_id'=>'','category'=>''],
|
||
['connect_good_id'=>'','category'=>''],
|
||
['connect_good_id'=>'','category'=>''],
|
||
];
|
||
return $this->msg(200, '获取列表成功', $list);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 批量禁用
|
||
*/
|
||
public function batchGoodsDisable():\think\Response\Json
|
||
{
|
||
$inputData = $this->request->param();
|
||
if (count($inputData['id']) > 0) {
|
||
//验证分类ID
|
||
$newData = [];
|
||
if (count($inputData['id']) > 0) {
|
||
foreach ($inputData['id'] as $item) {
|
||
$newData[] = [
|
||
'id' => $item,
|
||
'status' => 1
|
||
];
|
||
}
|
||
}
|
||
|
||
$result = $this->model->batchEditData($newData);
|
||
|
||
if ($result) {
|
||
return $this->msg(200,'禁用成功');
|
||
} else {
|
||
return $this->msg(504,'禁用失败');
|
||
}
|
||
} else {
|
||
return $this->msg(504,'提交的数据有问题,请检查后再提交');
|
||
}
|
||
}
|
||
}
|