1], [['params', 'chain_level_value', 'attr'], 'safe'] ]; } // 获取商品id列表 public function syncGoodsList($next_key = '', $page_size = 10) { try { if (!$this->miniProgram) { throw new \Exception('请检查小店状态'); } $data = [ 'next_key' => $next_key, 'page_size' => $page_size ]; $client = new BaseClient($this->miniProgram); $res = $client->httpPostJson('channels/ec/product/list/get', $data); if (!$res['errcode'] && !empty($res)) { //根据商品id同步商品 // .... foreach ($res['product_ids'] as $product_id) { $goods_ext = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id, 'mini_id' => $this->mini_id]); if ($goods_ext) { // continue; } $this->syncGoodsInfo($product_id); } //下一页 if (count($res['product_ids']) >= $page_size) { $this->syncGoodsList($res['next_key']); } } return [ 'code' => 0, 'msg' => '同步成功' ]; } catch (\Exception $e) { return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() ]; } } //根据商品id同步商品 public function syncGoodsInfo($product_id = 0) { $t = \Yii::$app->db->beginTransaction(); try { if (!$product_id) { throw new \Exception('缺少product_id'); } $store_id = $this->store_id; $mini_id = $this->mini_id; //foreach ($product_id as $item) { $data = [ 'product_id' => $product_id, 'data_type' => 3 ]; $client = new BaseClient($this->miniProgram); $res = $client->httpPostJson('channels/ec/product/get', $data); if (!$res['errcode'] && !empty($res)) { $goodsInfo = $res['product'] ?? $res['edit_product']; $goodsInfo['min_price'] *= 0.01; $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $store_id, 'product_id' => (string)$product_id, 'mini_id' => $mini_id]); $goods = null; $status = 1; if ($video_shop_goods && $video_shop_goods->goods_id) { $goods = Goods::findOne(['id' => $video_shop_goods->goods_id, 'is_delete' => 0]); if ($goods) { $status = $goods->status; } } if (!$goods) { $goods = new Goods(); } $goods->status = $status; $goods->store_id = $this->store_id; $goods->name = $goodsInfo['title'] ?: ''; $goods->price = $goodsInfo['min_price']; $goods->unit = '件';//单位 $goods->cover_pic = $goodsInfo['head_imgs'][0] ?: ""; $goods->original_price = $goodsInfo['min_price']; $goods->cost_price = $goodsInfo['min_price']; //拼接商品详情 $desc_text = "
"; foreach ($goodsInfo['desc_info']['imgs'] as $desc_img) { $desc_text .= "
"; } $desc_text .= "
{$goodsInfo['desc_info']['desc']}
"; $goods->detail = $desc_text; $goods->use_attr = count($goodsInfo['skus'][0]['sku_attrs']) > 0 ? 1 : 0; $goods->goods_no = $goodsInfo['spu_code'] ?: 0; //属性 if ($goodsInfo['attrs']) { $parameter_list = []; foreach ($goodsInfo['attrs'] as $parameter_) { $parameter_list[] = [ 'name' => $parameter_['attr_key'], 'value' => $parameter_['attr_value'] ]; } $goods->parameter_list = json_encode($parameter_list, JSON_UNESCAPED_UNICODE); } if (!$goods->save()) { return [ 'code' => 0, 'msg' => "导入失败" ]; } //增加导入记录 if (!$video_shop_goods) { $video_shop_goods = new VideoShopGoodsExt(); $video_shop_goods->product_id = (string)$product_id; $video_shop_goods->store_id = $store_id; $video_shop_goods->mini_id = $mini_id; } $video_shop_goods->wx_status = $goodsInfo['status']; $goodsInfo['edit_status'] && $video_shop_goods->wx_edit_status = $goodsInfo['edit_status']; $video_shop_goods->goods_id = $goods->id; $video_shop_goods->wx_product_info = json_encode($res, JSON_UNESCAPED_UNICODE); $video_shop_goods->save(); $goods_num = 0; // 获取总库存 foreach ($goodsInfo['skus'] as $sku_item) { $goods_num += $sku_item['stock_num']; } $goods->goods_num = $goods_num; $goods->use_attr = 1; //判断是否使用规格 if (count($goodsInfo['skus']) === 1 && empty($goodsInfo['skus'][0]['sku_attrs'])) { $goods->use_attr = 0; } $attr_result = $this->setAttr($goods, $goodsInfo['skus']); if ($attr_result['code'] === 0) { $goods->attr = json_encode($attr_result['data'], JSON_UNESCAPED_UNICODE); } $goods->save(); $pic_list = $goodsInfo['head_imgs']; if ($pic_list) { GoodsPic::deleteAll(['goods_id' => $goods->id]); foreach ($pic_list as $pic_index => $pic_item) { if ($pic_index >= 9) { break; } if (!empty($pic_item)) { $pic = new GoodsPic(); $pic->goods_id = $goods->id; $pic->pic_url = $pic_item; if (!$pic->save()) { continue; }; } } } } //} $t->commit(); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { debug_log([$e->getMessage()], __CLASS__); \Yii::error($e); $t->rollBack(); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function product_h5url($product_id, $openid) { try { $data = [ "openid" => $openid, "product_id" => $product_id, ]; $client = new BaseClient($this->miniProgram); $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_h5url', $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res['product_h5url'], ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function product_taglink($product_id, $openid) { try { $data = [ "openid" => $openid, "product_id" => $product_id, ]; $client = new BaseClient($this->miniProgram); $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_taglink', $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res['product_taglink'], ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function product_qrcode($product_id, $openid) { try { $data = [ "openid" => $openid, "product_id" => $product_id, ]; $client = new BaseClient($this->miniProgram); $res = $client->httpPostJson('channels/ec/sharer/get_sharer_product_qrcode', $data); if (!$res['errcode'] && !empty($res)) { return [ 'code' => 0, 'msg' => 'ok', 'data' => $res['product_qrcode'], ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function product_listing($product_id, $delisting = 0) { try { $url = 'channels/ec/product/delisting'; $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => (string)$product_id]); if(!$delisting){ if($video_shop_goods['wx_edit_status'] == 7){ throw new \Exception('审核中,禁止操作'); } $url = 'channels/ec/product/listing'; } $data = [ "product_id" => $product_id, ]; $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { $this->miniProgram = $miniProgram; $this->syncGoodsInfo($product_id); return [ 'code' => 0, 'msg' => 'ok', 'data' => $res, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function product_stock($product_id = []) { try { $url = 'channels/ec/product/stock/batchget'; $video_shop_goods = VideoShopGoodsExt::findOne(['store_id' => $this->store_id, 'product_id' => $product_id]); $data = [ "product_id" => $product_id, ]; $miniProgram = \app\utils\Wechat\WechatMini::getWechatConfig($this->store_id, $video_shop_goods['mini_id'], 1); $client = new BaseClient($miniProgram); $res = $client->httpPostJson($url, $data); if (!$res['errcode'] && !empty($res)) { $list = []; if($res['data']['spu_stock_list']){ foreach($res['data']['spu_stock_list'] as $item){ $item['__total_stock_num'] = array_sum(array_column($item['sku_stock'], 'total_stock_num')); $item['__finder_total_num'] = array_sum(array_column($item['sku_stock'], 'finder_total_num')); $item['__normal_stock_num'] = array_sum(array_column($item['sku_stock'], 'normal_stock_num')); $item['__limited_discount_stock_num'] = array_sum(array_column($item['sku_stock'], 'limited_discount_stock_num')); $list[$item['product_id']] = $item; } } return [ 'code' => 0, 'msg' => 'ok', 'data' => $list, ]; } return [ 'code' => $res['errcode'] ?? 1, 'msg' => $res['errmsg'] ?? '系统错误', 'data' => $res, ]; } catch (\Exception $e) { \Yii::error($e); return [ 'code' => $e->getCode() ?: 1, 'msg' => $e->getMessage() . $e->getLine() ]; } } public function setAttr($goods, $skus) { try { $attr = []; if (!$goods->use_attr) { list($default_attr, $default_attr_group) = $this->getDefaultAttr(); $attr = [ [ 'attr_list' => [ [ 'attr_group_name' => $default_attr_group->attr_group_name, 'attr_id' => $default_attr->id, 'attr_name' => $default_attr->attr_name, ], ], 'num' => intval($goods->goods_num) ?: 0, 'price' => $goods->price, 'cost_price' => $goods->cost_price, 'no' => $goods->goods_no, 'is_wholesale' => 0, 'wholesale_num' => 0, 'sku_id' => $skus[0]['sku_id'], 'pic' => $skus[0]['thumb_img'] ?: $goods->cover_pic, ], ]; } else { foreach ($skus as $sku) { $sku['sale_price'] *= 0.01; $sku_attrs = $sku['sku_attrs']; $sku_attr_list = []; foreach ($sku_attrs as $sku_attr) { $sku_attr_list[] = [ 'attr_group_name' => $sku_attr['attr_key'], 'attr_name' => $sku_attr['attr_value'] ]; } $attr[] = [ 'attr_list' => $sku_attr_list, 'num' => intval($sku['stock_num']) ?: 0, 'price' => $sku['sale_price'], 'cost_price' => $sku['sale_price'], 'no' => $sku['sku_code'], 'is_wholesale' => 0, 'wholesale_num' => 0, 'sku_id' => $sku['sku_id'], 'pic' => $sku['thumb_img'] ?: $goods->cover_pic, ]; } } if (empty($attr) || !is_array($attr)) { return [ 'code' => 1 ]; } $new_attr = []; foreach ($attr as $i => $item_) { // if ($item['original_price'] > 0) { // $attr_price = $item['original_price']; // } else { $attr_price = $item_['price']; // } $new_attr_item = [ 'attr_list' => [], 'num' => intval($item_['num']), 'price' => sprintf('%.2f', $attr_price), 'no' => $item_['no'] ?: '', 'pic' => $item_['pic'] ?: '', 'is_wholesale' => 0, 'wholesale_num' => 0, 'cost_price' => sprintf('%.2f', $item_['cost_price'] ?: $item_['price']), 'sku_id' => $item_['sku_id'] ]; foreach ($item_['attr_list'] as $a) { $attr_group_model = AttrGroup::findOne(['store_id' => $this->store_id, 'attr_group_name' => $a['attr_group_name'], 'is_delete' => 0]); if (!$attr_group_model) { $attr_group_model = new AttrGroup(); $attr_group_model->attr_group_name = $a['attr_group_name']; $attr_group_model->store_id = $this->store_id; $attr_group_model->is_delete = 0; $attr_group_model->save(); } $attr_model = Attr::findOne(['attr_group_id' => $attr_group_model->id, 'attr_name' => $a['attr_name'], 'is_delete' => 0]); if (!$attr_model) { $attr_model = new Attr(); $attr_model->attr_name = $a['attr_name']; $attr_model->attr_group_id = $attr_group_model->id; $attr_model->is_delete = 0; $attr_model->save(); } $new_attr_item['attr_list'][] = [ 'attr_id' => $attr_model->id, 'attr_name' => $attr_model->attr_name, ]; } $new_attr[] = $new_attr_item; } return [ 'code' => 0, 'data' => $new_attr ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getDefaultAttr() { $default_attr_name = '默认'; $default_attr_group_name = '规格'; $attr = Attr::findOne([ 'attr_name' => $default_attr_name, 'is_delete' => 0, 'is_default' => 1, ]); $attr_group = null; if (!$attr) { $attr_group = AttrGroup::findOne([ 'attr_group_name' => $default_attr_group_name, 'is_delete' => 0, ]); if (!$attr_group) { $attr_group = new AttrGroup(); $attr_group->store_id = $this->store_id; $attr_group->attr_group_name = $default_attr_group_name; $attr_group->is_delete = 0; $attr_group->save(false); } $attr = new Attr(); $attr->attr_group_id = $attr_group->id; $attr->attr_name = $default_attr_name; $attr->is_delete = 0; $attr->is_default = 1; $attr->save(false); } else { $attr_group = AttrGroup::findOne($attr->attr_group_id); } return [$attr, $attr_group]; } //获取同步产品列表 public function shopGoodsList($params = []) { try { $store_id = $this->store_id; $mini_id = $this->mini_id; $name = $this->name; $status = $this->status; $query = VideoShopGoodsExt::find()->alias('v') ->leftJoin(['g' => Goods::tableName()], 'v.goods_id = g.id') ->leftJoin(['s' => StoreMini::tableName()], 's.id = v.mini_id') ->where(['g.store_id' => $store_id, 'g.is_delete' => 0, 'v.is_delete' => 0, 's.is_cancle' => 0]); if ($mini_id) { $query->andWhere(['v.mini_id' => $mini_id]); } if ($name) { $query->andWhere(['LIKE', 'g.name', $name]); } if (in_array($status, [0, 1])) { $query->andWhere(['v.status' => $status]); } if($params['wx_status'] > 0){ if($params['wx_status'] == 11){ $params['wx_status'] = [0,11,13,14,15,20]; } $query->andWhere(['v.wx_status' => $params['wx_status']]); } $query->select('v.id, g.name, g.cover_pic, g.rate, g.price, v.*, g.share_commission_new_first')->orderBy(['g.id' => 'DESC']); $list = pagination_make($query); foreach ($list['list'] as &$item) { $item['mini_name'] = StoreMini::findOne($item['mini_id'])->mini_nickname ?: ''; $item['status'] = (int)$item['status']; $item['wx_status_name'] = VideoShopGoodsExt::wx_status_name($item['wx_status']); $item['wx_edit_status_name'] = VideoShopGoodsExt::wx_edit_status_name($item['wx_edit_status']); $item['wx_product_info'] = json_decode($item['wx_product_info'], true); $item['wx_min_price'] = sprintf("%.2f", $item['wx_product_info']['edit_product']['min_price'] / 100); $item['wx_product_stock_num'] = array_sum(array_column($item['wx_product_info']['edit_product']['skus'], 'stock_num')); $item['sale_num'] = \app\models\Order::find()->alias('o')->leftJoin(['od' => \app\models\OrderDetail::tableName()], 'o.id=od.order_id') ->rightJoin(['vo' => \app\models\VideoShopOrderExt::tableName()], 'vo.order_id=o.id') ->where(['od.goods_id' => $item['goods_id'], 'o.is_pay' => 1]) ->sum('od.num') ?? 0; $rebate = $this->getShareCommissionMoneyMax(Goods::findone($item['goods_id']), $item['price']); $item['rebate'] = floatval(sprintf('%.2f', $rebate)); } if($params['wx_stock'] && $list['list']){ $stock = $this->product_stock(array_column($list['list'], 'product_id')); if(!$stock['code']){ foreach ($list['list'] as &$list_item) { $list_item['wx_stock'] = $stock['data'][$list_item['product_id']]; } } } $store_mini = StoreMini::find()->where(['store_id' => $store_id, 'fuwu_type' => 1, 'is_cancle' => 0]) ->select('id, mini_nickname')->orderBy('id desc')->asArray()->all(); // return [ 'code' => 0, 'msg' => 'success', 'data' => [ 'data' => $list['list'], 'pageNo' => $list['pageNo'], 'totalCount' => $list['totalCount'], 'store_mini' => $store_mini ] ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function getShareCommissionMoneyMax($goods, $level_price) { $store_id = $goods->store_id; $share_commission_new_first = json_decode($goods->share_commission_new_first, true); $share_commission_money = 0; $attr = json_decode($goods->attr, true); // TODO: 价格 $item_price = max(array_column($attr, 'price')); // $item_price = doubleval($orderShare->pay_price); $rate_first = 0; if ($goods->individual_share == 1) { $rate_first = max($share_commission_new_first); $shareType = $goods->share_type; } else { $first_profit1 = ShareLevel::find()->where(['profit_type' => 1, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,'status' => ShareLevel::STATUS_ON])->max('first_profit'); $first_profit0 = ShareLevel::find()->where(['profit_type' => 0, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,'status' => ShareLevel::STATUS_ON])->max('first_profit'); if($first_profit1 || $first_profit0){ $rate1 = $first_profit1; $rate0 = $item_price * $first_profit0 / 100; $rate_first = $rate1 > $rate0 ? $first_profit1 : $first_profit0; $shareType = $rate1 > $rate0 ? 1 : 0; }else{ $setting_shara = Option::get('share_money_setting', $store_id, 'share')['value']; $setting_shara = json_decode($setting_shara, true); $rate_first = $setting_shara['level_one']['value'] ?? 0; $shareType = 0; } } if ($shareType == 1) { // 金钱 $share_commission_money += $rate_first * 1; } else { // 比例 $share_commission_money += $item_price * $rate_first / 100; } return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money); } public function setStatus() { try { $ids = $this->ids; $status = $this->status; $store_id = $this->store_id; if (!in_array($status, [0, 1])) { throw new \Exception('参数错误'); } $ids = explode(',', $ids); VideoShopGoodsExt::updateAll(['status' => $status], ['id' => $ids, 'store_id' => $store_id]); return [ 'code' => 0, 'msg' => '操作成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } public function setRateParams() { try { $params = $this->params; $store_id = $this->store_id; $goods_id = $this->id; if (!$goods_id) { throw new \Exception('缺少goods_id'); } $rate_type = $this->rate_type; //分红类型 0比例 1固定金额 $rate = $this->rate; //分润比例 $is_open = $this->is_open; //是否开启链动 $chain_level_value = $this->chain_level_value; //链动佣金设置 $individual_share = $this->individual_share; //是否单独分销设置 $attr_setting_type = $this->attr_setting_type; //分销设置类型 0.普通设置|1.详细设置 $share_type = $this->share_type; //佣金配比 0--百分比 1--固定金额 $share_commission_new_first = $this->share_commission_new_first; $share_commission_new_second = $this->share_commission_new_second; $share_commission_new_third = $this->share_commission_new_third; $attr = $this->attr; foreach ($attr as $i => &$item) { if (intval($individual_share) === 1 && intval($attr_setting_type) === 1) { $share_level = ShareLevel::find()->where(['store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE, 'status' => ShareLevel::STATUS_ON]) ->select('level id, name')->orderBy('level ASC')->asArray()->all(); $share_level = array_merge([[ 'id' => 0, 'name' => '默认等级' ]], $share_level); foreach ($share_level as $share_level_index => $share_level_) { if (!isset($item['share_commission_new_first'][$share_level_index])) { $item['share_commission_new_first'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $item['share_commission_new_first'][$share_level_index]['value'] = 0; } else { foreach ($item['share_commission_new_first'] as $share_commission_new_item) { if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) { $item['share_commission_new_first'][$share_level_index]['value'] = $share_commission_new_item['value']; } } } if (!isset($item['share_commission_new_second'][$share_level_index])) { $item['share_commission_new_second'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $item['share_commission_new_second'][$share_level_index]['value'] = 0; } else { foreach ($item['share_commission_new_second'] as $share_commission_new_item) { if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) { $item['share_commission_new_second'][$share_level_index]['value'] = $share_commission_new_item['value']; } } } if (!isset($item['share_commission_new_third'][$share_level_index])) { $item['share_commission_new_third'][$share_level_index]['name'] = 'share_commission_level_' . $share_level_['id']; $item['share_commission_new_third'][$share_level_index]['value'] = 0; } else { foreach ($item['share_commission_new_third'] as $share_commission_new_item) { if ($share_commission_new_item['name'] === 'share_commission_level_' . $share_level_['id']) { $item['share_commission_new_third'][$share_level_index]['value'] = $share_commission_new_item['value']; } } } } } } $goods = Goods::findOne($goods_id); $goods->rate_type = $rate_type; $goods->rate = $rate; $goods->share_type = $share_type; $goods->attr_setting_type = $attr_setting_type; $goods->individual_share = $individual_share; $goods->share_commission_new_first = $share_commission_new_first; $goods->share_commission_new_second = $share_commission_new_second; $goods->share_commission_new_third = $share_commission_new_third; $goods->attr = json_encode($attr, JSON_UNESCAPED_UNICODE); if (!$goods->save()) { throw new \Exception(json_encode($goods->errors, JSON_UNESCAPED_UNICODE)); } $goodsChainLevel = GoodsChainLevel::findOne(['goods_id' => $goods_id]); if (!$goodsChainLevel) { $goodsChainLevel = new GoodsChainLevel(); $goodsChainLevel->goods_id = $goods_id; } $goodsChainLevel->type = $this->chain_type; $goodsChainLevel->is_open = $is_open; $goodsChainLevel->value = json_encode($chain_level_value); $goodsChainLevel->save(); return [ 'code' => 0, 'msg' => '设置成功' ]; } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } } }