model = new ShopGoods(); } /** * @NodeAnotation(title="列表") */ public function index() { if ($this->request->isAjax()) { if (input('selectFields')) { return $this->selectList(); } list($page, $limit, $where) = $this->buildTableParames(); list($count, $list) = ShopGoodsLogic::getList($page, $limit, $where); $data = [ 'code' => 0, 'msg' => '', 'count' => $count, 'data' => $list, ]; return json($data); } return $this->fetch(); } public function goodsBase($id) { list($data, $hot_keywords, $shop_goods_type, $shop_supplier, $specAttrId) = ShopGoodsLogic::getGoodsBase($id); $this->assign('shop_goods_type', $shop_goods_type); $this->assign('box_type', system_box_typesetting()); $this->assign('hot_keywords', $hot_keywords); $this->assign('shop_supplier', $shop_supplier); $this->assign('specAttrId', $specAttrId); $this->assign('data', $data); } /** * @param $goodsId * @param $attriId * @return \think\response\Json * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException * 获取商品规格,商品sku */ public function getGoodsSpecData($goodsId, $attriId) { if ($this->request->isAjax()) { $specRelation = ShopGoodsSpecRelation::getArrListByGoodsId($goodsId); $specAttrId = ShopGoodsSpecType::getPidById($specRelation[0]['spec_id']); $specType = ShopGoodsSpecTypeLogic::getCacheList(); $specType = app()->make(Tree::class)->DeepTree($specType, 'child'); $specTypeArr = []; foreach ($specType as $k => $v) { if ($v['id'] == $specAttrId) { if (isset($v['child'])) $specTypeArr = $v['child']; } } $specRelationArr = array_reduce($specRelation, function ($result, $value) { return array_merge($result, array_values(json_decode($value['spec_value'], true))); }, array()); $specRelationArr = array_column($specRelationArr, 'id'); foreach ($specTypeArr as $k => &$v) { if (isset($v['child'])) { foreach ($v['child'] as $ck => &$cv) { if (in_array($cv['id'], $specRelationArr)) { $cv['checked'] = true; } else { $cv['checked'] = false; } } } } $goodsSkuArr = []; $goodsSku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($goodsId); foreach ($goodsSku as $sk => $sv) { $goodsSkuArr['skus[' . $sv['spec_ids'] . '][picture]'] = $sv['picture']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][price]'] = $sv['price']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][cost_price]'] = $sv['cost_price']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][weight]'] = $sv['weight']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][stock]'] = $sv['stock']; $goodsSkuArr['skus[' . $sv['spec_ids'] . '][rebate_score]'] = $sv['rebate_score']; } $data = [ 'code' => 0, 'msg' => '', 'spec' => $specTypeArr, 'sku' => $goodsSkuArr ]; return json($data); } } /** * @NodeAnotation(title="商品添加") */ public function add() { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { ShopGoods::singleSpecGoods($goods); } else { ShopGoods::manySpecGoods($goods); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('成功'); } $hot_keywords = ShopHotKeywordsLogic::getCacheList(); $shop_supplier = ShopSupplierLogic::getCacheList(); $this->assign('hot_keywords', $hot_keywords); $this->assign('shop_supplier', $shop_supplier); return $this->fetch(); } /** * @NodeAnotation(title="商品编辑") */ public function edit($id) { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { ShopGoods::singleSpecGoods($goods); } else { ShopGoods::manySpecGoods($goods); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('保存成功'); } $this->goodsBase($id); return $this->fetch(); } public function copy($id) { if ($this->request->isAjax()) { $goods = $this->request->post(); Db::startTrans(); try { if (!isset($goods['is_exist_many_spec'])) { ShopGoods::singleSpecGoods($goods); } else { ShopGoods::manySpecGoods($goods); } Db::commit(); } catch (\Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('保存成功'); } $this->goodsBase($id); return $this->fetch(); } /** * @NodeAnotation(title="入库") */ public function stock($id) { $sku = ShopGoodsSpecLogic::getGoodsSpecByGoodId($id); $this->assign('sku', $sku); if ($this->request->isAjax()) { $data = $this->request->post(); if (empty($data['goods_id'])) { return json(['code' => 0, 'msg' => '参数错误']); } unset($data['goods_id']); $stock = []; foreach ($data as $k => $v) { $stock[] = [ 'goods_spec_id' => explode('_', $k)[1], 'stock' => $v ]; } $ShopGoodsSpec = new ShopGoodsSpec(); $flag = $ShopGoodsSpec->saveAll($stock); return $flag ? json(['code' => 1, 'msg' => '保存成功']) : json(['code' => 0, 'msg' => '保存失败']); } return $this->fetch(); } }