request->post(); $res = Yii::$app->services->validate->validate($form, [ [['goods_name',], 'required', 'message' => '商品名称不能为空'], [['cats', 'cover_pic', 'original_price', 'price', 'stock',], 'required'], [['detail', 'bannar_pic', 'id', 'cost_price'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); // 编辑商品 $form['mall_id'] = $this->mall_id; $form['store_id'] = $this->store_id; $form['supplier_type'] = StoreEnum::SUPPLIER_SELF; $form['supplier_id'] = $this->store_id; $form['supplier_goods_id'] = 0; $form['goods_type'] = 1; $form['freight_type'] = [2]; if (empty($form['check_status'])) { $form['check_status'] = 0; $form['is_on_sale'] = 0; } else { if ($form['check_status'] == 2) { $form['check_status'] = 0; } } if ($form['goods_type'] == 1 && empty($form['freight_type'])) { return $this->error('请选择物流方式'); } $basic = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic'); if (empty($basic['goods_auth'])) { $form['check_status'] = 1; } else { $form['check_status'] = 0; // 开启了审核,保存都要重新审核 } if (empty($form['bannar_pic'])) $form['bannar_pic'] = []; // 处理前端上传的轮播图和后台一致 if (!empty($form['bannar_pic'])) { $form['bannar_pic'] = array_map(function ($item) { return ['pic_url' => $item]; }, $form['bannar_pic']); } $this->formatForm($form); $recommend_nums = \Yii::$app->services->setting->addons->get($this->mall_id, StoreEnum::ADDONS_NAME, 'basic.recommend_nums'); $counts = StoreGoods::find()->where(['store_id' => $this->store_id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'is_recommend' => 1])->count(); if($counts<$recommend_nums){ $form['is_recommend'] = 1; } $res = StoreGoods::setData($form); if ($res === false) { return $this->error(StoreGoods::getStaticError()); } return $this->success('操作成功'); } private function formatForm(&$form) { empty($form['id']) && $form['goods_type'] = 2; $form['subtitle'] = ''; $form['video_cover_pic'] = ''; // $form['bannar_pic'] = []; $form['video_url'] = ''; $form['mchCats'] = []; $form['services'] = []; $form['freight'] = null; $form['labels'] = []; $form['is_attr'] = 1; $form['attr'] = [ [ "attr_list" => [ [ "attr_group_id" => 1, "attr_group_name" => "套餐", "attr_id" => 2, "attr_name" => "套餐1", ] ], 'stock' => $form['stock'], 'price' => $form['price'], 'original_price' => $form['original_price'], 'cost_price' => $form['cost_price'], 'goods_no' => '', 'weight' => '', 'pic_url' => '', 'distributionLevelList' => [], 'levelPrice' => [], 'sign_id' => 2, 'member_price' => [], ] ]; $form['unit'] = ''; $form['stock_warning'] = 0; $form['goods_weight'] = 0; $form['goods_no'] = ''; $form['is_show_stock'] = 1; $form['is_show_sales'] = 1; $form['virtual_sales'] = 0; $form["coupon_is_enable"] = 0; $form["freight_type"] = [2]; $form["freight_rules_type"] = 2; $form["freight_id"] = ""; $form["shipping_fee"] = 0; $form["free_shipping_num"] = 0; $form["free_shipping_money"] = 0; $form["is_cross_border"] = 0; $form["is_id_card"] = 0; $form["is_on_sale"] = 1; $form["show_style"] = "2"; $form["extra"] = [ "full_reduce_price_limit" => 0, "full_reduce_money" => 0, "score_give_setting" => [ "code" => "score", "name" => "积分", "give_num" => 0, "give_type" => 1, ], "is_member_price" => 0, "member_price_setting" => [], "member_buy_limit_setting" => [ "member_level" => [], "every_num" => 0, "every_day_num" => 0, "every_week_num" => 0, "every_month_num" => 0, "nums" => 0, "total_num" => 0, ], "currency_deduct_setting" => [] ]; $form['coupon_selection'] = []; $form["goods_setting"] = [ "member_setting" => [ "member_price_setting" => "", "member_price" => [], "member_buy_limit_setting" => [ "member_level" => [], "every_num" => 0, "every_day_num" => 0, "every_week_num" => 0, "every_month_num" => 0, "nums" => 0, "total_num" => 0, ] ], "marketing_setting" => [ "full_reduce_price_limit" => 0, "full_reduce_money" => 0, "score_give_setting" => [ "code" => "score", "name" => "积分", "give_num" => 0, "give_type" => 1, ], "score_deduct_setting" => [ "code" => "score", "name" => "积分", "deduct_num" => 0, "deduct_type" => 1, ], "currency_deduct_setting" => [], ] ]; $form["is_area_limit"] = 0; $form["area_limit"] = []; $form["coupon"] = [ "is_enable" => 0, "multiple_selection" => [] ]; $form['addons'] = []; $form["attr_groups"] = [ 0 => [ "attr_group_id" => 1, "attr_group_name" => "套餐", "attr_list" => [ 0 => [ "attr_id" => 2, "attr_name" => "套餐1", "pic_url" => "", ] ] ] ]; return $form; } public function actionCateTree() { if (Yii::$app->request->isGet) { $cond = [['mall_id' => $this->mall_id, 'store_id' => $this->store_id]]; $list = StoreGoodsCate::getCates($cond, [], true, 'id,name,parent_id'); $list = StoreGoodsCate::getTreeCate($list); return $this->success('操作成功', compact('list')); } } //商品列表 public function actionList() { $params = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($params, [ [['limit', 'page', 'keyword'], 'safe'] ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $where[] = ['mall_id' => $this->mall_id]; $where[] = ['store_id' => $this->store_id]; $where[] = ['status' => [StatusEnum::ENABLED]]; if (isset($params['keyword'])) { $where[] = ['like', 'goods_name', trim($params['keyword'])]; } $params['where'] = $where; $params['select'] = 'id,goods_name,cover_pic,price,original_price,sales_num,stock,is_on_sale,check_status,store_id,is_recommend,goods_type'; $params['order'] = 'id DESC'; $goods_list = StoreGoods::listPage($params); if (!empty($goods_list['list'])) { foreach ($goods_list['list'] as &$item) { $item['status_text'] = '待审核'; switch ($item['check_status']) { case 0: $item['status_text'] = '待审核'; break; case 1: if ($item['is_on_sale'] == 1) { $item['status_text'] = '售卖中'; } else { $item['status_text'] = '已下架'; } break; case 2: $item['status_text'] = '审核未通过'; break; } } } return $this->success('操作成功', $goods_list); } public function actionGoodsHandle() { $params = Yii::$app->request->post(); $res = Yii::$app->services->validate->validate($params, [ [['id',], 'required', 'message' => '商品id不能为空'], [['is_on_sale',], 'required', 'message' => '上架/下架'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $res = StoreGoods::updateAll(['is_on_sale' => $params['is_on_sale']], ['id' => $params['id'], 'store_id' => $this->store_id]); if ($res == false) { return $this->error(StoreGoods::getStaticError()); } return $this->success('操作成功'); } public function actionDetail() { $params = Yii::$app->request->get(); $res = Yii::$app->services->validate->validate($params, [ [['id',], 'required', 'message' => '商品id不能为空'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $res = StoreGoods::getOne( [['id' => $params['id']], ['store_id' => $this->store_id]], true, ['cats' => function ($query) { $query->select('id,name'); }], false, 'id,goods_name,cover_pic, original_price, price, stock,bannar_pic,detail,cost_price'); if (!empty($res)) $res['bannar_pic'] = array_column(json_decode($res['bannar_pic'], true), 'pic_url'); return $this->success('操作成功', $res); } public function actionIsRecommend() { $params = Yii::$app->request->post(); $rules = [[['id'], 'required'], [['id'], 'integer']]; $rules[] = [['is_recommend'], 'required']; $rules[] = [['is_recommend'], 'in', 'range' => [0, 1]]; $res = Yii::$app->services->validate->validate($params, $rules); if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['store_id'] = $this->store_id; try { $recommend_nums = \Yii::$app->services->setting->addons->get($params['mall_id'], StoreEnum::ADDONS_NAME, 'basic.recommend_nums'); if (empty($recommend_nums)) throw new \Exception('允许推荐商品数为0'); if ($params['is_recommend'] == 1) { $counts = StoreGoods::find()->where(['store_id' => $params['store_id'], 'mall_id' => $params['mall_id'], 'status' => StatusEnum::ENABLED, 'is_recommend' => 1])->count(); if ($counts >= $recommend_nums) throw new \Exception('允许推荐商品数为' . $recommend_nums); $goods = StoreGoods::findOne(['id'=>$params['id']]); if(empty($goods)) throw new \Exception('商品不存在'); // if(empty($goods->is_on_sale)) throw new \Exception('商品未上架'); // if($goods->check_status!=1) throw new \Exception('只有审核通过的商品,才允许设置成推荐商品'); } $goods_id = $params['id']; unset($params['id']); $res = StoreGoods::goodsHandle($goods_id, $params); if ($res === false) throw new \Exception(StoreGoods::getStaticError()); return $this->success(); }catch (\Exception $e){ return $this->error($e->getMessage()); } } }