request->isAjax()) { $get = $this->request->get(); $get['shop_id'] = $this->shop_id; return JsonServer::success('', GoodsLogic::lists($get)); } $cate_list = MallCategoryLogic::categoryTreeeTree(); $shop_cate_list = ShopCategoryLogic::listAll($this->shop_id); $statistics = GoodsLogic::statistics($this->shop_id); return view('', [ 'statistics' => $statistics, 'cate_list' => $cate_list, //平台分类 'shop_cate_list' => $shop_cate_list, //商家分类 'goods_type' => GoodsEnum::getTypeDesc(), 'shop_type' => session('shop_info')['type'] ?? 2 ]); } /** * Notes: 添加 * @author 段誉(2021/4/15 10:49) */ public function add() { if ($this->request->isAjax()) { $post = $this->request->post(); $post['shop_id'] = $this->shop_id; //主表验证 (new GoodsValidate())->goCheck('add', ['shop_id' => $this->shop_id]); //单规格验证 if ($post['spec_type'] == 1) { (new GoodsOneSpec())->goCheck(); } $goods_number = array(); $w_discount = array(); $purchase_content = array(); $purchase_child_price = array(); //多规格验证 $spec_lists = []; if ($post['spec_type'] == 2) { $spec_lists = $post; unset($spec_lists['goods_image']); unset($spec_lists['spec_id']); unset($spec_lists['spec_name']); unset($spec_lists['spec_values']); unset($spec_lists['spec_value_ids']); unset($spec_lists['delivery_type']); unset($spec_lists['c_name']); unset($spec_lists['c_phone']); unset($spec_lists['work_remark']); unset($spec_lists['goods_remark']); unset($spec_lists['first_cate_id']); unset($spec_lists['second_cate_id']); unset($spec_lists['third_cate_id']); $goods_number = $spec_lists['goods_number']; $w_discount = $spec_lists['w_discount']; $purchase_content = $spec_lists['purchase_content']; $purchase_child_price = $spec_lists['purchase_child_price']; unset($spec_lists['w_discount']); unset($spec_lists['goods_number']); unset($spec_lists['one_goods_number']); unset($spec_lists['one_w_discount']); unset($spec_lists['purchase_content']); unset($spec_lists['purchase_child_price']); unset($spec_lists['one_purchase_content']); unset($spec_lists['one_purchase_child_price']); $spec_lists = ArrayServer::form_to_linear($spec_lists); //规格验证 if (empty($spec_lists)) { return JsonServer::error('至少添加一个规格'); } // 规格项及规格值是否重复验证 (new GoodsMoreSpec())->goCheck(); //规格商品列表验证 foreach ($spec_lists as $v) { (new GoodsMoreSpecLists())->goCheck('', $v); } // 校验规格 $total_stock = array_sum(array_column($spec_lists, 'stock')); if ($total_stock <= 0) { return JsonServer::error('至少有一个规格的库存大于0'); } } // 添加商品 $result = GoodsLogic::add($this->shop_id, $post, $spec_lists,$goods_number,$w_discount,$purchase_content,$purchase_child_price); if (true !== $result) { return JsonServer::error(GoodsLogic::getError() ?: '操作失败'); } return JsonServer::success('添加成功'); } $a = MallCategoryLogic::getAllTree(); return view('', [ 'category_lists_new' => $a, 'category_first' => "", 'category_second' => "", 'second_pid' => "", 'category_third' => "", 'third_pid' => "", 'category_lists' => json_encode(MallCategoryLogic::getAllTree(), JSON_UNESCAPED_UNICODE), 'shop_category_lists' => json_encode(ShopCategoryLogic::listAll($this->shop_id), JSON_UNESCAPED_UNICODE), 'brand_lists' => json_encode(GoodsBrand::getNameColumn(), JSON_UNESCAPED_UNICODE), 'supplier_lists' => json_encode(Supplier::getNameColumn($this->shop_id), JSON_UNESCAPED_UNICODE), 'unit_lists' => json_encode(GoodsUnit::getNameColumn(), JSON_UNESCAPED_UNICODE), 'freight_lists' => json_encode(Freight::getNameColumn($this->shop_id), JSON_UNESCAPED_UNICODE), ]); } /** * @notes 编辑 * @return \think\response\Json|\think\response\View * @author 段誉 * @date 2021/4/15 10:49 */ public function edit() { if ($this->request->isAjax()) { $post = $this->request->post(); $post['del'] = 0; $post['id'] = $post['goods_id']; //主表验证 (new GoodsValidate())->goCheck(null, ['shop_id' => $this->shop_id]); //单规格验证 if ($post['spec_type'] == 1) { (new GoodsOneSpec())->goCheck(); } $goods_number = array(); $w_discount = array(); $purchase_content = array(); $purchase_child_price = array(); //多规格验证 $spec_lists = []; // $goods_number = $post['one_goods_number']; // $w_discount = $post['one_w_discount']; // $purchase_content = $post['one_purchase_content']; // $purchase_child_price = $post['one_purchase_child_price']; 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']); unset($spec_lists['delivery_type']); unset($spec_lists['c_name']); unset($spec_lists['c_phone']); unset($spec_lists['goods_remark']); unset($spec_lists['work_remark']); unset($spec_lists['first_cate_id']); unset($spec_lists['second_cate_id']); unset($spec_lists['third_cate_id']); $goods_number = $spec_lists['goods_number']; $w_discount = $spec_lists['w_discount']; $purchase_content = $spec_lists['purchase_content']; $purchase_child_price = $spec_lists['purchase_child_price']; unset($spec_lists['goods_number']); unset($spec_lists['w_discount']); unset($spec_lists['one_goods_number']); unset($spec_lists['one_w_discount']); unset($spec_lists['purchase_content']); unset($spec_lists['purchase_child_price']); unset($spec_lists['one_purchase_content']); unset($spec_lists['one_purchase_child_price']); $spec_lists = ArrayServer::form_to_linear($spec_lists); //规格验证 if (empty($spec_lists)) { return JsonServer::error('至少添加一个规格'); } // 规格项验证 (new GoodsMoreSpec())->goCheck(); //规格商品列表验证 foreach ($spec_lists as $v) { (new GoodsMoreSpecLists())->goCheck('', $v); } // 校验规格 $total_stock = array_sum(array_column($spec_lists, 'stock')); if ($total_stock <= 0) { return JsonServer::error('至少有一个规格的库存大于0'); } } $result = GoodsLogic::edit($post, $spec_lists,$goods_number,$w_discount,$purchase_content,$purchase_child_price); if (true !== $result) { return JsonServer::error(GoodsLogic::getError() ?: '操作失败'); } return JsonServer::success('编辑成功'); } $goods_id = $this->request->get('goods_id'); $a = MallCategoryLogic::getAllTree(); $b = GoodsLogic::info($goods_id); $second_pid = array(); $arr = $arr1 = array(); if($b['base']['second_cate_id']!=0){ $second_pid = GoodsCategory::where("id in (".$b['base']['second_cate_id'].")")->select(); if(!empty($second_pid)){ foreach($second_pid as $key=>$value){ $arr[$key]=$value['pid']; } $second_pid = array_unique($arr); } } $third_pid = array(); if($b['base']['third_cate_id']!=0){ $third_pid = GoodsCategory::where("id in (".$b['base']['third_cate_id'].")")->select(); if(!empty($third_pid)){ foreach($third_pid as $key=>$value){ $arr1[$key]=$value['pid']; } $third_pid = array_unique($arr1); } } $b['base']['first_cate_id'] = explode(",",$b['base']['first_cate_id']); $b['base']['second_cate_id'] = explode(",",$b['base']['second_cate_id']); $b['base']['third_cate_id']= explode(",",$b['base']['third_cate_id']); return view('goods/goods/add', [ 'category_lists_new' => $a, 'category_first' => $b['base']['first_cate_id'], 'category_second' => $b['base']['second_cate_id'], 'second_pid' => $second_pid, 'category_third' => $b['base']['third_cate_id'], 'third_pid' => $third_pid, 'category_lists' => json_encode(MallCategoryLogic::getAllTree(), JSON_UNESCAPED_UNICODE), 'shop_category_lists' => json_encode(ShopCategoryLogic::listAll($this->shop_id), JSON_UNESCAPED_UNICODE), 'brand_lists' => json_encode(GoodsBrand::getNameColumn(), JSON_UNESCAPED_UNICODE), 'supplier_lists' => json_encode(Supplier::getNameColumn($this->shop_id), JSON_UNESCAPED_UNICODE), 'unit_lists' => json_encode(GoodsUnit::getNameColumn(), JSON_UNESCAPED_UNICODE), 'freight_lists' => json_encode(Freight::getNameColumn($this->shop_id), JSON_UNESCAPED_UNICODE), 'info' => json_encode(GoodsLogic::info($goods_id),JSON_UNESCAPED_UNICODE) ]); } /** * Notes: 删除 * @author 段誉(2021/4/15 10:49) * @return \think\response\Json */ public function del() { if ($this->request->isAjax()) { $id = $this->request->post('id'); (new GoodsValidate())->goCheck('del', ['goods_id' => $id, 'shop_id' => $this->shop_id]); $result = GoodsLogic::del($this->shop_id, $id); if($result) { return JsonServer::success('删除成功'); } return JsonServer::error(GoodsLogic::getError()); } } /** * Notes:修改商品字段(上下架、新品推荐、好物优选、猜你喜欢) * @throws \think\Exception * @throws \think\exception\PDOException */ public function changeFields(){ $table = 'goods'; $pk_name = 'id'; $pk_value = $this->request->post('id'); $field = $this->request->post('field'); $field_value = $this->request->post('value'); // 库存校验 $stock = GoodsModel::where('id', $pk_value)->value('stock'); if($field_value == 1 && $stock <= 0) { return JsonServer::error('库存为0的商品不允许上架'); } $result = CommonLogic::changeTableValue($table,$pk_name,$pk_value,$field,$field_value); if($result){ event('UpdateCollect', ['goods_id' => $pk_value]); return JsonServer::success('操作成功'); } return JsonServer::error('操作失败'); } /** * 放回仓库 */ public function backToWarehouse(){ $id = $this->request->post('id', '', 'intval'); if(empty($id)) { return JsonServer::error('id不存在'); } $result = GoodsLogic::backToWarehouse($id); if($result) { return JsonServer::success('操作成功'); } return JsonServer::error('操作失败'); } /** * @notes 获取统计数据 * @author Tab * @date 2021/7/13 18:03 */ public function totalCount() { if ($this->request->isAjax()) { return JsonServer::success('获取成功', GoodsLogic::statistics($this->shop_id)); } } /** * @notes 批量更新上下架状态 * @return \think\response\Json|void * @author 段誉 * @date 2022/3/17 11:53 */ public function setStatus() { if ($this->request->isAjax()) { $params = (new GoodsStatusValidate())->goCheck(null, ['shop_id' => $this->shop_id]); $result = GoodsLogic::setStatus($params['ids'], $params['status']); if($result) { return JsonServer::success('操作成功'); } return JsonServer::error(GoodsLogic::getError()); } } public function export() { $ids = $this->request->post('ids'); $result = GoodsLogic::exportData($this->shop_id, $ids); if(false === $result) { return JsonServer::error(GoodsLogic::getError() ?: '导出失败'); } return JsonServer::success('', ['url' => $result['url']]); } }