1.提交缺失的东西
This commit is contained in:
@ -51,13 +51,78 @@ class GoodsController extends BaseAdminController
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
// $params = (new GoodsValidate())->post()->goCheck('add');
|
||||
$params = $this->request->param();
|
||||
$result = GoodsLogic::add($params);
|
||||
if (true === $result) {
|
||||
return $this->success('添加成功', [], 1, 1);
|
||||
if ($this->request->isAjax() && $this->request->isPost()) {
|
||||
|
||||
$post = $this->request->post();
|
||||
$post['del'] = 0;
|
||||
|
||||
//主表验证
|
||||
$result = $this->validate($post, 'app\admin\validate\Goods.add');
|
||||
if ($result !== true) {
|
||||
return $this->fail($result);
|
||||
}
|
||||
|
||||
//单规格验证
|
||||
if ($post['spec_type'] == 1) {
|
||||
$result = $this->validate($post, 'app\admin\validate\GoodsOneSpec');
|
||||
if ($result !== true) {
|
||||
return $this->fail($result);
|
||||
}
|
||||
}
|
||||
|
||||
//多规格验证
|
||||
$spec_lists = [];
|
||||
if ($post['spec_type'] == 2) {
|
||||
$spec_lists = $post;
|
||||
|
||||
// 规格值验证长度验证
|
||||
foreach($spec_lists['spec_value_str'] as $key => $item) {
|
||||
$itemArr = explode(',', $item);
|
||||
foreach($itemArr as $subItem) {
|
||||
if(mb_strlen($subItem) > 64) {
|
||||
return $this->fail('第'. ($key+1) .'个SKU规格值超过了64个字符');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unset($spec_lists['goods_image']);
|
||||
unset($spec_lists['spec_name']);
|
||||
unset($spec_lists['spec_values']);
|
||||
unset($spec_lists['spec_id']);
|
||||
unset($spec_lists['spec_value_ids']);
|
||||
$spec_lists = form_to_linear($spec_lists);
|
||||
|
||||
//规格验证
|
||||
if (empty($spec_lists)) {
|
||||
return $this->fail('至少添加一个规格');
|
||||
}
|
||||
$result = $this->validate($post, 'app\admin\validate\GoodsMoreSpec');
|
||||
if ($result !== true) {
|
||||
return $this->fail($result);
|
||||
}
|
||||
|
||||
//规格商品列表验证
|
||||
foreach ($spec_lists as $v) {
|
||||
$result = $this->validate($v, 'app\admin\validate\GoodsMoreSpecLists');
|
||||
if ($result !== true) {
|
||||
return $this->fail($result);
|
||||
}
|
||||
}
|
||||
// 校验规格
|
||||
$total_stock = array_sum(array_column($spec_lists, 'stock'));
|
||||
if ($total_stock <= 0) {
|
||||
return $this->fail('至少有一个规格的库存大于0');
|
||||
}
|
||||
}
|
||||
|
||||
//添加商品
|
||||
$result = GoodsLogic::add($post, $spec_lists);
|
||||
|
||||
if ($result !== true) {
|
||||
return $this->fail('添加失败:' . $result);
|
||||
}
|
||||
return $this->success('添加成功');
|
||||
}
|
||||
return $this->fail(GoodsLogic::getError());
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user