dataLists(new GoodsLists()); } /** * @notes 添加 * @return \think\response\Json * @author likeadmin * @date 2025/05/08 15:34 */ public function add() { $post = $this->request->post(); $post['del'] = 0; //主表验证 $result = $this->validate($post, 'app\adminapi\validate\GoodsValidate.add'); if ($result !== true) { return $this->fail($result); } //单规格验证 if ($post['spec_type'] == 1) { $result = $this->validate($post, 'app\adminapi\validate\GoodsOneSpecValidate'); 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\adminapi\validate\GoodsMoreSpecValidate'); if ($result !== true) { return $this->fail($result); } //规格商品列表验证 foreach ($spec_lists as $v) { $result = $this->validate($v, 'app\adminapi\validate\GoodsMoreSpecListsValidate'); 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('添加成功'); } /** * @notes 编辑 * @return \think\response\Json * @author likeadmin * @date 2025/05/08 15:34 */ public function edit() { if ($this->request->isAjax() && $this->request->isPost()) { $post = $this->request->post(); $post['del'] = 0; $post['id'] = $post['goods_id']; //主表验证 $result = $this->validate($post, 'app\adminapi\validate\GoodsValidate'); if ($result !== true) { return $this->fail($result); } //单规格验证 if ($post['spec_type'] == 1) { $result = $this->validate($post, 'app\adminapi\validate\GoodsOneSpecValidate'); if ($result !== true) { return $this->fail($result); } } //多规格验证 $spec_lists = []; if ($post['spec_type'] == 2) { $spec_lists = $post; 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\adminapi\validate\GoodsMoreSpecValidate'); if ($result !== true) { return $this->fail($result); } //规格商品列表验证 foreach ($spec_lists as $v) { $result = $this->validate($v, 'app\adminapi\validate\GoodsMoreSpecListsValidate'); if ($result !== true) { return $this->fail($result); } } // 校验规格 $total_stock = array_sum(array_column($spec_lists, 'stock')); if ($total_stock <= 0) { $this->fail('至少有一个规格的库存大于0'); } } if ($post['status'] == 0) { $status = Db::name('goods') ->where(['id' => $post['goods_id']]) ->value('status'); if ($status == 1) { $res = Db::name('team_activity') ->where(['status' => 1, 'goods_id'=> $post['goods_id']]) ->find(); if ($res) { return $this->fail('该商品正在参与拼团,请先关闭后才允许下架'); } } } //添加商品 $result = GoodsLogic::edit($post, $spec_lists); if ($result !== true) { return $this->fail('添加失败:' . $result); } return $this->success('修改成功'); } } /** * @notes 删除 * @return \think\response\Json * @author likeadmin * @date 2025/05/08 15:34 */ public function delete() { $params = (new GoodsValidate())->post()->goCheck('delete'); GoodsLogic::delete($params); return $this->success('删除成功', [], 1, 1); } /** * @notes 获取详情 * @return \think\response\Json * @author likeadmin * @date 2025/05/08 15:34 */ public function detail() { $params = (new GoodsValidate())->goCheck('detail'); // $result = GoodsLogic::detail($params); $result = GoodsLogic::info($params['id']); return $this->data($result); } }